fgm24/Assets/Scripts/Upgrade/Upgrader.cs

379 lines
11 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");
2024-02-04 11:32:07 +01:00
public Upgrade reviveUpgrade = new Upgrade("Adds an extra revive");
2024-02-03 16:22:10 +01:00
}
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
2024-02-04 09:07:05 +01:00
public int upgradeCost = 7000;
2024-02-03 18:07:52 +01:00
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
public static Upgrader instance;
private void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(this);
}
}
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())
{
if (!prevCouldUpgrade)
2024-02-06 14:53:30 +01:00
{
background.color = Color.white;
foreach (Image i in upgradeImages)
{
i.color = Color.white;
}
Player1Cursor.gameObject.SetActive(true);
Player2Cursor.gameObject.SetActive(true);
2024-02-06 14:53:30 +01:00
}
int p1a = getRegion(Player1Input.look.x, Player1Input.look.y, 8);
int p2a = getRegion(Player2Input.look.x, Player2Input.look.y, 8);
//keyboard upgrade
if (Input.GetKey(KeyCode.Alpha1))
{
p1a = p2a = 0;
2024-02-06 14:53:30 +01:00
}
else if (Input.GetKey(KeyCode.Alpha2))
2024-02-06 14:53:30 +01:00
{
p1a = p2a = 1;
2024-02-06 14:53:30 +01:00
}
else if (Input.GetKey(KeyCode.Alpha3))
2024-02-06 14:53:30 +01:00
{
p1a = p2a = 2;
2024-02-06 14:53:30 +01:00
}
else if (Input.GetKey(KeyCode.Alpha4))
2024-02-06 14:53:30 +01:00
{
p1a = p2a = 3;
2024-02-06 14:53:30 +01:00
}
else if (Input.GetKey(KeyCode.Alpha5))
2024-02-06 14:53:30 +01:00
{
p1a = p2a = 4;
2024-02-06 14:53:30 +01:00
}
else if (Input.GetKey(KeyCode.Alpha6))
2024-02-06 14:53:30 +01:00
{
p1a = p2a = 5;
2024-02-06 14:53:30 +01:00
}
else if (Input.GetKey(KeyCode.Alpha7))
2024-02-06 14:53:30 +01:00
{
p1a = p2a = 6;
2024-02-06 14:53:30 +01:00
}
else if (Input.GetKey(KeyCode.Alpha8))
2024-02-03 21:22:37 +01:00
{
p1a = p2a = 7;
2024-02-03 21:22:37 +01:00
}
2024-02-03 20:43:17 +01:00
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:
2024-02-04 11:32:07 +01:00
ReviveUpgrade();
2024-02-03 18:07:52 +01:00
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;
2024-02-04 23:18:40 +01:00
upgradeCost = (int)(upgradeCost * 1.2f);
2024-02-03 18:07:52 +01:00
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
}
2024-02-04 11:32:07 +01:00
switch (p1a)
{
case 0:
description.text = upgrades.mopUpgrade.name;
break;
case 1:
description.text = upgrades.speedUpgrade.name;
break;
case 2:
description.text = upgrades.ropeUpgrade.name;
break;
case 3:
description.text = upgrades.healthUpgrade.name;
break;
case 4:
description.text = upgrades.damageUpgrade.name;
break;
case 5:
description.text = upgrades.bloodUpgrade.name;
break;
case 6:
description.text = upgrades.reelUpgrade.name;
break;
case 7:
description.text = upgrades.reviveUpgrade.name;
break;
}
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 11:32:07 +01:00
AudioManager.PlaySound("Blood_Splatter_Large_SFX", Vector3.zero, false, false);
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 11:32:07 +01:00
public void ReviveUpgrade()
{
description.text = upgrades.reviveUpgrade.name;
}
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
}