-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
177 lines (175 loc) · 5.72 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
165
166
167
168
169
170
171
172
173
174
175
176
177
function Player(i, j){
this.i = i; this.j = j; this.pSize = 40;
this.x = (this.i-1) * this.pSize;
this.y = size - ((this.j) * this.pSize);
this.isArrowShot = false;
this.arrow = { i: this.i, j: this.j };
this.hasFallenIntoPit = false;
this.isHitByWumpus = false;
this.hasShotWumpus = false;
this.facing = 0; this.direction = 'right';
this.faceLeft = function(){ this.facing = 180; this.direction = 'left'; }
this.faceRight = function(){ this.facing = 0; this.direction = 'right'; }
this.faceUp = function(){ this.facing = 270; this.direction = 'up'; }
this.faceDown = function(){ this.facing = 90; this.direction = 'down'; }
this.moveForward = function(){
switch (this.facing) {
case 0:
if( this.x + w < size ){
this.x += w;
this.i++;
} break;
case 90:
if( this.y + w < size ){
this.y += w;
this.j--;
} break;
case 180:
if( this.x - w >= 0 ){
this.x -= w;
this.i--;
} break;
case 270:
if( this.y - w >= 0 ){
this.y -= w;
this.j++;
} break;
default:
}
stepCounter++;
this.checkEnvironment();
this.arrow.i = this.i; this.arrow.j = this.j;
}
this.show = function(){
translate(this.x+(this.pSize), this.y);
if (this.direction == 'right') {
image(img_hunter_r, 0-this.pSize*(4/5), 0-this.pSize/5, this.pSize, this.pSize);
} else if (this.direction == 'left') {
image(img_hunter_l, 0-this.pSize*(4/5), 0-this.pSize/5, this.pSize, this.pSize);
} else if (this.direction == 'up') {
image(img_hunter_u, 0-this.pSize*(4/5), 0-this.pSize/5, this.pSize, this.pSize);
} else if (this.direction == 'down') {
image(img_hunter_d, 0-this.pSize*(4/5), 0-this.pSize/5, this.pSize, this.pSize);
}
}
this.checkEnvironment = function(){
// SHOW / HIDE GRID IF IT IS VISITED
for (let l=0; l<grid.length; l++) {
if(grid[l].i == this.i && grid[l].j == this.j) grid[l].visited = true;
}
// Check for PITS
for (let l=0; l<pits.length; l++) {
if( (pits[l].i == this.i) && (pits[l].j == this.j) ){
pits[l].visited = true;
this.hasFallenIntoPit = true;
console.log('You have fallen into the ENDLESS PIT!');
console.log('GAME OVER!!');
let finalScore = pointCounter - stepCounter - 1000;
console.log('Steps Taken: ' + stepCounter);
console.log('SCORE: ' + finalScore);
}
}
// Check for WUMPUS
if( (wi == this.i) && (wj == this.j) && !this.hasShotWumpus ){
this.isHitByWumpus = true;
console.log('The WUMPUS has attacked!');
console.log('GAME OVER!!');
let finalScore = pointCounter - stepCounter - 1000;
console.log('Steps Taken: ' + stepCounter);
console.log('SCORE: ' + finalScore);
}
// Check for GOLD
if( (gold.i == this.i) && (gold.j == this.j) ){
gold.visited = true;
console.log('I like GOLD!');
}
// Check for PERCEPTS: SMELL, BREEZE, GLITTER
let currentBlock;
for (let l=0; l<grid.length; l++) {
if(grid[l].i == this.i && grid[l].j == this.j) {
currentBlock = grid[l];
grid[l].visited = true;
}
}
let p = currentBlock.percepts;
for (let k=0; k<p.length; k++) {
switch (p[k].type) {
case 'Smell': this.detectSmell = true; break;
case 'Breeze': this.detectBreeze = true; break;
case 'Glitter': this.detectGlitter = true; break;
default:
this.detectSmell = false;
this.detectBreeze = false;
this.detectGlitter = false;
}
p[k].visited = true;
}
}
this.grab = function(gold, grid){
if( (gold.i == this.i) && (gold.j == this.j) && !this.stopGame ){
gold.grabbed = true;
this.goldGrabbed = true;
console.log('GOLD grabbed!');
// Remove GLITTER
for (let l=0; l<grid.length; l++) {
if(grid[l].i == gold.i && grid[l].j == gold.j){
let p = grid[l].percepts;
for (let k=0; k<p.length; k++) {
if (p[k].type == 'Glitter') p.splice(k, 1);
}
}
}
for (let l=0; l<this.grid.length; l++) {
if(grid[l].i == gold.i && grid[l].j == gold.j){
this.grid[l].containsGold = false;
}
}
}
this.returnToOrigin();
};
this.drop = function(gold){
if( gold.grabbed ){
gold.i = this.i;
gold.j = this.j;
gold.updatePosition();
gold.grabbed = false;
if( gold.i == 1 && gold.j == 1 ){
return true;
} else return false;
}
};
this.shootArrow = function(grid){
// 1. Get the direction Player is facing
// and move the arrow to the square the Player is facing
if(this.direction == 'left'){ this.arrow.i--; }
if(this.direction == 'right'){ this.arrow.i++; }
if(this.direction == 'up'){ this.arrow.j++; }
if(this.direction == 'down'){ this.arrow.j--; }
for (let x=0; x<grid.length; x++) {
for (let u=0; u<grid[x].percepts.length; u++) {
if( grid[x].percepts[u].isWumpus ){
// 3. Check if the Wumpus and the arrow are in the same square
if(grid[x].percepts[u].i == this.arrow.i && grid[x].percepts[u].j == this.arrow.j){
this.hasShotWumpus = true;
// 4. Remove Wumpus
grid[x].percepts.splice(u, 1);
wi = 0; wj = 0;
}
}
}
}
// 5. Remove adjacent Smells
if( this.hasShotWumpus ){
for (let x=0; x<grid.length; x++) {
for (let u=0; u<grid[x].percepts.length; u++) {
if( grid[x].percepts[u].type == 'Smell' ){
grid[x].percepts.splice(u, 1);
}
}
}
}
// 6. Remove Arrow
this.isArrowShot = true;
return grid;
};
}