-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwall.js
More file actions
105 lines (101 loc) · 3.54 KB
/
wall.js
File metadata and controls
105 lines (101 loc) · 3.54 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
101
102
103
104
105
function Wall(){
this.wall = []; // (WallCell array)
this.initialization = function(){ // (void)
var cols = floor(width/scl);
var rows = floor(height/scl);
for(var i = 0; i < cols; i++){
this.wall[i] = WallCell.prototype.createWallCell(i, 0);
this.wall[i].Location.mult(scl);
this.wall[i].bounce = bounceFactor;
this.wall[i].dir = createVector(0, 1); // 因為等一下 "不" 會先 UTurn 一次
}
}
this.update = function(){ // the wall moves (void)
for(var i = 0;i < this.wall.length; i++){
var pos = this.wall[i].Location;
var move = true;
/*if(snake1.x === pos.x && snake1.y === pos.y+20){
move = false;
}*/
for(var j = 0; j < snake1.tail.length; j++){
if(this.wall[i].dir.y === 1 && snake1.tail[j].x === pos.x && snake1.tail[j].y === pos.y+2*scl){
move = false;
}
if(this.wall[i].dir.y === 1 && snake1.tail[j].x === pos.x && snake1.tail[j].y === pos.y+scl){
move = false;
}
if(this.wall[i].dir.y === -1 && snake1.tail[j].x === pos.x && snake1.tail[j].y === pos.y-2*scl){
move = false;
}
if(this.wall[i].dir.y === -1 && snake1.tail[j].x === pos.x && snake1.tail[j].y === pos.y-scl){
move = false;
}
}
if(move){
this.wall[i].update();
}
}
var Dispose = true;
for(var i = 0; i < this.wall.length; i++){
if(this.wall[i].Location.y <= height && this.wall[i].Location.y >= 0){
Dispose = false;
}
}
if(Dispose){
thereIsaWall = false;
timer_tmp = 5;
bounceFactor += 1; label4.style.color = "green";
this.Reset();
}
for(var i = 0; i < this.wall.length; i++){
if(Wall.prototype.edge(this.wall[i])){
this.wall[i].UTurn();
this.wall[i].bounce -= 1;
}
}
}
this.show = function(){ // update screen (void)
fill(128);
for(var i = 0; i < this.wall.length; i++){
rect(this.wall[i].Location.x, this.wall[i].Location.y, scl, scl)
}
}
this.Reset = function(){
console.log(this);
this.wall = [];
thereIsaWall = false;
}
}
Wall.prototype.edge = function(wallCell){ // whether the wall hits the edge (boolean)
if(wallCell.Location.y === height-scl){
return true;
}
if(wallCell.Location.y === 0){
return true;
}
return false;
}
function WallCell(){
this.Location; // (vector)
this.bounce; // bounces remains (integer)
this.dir; // direction of its speed (vector)
this.update = function(){ // move (void)
this.Location.x += this.dir.x*scl;
this.Location.y += this.dir.y*scl;
}
this.UTurn = function(){ // turn back (void)
if(this.bounce === 0){
return;
}
if(this.dir.y === 1){
this.dir.y = -1
}else if(this.dir.y === -1){
this.dir.y = 1;
}
}
}
WallCell.prototype.createWallCell = function(x, y){ // (WallCell)
var toReturn = new WallCell();
toReturn.Location = createVector(x, y);
return toReturn;
}