-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
115 lines (93 loc) · 2.63 KB
/
game.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
"use strict";
function foodObject() {
this.cell = new cellObject([0,0]);
this.move = function() {
var cell = new cellObject([0,0]), grid = m.grid;
for (var i=0;i<=50;i++) {
if (i===50) {
game.win();
return false;
}
cell = getRandomCell();
if (food.isObstructed(cell)) continue; else break;
}
this.cell = cell;
game.draw();
}
this.isObstructed = function(cell) {
if (cell.isWall()) return true;
if (cell.isBody(snake.body)) return true;
if (cell.isPortal()) return true;
return false;
}
this.eat = function() {
if ((snake.body[0].pos[0] === this.cell.pos[0]) && (snake.body[0].pos[1] === this.cell.pos[1])) {
this.move();
snake.maxLength++;
}
}
this.draw = function() {
ctx.strokeStyle = toRGBA(settings.color.food.stroke);
ctx.lineWidth = m.cell/16;
ctx.fillStyle = toRGBA(settings.color.food.fill);
var size = m.cell;
var x = m.border[0] + (this.cell.pos[0] * size);
var y = m.border[1] + (this.cell.pos[1] * size);
ctx.fillRect(x,y,size,size);
ctx.strokeRect(x,y,size,size);
}
}
function portalObject() {
this.opened = false;
this.gate = [];
this.gate[0] = new cellObject([0,0]); //Orange
this.gate[1] = new cellObject([0,0]); //Blue
this.cooldown = 0;
this.click = function(x,y) {
var cell = new cellObject([x,y],snake.lastDir);
cell.convertToGrid();
if (this.isObstructed(cell)) return false; else this.open(cell);
}
this.isObstructed = function(cell) {
if (cell.isWall()) return true;
if (cell.isBody(snake.body)) return true;
return false;
}
this.open = function(cell) {
if (this.inUse()) return false;
this.gate[0] = snake.body[0];
this.gate[1] = cell;
this.opened = true;
snake.move(cell);
game.draw();
}
this.inUse = function() {
if (!this.opened) return false;
var l = this.gate.length;
for (var i=0;i<2;i++) {
var result = this.gate[i].isBody(snake.body);
if (result) return true;
}
return false;
}
this.draw = function() {
if (!this.opened) return false;
if (snake.body[0].isPortal()) ctx.fillStyle = toRGBA(settings.color.snakehead.fill);
else if (this.inUse()) ctx.fillStyle = toRGBA(settings.color.snakebody.fill);
else ctx.fillStyle = toRGBA(settings.color.transparent.fill);
var size = m.cell,
l = this.gate.length;
ctx.lineWidth = size/16;
for (var i=0;i<l;i++) {
var x = this.gate[i].pixels[0],
y = this.gate[i].pixels[1];
if (i===0) {
ctx.strokeStyle = toRGBA(settings.color.orangeportal.stroke);
} else {
ctx.strokeStyle = toRGBA(settings.color.blueportal.stroke);
}
ctx.fillRect(x, y, size, size);
ctx.strokeRect(x, y, size, size);
}
}
}