forked from Kristen848/Encom-TD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurret.cs
112 lines (100 loc) · 3.06 KB
/
Turret.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using UnityEngine;
using System.Collections;
public class Turret : MonoBehaviour, ITurret
{
public bool moveable_ = true;
public bool targetFound_ = false;
public Transform bulletPrefab_;
public Vector3 targetToLookAt_ = new Vector3(0f, 0f, 0f);
public Vector3 targetVelocity_ = new Vector3 (0f, 0f, 0f);
public Vector3 turretPosition_;
public float nextShotTime_ = 0.0f;
public float timeBetweenShots_ = 2.0f;
public float maxRange_ = 5.0f;
public int bulletForce_ = 1000;
private string targetTag_ = "Balloon";
public float zLocation_ = -1.0f;
const float PHYSICS_CONST = .1f;
public float targetSpeed_ = 0f;
public float bulletVelocity_;
// Use this for initialization
public void Start () {
CalculateBulletVelocity ();
}
// Update is called once per frame
public void Update ()
{
GameObject Target = findClosestTarget ();
if (Target != null)
{
StartCoroutine(CalcVelocity(Target));
CalculateDistance(Target);
targetToLookAt_ = PositionToShootAt(Target);
transform.LookAt (targetToLookAt_);
/*Debug.Log("targets");
Debug.Log(Target.transform.position);
Debug.Log (targetSpeed_);
Debug.Log (CalculateDistance(Target));
Debug.Log (bulletVelocity_);
Debug.Log (targetVelocity_);
Debug.Log(targetToLookAt_);*/
if (nextShotTime_ <= Time.time)
{
Shoot ();
nextShotTime_ = Time.time + timeBetweenShots_;
}
}
}
public void Shoot()
{
Vector3 bulletPosition = new Vector3 (bulletPrefab_.position.x, bulletPrefab_.position.y, zLocation_);
Transform bullet = (Transform)Instantiate (bulletPrefab_, bulletPosition, transform.rotation);
bullet.rigidbody.AddForce (transform.forward * bulletForce_);
}
public GameObject findClosestTarget()
{
GameObject[] allBalloons_= GameObject.FindGameObjectsWithTag(targetTag_);
float Distance;
GameObject Target=null;
int size = allBalloons_.Length;
int i = 0;
while (i<size && Target==null)
{
Distance = Vector3.Distance(allBalloons_[i].transform.position, transform.position);
if (Distance < maxRange_)
{
Target=allBalloons_[i];
}
i++;
}
return Target;
}
public IEnumerator CalcVelocity(GameObject target)
{
targetFound_ = false;
// Position at frame start
Vector3 prevPos = target.transform.position;
// Wait till it the end of the frame
yield return new WaitForEndOfFrame();
// Calculate velocity: Velocity = DeltaPosition / DeltaTime
targetVelocity_ = -((prevPos - target.transform.position) / Time.deltaTime);
targetSpeed_ = Vector3.Distance (prevPos, target.transform.position) / Time.deltaTime;
targetFound_ = true;
}
public void CalculateBulletVelocity()
{
bulletVelocity_ = bulletForce_ * PHYSICS_CONST;
}
public float CalculateDistance(GameObject target)
{
float distance = 0f;
distance = Vector3.Distance(target.transform.position, transform.position);
return distance;
}
public Vector3 PositionToShootAt(GameObject target)
{
Vector3 position = target.transform.position;
position += (targetSpeed_ * (CalculateDistance(target)/bulletVelocity_)) * targetVelocity_;
return position;
}
}