-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.js
150 lines (138 loc) · 3.13 KB
/
snake.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
var lerp = require('lerp')
var directions = {
left: {
vx: -1,
vy: 0
},
right: {
vx: 1,
vy: 0
},
up: {
vx: 0,
vy: -1
},
down: {
vx: 0,
vy: 1
}
}
module.exports = Snake
function Snake (clanvas) {
this.clanvas = clanvas
var initialSize = 7
this.cells = Array.apply(null, Array(initialSize))
.map(function (_, idx) {
return {
x: idx,
y: 0,
dir: 'down'
}
}).reverse()
this.direction = directions.down
this.turningEnabled = true
}
Snake.prototype.turn = function (dir) {
var newDir = directions[dir]
var currentDir = this.direction
var isOppositeDirection = newDir.vx && currentDir.vx || newDir.vy && currentDir.vy
if (this.turningEnabled && !isOppositeDirection) {
this.direction = directions[dir]
this.turningEnabled = false
this.cells[0].dir = dir
}
}
Snake.prototype.moveHead = function () {
var head = this.cells[0]
var rb = this.clanvas.width
var bb = this.clanvas.height
head.x += this.direction.vx
head.y += this.direction.vy
if (head.x > rb) head.x = 0
if (head.x < 0) head.x = rb
if (head.y > bb) head.y = 0
if (head.y < 0) head.y = bb
}
Snake.prototype.contains = function (pos) {
return this.cells.filter(function (cell) {
return cell.x === pos.x && cell.y === pos.y
}).length > 0
}
Snake.prototype.isCollidingWithSelf = function () {
var cells = this.cells
var head = cells[0]
for (var i = 1; i < cells.length; i++) {
var cell = cells[i]
if (cell.x === head.x && cell.y === head.y) {
return i
}
}
return false
}
Snake.prototype.grow = function () {
var lastCell = this.cells[this.cells.length - 1]
this.cells.push({
x: lastCell.x,
y: lastCell.y,
dir: lastCell.dir
})
}
Snake.prototype.move = function () {
this.cells = this.cells.map(function (cell, idx) {
var newCell = idx ? this.cells[idx - 1] : cell
return {
x: newCell.x,
y: newCell.y,
dir: newCell.dir
}
}.bind(this))
this.moveHead()
this.turningEnabled = true
}
Snake.prototype.render = function () {
var edges = ['◥', '◤', '◣', '◢']
// var edges = ['◹', '◸', '◺', '◿']
var clanvas = this.clanvas
this.cells.forEach(function (cell, idx, cells) {
var g = lerp(120, 200, idx / cells.length)
var char = ' '
var bg = clanvas.backgroundColor
var fg = rgb(25, g, 10)
var nextCell = cells[idx + 1] || cell
var tx = cell.dir[0] + nextCell.dir[0]
switch (tx) {
case 'rd':
case 'ul':
char = edges[0]
break
case 'ru':
case 'dl':
char = edges[3]
break
case 'ld':
case 'ur':
char = edges[1]
break
case 'lu':
case 'dr':
char = edges[2]
break
default:
bg = fg
break
}
clanvas.paintCell(cell.x, cell.y, fg, bg, char)
})
}
function rgb (r, g, b) {
var red = r / 255 * 5
var green = g / 255 * 5
var blue = b / 255 * 5
return rgb5(red, green, blue)
}
function rgb5 (r, g, b) {
var red = Math.round(r)
var green = Math.round(g)
var blue = Math.round(b)
return 16 + (red * 36) + (green * 6) + blue
}