Subversion Repository Public Repository

Nextrek

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using UnityEngine;
using System.Collections;

public class EnemyShip : MonoBehaviour 
{
	public static float ROTATION_SPEED = 0.75f; // degrees

	public enum ShipType {SMALL_SHIP, BIG_SHIP};
	public ShipType shipType;
	public float life = 1000;
	public float topSpeed = 100.0f;
	public int maxShoots = 5;
	public float fireRate = 0.5f;
	public float reloadTime = 5.0f;
	public float shootRange = 2000.0f;
	public float fireTemp = 5f;

	public Vector3[] weaponMountPoints;
	public Transform laserShotPrefab;
	public AudioClip soundEffectFire;

	private static float BIG_ENEMY_MIN_RANGE = 10000.0f;
	private static float BIG_ENEMY_MAX_RANGE = 15000.0f;

	private static float SMALL_ENEMY_INITIAL_RANGE = 20000.0f;
	private static float SMALL_ENEMY_START_RANGE = 3500.0f;

	private static float WARP_SPEED = 10000.0f;
	private static float EVADE_DISTANCE = 400.0f;
	
	private float timeToShot = 0;
	private int availableShoots = 0;

	private bool isAlive = true;

	private bool isEnteringArea = true;
	private Vector3 initialPosition;

	float genCoord(float min_distance, float max_distance) {
		return (Random.value - Random.value) * (min_distance + Random.value * (max_distance-min_distance));
	}

	void Start()
	{
		if(shipType==ShipType.SMALL_SHIP)
		{
			this.transform.position = Random.onUnitSphere * SMALL_ENEMY_INITIAL_RANGE;
			Transform spaceShip = GameManager.GetHero();
			if (spaceShip != null) 
			{
				this.transform.LookAt(spaceShip.position);
				return;
			}

		}
		else
		{
			this.transform.position = new Vector3(
				genCoord(BIG_ENEMY_MIN_RANGE, BIG_ENEMY_MAX_RANGE),
				genCoord(BIG_ENEMY_MIN_RANGE, BIG_ENEMY_MAX_RANGE),
				genCoord(BIG_ENEMY_MIN_RANGE, BIG_ENEMY_MAX_RANGE) );
		}
		this.transform.rotation = Random.rotation;
	}

	public void getHitted(float hit)
	{
		if(!isAlive) return;

		life -= hit;
		if(life<=0)
		{
			isAlive = false;
			Instantiate(GameManager.Instance.explosionEffect, this.transform.position, this.transform.rotation);

			CommunicationCenter.Instance.DestroyOneEnemy(this.gameObject);
			/*if(shipType==ShipType.SMALL_SHIP)
			{
				Destroy(this.transform.parent.gameObject);
			}
			else
			{
				Destroy(this.transform.gameObject);
			}*/
			Destroy(this.gameObject);
		}
	}

	GameObject _lastTarget = null;
	float _timer = 0;
	private static float CHANGE_TARGET_TIMER = 5000.0f;

	void Update()
	{
		fireTemp -= Time.deltaTime;
		//Transform spaceShip = GameManager.Instance.spaceShip;
		//if (spaceShip==null) return;
		_timer += Time.deltaTime;
		
		if ((_lastTarget == null) || (_timer > CHANGE_TARGET_TIMER))
		{
			_timer = 0;
			_lastTarget = GameManager.GetClosestShip(GameManager.ALLY_TAG, this.gameObject);
		}
		
		if (_lastTarget == null) return;

		Transform spaceShip = _lastTarget.transform;

		if(shipType==ShipType.SMALL_SHIP)
		{
			DoSmallShip(spaceShip);
		}

		/***
		 * Fire routine
		 */
		
		timeToShot -= Time.deltaTime;
		if(timeToShot>0.0f) {
			return;
		}
		
		Vector3 targetDir = spaceShip.position - this.transform.position;
		if (targetDir.magnitude > shootRange) return;

		if(shipType==ShipType.SMALL_SHIP)
		{
			float cosAlpha = Vector3.Dot (this.transform.forward, targetDir);
			if (cosAlpha < 0.5f) return;
		}

		if (availableShoots > 0) {
			availableShoots--;
		
			Fire();
			
			timeToShot += fireRate;
		} else {
			timeToShot = reloadTime;
			availableShoots += maxShoots;
		}

	}

