fgm24/Assets/Scripts/Upgrader.cs

295 lines
8.2 KiB
C#
Raw Normal View History

2024-02-03 16:22:10 +01:00
using System.Collections;
2024-02-04 02:45:51 +01:00
using TMPro;
2024-02-03 16:22:10 +01:00
using System.Collections.Generic;
2024-02-03 20:43:17 +01:00
using UnityEngine.UI;
2024-02-03 16:22:10 +01:00
using UnityEngine;
public class Upgrade
{
public string name = "MT";
public int amount = 0;
public Upgrade(string n)
{
name = n;
}
}
public class Upgrades
{
public Upgrade mopUpgrade = new Upgrade("Mop Radius");
public Upgrade speedUpgrade = new Upgrade("Move speed");
public Upgrade ropeUpgrade = new Upgrade("Longer Rope");
public Upgrade healthUpgrade = new Upgrade("More health");
public Upgrade damageUpgrade = new Upgrade("More rope damage");
public Upgrade bloodUpgrade = new Upgrade("MORE BLOOD!");
public Upgrade reelUpgrade = new Upgrade("Faster rope reel speed");
}
public class Upgrader : MonoBehaviour
{
public GameObject player1;
public GameObject player2;
public BloodComputeShader bloodManager;
public RopeSimulator rope;
public Upgrades upgrades { get; private set; }
2024-02-03 18:07:52 +01:00
public float mopSizeIncrease = 1.1f;
public float speedIncrease = 1.1f;
2024-02-03 20:43:17 +01:00
public float ropeIncrease = 1.1f;
2024-02-03 18:07:52 +01:00
public float healthIncrease = 1.1f;
public float damageIncrease = 1.1f;
public float bloodIncrease = 1.1f;
2024-02-03 20:43:17 +01:00
public float reelIncrease = 1.1f;
2024-02-03 18:07:52 +01:00
public int upgradeCost = 10000;
private RectTransform Player1Cursor;
private RectTransform Player2Cursor;
private PlayerInput Player1Input;
private PlayerInput Player2Input;
private float acceptTime = 0f;
2024-02-03 20:43:17 +01:00
private Image background;
private Image[] upgradeImages = new Image[8];
2024-02-03 21:22:37 +01:00
private bool prevCouldUpgrade = true;
public Image BloodMeter;
2024-02-04 02:58:23 +01:00
public TMPro.TextMeshProUGUI description;
2024-02-03 21:22:37 +01:00
2024-02-03 16:22:10 +01:00
// Start is called before the first frame update
void Start()
{
2024-02-03 18:07:52 +01:00
Player1Cursor = transform.Find("Player1 Cursor").GetComponent<RectTransform>();
Player2Cursor = transform.Find("Player2 Cursor").GetComponent<RectTransform>();
Player1Input = player1.GetComponent<PlayerInput>();
Player2Input = player2.GetComponent<PlayerInput>();
2024-02-03 20:43:17 +01:00
background = GetComponent<Image>();
int idx = 0;
foreach (Image i in GetComponentsInChildren<Image>())
{
if (i.gameObject.name.StartsWith("Upgrade"))
upgradeImages[idx++] = i;
}
upgrades = new Upgrades();
2024-02-03 21:22:37 +01:00
StartCoroutine(updateBloodMeter());
2024-02-03 18:07:52 +01:00
}
2024-02-03 16:22:10 +01:00
2024-02-03 18:07:52 +01:00
bool canUpgrade()
{
2024-02-03 21:22:37 +01:00
return bloodManager.score >= upgradeCost;
// return true;
2024-02-03 20:43:17 +01:00
}
int getRegion(float x, float y, int regions)
{
if (x + y == 0)
return -1;
float angle = Mathf.Atan2(x, -y) + Mathf.PI;
float regionSize = (Mathf.PI * 2.0f) / ((float)regions);
int region = (int)((((Mathf.PI * 2.0f) - angle) / regionSize) + 0.5f);
if (region > regions - 1)
return 0;
return region;
2024-02-03 16:22:10 +01:00
}
// Update is called once per frame
void Update()
{
2024-02-04 00:48:43 +01:00
bool upgrade = canUpgrade();
2024-02-03 18:07:52 +01:00
if (canUpgrade())
{
2024-02-03 21:22:37 +01:00
if (!prevCouldUpgrade)
{
background.color = Color.white;
foreach (Image i in upgradeImages)
{
i.color = Color.white;
}
Player1Cursor.gameObject.SetActive(true);
Player2Cursor.gameObject.SetActive(true);
}
2024-02-03 20:43:17 +01:00
int p1a = getRegion(Player1Input.look.x, Player1Input.look.y, 8);
int p2a = getRegion(Player2Input.look.x, Player2Input.look.y, 8);
if (p1a != -1 && p2a == p1a)
2024-02-03 18:07:52 +01:00
{
if (acceptTime > 2f)
{
switch (p1a)
{
case 0:
UpgradeMopSize();
break;
case 1:
UpgradeSpeed();
break;
case 2:
RopeUpgrade();
break;
case 3:
HealthUpgrade();
break;
case 4:
DamageUpgrade();
break;
case 5:
2024-02-03 20:43:17 +01:00
BloodUpgrade();
2024-02-03 18:07:52 +01:00
break;
case 6:
ReelUpgrade();
break;
case 7:
break;
}
2024-02-03 16:22:10 +01:00
2024-02-03 21:22:37 +01:00
// Subtract blood
2024-02-03 18:07:52 +01:00
bloodManager.score -= upgradeCost;
acceptTime = 0f;
}
else
2024-02-03 20:43:17 +01:00
{
2024-02-03 18:07:52 +01:00
acceptTime += Time.deltaTime;
2024-02-03 20:43:17 +01:00
foreach (Image i in upgradeImages)
{
i.fillAmount = 1f;
2024-02-04 00:19:46 +01:00
i.gameObject.transform.localScale = Vector3.one;
2024-02-03 20:43:17 +01:00
}
upgradeImages[p1a].fillAmount = acceptTime / 2f;
2024-02-04 00:19:46 +01:00
upgradeImages[p1a].transform.localScale = Vector3.one * 2f;
2024-02-03 20:43:17 +01:00
}
2024-02-03 18:07:52 +01:00
}
else
2024-02-03 20:43:17 +01:00
{
2024-02-04 02:45:51 +01:00
description.text = "";
2024-02-03 20:43:17 +01:00
if (acceptTime > 0f)
{
foreach (Image i in upgradeImages)
{
i.fillAmount = 1f;
2024-02-04 00:19:46 +01:00
i.gameObject.transform.localScale = Vector3.one;
2024-02-03 20:43:17 +01:00
}
}
2024-02-03 18:07:52 +01:00
acceptTime = 0f;
2024-02-03 20:43:17 +01:00
// background.fillAmount = 0f;
}
2024-02-03 18:07:52 +01:00
}
2024-02-03 21:22:37 +01:00
else if (prevCouldUpgrade)
{
2024-02-04 02:45:51 +01:00
// TODO: PLAY SOUND HERE!!
2024-02-03 21:22:37 +01:00
2024-02-04 02:45:51 +01:00
description.text = "";
background.color = Color.gray;
foreach (Image i in upgradeImages)
{
i.color = Color.gray;
i.gameObject.transform.localScale = Vector3.one;
}
Player1Cursor.gameObject.SetActive(false);
Player2Cursor.gameObject.SetActive(false);
2024-02-03 21:22:37 +01:00
}
2024-02-03 18:07:52 +01:00
Player1Cursor.localPosition = Player1Input.look.normalized * (125 - 12);
Player2Cursor.localPosition = Player2Input.look.normalized * (125 - 12);
2024-02-03 21:22:37 +01:00
2024-02-04 00:48:43 +01:00
prevCouldUpgrade = upgrade;
2024-02-03 16:22:10 +01:00
}
/// Increases mop radius by 10%
public void UpgradeMopSize()
{
2024-02-04 02:45:51 +01:00
description.text = upgrades.mopUpgrade.name;
2024-02-03 18:07:52 +01:00
bloodManager.CleanRadius *= mopSizeIncrease;
upgrades.mopUpgrade.amount++;
2024-02-03 16:22:10 +01:00
}
/// Increases move speed by 10%
public void UpgradeSpeed()
{
2024-02-04 02:45:51 +01:00
description.text = upgrades.speedUpgrade.name;
2024-02-03 18:07:52 +01:00
player1.GetComponent<PlayerMovement>().moveSpeed *= speedIncrease;
player2.GetComponent<PlayerMovement>().moveSpeed *= speedIncrease;
upgrades.speedUpgrade.amount++;
2024-02-03 16:22:10 +01:00
}
2024-02-03 18:07:52 +01:00
public void RopeUpgrade()
{
2024-02-03 16:22:10 +01:00
// todo: public methods
2024-02-04 02:45:51 +01:00
description.text = upgrades.ropeUpgrade.name;
2024-02-03 20:43:17 +01:00
rope.ropeMaxLength *= ropeIncrease;
2024-02-03 18:07:52 +01:00
upgrades.ropeUpgrade.amount++;
2024-02-03 16:22:10 +01:00
}
2024-02-03 18:07:52 +01:00
public void HealthUpgrade()
{
2024-02-04 02:45:51 +01:00
description.text = upgrades.healthUpgrade.name;
2024-02-03 18:07:52 +01:00
var comp1 = player1.GetComponent<HealthComponent>();
comp1.setMaxHealth((int)(comp1.getMaxHealth() * healthIncrease));
var comp2 = player2.GetComponent<HealthComponent>();
comp2.setMaxHealth((int)(comp2.getMaxHealth() * healthIncrease));
upgrades.healthUpgrade.amount++;
}
public void DamageUpgrade()
{
2024-02-04 02:45:51 +01:00
description.text = upgrades.damageUpgrade.name;
2024-02-03 18:07:52 +01:00
// todo: public rope methods
2024-02-03 20:43:17 +01:00
// rope.da
2024-02-03 18:07:52 +01:00
upgrades.damageUpgrade.amount++;
}
public void BloodUpgrade()
{
2024-02-04 02:45:51 +01:00
description.text = upgrades.bloodUpgrade.name;
2024-02-03 18:07:52 +01:00
bloodManager.scoreMult *= bloodIncrease;
upgrades.bloodUpgrade.amount++;
}
public void ReelUpgrade()
{
// todo rope methods
2024-02-04 02:45:51 +01:00
description.text = upgrades.reelUpgrade.name;
2024-02-03 20:43:17 +01:00
rope.ropeExtendSpeed *= reelIncrease;
2024-02-03 18:07:52 +01:00
upgrades.reelUpgrade.amount++;
}
2024-02-03 16:22:10 +01:00
2024-02-04 02:45:51 +01:00
IEnumerator updateBloodMeter()
{
while (true)
{
2024-02-03 21:22:37 +01:00
yield return new WaitForSeconds(0.5f);
2024-02-04 02:45:51 +01:00
BloodMeter.fillAmount = ((float)bloodManager.score) / ((float)upgradeCost);
2024-02-03 21:22:37 +01:00
}
2024-02-04 02:45:51 +01:00
2024-02-03 21:22:37 +01:00
// dont care
yield return null;
}
2024-02-03 16:22:10 +01:00
}