-
Notifications
You must be signed in to change notification settings - Fork 5
/
snake.js
124 lines (118 loc) · 3.42 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
//snake
function Snake(scene, size, color) {
this.snake = [];
this.scene = scene;
this.size = size; // snake is made up of cubes
this.color = color;
this.distance = 50; // distance to move by
this.direction = null;
this.axis = null;
this.onSelfCollision = function() {};
this.onTagCollision = function() {};
this.position = null; // current position of the snake instance
this.tagPosition = null; // the current position of the tag to be hit
this.geometry = new THREE.BoxGeometry(this.size, this.size, this.size);
this.material = new THREE.MeshLambertMaterial( { color: this.color } );
this.init();
};
Snake.prototype = {
init: function() {
this.addCube();
this.addCube();
this.setDefaultPositions();
},
setDefaultPositions: function() {
var self = this;
this.snake.forEach(function(cube, index) {
cube.position.z = -1 * (self.size / 2 * (index + 1));
cube.position.y = self.size / 2;
cube.position.x = -500 + 25;
});
},
reset: function() {
this.init();
},
selfCollision: function() {
this.onSelfCollision();
this.clear();
},
tagCollision: function() {
this.onTagCollision();
this.addCube();
},
setCurrentTagPosition: function(position) {
this.tagPosition = position;
},
isHit: function(p1, p2) {
if (p1.x === p2.x && p1.y === p2.y && p1.z === p2.z) {
return true;
}
return false;
},
createCube: function(position) {
var cube = new THREE.Mesh(this.geometry, this.material);
return cube;
},
addCube: function() {
this.snake.push(this.createCube());
},
render: function() {
var self = this;
var next = null;
this.snake.forEach(function(cube) {
var temp = null;
if (self.axis !== null && self.direction !== null) {
if (!next) {
next = { x: cube.position.x, y: cube.position.y, z: cube.position.z };
cube.position[self.axis] += (self.direction * self.distance);
self.position = { x: cube.position.x, y: cube.position.y, z: cube.position.z };
if (self.tagPosition) {
if (self.isHit(self.position, self.tagPosition)) {
self.tagCollision();
}
}
} else {
temp = { x: cube.position.x, y: cube.position.y, z: cube.position.z };
cube.position.set(next.x, next.y, next.z);
// check if it collides with itself
if (self.isHit(self.position, cube.position)) {
self.selfCollision();
};
next = { x: temp.x, y: temp.y, z: temp.z };
}
}
self.renderCube(cube);
});
},
back: function() {
this.axis = 'z';
this.direction = -1;
},
forward: function() {
this.axis = 'z';
this.direction = 1;
},
up: function() {
this.axis = 'y';
this.direction = 1;
},
down: function() {
this.axis = 'y';
this.direction = -1;
},
right: function() {
this.axis = 'x';
this.direction = 1;
},
left: function() {
this.axis = 'x';
this.direction = -1;
},
clear: function() {
this.axis = null;
this.direction = null;
},
renderCube: function(cube) {
this.scene.add(cube)
}
};