-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlayer.js
164 lines (153 loc) · 6.07 KB
/
Player.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
class Player{
constructor(){
this.pos = createVector(3*tileSize + xoff,8* tileSize + yoff);
this.vel = createVector(0,0);
this.size = tileSize/2.0;
this.playerSpeed = tileSize/15.0;
this.dead = false;
this.reachedGoal = false;
this.fadeCounter = 255;
this.isBest = false;
this.deathByDot = false;
this.deathAtStep = 0;
this.moveCount = 0;
this.gen =1;
this.fitness = 0;
this.nodes = [];
this.fading = false;
this.brain = new Brain(numberOfSteps);
this.human = false;
this.coins = [
new Coin(18.5*tileSize+xoff, 7*tileSize+yoff),
new Coin(10.5*tileSize+xoff, 3.5*tileSize+yoff),
new Coin(7*tileSize+xoff, 6*tileSize+yoff),
new Coin(6*tileSize+xoff, 5*tileSize+yoff),
new Coin(2.5*tileSize+xoff, 3.5*tileSize+yoff),
new Coin(4.5*tileSize+xoff, 3.5*tileSize+yoff),
new Coin(4.5*tileSize+xoff, 2.5*tileSize+yoff),
];
this.setNodes();
}
setNodes() {
this.nodes[0] = new Node(this.coins[0],false,true);
this.nodes[1] = new Node(this.coins[1],false,true);
this.nodes[2] = new Node(this.coins[2],false,true);
this.nodes[3] = new Node(this.coins[3],false,true);
this.nodes[4] = new Node(this.coins[4],false,true);
this.nodes[5] = new Node(this.coins[5],false,true);
this.nodes[6] = new Node(this.coins[6],false,true);
this.nodes[7] = new Node(tiles[19][2],true,false);
this.nodes[6].setDistanceToFinish(this.nodes[7]);
this.nodes[5].setDistanceToFinish(this.nodes[6]);
this.nodes[4].setDistanceToFinish(this.nodes[5]);
this.nodes[3].setDistanceToFinish(this.nodes[4]);
this.nodes[2].setDistanceToFinish(this.nodes[3]);
this.nodes[1].setDistanceToFinish(this.nodes[2]);
this.nodes[0].setDistanceToFinish(this.nodes[1]);
}
show(){
fill(230, 230, 255, this.fadeCounter);
if (this.isBest && !showBest) {
fill(255, 0, 0, this.fadeCounter);
}
stroke(0, 0, 0, this.fadeCounter);
strokeWeight(4);
rect(this.pos.x, this.pos.y, this.size, this.size);
stroke(0);
for (var coinNum = 0; coinNum < this.coins.length; coinNum++) {
this.coins[coinNum].show();
}
}
move(){
if (!humanPlaying){
if (this.moveCount == 0) {//move in the direction for 6 frames
if (this.brain.directions.length > this.brain.step) {//if there are still directions left then set the velocity as the next PVector in the direcitons array
this.vel = this.brain.directions[this.brain.step];
this.brain.step++;
} else {//if at the end of the directions array then the player is dead
this.dead = true;
this.fading = true;
}
this.moveCount =6;
} else {
this.moveCount--;
}
}
var temp = createVector(this.vel.x, this.vel.y);
temp.normalize();
temp.mult(this.playerSpeed);
for (var i = 0; i< solids.length; i++) {
temp = solids[i].restrictMovement(this.pos, createVector(this.pos.x+this.size, this.pos.y+this.size), temp);
}
this.pos.add(temp);
}
//checks if the player
checkCollisions() {
for (var coinNum = 0; coinNum < this.coins.length; coinNum++) {
this.coins[coinNum].collides(this.pos, createVector(this.pos.x+this.size, this.pos.y+this.size));
}
for (var i = 0; i< dots.length; i++) {
if (dots[i].collides(this.pos, createVector(this.pos.x+this.size, this.pos.y+this.size))) {
this.fading = true;
this.dead = true;
this.deathByDot = true;
this.deathAtStep = this.brain.step;
}
}
if (this.coins[0].taken && this.coins[1].taken && this.coins[2].taken && this.coins[3].taken &&
this.coins[4].taken && this.coins[5].taken && this.coins[6].taken &&
winArea.collision(this.pos, createVector(this.pos.x+this.size, this.pos.y+this.size))) {
this.reachedGoal = true;
}
for (var i = 0; i< this.nodes.length; i++) {
this.nodes[i].collision(this.pos, createVector(this.pos.x+this.size, this.pos.y+this.size));
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
update() {
if (!this.dead && !this.reachedGoal) {
this.move();
this.checkCollisions();
} else if (this.fading) {
if (this.fadeCounter > 0) {
if(humanPlaying || replayGens){
this.fadeCounter -=10;
}else{
this.fadeCounter = 0;
}
}
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
calculateFitness() {
if (this.reachedGoal) {//if the dot reached the goal then the fitness is based on the amount of steps it took to get there
this.fitness = 1.0/16.0 + 10000.0/(this.brain.step * this.brain.step);
} else {//if the dot didn't reach the goal then the fitness is based on how close it is to the goal
var estimatedDistance = 0.0;//the estimated distance of the path from the player to the goal
for (var i = this.nodes.length-1; i>=0; i--) {
if (!this.nodes[i].reached) {
estimatedDistance = this.nodes[i].distToFinish;
estimatedDistance += dist(this.pos.x, this.pos.y, this.nodes[i].pos.x, this.nodes[i].pos.y);
}
}
if (this.deathByDot) {
estimatedDistance *= 0.9;
}
this.fitness = 1.0/(estimatedDistance * estimatedDistance);
}
this.fitness*=this.fitness;
if(this.coins[0].taken || this.coins[1].taken || this.coins[2].taken || this.coins[3].taken ||
this.coins[4].taken || this.coins[5].taken || this.coins[6].taken){
this.fitness *=1.2;
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
gimmeBaby() {
var baby = new Player();
baby.brain = this.brain.clone();//babies have the same brain as their parents
baby.deathByDot = this.deathByDot;
baby.deathAtStep = this.deathAtStep;
baby.gen = this.gen;
return baby;
}
}