-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slime.cs
256 lines (219 loc) · 9.6 KB
/
Slime.cs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Slime : MonoBehaviour
{
public Rigidbody rb;
public Transform tf;
GameObject constante;
CONSTANTES constantes;
public int Life = 100;
//Genes
public int maxLife = 100;
public float height = 1f;
public float speed = 1f;
public float viewRange = 5f;
public float beauty = 1f;
public int gestationDelay = 10; //Nombre de tours avant naissance des enfants
public double direction = -1;
//Params
public string gender = "male";
public float forceIntensity = 100f;
float lateralForce = 0f;
float forwardForce = 0f;
int sinceTick = 0;
int sinceMate = 0; //Duree actuelle de gestation; ne concerne que female
public Slime partner = null; //Référence du partenaire, pour avoir les gènes
float[] mateGenes;
float[] Genes;
List<Slime> localSlimeList;
List<Food> localFoodList;
void Start() {
Genes = new float[] {(float)maxLife, height, speed, viewRange, beauty, (float)gestationDelay};
//Récupération des constantes de jeu
constante = GameObject.Find("CONSTANTES");
constantes = constante.GetComponent<CONSTANTES>();
//Préparation des données locales
Vector3 scale = new Vector3 (height, height, height);
transform.localScale = scale;
rb.mass = height * height * height;
sinceTick = (int)(Random.Range(0, constantes.tickSpeed));
}
void OnEnable() { //Slime ajouté la la liste globale à la naissance
CONSTANTES.slimeList.Add(this);
}
void OnDisable() {
CONSTANTES.slimeList.Remove(this);
}
void Update () {
if (sinceTick >= constantes.tickSpeed) { //Actions à chaque tick de jeu
direction = -1;
//Survie
if (Life <= 0 | transform.position.y < -1f){ //Slime meurt si plus de vie
CONSTANTES.slimeList.Remove(this);
Destroy(gameObject);
}
//Reproduction
if (partner != null){ //Si un autre slime a cherché à mate et critère de beauté
if (partner.beauty/beauty >= Random.Range(0f, 2f)) {
Life = Life / 2;
partner.Life = partner.Life / 2;
if (gender == "female") {
mateGenes = new float[] {(float)partner.maxLife, partner.height, partner.speed, partner.viewRange, partner.beauty, (float)partner.gestationDelay};
partner.partner = null;
partner = null;
}
else {
partner.mateGenes = new float[] {(float)partner.maxLife, partner.height, partner.speed, partner.viewRange, partner.beauty, (float)partner.gestationDelay};
partner.partner = null;
partner = null;
}
}
else {
partner = null;
}
}
if (mateGenes != null) { // Si on a un génome de partner
if (sinceMate < gestationDelay) {
sinceMate += 1;
}
else {
float[] offspringGenome = mutateGenome(mixGenomes(Genes, mateGenes)); //Récupère le génome enfant, muté
GameObject.Find("Spawner").GetComponent<Spawner>().spawnOffspring(offspringGenome); //Spawn l'enfant
Debug.Log("Mated and reproduced !");
sinceMate = 0;
mateGenes = null;
}
}
if (CONSTANTES.slimeList.Count > 0) { //Action si un slime dans le coin
Slime nearestSlime = getNearestSlime(CONSTANTES.slimeList);
if (Life >= maxLife / 2 & nearestSlime.gender != gender & (nearestSlime.transform.position - transform.position).magnitude <= viewRange) { //S'il est de genre opposé -> mate
tryMate(nearestSlime);
}
else { //Sinon -> Attack
if ((nearestSlime.transform.position - transform.position).magnitude <= viewRange) {
attack(getNearestSlime(CONSTANTES.slimeList), 10);
}
}
}
if (CONSTANTES.foodList.Count > 0) { //Recherche de nourriture
Food nearestFood = getNearestFood(CONSTANTES.foodList);
if ((nearestFood.transform.position - transform.position).magnitude <= viewRange) {
eat(nearestFood, 10);
}
else {
float deltaz = nearestFood.transform.position.z - transform.position.z;
float deltax = nearestFood.transform.position.x - transform.position.x;
direction = Mathf.Atan2(deltaz, deltax);
}
}
//Perte d'énergie après le mouvement
Life -= (int)(speed * (double)constantes.foodUsageMultiplier * height * height * height);
//Direction, mouvement
if (direction == -1 | (float) Life / (float) maxLife >= 0.5) { //Conditions pour ne pas avoir à focus la nourriture
direction = Random.Range((float)0, (float)(2 * System.Math.PI));
}
//Rotation
var angles = transform.rotation.eulerAngles;
angles.z = (float)direction * (180f / (float)System.Math.PI);
var rotation = Quaternion.Euler(angles);
tf.rotation = rotation;
//Forces
forwardForce = (speed * (float)System.Math.Sin(direction) * constantes.movementForceMultiplier);
lateralForce = (speed * (float)System.Math.Cos(direction) * constantes.movementForceMultiplier);
//Debug.Log(string.Format("{0}, {1}, {2}", System.Math.Sin(direction), lateralForce, forwardForce));
sinceTick = 0;
}
else {
sinceTick += 1;
}
}
void FixedUpdate () {
//Application des forces calculées dans Update
rb.AddForce(lateralForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
rb.AddForce(0, 0, forwardForce * Time.deltaTime, ForceMode.VelocityChange);
}
void attack(Slime autreSlime, int degats){
Life -= constantes.minimalAttackHarm;
autreSlime.Life -= constantes.minimalAttackHarm;
Life -= constantes.attackHarmMultiplier * ((int)(autreSlime.height / height) - 1);
autreSlime.Life += constantes.attackHarmMultiplier * ((int)(autreSlime.height / height) - 1);
}
void eat(Food food, int amount) {
food.GetComponent<Food>().foodAmount -= amount;
Life = Mathf.Min(maxLife, Life + amount);
}
Slime getNearestSlime(List<Slime> slimes) {
// Détermination du slime le plus proche
int length = slimes.Count;
float minimum = 0f;
int plusProcheSlime = 0;
Slime autreSlime;
for (int i = 0; i < length; i++){
autreSlime = slimes[i];
if (autreSlime != this & autreSlime != null) {
float distance = (autreSlime.transform.position - transform.position).magnitude;
if (distance < minimum | minimum == 0f) {
plusProcheSlime = i;
minimum = distance;
}
}
}
return slimes[plusProcheSlime];
}
Food getNearestFood(List<Food> foods) {
//Détermination de la nourriture la plus proche
int length = foods.Count;
float minimum = 0f;
int plusProcheFood = 0;
Food food;
for (int i = 0; i < length; i++){
food = foods[i];
if (food != null) {
float distance = (food.transform.position - transform.position).magnitude;
if ((distance < minimum | minimum == 0f) & food.GetComponent<Food>().foodAmount > 0) {
plusProcheFood = i;
minimum = distance;
}
}
}
return foods[plusProcheFood];
}
void tryMate(Slime potentialMatePartner) { //Approche pour mate. La réussite dépend de la beauté, bien sûr.
if (potentialMatePartner.beauty/beauty >= Random.Range(0f, 2f)) {
potentialMatePartner.partner = this;
}
}
//Utilitaires génétiques
float[] mixGenomes(float[] G1, float[] G2){
int L = G1.Length;
float[] res = new float[L];
for (int i = 0; i < L; i++) {
res[i] = 0.5f * (G1[i] + G2[i]);
}
return res;
}
float[] mutateGenome(float[] genome) {
int L = genome.Length;
for (int i = 0; i < L; i++) {
if (Random.Range(0f, 1f) <= constantes.MutationRate) {
genome[i] = Mathf.Max(0f, genome[i] + GaussianExtended(0f, genome[i] * constantes.MutationEffectMultiplier));
}
}
return genome;
}
public static float Gaussian() {
float v1, v2, s;
do {
v1 = 2.0f * Random.Range(0f,1f) - 1.0f;
v2 = 2.0f * Random.Range(0f,1f) - 1.0f;
s = v1 * v1 + v2 * v2;
} while (s >= 1.0f || s == 0f);
s = Mathf.Sqrt((-2.0f * Mathf.Log(s)) / s);
return v1 * s;
}
public static float GaussianExtended(float mean, float standard_deviation)
{
return mean + Gaussian() * standard_deviation;
}
}