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

77 lines
2.6 KiB
C#
Raw 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-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;
}
towerGhost = spriteRenderer;
}
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 11:50:24 +00:00
if (hasEnoughMoney)
{
cursorSprite.color = Color.white;
}
else
{
cursorSprite.color = Color.yellow;
}
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
}
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
gameStatus.towerCosts = new int[] { 1, 1, 1 };
}
else
{
gameStatus.towerCosts[towerSelector.index] *= 2;
}
2018-04-21 12:09:09 +00:00
}
}
}