1
Fork 0
mirror of https://github.com/Steffo99/gravity-fusion.git synced 2024-11-22 08:24:17 +00:00
gravity-fusion/Assets/Components/UpgradeButton.cs

101 lines
2.6 KiB
C#
Raw Normal View History

2019-10-06 22:21:35 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[Serializable]
public enum UpgradeType {
ANTIG, MATTER, FISSION
}
public class UpgradeButton : MonoBehaviour
{
2019-10-07 11:44:26 +00:00
[Header("Properties")]
2019-10-06 22:21:35 +00:00
public int cost;
public int level;
public UpgradeType type;
2019-10-07 11:44:26 +00:00
[Header("Sprites")]
2019-10-06 22:21:35 +00:00
public Sprite cantBuySprite;
public Sprite canBuySprite;
public Sprite hoveredSprite;
public Sprite boughtSprite;
2019-10-07 13:11:17 +00:00
protected bool started;
2019-10-06 22:21:35 +00:00
protected bool CanBeBought {
get {
return gameController.blackHole.UnspentMass >= cost;
}
}
protected bool HasBeenBought {
get {
switch(type) {
case UpgradeType.ANTIG: return gameController.LevelAntig >= level;
case UpgradeType.MATTER: return gameController.LevelMatter >= level;
case UpgradeType.FISSION: return gameController.LevelFission >= level;
2019-10-07 11:44:26 +00:00
default: return false;
2019-10-06 22:21:35 +00:00
}
}
}
protected GameController gameController;
protected Image image;
protected void Awake() {
gameController = GameObject.Find("GameController").GetComponent<GameController>();
image = GetComponent<Image>();
2019-10-07 13:11:17 +00:00
started = false;
}
protected void OnEnable() {
if(started) RefreshUpgrades();
2019-10-06 22:21:35 +00:00
}
protected void Start() {
2019-10-07 13:11:17 +00:00
started = true;
2019-10-06 22:21:35 +00:00
}
public void OnPointerEnter() {
if(!HasBeenBought && CanBeBought) {
image.sprite = hoveredSprite;
}
2019-10-07 13:11:17 +00:00
gameController.canvas.BroadcastMessage("DisplayCost", cost);
2019-10-06 22:21:35 +00:00
}
public void OnPointerExit() {
if(!HasBeenBought && CanBeBought) {
image.sprite = canBuySprite;
}
2019-10-07 13:11:17 +00:00
gameController.canvas.BroadcastMessage("DisplayCost", -1f);
2019-10-06 22:21:35 +00:00
}
public void OnPointerClick() {
if(!HasBeenBought && CanBeBought) {
gameController.blackHole.UnspentMass -= cost;
switch(type) {
case UpgradeType.ANTIG: gameController.LevelAntig = level; break;
case UpgradeType.MATTER: gameController.LevelMatter = level; break;
case UpgradeType.FISSION: gameController.LevelFission = level; break;
}
gameController.canvas.BroadcastMessage("RefreshUpgrades");
}
}
public void RefreshUpgrades() {
if(HasBeenBought) {
image.sprite = boughtSprite;
}
else if(CanBeBought) {
image.sprite = canBuySprite;
}
else {
image.sprite = cantBuySprite;
}
}
}