-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
212 lines (188 loc) · 5.37 KB
/
game.js
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
window.addEventListener('load', function () {
let canvas = document.querySelector('canvas');
if (canvas) {
canvas.addEventListener('wheel', function (e) {
window.scrollBy(0, e.deltaY);
e.preventDefault();
}, false);
}
let cellImage = document.getElementById('whiteblood-cell');
let wbcImages = ['./white_blood_cell_1.png', './white_blood_cell_2.png'];
let currentImageWbc = 0;
setInterval(function () {
currentImageWbc = (currentImageWbc + 1) % wbcImages.length;
cellImage.src = wbcImages[currentImageWbc];
}, 800);
let virusImage = document.getElementById('virus');
let virusImages = ['./greenvirus1.png', './greenvirus3.png'];
let currentImageVirus = 0;
setInterval(function () {
currentImageVirus = (currentImageVirus + 1) % virusImages.length;
virusImage.src = virusImages[currentImageVirus];
}, 800);
});
let NEATconfig = {
model: [
// Angle to nearest food relative to world, current angle
{ nodeCount: 3, type: "input" },
{ nodeCount: 2, type: "output", activationfunc: activation.SOFTMAX },
],
mutationRate: 0.1,
crossoverMethod: crossover.RANDOM,
mutationMethod: mutate.RANDOM,
populationSize: TOTALkirbies,
};
const config = {
type: Phaser.AUTO,
width: 3000,
height: 3000,
physics: {
default: "arcade",
arcade: {
gravity: { y: 0 },
debug: false,
},
},
scene: {
preload: preload,
create: create,
update: update,
},
};
const game = new Phaser.Game(config);
let npcs = [];
function preload() {
this.load.image("dot", "./white_blood_cell_1.png");
this.load.image("food", "./greenvirus1.png");
this.load.image("background", "./vessel5.png");
}
let reproductionRound = 0; // how many rounds have passed
let repoductionTriggers = [10, 10, 15, 20, 1, 1, 1, 1,1, 1, 1, 1,1, 1, 1, 1]; // hard coded trigger values
let whichhitTrigger = []; // which hit trigger so they dont hit twice
let NEATstore = []; // store the NEAT objects
function create() {
let bg = this.add.image(0, 0, "background").setOrigin(0, 0);
bg.displayWidth = this.sys.game.config.width;
bg.displayHeight = this.sys.game.config.height;
food = this.physics.add.group();
this.time.addEvent({
delay: 3000,
callback: createFood,
callbackScope: this,
loop: true,
});
npcs = [];
runtimePrint("spawning " + TOTALkirbies);
for (let i = 0; i < TOTALkirbies; i++) {
npcs.push(
new npc(
this,
Phaser.Math.Between(0, config.width),
Phaser.Math.Between(0, config.height),
i,
),
);
}
for (let i = 0; i < 300; i++) {
createFood();
}
//create neat
NEATstore[0] = new NEAT(NEATconfig);
this.time.addEvent({
delay: 150,
callback: createFood,
callbackScope: this,
loop: true,
});
this.time.addEvent({
delay: 1000,
callback: tickHunger,
callbackScope: this,
loop: true,
});
}
function update() {
if (npcs.length < 1) {
runtimePrint("All NPC's dead");
}
for (const npc of npcs) {
if (!npc) continue;
npc.update();
this.physics.overlap(npc.sprite, food, eatFood, null, this);
}
let decisionsPerRound = [];
for (let i in NEATstore) {
NEATstore[i].feedForward();
decisionsPerRound[i] = NEATstore[i].getDesicions();
}
for (let i = 0; i < npcs.length; i++) {
if (npcs[i] && npcs[i].sprite) {
npcs[i].move(
decisionsPerRound[Math.floor(i / TOTALkirbies)][i % TOTALkirbies],
);
}
}
}
function tickHunger() {
for (let index in npcs) {
let tickedNPC = npcs[index];
if (!tickedNPC) continue;
tickedNPC.hunger -= 2;
if (tickedNPC.hunger <= 0) {
tickedNPC.bar.destroy();
tickedNPC.sprite.destroy();
npcs[index] = undefined;
//npcs.splice(index, 1);
//delete spawnedNPC;
}
if (tickedNPC.hunger >= 100) {
runtimePrint("hunger threshold hit");
if (
!whichhitTrigger.includes(index) &&
Math.floor((npcs.length - 1) / TOTALkirbies) == reproductionRound
) {
whichhitTrigger.push(index);
}
if (whichhitTrigger.length >= repoductionTriggers[reproductionRound] || reproductionRound > 5 && whichhitTrigger.length >= 1) {
reproductionRound++;
whichhitTrigger = [];
NEATstore[reproductionRound] = new NEAT(NEATconfig);
NEATstore[reproductionRound].import(
NEATstore[reproductionRound - 1].export(),
);
for (let i = reproductionRound * TOTALkirbies; i < npcs.length; i++) {
if (npcs[i]) {
NEATstore[reproductionRound].setFitness(
npcs[i].hunger,
i % TOTALkirbies,
);
}
}
NEATstore[reproductionRound].doGen();
//generational debt..
}
for (let oldnpc of npcs) {
if (reproductionRound * TOTALkirbies + TOTALkirbies <= npcs.length) {
break;
}
if (oldnpc && oldnpc.hunger >= 100 && oldnpc.sprite) {
oldnpc.hunger = 75;
npcs.push(
new npc(this, oldnpc.sprite.x, oldnpc.sprite.y, npcs.length),
);
}
}
}
}
}
function createFood() {
const x = Phaser.Math.Between(0, config.width);
const y = Phaser.Math.Between(0, config.height);
food.create(x, y, "food").setScale(1);
}
function eatFood(sprite, food) {
sprite.npc.hunger += 15;
food.destroy();
}
runtimePrint = console.log;
// console.log = () => { }; // disable console.log