-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefenderSpawner.cs
55 lines (42 loc) · 1.46 KB
/
DefenderSpawner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using UnityEngine;
using System.Collections;
public class DefenderSpawner : MonoBehaviour {
private GameObject defenderParent;
private StarDisplay starDisplay;
// Use this for initialization
void Start () {
starDisplay = GameObject.FindObjectOfType<StarDisplay>();
defenderParent = GameObject.Find("Defender Parent");
if(!defenderParent){
defenderParent = new GameObject("Defender Parent");
}
}
// Update is called once per frame
void Update () {
}
void OnMouseDown(){
GameObject selectedDefender = Button.selectedDefender;
if(selectedDefender){
SpawnDefender(selectedDefender);
}
}
void SpawnDefender(GameObject defender){
int defenderCost = defender.GetComponent<Defender>().starCost;
if(starDisplay.UseStars(defenderCost) == StarDisplay.Status.SUCCESS){
GameObject newDefender = Instantiate(defender, (SnapToGrid(CalculateWorldPointOfMouseClick())), Quaternion.identity) as GameObject;
newDefender.transform.parent = defenderParent.transform;
} else {
Debug.Log ("Not Enough Stars");
}
}
Vector2 CalculateWorldPointOfMouseClick(){
Camera camera = GameObject.Find("Game Camera").GetComponent<Camera>();
return camera.ScreenToWorldPoint(Input.mousePosition);
}
Vector2 SnapToGrid(Vector2 rawWorldPosition){
// Round the values from CalculateWorldPointOfMouseClick
int newX = Mathf.RoundToInt(rawWorldPosition.x);
int newY = Mathf.RoundToInt(rawWorldPosition.y);
return new Vector2(newX, newY);
}
}