Introduction:
In this update, we introduce an economy system to manage player resources, specifically for purchasing weapons and rewarding players based on match performance. By implementing this system, we aim to add depth to gameplay, encourage strategic decision-making, and balance the in-game economy for fair competition.
Economy Manager:
The EconomyManager
script is responsible for managing player coins and handling transactions:
Player Coins: Tracks the current amount of coins owned by the player.
Initial Coins: Sets the starting amount of coins for each player at the beginning of the match.
Winning Coin: Specifies the reward amount for winning a match.
Losing Coin: Determines the reward amount for losing a match.
Kill Coin: Sets the reward amount for each kill during a match.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using Hashtable = ExitGames.Client.Photon.Hashtable;
using Photon.Pun;
using Photon.Realtime;
using TMPro;
public class EconomyManager : MonoBehaviour
{
public int playerCoins = 0;
public int initialCoins = 100;
public int winningCoin = 1600;
public int losingCoin = 1200;
public int killCoin = 20;
public TextMeshProUGUI playerCoinsText;
public MatchManager matchManager;
// Start is called before the first frame update
void Start()
{
playerCoins = initialCoins;
PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable { { "PlayerCoins", playerCoins } });
}
// Update is called once per frame
void Update()
{
if (matchManager.isBuyPhase)
{
playerCoinsText.text = Mathf.Round(playerCoins).ToString();
}
}
public void Purchase(int price)
{
if ((int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerCoins"] >= price)
{
playerCoins -= price;
PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable { { "PlayerCoins", playerCoins } });
if(price == 80)
{
matchManager.BuyPistol();
}
else if(price == 140)
{
matchManager.BuyRifle();
}
Debug.Log("Purchased for: "+playerCoins);
}
else
{
Debug.Log("Not enough money");
}
}
public void Reward(int price)
{
playerCoins += price;
PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable { { "PlayerCoins", playerCoins } });
Debug.Log("Rewarded: " + playerCoins);
}
}
UI Integration:
The playerCoinsText
field references a TextMeshProUGUI element to display the player's current coin balance on the user interface.
Match Manager Integration:
The matchManager
field links to the MatchManager script to coordinate economy-related actions with the match flow.
Functionality:
Start(): Initializes the player's coin balance at the start of the match and updates the player's custom properties in the Photon network.
Update(): Updates the player's coin balance display on the UI during the buy phase.
Purchase(int price): Allows the player to purchase weapons or items by deducting the specified price from their coin balance if they have sufficient funds. Updates the player's custom properties accordingly and triggers the appropriate buy action in the match manager.
Reward(int price): Rewards the player with the specified amount of coins based on match performance, such as winning the match or scoring kills. Updates the player's custom properties and logs the reward amount.
Conclusion:
With the introduction of the economy system, players can now manage their resources strategically, purchase weapons to enhance their combat capabilities, and earn rewards based on their performance in matches. By carefully balancing the economy, we ensure a fair and engaging gameplay experience for all participants. In the next update, we'll explore further enhancements, such as expanding the item shop and refining the reward system based on player feedback and testing.
PS: Will update this to the server and sync there in future.