-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
100 lines (84 loc) · 2.85 KB
/
engine.js
File metadata and controls
100 lines (84 loc) · 2.85 KB
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
Window.Engine = function Engine(){
//Random Generators
this.getRandomNumber = function (seed){
return Math.floor((Math.random() * seed) + 1)
}
this.randomIntFromInterval = function(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
this.newDirection = function (obj){
/*
* Override me
*/
}
//Random functions use by diferent engines
this.getRandomDirection = function(min,max){
switch(this.randomIntFromInterval(min,max))
{
case 1:
return 'up';
break;
case 2:
return 'down';
break;
case 3:
return 'left';
break;
case 4:
return 'right';
break;
default:
return 'up'
break;
}
}
this.compareDirections = function(charDirection, charPreviousDirection,character){
//Detect if Ghost is stuck
if (this.detectIfStuck(character)){
/*
* Detect when Character gets Stuck
*/
return this.getRandomDirection(1,4);
}
if ((charDirection === 'up' && charPreviousDirection === 'down') ||(charDirection === 'down' && charPreviousDirection === 'up')){
return this.getRandomDirection(3,4)
}
else if ((charDirection === 'left' && charPreviousDirection === 'righ' )||(charDirection === 'right' && charPreviousDirection === 'left' )){
return this.getRandomDirection(1,2)
}
if ((charDirection === 'up' && charPreviousDirection === 'up' ) || (charDirection === 'down' && charPreviousDirection === 'down' )){
return this.getRandomDirection(3,4)
}
else if ((charDirection === 'left' && charPreviousDirection === 'left' ) || (charDirection === 'right' && charPreviousDirection === 'right' )){
return this.getRandomDirection(1,2)
}
if ((charDirection === 'up' && charPreviousDirection === 'left' )||(charDirection === 'up' && charPreviousDirection === 'right' )){
return this.getRandomDirection(3,4)
}
else if ((charDirection === 'left' && charPreviousDirection === 'up' ) || (charDirection === 'right' && charPreviousDirection === 'up' )){
return this.getRandomDirection(1,2)
}
if ((charDirection === 'down' && charPreviousDirection === 'left' ) || (charDirection === 'down' && charPreviousDirection === 'right' )){
return this.getRandomDirection(3,4)
}
//If we reach this point just return random chit!
return this.getRandomDirection(1,4);
}
this.detectIfStuck = function(character){
var coordinates = character.getCoordinates();
character.storeCordinates(coordinates);
var stack = character.getCordinatesStack();
if (stack.length > 10){
for(i = 0; i < stack.length - 1;i++){
if (stack[i][0] == stack[i + 1][0] && stack[i][1] == stack[i + 1][1]){
continue;
} else{
return false;
}
}
//it is stuck!
return true;
}
}
};