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

public class EnemyShip : MonoBehaviour 
{
	public enum ShipType {SMALL_SHIP, BIG_SHIP};
	public ShipType shipType;
	public float life = 1000;
	public int ID;
	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_RANGE = 5000.0f;
	public static float SMALL_ENEMY_RANGE = 1000.0f;

	public static float EVADE_DISTANCE = 300.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;

	void Start()
	{

		if(shipType==ShipType.SMALL_SHIP)
		{
			this.transform.position = new Vector3(Random.value-Random.value,Random.value-Random.value,Random.value-Random.value)*SMALL_ENEMY_RANGE;
			this.transform.rotation = Random.rotation;
		}
		else
		{
			this.transform.position = new Vector3(Random.value-Random.value,Random.value-Random.value,Random.value-Random.value)*BIG_ENEMY_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);
			if(shipType==ShipType.SMALL_SHIP)
			{
				Destroy(this.transform.parent.gameObject);
			}
			else
			{
				Destroy(this.transform.gameObject);
			}
			CommunicationCenter.Instance.DestroyOneEnemy(ID);
		}
	}

	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.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
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