-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
275 lines (235 loc) · 9.58 KB
/
scripts.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// May need to go back to dynamically adjusting the dimensions of the sprite as well as
// controlling the background position strictly, so that the direction of movement
// places the lemming flush against the leading edge.
// When changing the action, any changes to the dimensions should be relative to an anchor
// determined by the direction of movement (eg bottom right corner). This will allow the
// dimensions to be changed without affecting any adjacent elements.
var Lemming = {};
$(document).ready(function(){
// Keep track of all the lemmings in the DOM
Lemming.lemmings = [];
// Helper to count the lemmings in the DOM (for namespacing only)
Lemming.counter = 0;
// Define defaults
Lemming.options = {
speed: 100,
selector: '.collidable'
}
// Define corrected dimensions for each animation [width, height]
// This is so that the background position can be adjusted when collisions are detected
// so that the graphic is flush against the collided side
Lemming.dimensions = {
walk: [12,20],
fall: [12,20],
clim: [18,22]
}
// Launch a new lemming into the page and auto-start the animation & movement
Lemming.launch = function() {
var lemming = new Lemming.instance();
lemming.start();
return lemming;
}
// Returns an actual lemming instance, inserted into the DOM
Lemming.instance = function() {
var l = this;
Lemming.lemmings.push(l);
// Increment counter
// This is solely to ensure sprite elements are named with a unique ID
Lemming.counter = Lemming.counter + 1;
// Init sprite DOM element
var sprite = document.createElement('div');
// Define the window bounds
l.windowBounds = function() {
return {
left: $(window).width(),
top: $(window).height()
}
}
// Collect the collidable elements
l.collidables = function() {
return $(Lemming.options.selector);
}
// Init initial properties
// [name_of_gif, number_of_frames]
l.gif = ['walk_r', 8];
// Config sprite
sprite.id = 'lemming_' + Lemming.counter;
sprite.className = 'lemming';
sprite.style.backgroundPosition = 'left bottom';
l.sprite = $(sprite);
// Background image & positioning
// The offset is adjusted when a collision is detected
l.offset = -10;
l.backgroundPosition = -10;
// Convert the position (array) into CSS
l.getBackgroundPosition = function() {
return [(l.backgroundPosition - ((32 - l.sprite.width()) / 2)) + 'px', 'bottom'].join(' ');
}
// Change the background image
l.setMovement = function(name, frames) {
l.gif = [name, frames];
l.sprite.get(0).style.backgroundImage = 'url(gifs/lemming_' + name + '.gif)';
l.action = name;
}
// Init initial movements
l.setMovement('walk_r', 8);
l.moving = false;
l.climbing = false;
// Insert sprite into page
$('body').append(sprite);
// Set the initial direction of movement and initial page position
l.direction = 'r';
l.sprite.offset({ left: ($(window).width() / 2) + (l.sprite.width() / 2), top: 0 }); // top center
// Animate action (not directional speed but frame rate)
l.animate = function() {
var frameCounter = 1;
l.animation = setInterval(function(){
if (frameCounter >= l.gif[1]) {
l.backgroundPosition = (0 + l.offset); frameCounter = 0;
} else {
l.backgroundPosition = ((l.backgroundPosition - 32));
}
l.sprite.get(0).style.backgroundPosition = l.getBackgroundPosition();
frameCounter = frameCounter + 1;
}, Lemming.options.speed);
}
// Init collisions
l.adjacents = {
top: false,
right: false,
bottom: false,
left: false
}
// Helper to quickly check if we're in free fall (no adjacent elements)
l.adjacents.any = function() {
return (l.adjacents.top || l.adjacents.right || l.adjacents.bottom || l.adjacents.left);
}
// Calculate collisions
// This is run every time the position is changed in order to determine the next action
l.calculateCollisions = function() {
var top = false;
var right = false;
var bottom = false;
var left = false;
var spriteBounds = {
top: l.sprite.offset().top,
right: l.sprite.offset().left + l.sprite.width(),
bottom: l.sprite.offset().top + l.sprite.height(),
left: l.sprite.offset().left
}
// Measure against the window first
top = spriteBounds.top == 0;
right = (spriteBounds.right) == l.windowBounds().left;
bottom = (spriteBounds.bottom) == l.windowBounds().top;
left = spriteBounds.left == 0;
// Then measure against the collidable elements
l.collidables().each(function(){
// Bounds of the collidable
var objectBounds = {
top: $(this).offset().top,
right: $(this).offset().left + $(this).width(),
bottom: $(this).offset().top + $(this).height(),
left: $(this).offset().left
}
if ((spriteBounds.top == objectBounds.bottom) && (spriteBounds.right > objectBounds.left) && (spriteBounds.left < objectBounds.right)) { top = $(this); }
if ((spriteBounds.right == objectBounds.left) && (spriteBounds.bottom > objectBounds.top) && (spriteBounds.top < objectBounds.bottom)) { right = $(this); }
if ((spriteBounds.bottom == objectBounds.top) && (spriteBounds.right > objectBounds.left) && (spriteBounds.left < objectBounds.right)) { bottom = $(this); }
if ((spriteBounds.left == objectBounds.right) && (spriteBounds.bottom > objectBounds.top) && (spriteBounds.top < objectBounds.bottom)) { left = $(this); }
});
l.adjacents.top = top;
l.adjacents.right = right;
l.adjacents.bottom = bottom;
l.adjacents.left = left;
}
// Tell the lemming which action to perform and direction to move
l.move = function(action) {
l.climbing = false;
if (action == 'walk_r' || action == 'walk_l') {
l.setMovement(action, 8);
l.movement = setTimeout(function(){
l.sprite.offset({ left: l.sprite.offset().left + (l.direction == 'r' ? 1 : -1) });
if (l.moving) { l.go(); }
}, 50);
} else if (action == 'fall_r' || action == 'fall_l') {
l.setMovement(action, 4);
l.movement = setTimeout(function(){
l.sprite.offset({ top: l.sprite.offset().top + 1 });
if (l.moving) { l.go(); }
}, 15);
} else if (action == 'climb_r' || action == 'climb_l') {
l.climbing = true;
l.setMovement(action, 8);
l.movement = setTimeout(function(){
l.sprite.offset({ top: l.sprite.offset().top - 1 });
if (l.moving) { l.go(); }
}, 75);
}
}
// Stop and reset ALL animation/movement
l.stop = function() {
l.moving = false;
if (l.movement) { setTimeout(l.movement); delete l.movement; }
if (l.animation) { clearInterval(l.animation); delete l.animation; }
l.backgroundPosition = 0;
l.sprite.get(0).style.backgroundPosition = l.getBackgroundPosition();
}
// Begin moving
l.go = function() {
l.moving = true;
l.calculateCollisions();
if (l.adjacents.bottom && !l.adjacents.left && l.adjacents.right && l.direction == 'r') {
if (l.offset == ((32 - Lemming.dimensions[l.action.slice(0,4)][0]) / 2)) {
l.move('climb_' + l.direction);
} else {
setTimeout(function(){
l.offset = l.offset + 1; l.backgroundPosition = l.backgroundPosition + 1; l.go();
}, (Lemming.options.speed / 2));
}
} else if (!l.adjacents.bottom && (l.adjacents.right || l.adjacents.left) && l.adjacents.top) {
l.direction = (l.direction == 'r' ? 'l' : 'r');
l.move('fall_' + l.direction);
} else if (l.adjacents.right && l.direction == 'r') {
l.move('climb_' + l.direction);
} else if (l.adjacents.left && l.direction == 'l') {
l.move('climb_' + l.direction);
} else if (!l.adjacents.bottom && l.climbing && l.direction == 'l') {
l.backgroundPosition = (l.backgroundPosition + (32 - Lemming.dimensions.walk[0]));
l.offset = ((32 - Lemming.dimensions.walk[0]) / 2);
l.move('walk_' + l.direction);
} else if (!l.adjacents.bottom && l.climbing && l.direction == 'r') {
var adjustment = (32 - Lemming.dimensions.walk[0]);
l.backgroundPosition = (l.backgroundPosition - adjustment);
// l.sprite.offset({ left: l.sprite.offset().left + adjustment });
l.offset = -(adjustment / 2);
l.move('walk_' + l.direction);
} else if (!l.adjacents.bottom) {
l.move('fall_' + l.direction);
} else if (l.adjacents.bottom && !l.adjacents.left && l.direction == 'l') {
l.move('walk_' + l.direction);
} else if (l.adjacents.bottom && !l.adjacents.right && l.direction == 'r' ) {
l.move('walk_' + l.direction);
} else if (!l.adjacents.any()) {
l.move('fall_' + l.direction);
} else {
throw "WTF!";
}
}
// Change direction
l.reverse = function() {
l.stop(); l.direction = (l.direction == 'r' ? 'l' : 'r');
setTimeout(function(){
l.start();
}, 100);
}
// Trigger the animation and movement
l.start = function() {
l.animate(); l.go();
}
// Delete an individual lemming
l.destroy = function() {
Lemming.lemmings.splice(l);
l.sprite.remove();
return true;
}
}
});