1
Fork 0
mirror of https://github.com/Steffo99/beat-td.git synced 2024-11-23 15:54:18 +00:00
beat-td/Assets/Scripts/TowerPlacer.cs

83 lines
2.8 KiB
C#
Raw Permalink Normal View History

2018-04-21 12:09:09 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TowerPlacer : MonoBehaviour
{
public GameObject selected = null;
2018-04-23 11:50:24 +00:00
GameStatus gameStatus;
TowerSelector towerSelector;
2018-04-21 12:09:09 +00:00
SpriteRenderer cursorSprite;
2018-04-22 12:07:55 +00:00
SpriteRenderer towerGhost;
2018-04-23 18:09:13 +00:00
SpriteRenderer costCounter;
2018-04-21 12:09:09 +00:00
void Start()
{
2018-04-23 11:50:24 +00:00
gameStatus = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameStatus>();
towerSelector = gameObject.GetComponent<TowerSelector>();
2018-04-22 12:07:55 +00:00
SpriteRenderer[] spriteRenderers = gameObject.GetComponentsInChildren<SpriteRenderer>();
foreach (SpriteRenderer spriteRenderer in spriteRenderers)
{
if (spriteRenderer.gameObject == gameObject)
{
cursorSprite = spriteRenderer;
continue;
}
2018-04-23 18:09:13 +00:00
if (spriteRenderer.gameObject.name == "Tower Ghost")
{
towerGhost = spriteRenderer;
}
if (spriteRenderer.gameObject.name == "Cost Counter")
{
costCounter = spriteRenderer;
}
2018-04-22 12:07:55 +00:00
}
2018-04-21 12:09:09 +00:00
}
void Update () {
2018-04-23 11:50:24 +00:00
//Check if the player has enough money to place the tower
bool hasEnoughMoney = gameStatus.money >= gameStatus.towerCosts[towerSelector.index];
2018-04-21 12:09:09 +00:00
//Check if there's nothing else under the cursor
Collider2D collider = Physics2D.OverlapPoint(transform.position);
2018-04-23 08:45:40 +00:00
if (collider == null || collider.tag == "Hit")
2018-04-21 12:09:09 +00:00
{
2018-04-23 18:09:13 +00:00
cursorSprite.color = Color.white;
2018-04-23 11:50:24 +00:00
towerGhost.color = new Color(towerGhost.color.r, towerGhost.color.g, towerGhost.color.b, 1);
2018-04-21 12:09:09 +00:00
}
else
{
cursorSprite.color = Color.red;
2018-04-23 11:50:24 +00:00
towerGhost.color = new Color(towerGhost.color.r, towerGhost.color.g, towerGhost.color.b, 0);
2018-04-21 12:09:09 +00:00
}
2018-04-23 18:09:13 +00:00
if (hasEnoughMoney)
{
costCounter.color = Color.white;
}
else
{
costCounter.color = Color.red;
}
2018-04-21 12:09:09 +00:00
if (Input.GetMouseButtonDown(0))
{
2018-04-23 11:50:24 +00:00
//Check if the player has enough money
if (!hasEnoughMoney) return;
2018-04-21 12:09:09 +00:00
//Ensure there is nothing below
2018-04-23 08:45:40 +00:00
if (collider != null && collider.tag != "Hit") return;
2018-04-21 12:09:09 +00:00
//Place the item
Vector3 position = new Vector3(transform.position.x, transform.position.y, 0);
Instantiate(selected, position, transform.rotation);
2018-04-23 11:50:24 +00:00
//Deduct the money
gameStatus.money -= gameStatus.towerCosts[towerSelector.index];
//Increase the costs
if(gameStatus.towerCosts[towerSelector.index] == 0)
{
//TODO: quick hack
2018-04-23 18:09:13 +00:00
gameStatus.towerCosts = new int[] { 5, 5, 5 };
2018-04-23 11:50:24 +00:00
}
2018-04-23 18:09:13 +00:00
gameStatus.towerCosts[towerSelector.index] *= 2;
2018-04-23 11:50:24 +00:00
2018-04-21 12:09:09 +00:00
}
}
}