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

public class GameManager : MonoBehaviour 
{
	public static GameManager Instance;
	public Transform[] spaceShipPrefebs;

	public Transform explosionEffect;

	public static string HERO_TAG = "Player";
	public static string ENEMY_TAG = "Enemy";
	public static string ALLY_TAG = "Ally";

	void Awake()
	{
		Instance = this;

		int heroShipType = 0;
		if (GlobalObject.Instance) {
			heroShipType = GlobalObject.Instance.shipType;
		} else {
			Debug.LogError("GlobalObject.Instance is NULL!");
		}

		CreateHero(spaceShipPrefebs[heroShipType]);
	}

	public static Transform CreateHero(Transform template)
	{
		Transform tp = Instantiate(template, Vector3.zero, Quaternion.identity) as Transform;
		//tp.tag = HERO_NAME;

		return tp;
	}

	public static Transform CreateEnemy(Transform template)
	{
		Transform tp = Instantiate(template, Vector3.zero, Quaternion.identity) as Transform;
		// Defined in prefabs
		// tp.tag = ENEMY_NAME;
		
		return tp;
	}

	public static Transform CreateAlly(Transform template)
	{
		Transform tp = Instantiate(template, Vector3.zero, Quaternion.identity) as Transform;
		// Defined in prefabs
		// tp.tag = ALLY_NAME;
		
		return tp;
	}

	public static Transform GetHero()
	{
		GameObject heroGameObject = GameObject.FindGameObjectWithTag(HERO_TAG);
		if (heroGameObject != null) {
			return heroGameObject.transform;
		}
		return null;
	}

	private static float evaluateShip(GameObject observer, GameObject enemy)
	{
		Vector3 relativePosition = enemy.transform.position - observer.transform.position;
		float distance = relativePosition.magnitude;
		relativePosition.Normalize();
		
		// -1 0 +1 -> 3 2 1
		float penalty = (2.0f - Vector3.Dot(observer.transform.forward, relativePosition)) / 3.0f;
		float score = distance * penalty * penalty * penalty;

		return score;
	}

	public static GameObject GetClosestShip(string tag, GameObject observer)
	{
		GameObject[] entities = GameObject.FindGameObjectsWithTag(tag);

		GameObject closest = null;
		float bestScore = Mathf.Infinity;

		foreach(GameObject entity in entities) 
		{
			float score = GameManager.evaluateShip(observer, entity);

			if (score < bestScore) {
				bestScore = score;
				closest = entity;
			}
		}

		if (tag == GameManager.ALLY_TAG)
		{
			// Include Hero in Ally list
			GameObject entity = GameObject.FindGameObjectWithTag(HERO_TAG);
			if (entity != null)
			{
				float score = GameManager.evaluateShip(observer, entity);
				
				if (score < bestScore) {
					bestScore = score;
					closest = entity;
				}
			}
		}
		/*
		if (closest) 
		{
			Debug.Log("Found: " + closest.transform.position.ToString());
		} else {
			Debug.Log("Nothing FOUND!!");
		}*/
		return closest;
	}

	public static void HandleWeaponHit(RaycastHit _hit, float hitPower, Transform firedBy, Transform explosionEffect, Quaternion _rotation)
	{
		if(_hit.transform.tag==HERO_TAG && firedBy.tag!=HERO_TAG)
		{
			Life l = _hit.transform.GetComponent<Life>();
			if(l!=null)
				l.getHitted(hitPower);
			CommunicationCenter.Instance.ShowSmallCamera2(true);
		}
		else if(_hit.transform.tag==ENEMY_TAG)
		{
			EnemyShip es = _hit.transform.GetComponent<EnemyShip>();
			if(es!=null)
				es.getHitted(hitPower);
		}
		else if(_hit.transform.tag==ALLY_TAG)
		{
			AllyShip a = _hit.transform.GetComponent<AllyShip>();
			if(a !=null)
				a.getHitted(hitPower);
		}
		else if (_hit.transform.tag!=HERO_TAG && Random.Range(0,20) < 2) 
		{ 
			if(StartGameUI.currentLanguage == 0)
			MessageWindow.Instance.NewMessage ("An asteroid has been destroyed");
			if(StartGameUI.currentLanguage == 1)
			MessageWindow.Instance.NewMessage ("Un asteroide e' stato distrutto");
			
			// Instantiate the explosion effect at the point of impact
			Instantiate(explosionEffect, _hit.transform.position, _rotation);
			// Destroy the game object that we just hit
			Destroy(_hit.transform.gameObject);
		}
	}

	void Start()
	{

	}
	
	// Update is called once per frame
	void Update () 
	{
		if (Input.GetKey (KeyCode.Escape))					
		{
			Application.Quit();
		}

		if (Input.GetKey (KeyCode.Z))
		{
			CommunicationCenter.Instance.ShowSmallCamera1(true);
		}
	}
}

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

Diff revisions: vs.
Revision Author Commited Message
160 Diff Diff LMancini picture LMancini Fri 20 Feb, 2015 15:04:04 +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.

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.

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 :) :)

126 Diff Diff DRuega picture DRuega Thu 06 Nov, 2014 14:45:25 +0000

Cleanup, warnings e altre minchiate :)

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 DRuega picture DRuega Tue 28 Oct, 2014 13:39:51 +0000