Skip to content

Commit af03af5

Browse files
author
Batiste Bieler
committed
Start a animation framework
1 parent dd57fe2 commit af03af5

File tree

9 files changed

+4670
-79
lines changed

9 files changed

+4670
-79
lines changed

static/chat.css

Whitespace-only changes.

static/chat.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

static/common.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
// evil
3+
Array.prototype.remove = function(from, to) {
4+
var rest = this.slice((to || from) + 1 || this.length);
5+
this.length = from < 0 ? this.length + from : from;
6+
return this.push.apply(this, rest);
7+
};
8+
9+
window.game = new Function();
10+
window.game.getCookie = function getCookie(name) {
11+
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
12+
return r ? r[1] : undefined;
13+
}
14+
15+
if (!window.console) window.console = {};
16+
if (!window.console.log) window.console.log = function() {};

static/effects.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
function Animation() {
4+
this.ctx = document.getElementById('canvas').getContext('2d');
5+
this.ctx.translate(100,100);
6+
this.animation_step = 0;
7+
this.animation_timeout = false;
8+
};
9+
10+
Animation.prototype.anim = function () {
11+
if(this.animation_step > 0)
12+
return;
13+
this.ctx.clearRect(0, 0, 200, 200);
14+
var that = this;
15+
var a = function() {
16+
that._anim();
17+
}
18+
this.animation_timeout = setInterval(a, 25);
19+
};
20+
21+
Animation.prototype._anim = function () {
22+
if(this.animation_step > 100) {
23+
clearTimeout(this.animation_timeout);
24+
this.animation_step = 0;
25+
return;
26+
}
27+
this.ctx.clearRect(-10, -10, 30, 30);
28+
this.ctx.fillStyle = "rgb(200,0,0)";
29+
this.ctx.fillRect(-10, -10, 20, 20);
30+
this.ctx.rotate(0.2);
31+
this.animation_step++;
32+
};

