-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.js
132 lines (129 loc) · 4.37 KB
/
classes.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
function Pipe(x,y,spriteNum,state,filled = false){
this.filled = filled;
this.x = x;
this.y = y;
this.spriteNum = spriteNum;
this.state = state; // state of the pipe e.g: 0 is an unturned pipe, while 1 is a sideways turned pipe (if sprite is 0);
var rotation = 0;
switch(state){
case 0:{
rotation = 0;
break;
}
case 1:{
rotation = 90;
break;
}
case 2:{
rotation = 180;
break;
}
case 3:{
rotation = 270;
break;
}
}
this.rotation = rotation;
this.rotationLeft = 0;
this.sprite = s_pipe[spriteNum];
this.accept = function (){
var accepts = [];
switch(this.spriteNum){
case 0:{
switch(this.state){
case 0: {accepts = [{x:this.x,y:this.y-1},{x:this.x,y:this.y+1}]; break;}
case 1: {accepts = [{x:this.x+1,y:this.y},{x:this.x-1,y:this.y}]; break;}
case 2: {accepts = [{x:this.x,y:this.y-1},{x:this.x,y:this.y+1}]; break;}
case 3: {accepts = [{x:this.x+1,y:this.y},{x:this.x-1,y:this.y}]; break;}
}
break;
}
case 1:{
switch(this.state){
case 0: {accepts = [{x:this.x,y:this.y-1},{x:this.x,y:this.y+1},{x:this.x+1,y:this.y}]; break;}
case 1: {accepts = [{x:this.x+1,y:this.y},{x:this.x-1,y:this.y},{x:this.x,y:this.y+1}]; break;}
case 2: {accepts = [{x:this.x,y:this.y-1},{x:this.x,y:this.y+1},{x:this.x-1,y:this.y}]; break;}
case 3: {accepts = [{x:this.x+1,y:this.y},{x:this.x-1,y:this.y},{x:this.x,y:this.y-1}]; break;}
}
break;
}
case 2:{
accepts = [{x:this.x,y:this.y-1},{x:this.x,y:this.y+1},{x:this.x+1,y:this.y},{x:this.x-1,y:this.y}];
break;
}
case 3:{
switch(this.state){
case 0: {accepts = [{x:this.x,y:this.y-1},{x:this.x+1,y:this.y}]; break;}
case 1: {accepts = [{x:this.x,y:this.y+1},{x:this.x+1,y:this.y}]; break;}
case 2: {accepts = [{x:this.x,y:this.y+1},{x:this.x-1,y:this.y}]; break;}
case 3: {accepts = [{x:this.x,y:this.y-1},{x:this.x-1,y:this.y}]; break;}
}
break;
}
case 4:{
switch(this.state){
case 0: {accepts = [{x:this.x+1,y:this.y}]; break;}
case 1: {accepts = [{x:this.x,y:this.y+1}]; break;}
case 2: {accepts = [{x:this.x-1,y:this.y}]; break;}
case 3: {accepts = [{x:this.x,y:this.y-1}]; break;}
}
}
}
this.accepts = accepts;
}
this.accept();
}
Pipe.prototype.draw = function(ctx,fc,mY){
this.sprite.draw(ctx, this.x, this.y, this.rotation, fc, mY,this.filled);
}
Pipe.prototype.rotate = function(){
if(this.filled==false){
this.rotationLeft += 90;
}
}
Pipe.prototype.update = function(){
var newState = this.state;
if(this.rotationLeft!=0){
this.rotation += 5;
this.rotationLeft -= 5;
if(this.rotation == 360){
this.rotation = 0;
}
newState = Math.floor(this.rotation/90);
}
if(newState != this.state){
this.state = newState;
this.accept();
}
if (this.filled)
return true;
else
return false;
}
Pipe.prototype.fillArround = function(){
for(var i in this.accepts){
if(this.accepts[i].x>=0 && this.accepts[i].x<pipes.length){
if(this.accepts[i].y>=0 && this.accepts[i].y<pipes[this.accepts[i].x].length){
if(pipes[this.accepts[i].x][this.accepts[i].y].check(this.x,this.y)){
pipes[this.accepts[i].x][this.accepts[i].y].fill();
}
}
}
}
}
Pipe.prototype.check = function(otherX, otherY){
if(this.filled || otherX<0 || otherY<0){
return false;
}
for(var i in this.accepts){
if(this.accepts[i].x==otherX && this.accepts[i].y==otherY){
return true;
}
}
return false;
}
Pipe.prototype.fill = function(){
if(this.rotationLeft==0){
this.filled = true;
}
}