From 8a271ae31aecea10684b07e3e2088da7bc82169f Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Thu, 9 Dec 2021 21:01:35 -0500 Subject: [PATCH] currency deduction on buy --- Assets/Scripts/Player.cs | 9 +++++++++ Assets/Scripts/Tile.cs | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index 6f2c466..238d0dd 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -33,6 +33,15 @@ public void Damage(int damage) } } + public bool Buy(int cost) + { + if (Currency < cost) + return false; + + Currency -= cost; + return true; + } + public void Reward(int currency) { Currency += currency; diff --git a/Assets/Scripts/Tile.cs b/Assets/Scripts/Tile.cs index 631c9bb..a9a8999 100644 --- a/Assets/Scripts/Tile.cs +++ b/Assets/Scripts/Tile.cs @@ -34,7 +34,10 @@ void OnMouseDown() return; var building = BuildManager.instance.GetBuilding(); - weapon = Instantiate(building, transform.position + building.transform.position, transform.rotation); + var cost = building.GetComponent().cost; + + if (Player.instance.Buy(cost)) + weapon = Instantiate(building, transform.position + building.transform.position, transform.rotation); } void OnMouseEnter() {