static/game.js

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
$(function() {
2-
3-
// evil
4-
Array.prototype.remove = function(from, to) {
5-
var rest = this.slice((to || from) + 1 || this.length);
6-
this.length = from < 0 ? this.length + from : from;
7-
return this.push.apply(this, rest);
8-
};
9-
10-
function getCookie(name) {
11-
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
12-
return r ? r[1] : undefined;
13-
}
14-
15-
if (!window.console) window.console = {};
16-
if (!window.console.log) window.console.log = function() {};
17-
2+
183
var event_cursor = false;
194
// polling update
205
var updater = {
216
errorSleepTime: 500,
227
cursor: null,
238

249
poll: function() {
25-
var args = {"_xsrf": getCookie("_xsrf")};
10+
var args = {"_xsrf": game.getCookie("_xsrf")};
2611
if(event_cursor)
2712
args["cursor"] = event_cursor
2813
if (updater.cursor) args.cursor = updater.cursor;
@@ -53,8 +38,8 @@ $(function() {
5338
}
5439
};
5540

56-
jQuery.postJSON = function(url, args, callback) {
57-
args._xsrf = getCookie("_xsrf");
41+
game.postJSON = function(url, args, callback) {
42+
args._xsrf = game.getCookie("_xsrf");
5843
$.ajax({
5944
url: url,
6045
data: $.param(args),
@@ -157,7 +142,7 @@ $(function() {
157142
}
158143
if(direction) {
159144
this.is_loading_room = true;
160-
$.postJSON("/a/change_room", {'direction':direction}, function change_room(response) {
145+
game.postJSON("/a/change_room", {'direction':direction}, function change_room(response) {
161146
var others_number = other_players.length - 1;
162147
while(others_number >= 0) {
163148
if(other_players[0]) {
@@ -251,14 +236,14 @@ $(function() {
251236

252237
// PLAYER
253238

254-
function player(init_position, key, pname) {
239+
function Player(init_position, key, pname) {
255240
this.position = [init_position[0], init_position[1]];
256241
this.last_sent_position = [init_position[0], init_position[1]];
257242
this.target_position = [init_position[0], init_position[1]];
258243
this.pname = pname;
259244
this.key = key;
260245
this.walking = false;
261-
this.speed = 3;
246+
this.speed = 2;
262247
this.element = $('<div class="player">\
263248
<span class="text"><span class="name">'+this.pname+'</span>\
264249
<span class="message"></span></span>\
@@ -271,16 +256,16 @@ $(function() {
271256
this.move(this.position);
272257
};
273258

274-
player.prototype.say = function(message) {
259+
Player.prototype.say = function(message) {
275260
clearTimeout(this.message_timeout);
276261
var el = this.message_element;
277262
this.message_element.text(message);
278263
this.message_element.slideDown("slow");
279264
var _hide_message = function(){el.slideUp("slow");}
280-
this.message_timeout = setTimeout(_hide_message, 10000);
265+
this.message_timeout = setTimeout(_hide_message, 12000);
281266
}
282267

283-
player.prototype.anim = function() {
268+
Player.prototype.anim = function() {
284269
if(!this.walking) {
285270
this.cycle = 1;
286271
};
@@ -294,20 +279,20 @@ $(function() {
294279
this.cycle = 0;
295280
};
296281

297-
player.prototype.move = function(pos) {
298-
this.element.css('left', pos[0]+'px');
299-
this.element.css('top', pos[1]+'px');
282+
Player.prototype.move = function(pos) {
283+
this.element[0].style.left = pos[0]+'px';
284+
this.element[0].style.top = pos[1]+'px';
300285
};
301286

302-
player.prototype.init_position = function() {
287+
Player.prototype.init_position = function() {
303288
this.move(this.position);
304289
};
305290

306-
player.prototype.remove = function() {
291+
Player.prototype.remove = function() {
307292
this.element.remove();
308293
};
309294

310-
player.prototype.update_target_position = function() {
295+
Player.prototype.update_target_position = function() {
311296
var vect = keyboard_vector();
312297
if(vect) {
313298
var next_pos = [
@@ -322,7 +307,7 @@ $(function() {
322307
};
323308
};
324309

325-
player.prototype.set_start_cycle = function(vect) {
310+
Player.prototype.set_start_cycle = function(vect) {
326311
//var angle = Math.atan(vect[0]/vect[1]);
327312
if(vect[1] > 0) {
328313
this.start_cycle = 24 * 9;
@@ -338,39 +323,46 @@ $(function() {
338323
};
339324
}
340325

341-
player.prototype.move_to_target = function() {
326+
Player.prototype.move_to_target = function() {
342327
var vect = [
343328
this.target_position[0] - this.position[0],
344329
this.target_position[1] - this.position[1]
345330
];
346331
var norm = norm_vector(vect);
347-
if(norm <= 2) {
332+
if(norm < 1) {
348333
this.position[0] = this.target_position[0];
349334
this.position[1] = this.target_position[1];
350335
this.move(this.position);
351336
this.walking = false;
337+
this.last_time = false;
352338
return;
353-
};
354-
if(norm > 2) {
339+
} else {
340+
/*var d = new Date().getTime();
341+
if(this.last_time) {
342+
var diff = Math.min(2, (d - this.last_time) / 16.0);
343+
} else {
344+
var diff = 1;
345+
}
346+
this.last_time = d;*/
355347
this.walking = true
356-
vect[0] = vect[0] / norm;
357-
vect[1] = vect[1] / norm;
348+
vect[0] = vect[0] / norm * this.speed;
349+
vect[1] = vect[1] / norm * this.speed;
358350
this.set_start_cycle(vect);
359-
this.position[0] += parseInt(this.speed * vect[0]);
360-
this.position[1] += parseInt(this.speed * vect[1]);
351+
this.position[0] += parseInt(vect[0]);
352+
this.position[1] += parseInt(vect[1]);
361353
this.move(this.position);
362354
return;
363355
}
364356
};
365357

366-
player.prototype.send_position = function () {
358+
Player.prototype.send_position = function () {
367359
if(this.last_sent_position[0] != this.target_position[0]
368360
|| this.last_sent_position[1] != this.target_position[1])
369361
{
370362
var pos = $.toJSON(this.target_position)
371363
this.last_sent_position[0] = this.target_position[0];
372364
this.last_sent_position[1] = this.target_position[1];
373-
$.postJSON("/a/player/update_position", {
365+
game.postJSON("/a/player/update_position", {
374366
'body':pos
375367
},
376368
function(response) {}
@@ -384,7 +376,7 @@ $(function() {
384376
e.preventDefault();
385377
var message = $('#message');
386378
message.focus();
387-
$.postJSON("/a/message/new", {'body':message.val()}, function(response) {
379+
game.postJSON("/a/message/new", {'body':message.val()}, function(response) {
388380
me.say(message.val());
389381
$('#message').val('');
390382
});
@@ -404,7 +396,7 @@ $(function() {
404396

405397
function bootstrap() {
406398

407-
me = new player(window.player_position, window.personnal_key, window.player_name);
399+
me = new Player(window.player_position, window.personnal_key, window.player_name);
408400
me.init_position();
409401
// start polling events
410402
updater.poll();
@@ -419,7 +411,7 @@ $(function() {
419411
other_players[i].move_to_target();
420412
};
421413
};
422-
setInterval(_players_move, 25);
414+
setInterval(_players_move, 16);
423415

424416
var _anim = function() {
425417
if(!grid1.is_loading_room) {
@@ -442,7 +434,7 @@ $(function() {
442434

443435
if(!window.player_position) {
444436
var player_name = prompt("Choose your hero name");
445-
$.postJSON("/a/player/new", {'body':player_name}, function(response) {
437+
game.postJSON("/a/player/new", {'body':player_name}, function(response) {
446438
response = $.evalJSON(response);
447439
window.personnal_key = response["you"]["key"];
448440
window.player_position = response["you"]["position"];
@@ -474,7 +466,7 @@ $(function() {
474466
}
475467
var p = get_player(item['key']);
476468
if(p === false) {
477-
p = new player([pos[0], pos[1]], item['key'], item['name']);
469+
p = new Player([pos[0], pos[1]], item['key'], item['name']);
478470
other_players.push(p);
479471
}
480472
p.target_position = [pos[0], pos[1]];
@@ -573,24 +565,19 @@ $(function() {
573565
keyboard_events.keydown(function(e) {
574566
update_keyboard(e, 1);
575567
//reset_cycle();
576-
player.walking = true;
568+
me.walking = true;
577569
});
578570
keyboard_events.keyup(function(e) {
579571
update_keyboard(e, 0);
580572
if( !keyboard["up"] && !keyboard["left"] && !keyboard["right"] && !keyboard["down"] )
581-
player.walking=false;
573+
me.walking=false;
582574
});
583575

584576
$('#save-map').click(function() {
585577
var content = $.toJSON(grid1.blocs);
586-
$.postJSON("/a/save_map", {'content':content}, function change_room(response) {
578+
game.postJSON("/a/save_map", {'content':content}, function change_room(response) {
587579
$('#save-message').show().fadeOut("slow");
588580
});
589581
})
590582

591-
592-
/*keyboard_events.blur(function(e) {
593-
keyboard = {'up':0, 'left':0, 'right':0, 'down':0};
594-
keyboard_events.focus();
595-
});*/
596583
});

0 commit comments

Comments
 (0)