Nextrek
Diff Revisions
138
vs
139
for /3DSpace/Assets/Custum/Scripts/GameManager.cs
|
@@ -61,26 +61,48 @@ |
61 |
61 |
|
return null; |
62 |
62 |
|
} |
63 |
63 |
|
|
64 |
|
- |
public static GameObject GetClosestEnemy(Transform observer) |
|
64 |
+ |
private static float evaluateShip(GameObject observer, GameObject enemy) |
65 |
65 |
|
{ |
66 |
|
- |
GameObject[] enemies = GameObject.FindGameObjectsWithTag(ENEMY_TAG); |
|
66 |
+ |
Vector3 relativePosition = enemy.transform.position - observer.transform.position; |
|
67 |
+ |
float distance = relativePosition.magnitude; |
|
68 |
+ |
relativePosition.Normalize(); |
|
69 |
+ |
|
|
70 |
+ |
// -1 0 +1 -> 3 2 1 |
|
71 |
+ |
float penalty = (2.0f - Vector3.Dot(observer.transform.forward, relativePosition)) / 3.0f; |
|
72 |
+ |
float score = distance * penalty * penalty * penalty; |
|
73 |
+ |
|
|
74 |
+ |
return score; |
|
75 |
+ |
} |
|
76 |
+ |
|
|
77 |
+ |
public static GameObject GetClosestShip(string tag, GameObject observer) |
|
78 |
+ |
{ |
|
79 |
+ |
GameObject[] entities = GameObject.FindGameObjectsWithTag(tag); |
67 |
80 |
|
|
68 |
81 |
|
GameObject closest = null; |
69 |
82 |
|
float bestScore = Mathf.Infinity; |
70 |
83 |
|
|
71 |
|
- |
foreach(GameObject enemy in enemies) |
|
84 |
+ |
foreach(GameObject entity in entities) |
72 |
85 |
|
{ |
73 |
|
- |
Vector3 relativePosition = enemy.transform.position - observer.transform.position; |
74 |
|
- |
float distance = relativePosition.magnitude; |
75 |
|
- |
relativePosition.Normalize(); |
76 |
|
- |
|
77 |
|
- |
// -1 0 +1 -> 3 2 1 |
78 |
|
- |
float penalty = (2.0f - Vector3.Dot(observer.transform.forward, relativePosition)) / 3.0f; |
79 |
|
- |
float score = distance * penalty * penalty * penalty; |
|
86 |
+ |
float score = GameManager.evaluateShip(observer, entity); |
80 |
87 |
|
|
81 |
88 |
|
if (score < bestScore) { |
82 |
89 |
|
bestScore = score; |
83 |
|
- |
closest = enemy; |
|
90 |
+ |
closest = entity; |
|
91 |
+ |
} |
|
92 |
+ |
} |
|
93 |
+ |
|
|
94 |
+ |
if (tag == GameManager.ALLY_TAG) |
|
95 |
+ |
{ |
|
96 |
+ |
// Include Hero in Ally list |
|
97 |
+ |
GameObject entity = GameObject.FindGameObjectWithTag(HERO_TAG); |
|
98 |
+ |
if (entity != null) |
|
99 |
+ |
{ |
|
100 |
+ |
float score = GameManager.evaluateShip(observer, entity); |
|
101 |
+ |
|
|
102 |
+ |
if (score < bestScore) { |
|
103 |
+ |
bestScore = score; |
|
104 |
+ |
closest = entity; |
|
105 |
+ |
} |
84 |
106 |
|
} |
85 |
107 |
|
} |
86 |
108 |
|
/* |