	void OnComplete()
	{
	}

	void Fire()
	{
		foreach (Vector3 _wmp in weaponMountPoints) {
		Vector3 _pos = transform.position + transform.right * _wmp.x + transform.up * _wmp.y + transform.forward * _wmp.z;
		if (shipType == ShipType.SMALL_SHIP) {
				WeaponFireUtils.FireLaser (laserShotPrefab, _pos, this.transform.rotation, this.gameObject, GameManager.ALLY_TAG);
		} else {
			    WeaponFireUtils.FireLaserTurret (laserShotPrefab, _pos, this.transform.rotation, this.gameObject, GameManager.ALLY_TAG);
			}
		}

		if (soundEffectFire != null) {
		GetComponent<AudioSource>().PlayOneShot (soundEffectFire);
			}
	}
	
	void DoSmallShip(Transform spaceShip)
	{

		if (isEnteringArea)
		{
			if (Vector3.Distance(this.transform.position, spaceShip.position) > SMALL_ENEMY_START_RANGE)
			{
				this.transform.Translate( Vector3.forward * WARP_SPEED * Time.deltaTime );
				return;
			}
			else 
			{
				isEnteringArea = false;
				if(StartGameUI.currentLanguage == 0)
				MessageWindow.Instance.NewMessage ("An enemy has entered in radar range");
				if(StartGameUI.currentLanguage == 1)
				MessageWindow.Instance.NewMessage ("Un nemico e' apparso sul radar");
			}
		}

		/***
		 * Navigation routine
		 */
		float distance = Vector3.Distance(this.transform.position, spaceShip.position);
		
		// The step size is equal to speed times frame time.
		float step = ROTATION_SPEED * Time.deltaTime;

		float speed = topSpeed * Time.deltaTime;

		Vector3 targetDir;

		if (distance < EVADE_DISTANCE) {
			// evade!!
			targetDir = Vector3.Cross (this.transform.forward, spaceShip.right);


		} else {
			targetDir = spaceShip.position - this.transform.position;
			targetDir.Normalize();
		}

		//Debug.DrawLine (this.transform.position, this.transform.position + targetDir*100.0f, Color.green);

		Vector3 newDir = Vector3.RotateTowards (this.transform.forward, targetDir, step, 0.0f);
		this.transform.rotation = Quaternion.LookRotation (newDir);

		this.transform.Translate( Vector3.forward * speed );
	}
}

Commits for Nextrek/3DSpace/Assets/Custum/Scripts/EnemyShip.cs

Diff revisions: vs.
Revision Author Commited Message
168 Diff Diff LMancini picture LMancini Wed 08 Apr, 2015 12:33:35 +0000
160 Diff Diff LMancini picture LMancini Fri 20 Feb, 2015 15:04:04 +0000
144 Diff Diff LMancini picture LMancini Mon 17 Nov, 2014 15:18:43 +0000
141 Diff Diff DRuega picture DRuega Fri 14 Nov, 2014 08:20:17 +0000

Primo tentativo di unificare le procedure di fuoco delle armi. Passo necessario per far si che i nemici possano sparare i missili

140 Diff Diff LMancini picture LMancini Thu 13 Nov, 2014 23:04:13 +0000
139 Diff Diff DRuega picture DRuega Mon 10 Nov, 2014 21:34:58 +0000

I nemici ora se la prendono pure con gli alleati, non puntano solo il giocatore.
La funzione GetNearestEnemy è diventata GetNearestShip a cui si può indicare il tag da cercare.
Sarà utile nel caso i nemici sparino missili.

138 Diff Diff DRuega picture DRuega Mon 10 Nov, 2014 18:41:38 +0000

AllyShip: Smooth Step Rotation.

134 Diff Diff DRuega picture DRuega Fri 07 Nov, 2014 18:48:12 +0000

In GameManager ho aggiunto HandleWeaponHit per centralizzare e generalizzare la gestione degli impatti delle armi (SU_LaserShot e Missile).
Ho modificato i parametri dei nemici per renderli più aggressivi.

131 Diff Diff DRuega picture DRuega Thu 06 Nov, 2014 18:25:48 +0000

Merge.

130 LMancini picture LMancini Thu 06 Nov, 2014 18:03:48 +0000