-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsnake.js
204 lines (187 loc) · 5.11 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict";
require('./util');
var util = require('util');
var events = require('events');
var Ball = require('./ball');
var Vector = require('./vector');
var Snake = function Snake(length, color, pos, world) {
events.EventEmitter.call(this);
this.onHeadHit = Snake.onHeadHit.bind(this);
var ballSize = 10;
this.color = color;
this.balls = [];
this.world = world;
this.balls[0] = this.head = new Ball(pos, ballSize, color.randomized(16))
this.maxMass = this.head.mass;
for (var i = 1; i < length; i++) {
this.addBall(new Ball(new Vector(), ballSize, color.randomized(16)));
};
this.balls.forEach(function(b) {
this.world.addEntity(b);
}, this);
}
util.inherits(Snake, events.EventEmitter);
Snake.onHeadHit = function(thing, cancelled) {
if(cancelled()) return;
var that = thing.ownerSnake;
if(that == undefined) {
if(this.eat(thing)) {
this.emit('eat.free', thing);
cancelled(true); //prevent balls interacting
}
}
else if(that != this) {
if(thing == that.head) {
if(that.length == 1 && this.head.mass > that.head.mass*2) {
this.emit('eat.head', thing);
this.eat(thing);
that.balls = [];
that.destroy();
that.emit('death', this);
cancelled(true);
}
}
else if(this.canEat(thing)) {
this.emit('eat.tail', thing);
that.eatenAt(thing);
this.eat(thing);
cancelled(true);
}
}
}
Object.defineProperty(Snake.prototype, 'tail', {
get: function() { return this.balls[this.balls.length - 1]; }
});
Object.defineProperty(Snake.prototype, 'head', {
get: function() { return this._head; },
set: function(h) {
var current = this.head;
if(h != current) {
var force = Vector.zero
if(current) {
force = current.forces.player;
current.removeListener('interaction', this.onHeadHit);
delete current.forces.player;
}
if(h) {
var snake = this;
h.forces.player = force
h.on('interaction', this.onHeadHit);
h.ownerSnake = this;
}
this._head = h;
}
}
});
Object.defineProperty(Snake.prototype, 'mass', {
get: function() { return this.balls.reduce(function(sum, x) { return sum + x.mass }, 0); }
});
Object.defineProperty(Snake.prototype, 'length', {
get: function() { return this.balls.length; }
});
Snake.prototype.drawTo = function(ctx) {
ctx.save();
ctx.fillStyle = (this.color.r + this.color.g + this.color.b > 192*3 ? "black" : "white");
ctx.beginPath();
ctx.arc(this.head.position.x, this.head.position.y, this.head.radius / 2, 0, Math.PI * 2, false);
ctx.fill();
ctx.restore();
return this;
};
Snake.prototype.addBall = function(ball) {
ball.clearForces();
ball.velocity.set(0, 0);
ball.ownerSnake = this;
var tail = this.tail
var pos = tail.position
var dist = ball.radius + tail.radius;
for(var j = 0; j < 100; j++) {
var p = Vector.fromPolarCoords(dist, Math.random() * Math.PI * 2)
ball.position = p.plusEquals(pos);
var collides = this.balls.some(function(b) { return b != tail && b.touches(ball) });
if(!collides) break;
}
this.balls.push(ball);
}
//Respond to being eaten
Snake.prototype.eatenAt = function(ball) {
var index = this.balls.indexOf(ball);
if(index > 0) {
var removed = this.balls.splice(index);
removed.shift();
var removedMass = removed.reduce(function(sum, b) {return sum + b.mass}, 0);
var remainingMass = this.mass;
//Reverse the snake if too much was taken off
if(removedMass > remainingMass) {
var r = this.balls;
this.balls = removed.reverse();
this.head = this.balls[0];
removed = r;
}
removed.forEach(function(b) {
delete b.ownerSnake;
b.clearForces();
});
}
}
Snake.prototype.canEat = function(ball) {
var that = ball.ownerSnake;
if(that == this) return false;
else if(that && ball == that.head)
return this.head.mass > that.head.mass * 2
else
return this.head.mass * 2 > ball.mass;
}
Snake.prototype.eat = function(ball) {
if(!this.canEat(ball)) return false;
this.maxMass *= 1.05;
this.addBall(ball);
return true;
}
Snake.prototype.destroy = function() {
this.balls.forEach(function(b) {
this.world.removeEntity(b);
delete b.ownerSnake;
}, this);
this.balls = [];
this.head = null;
}
var balls = [];
Snake.prototype.update = function(dt) {
//Shortening
this.balls.forAdjacentPairs(function(a, b, ai, bi) {
var rate = 50;// + 5*(this.length - ai);
var aMass = a.mass;
var diff = aMass - this.maxMass;
if(diff > rate) {
a.mass = aMass - rate;
b.mass += rate;
} else if(diff < -rate) {
var bMass = b.mass;
if(bMass < rate) {
b.mass = 0;
a.mass = aMass + bMass;
} else {
a.mass = aMass + rate;
b.mass = bMass - rate;
}
} else {
a.mass = this.maxMass;
b.mass += diff;
}
}, this);
var last = this.tail;
if(!(last.mass > 0)) { //NaNs
this.balls.pop();
this.world.removeEntity(last);
}
//Update ball colors
this.balls.forEach(function(b) {
b.color.lerp(this.color, 0.05);
}, this);
//Force them into a line
this.balls.forAdjacentPairs(function(b1, b2) {
b2.follow(b1);
}, this);
};
module.exports = Snake;