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
using UnityEngine;
using System.Collections;

public class EnemyShip : MonoBehaviour 
{
	public enum ShipType {SMALL_SHIP, BIG_SHIP};
	public ShipType shipType;
	public float life = 1000;

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

	public static float FIRE_RATE = 0.5f;
	public static float RELOAD_TIME = 5.0f;
	public static int MAX_SHOOTS = 5;

	public static float MIN_DISTANCE = 50.0f;

	public static float MAX_SPEED = 100.0f;
	public static float ROTATION_SPEED = 0.75f; // degrees

	public static float BIG_ENEMY_MIN_RANGE = 10000.0f;
	public static float BIG_ENEMY_MAX_RANGE = 20000.0f;

	public static float SMALL_ENEMY_MIN_RANGE = 3000.0f;
	public static float SMALL_ENEMY_MAX_RANGE = 8000.0f;
	
	public static float EVADE_DISTANCE = 400.0f;

	public static float WEAPON_AIM = 2.5f; // degrees

	private float timeToShot = RELOAD_TIME;
	private int availableShoots = MAX_SHOOTS;

	private Material mat;
	private bool isAlive = true;

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

	public string pilotName;

	void Start()
	{
		pilotName = PilotNameGenerator.GetNick();

		if(shipType==ShipType.SMALL_SHIP)
		{
			this.transform.position = new Vector3(
				genCoord(SMALL_ENEMY_MIN_RANGE, SMALL_ENEMY_MAX_RANGE),
				genCoord(SMALL_ENEMY_MIN_RANGE, SMALL_ENEMY_MAX_RANGE),
				genCoord(SMALL_ENEMY_MIN_RANGE, SMALL_ENEMY_MAX_RANGE) );
		}
		else
		{
			pilotName = PilotNameGenerator.GetTitle() + " " + pilotName;

			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;

		/***
		 * 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 = MAX_SPEED * Time.deltaTime;

		Vector3 targetDir;

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


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

		//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);
		if (distance > MIN_DISTANCE) {
			this.transform.Translate( Vector3.forward * speed );
		}
		/***
		 * Fire routine
		 */

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

		targetDir = spaceShip.position - this.transform.position;
		float cosAlpha = Vector3.Dot (this.transform.forward, targetDir);
		if (cosAlpha < 0.0f) return;

		if (availableShoots > 0) {
			availableShoots--;
			Fire();
			timeToShot += FIRE_RATE;
		} else {
			timeToShot = RELOAD_TIME;
			availableShoots += MAX_SHOOTS;
		}
	}
}

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

Diff revisions: vs.
Revision Author Commited Message
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 Diff Diff DRuega picture DRuega Wed 15 Oct, 2014 08:03:00 +0000

Aggiunta tecnica di evasione per i nemici.

112 Diff Diff FMMortaroli picture FMMortaroli Thu 09 Oct, 2014 14:21:59 +0000
107 FMMortaroli picture FMMortaroli Thu 09 Oct, 2014 11:56:46 +0000