Subversion Repository Public Repository

Nextrek

Diff Revisions 141 vs 144 for /3DSpace/Assets/Custum/Scripts/AllyShip.cs

Diff revisions: vs.
  @@ -1,146 +1,146 @@
1 - using UnityEngine;
2 - using System.Collections;
3 -
4 - public class AllyShip : MonoBehaviour
5 - {
6 - public SU_Thruster[] thrusters;
7 - public float rollRate = 100.0f;
8 - public float yawRate = 30.0f;
9 - public float pitchRate = 100.0f;
10 - public Vector3[] weaponMountPoints;
11 - public Transform[] laserShotPrefab;
12 - public AudioClip[] soundEffectFire;
13 - public CameraFollow followCamera;
14 - public float life = 100;
15 -
16 - private bool isAlive = true;
17 -
18 - private Rigidbody _cacheRigidbody;
19 - private float shootFreq = 1f;
20 - private float lastShootTime = 0;
21 - private int weaponType = 0;
22 -
23 - private GameObject currentEnemyTarget;
24 -
25 - private static float ROTATION_SPEED = 0.75f; // degrees
26 - private static float SHOOT_RANGE = 2000.0f;
27 - private static float MIN_ENGAGE_RANGE = 300.0f;
28 -
29 - private static float HERO_MIN_RANGE = 1000.0f;
30 - private static float HERO_MAX_RANGE = 2000.0f;
31 -
32 - float genCoord(float min_distance, float max_distance) {
33 - return (Random.value - Random.value) * (min_distance + Random.value * (max_distance-min_distance));
34 - }
35 -
36 - void Start()
37 - {
38 - this.transform.position = new Vector3(
39 - genCoord(HERO_MIN_RANGE, HERO_MAX_RANGE),
40 - genCoord(HERO_MIN_RANGE, HERO_MAX_RANGE),
41 - genCoord(HERO_MIN_RANGE, HERO_MAX_RANGE)
42 - );
43 -
44 - this.transform.rotation = Random.rotation;
45 -
46 - foreach (SU_Thruster _thruster in thrusters)
47 - {
48 - if (_thruster == null)
49 - {
50 - Debug.LogError("Thruster array not properly configured. Attach thrusters to the game object and link them to the Thrusters array.");
51 - }
52 - }
53 -
54 - _cacheRigidbody = rigidbody;
55 - if (_cacheRigidbody == null)
56 - {
57 - Debug.LogError("Spaceship has no rigidbody - the thruster scripts will fail. Add rigidbody component to the spaceship.");
58 - }
59 - }
60 -
61 - public void getHitted(float hit)
62 - {
63 - if (!isAlive) return;
64 -
65 - life -= hit;
66 - if (life<=0)
67 - {
68 - isAlive = false;
69 - Instantiate(GameManager.Instance.explosionEffect, this.transform.position, this.transform.rotation);
70 - CommunicationCenter.Instance.DestroyOneAlly(this.gameObject);
71 - Destroy(this.gameObject);
72 - }
73 - }
74 -
75 - void StartThruster()
76 - {
77 - foreach (SU_Thruster _thruster in thrusters)
78 - {
79 - _thruster.StartThruster();
80 - }
81 - }
82 -
83 - void StopThruster()
84 - {
85 - foreach (SU_Thruster _thruster in thrusters)
86 - {
87 - _thruster.StopThruster();
88 - }
89 - }
90 -
91 - void Update()
92 - {
93 - lastShootTime+=Time.deltaTime;
94 -
95 - if (currentEnemyTarget == null)
96 - {
97 - //Debug.Log ("Find target");
98 - currentEnemyTarget = GameManager.GetClosestShip(GameManager.ENEMY_TAG, this.gameObject);
99 - }
100 -
101 - if (currentEnemyTarget)
102 - {
103 - Transform enemyShip = currentEnemyTarget.transform;
104 - Debug.DrawLine(transform.position, enemyShip.position, Color.yellow);
105 -
106 - //transform.LookAt(enemyShip);
107 - Vector3 relative = enemyShip.position - transform.position;
108 - float distance = relative.magnitude;
109 -
110 - relative.Normalize();
111 -
112 - //Debug.DrawLine (this.transform.position, this.transform.position + targetDir*100.0f, Color.green);
113 - float step = ROTATION_SPEED * Time.deltaTime;
114 - Vector3 newDir = Vector3.RotateTowards (this.transform.forward, relative, step, 0.0f);
115 - this.transform.rotation = Quaternion.LookRotation (newDir);
116 -
117 - if (distance<MIN_ENGAGE_RANGE)
118 - {
119 - StopThruster();
120 - }
121 - else
122 - {
123 - StartThruster();
124 - }
125 -
126 - if ((distance<SHOOT_RANGE) && (Vector3.Dot (relative, transform.forward)>0.5) && (lastShootTime>shootFreq))
127 - {
128 - lastShootTime=0;
129 - foreach (Vector3 _wmp in weaponMountPoints)
130 - {
131 - // Calculate where the position is in world space for the mount point
132 - Vector3 _pos = transform.position + transform.right * _wmp.x + transform.up * _wmp.y + transform.forward * _wmp.z;
133 - // Instantiate the laser prefab at position with the spaceships rotation
134 - // Specify which transform it was that fired this round so we can ignore it for collision/hit
135 - //Transform _laserShot = (Transform) Instantiate(laserShotPrefab[weaponType], _pos, transform.rotation);
136 - //_laserShot.GetComponent<SU_LaserShot>().firedBy = transform;
137 - WeaponFireUtils.FireLaser(laserShotPrefab[weaponType], _pos, this.transform.rotation, this.gameObject, GameManager.ENEMY_TAG);
138 - }
139 - if (soundEffectFire[weaponType] != null)
140 - {
141 - audio.PlayOneShot(soundEffectFire[weaponType]);
142 - }
143 - }
144 - }
145 - }
146 - }
1 + using UnityEngine;
2 + using System.Collections;
3 +
4 + public class AllyShip : MonoBehaviour
5 + {
6 + public SU_Thruster[] thrusters;
7 + public float rollRate = 100.0f;
8 + public float yawRate = 30.0f;
9 + public float pitchRate = 100.0f;
10 + public Vector3[] weaponMountPoints;
11 + public Transform[] laserShotPrefab;
12 + public AudioClip[] soundEffectFire;
13 + public CameraFollow followCamera;
14 + public float life = 100;
15 +
16 + private bool isAlive = true;
17 +
18 + private Rigidbody _cacheRigidbody;
19 + private float shootFreq = 1f;
20 + private float lastShootTime = 0;
21 + private int weaponType = 0;
22 +
23 + private GameObject currentEnemyTarget;
24 +
25 + private static float ROTATION_SPEED = 0.75f; // degrees
26 + private static float SHOOT_RANGE = 2000.0f;
27 + private static float MIN_ENGAGE_RANGE = 300.0f;
28 +
29 + private static float HERO_MIN_RANGE = 1000.0f;
30 + private static float HERO_MAX_RANGE = 2000.0f;
31 +
32 + float genCoord(float min_distance, float max_distance) {
33 + return (Random.value - Random.value) * (min_distance + Random.value * (max_distance-min_distance));
34 + }
35 +
36 + void Start()
37 + {
38 + this.transform.position = new Vector3(
39 + genCoord(HERO_MIN_RANGE, HERO_MAX_RANGE),
40 + genCoord(HERO_MIN_RANGE, HERO_MAX_RANGE),
41 + genCoord(HERO_MIN_RANGE, HERO_MAX_RANGE)
42 + );
43 +
44 + this.transform.rotation = Random.rotation;
45 +
46 + foreach (SU_Thruster _thruster in thrusters)
47 + {
48 + if (_thruster == null)
49 + {
50 + Debug.LogError("Thruster array not properly configured. Attach thrusters to the game object and link them to the Thrusters array.");
51 + }
52 + }
53 +
54 + _cacheRigidbody = rigidbody;
55 + if (_cacheRigidbody == null)
56 + {
57 + Debug.LogError("Spaceship has no rigidbody - the thruster scripts will fail. Add rigidbody component to the spaceship.");
58 + }
59 + }
60 +
61 + public void getHitted(float hit)
62 + {
63 + if (!isAlive) return;
64 +
65 + life -= hit;
66 + if (life<=0)
67 + {
68 + isAlive = false;
69 + Instantiate(GameManager.Instance.explosionEffect, this.transform.position, this.transform.rotation);
70 + CommunicationCenter.Instance.DestroyOneAlly(this.gameObject);
71 + Destroy(this.gameObject);
72 + }
73 + }
74 +
75 + void StartThruster()
76 + {
77 + foreach (SU_Thruster _thruster in thrusters)
78 + {
79 + _thruster.StartThruster();
80 + }
81 + }
82 +
83 + void StopThruster()
84 + {
85 + foreach (SU_Thruster _thruster in thrusters)
86 + {
87 + _thruster.StopThruster();
88 + }
89 + }
90 +
91 + void Update()
92 + {
93 + lastShootTime+=Time.deltaTime;
94 +
95 + if (currentEnemyTarget == null)
96 + {
97 + //Debug.Log ("Find target");
98 + currentEnemyTarget = GameManager.GetClosestShip(GameManager.ENEMY_TAG, this.gameObject);
99 + }
100 +
101 + if (currentEnemyTarget)
102 + {
103 + Transform enemyShip = currentEnemyTarget.transform;
104 + Debug.DrawLine(transform.position, enemyShip.position, Color.yellow);
105 +
106 + //transform.LookAt(enemyShip);
107 + Vector3 relative = enemyShip.position - transform.position;
108 + float distance = relative.magnitude;
109 +
110 + relative.Normalize();
111 +
112 + //Debug.DrawLine (this.transform.position, this.transform.position + targetDir*100.0f, Color.green);
113 + float step = ROTATION_SPEED * Time.deltaTime;
114 + Vector3 newDir = Vector3.RotateTowards (this.transform.forward, relative, step, 0.0f);
115 + this.transform.rotation = Quaternion.LookRotation (newDir);
116 +
117 + if (distance<MIN_ENGAGE_RANGE)
118 + {
119 + StopThruster();
120 + }
121 + else
122 + {
123 + StartThruster();
124 + }
125 +
126 + if ((distance<SHOOT_RANGE) && (Vector3.Dot (relative, transform.forward)>0.5) && (lastShootTime>shootFreq))
127 + {
128 + lastShootTime=0;
129 + foreach (Vector3 _wmp in weaponMountPoints)
130 + {
131 + // Calculate where the position is in world space for the mount point
132 + Vector3 _pos = transform.position + transform.right * _wmp.x + transform.up * _wmp.y + transform.forward * _wmp.z;
133 + // Instantiate the laser prefab at position with the spaceships rotation
134 + // Specify which transform it was that fired this round so we can ignore it for collision/hit
135 + //Transform _laserShot = (Transform) Instantiate(laserShotPrefab[weaponType], _pos, transform.rotation);
136 + //_laserShot.GetComponent<SU_LaserShot>().firedBy = transform;
137 + WeaponFireUtils.FireLaser(laserShotPrefab[weaponType], _pos, this.transform.rotation, this.gameObject, GameManager.ENEMY_TAG);
138 + }
139 + if (soundEffectFire[weaponType] != null)
140 + {
141 + audio.PlayOneShot(soundEffectFire[weaponType]);
142 + }
143 + }
144 + }
145 + }
146 + }