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
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 Vector3[] weaponMountPoints;
	public Transform laserShotPrefab;
	public AudioClip soundEffectFire;

	private static float BIG_ENEMY_MIN_RANGE = 10000.0f;
	private static float BIG_ENEMY_MAX_RANGE = 20000.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 static float WEAPON_AIM = 2.5f; // degrees

	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);
		}
	}

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

	void OnComplete()
	{
	}

	void Fire()
	{

		foreach (Vector3 _wmp in weaponMountPoints) 
		{
			Quaternion small = Quaternion.Euler(Random.Range(-WEAPON_AIM,WEAPON_AIM),
			                                    Random.Range(-WEAPON_AIM,WEAPON_AIM),
			                                    Random.Range(-WEAPON_AIM,WEAPON_AIM)
			                                    );
			small = small*this.transform.rotation;

			Vector3 _pos = transform.position + transform.right * _wmp.x + transform.up * _wmp.y + transform.forward * _wmp.z;
			Transform _laserShot = (Transform) Instantiate(laserShotPrefab, _pos, small);
			_laserShot.GetComponent<SU_LaserShot>().firedBy = transform;
		}

		if (soundEffectFire != null) 
		{
			audio.PlayOneShot(soundEffectFire);
		}
	}

	void DoSmallShip()
	{
		Transform spaceShip = GameManager.GetHero();//GameManager.Instance.spaceShip;

		if (spaceShip==null) return;

		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;
			}
		}

		/***
		 * 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 );

		/***
		 * Fire routine
		 */

		timeToShot -= Time.deltaTime;
		if(timeToShot>0.0f) {
			return;
		}

		targetDir = spaceShip.position - this.transform.position;
		if (targetDir.magnitude > shootRange) return;

		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;
		}
	}
}

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

Diff revisions: vs.
Revision Author Commited Message
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 Diff Diff LMancini picture LMancini Thu 06 Nov, 2014 18:03:48 +0000
127 Diff Diff DRuega picture DRuega Thu 06 Nov, 2014 16:52:49 +0000

Aggiungo PilotNameGenerator.cs

Genera nomi random alle navi... così sembra più figo :) :)

125 Diff Diff DRuega picture DRuega Thu 06 Nov, 2014 13:44:02 +0000

Migliorata la ricerca del nemico più vicino pesando di più la differenza angolare.
Migliorato lo script dei missili.

122 Diff Diff DRuega picture DRuega Thu 06 Nov, 2014 10:12:00 +0000

Tocco Magico :P
1) Centralizzata creazione navi
2) Levato dove possibile *.Instance.spaceShip
3) Implementato GameManager.GetClosestEnemy usato da Ally. In un secondo momento va messo nella logica del missile.
GetClosestEnemy usa una funzione score valutando distanza e angolo; Privilegia obiettivi sul vettore forward.

119 Diff Diff LMancini picture LMancini Wed 05 Nov, 2014 15:33:48 +0000
117 Diff Diff DRuega picture DRuega Tue 28 Oct, 2014 13:39:51 +0000
116 Diff Diff DRuega picture DRuega Mon 20 Oct, 2014 20:14:25 +0000
113 DRuega picture DRuega Wed 15 Oct, 2014 08:03:00 +0000

Aggiunta tecnica di evasione per i nemici.