-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlibhtml5.js
executable file
·361 lines (329 loc) · 9.01 KB
/
libhtml5.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* This file is part of the Lunatic Systems Canvas Library
* Copyright (C) 2010 Adam Etienne <eadam@lunasys.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
// For FPS
var fps = 0;
var lasttime = 0;
var frame = 0;
var debug_mode = false;
var Point = $.inherit({
__constructor : function( x, y ){
this.x = x;
this.y = y;
},
role: function(){
return "point";
},
doAnim: function(){
},
render: function( ctx ){
ctx.beginPath();
ctx.lineWidth = 1;
ctx.lineCap = 'square';
ctx.strokeStyle = 'rgba(255,255,255,1)';
var locx = this.x+game.origin.x;
var locy = this.y+game.origin.y;
ctx.moveTo(locx-0.5, locy-0.5);
ctx.lineTo(locx+0.5, locy-0.5);
ctx.lineTo(locx+0.5, locy+0.5);
ctx.lineTo(locx-0.5, locy+0.5);
ctx.lineTo(locx-0.5, locy-0.5);
ctx.stroke();
},
info: function(){
return this.x + "/" + this.y + "est un " + this.role();
}
});
var Obj = $.inherit( Point, {
__constructor : function( game, p, img_name ){
this.x = p.x;
this.y = p.y;
this.img_name = img_name;
this.game = game;
},
role : function(){
return "obj";
},
render : function( ctx ) {
ctx.drawImage( this.img, Math.round(this.x-Math.round(this.w/2))+game.origin.x, Math.floor(this.y-this.h/2)+game.origin.y, this.w, this.h );
if( this.ind && debug_mode ) {
ctx.fillStyle = "#fff";
ctx.font = 'arial 5px';
ctx.fillText( this.ind, Math.floor(this.x-this.w/2)+4+game.origin.x, Math.floor(this.y-this.h/2)+10+game.origin.y );
ctx.fillText( this.obj_id, Math.floor(this.x-this.w/2)+4+game.origin.x, Math.floor(this.y-this.h/2)+18+game.origin.y );
}
}
});
var Cursor = $.inherit( Point, {
__constructor: function( game ) {
this.attached_obj = null;
this.game = game;
},
doAnim: function() {
this.x = this.game.mouse.x - this.game.origin.x;
this.y = this.game.mouse.y - this.game.origin.y;
if( this.attached_obj ) {
this.attached_obj.x = this.x;
this.attached_obj.y = this.y;
this.attached_obj.doAnim();
}
},
render : function( ctx ) {
// Nothing
},
role : function() {
return "cursor";
}
});
var logs = [];
debug = function( s ) {
//$("#debug").append( "DBG: "+txt+"\n" );
logs.push( s );
if( logs.length>80 )
logs.splice( 0, 1 );
$("#debug").html( logs.join( "<br>" ) ).get(0).scrollTop = $("#debug").get(0).scrollHeight;
}
var LSGame = $.inherit({
__constructor : function( canvas ) {
//this.canvas = canvas;
this.ctx = document.getElementById( "hs_canvas" ).getContext('2d');
this.logs = [];
this.origin = new Point( 152, 10 );
this.media_path = "/media/himesama";
// Game objects
this.objs = [];
this.explosions = [];
this.cursor = new Cursor( this );
this.mouse = new Point( 430, 100 );
// Images arrays
this.all_imgs = [];
this.imagesLoaded = 0;
this.imagesToPreload = 0;
// Sounds array
this.audios = [];
this.loadedAudioElements = 0;
this.toloadAudioElements = 0;
this.canvas_element = $('#'+canvas);
this.canvas_element.bind( "mousemove", {o:this}, this.mousemove );
this.canvas_element.bind( "click", {o:this}, this.click );
this.canvas_element.bind( "dblclick", {o:this}, this.dblclick );
//this.canvas_element.dblclick( this.dblclick );
// Disable double click selection:
this.canvas_element.get(0).onselectstart = function () { return false; };
},
mousemove : function( e ) {
var o = e.data.o; // o is 'this' object, passed as argument because it's an event callback
o.mouse.x = e.pageX - o.canvas_element.parent().get(0).offsetLeft;
o.mouse.y = e.pageY - o.canvas_element.parent().get(0).offsetTop;
},
drawScreen : function() {
},
dblclick : function( e ) {
return false;
},
click : function( e ) {
return false;
},
delObj : function( obj ) {
//if( obj && obj.obj_id && this.objs[obj.obj_id] ) {
//this.objs.splice(obj.obj_id, 1);
//if( obj.ind==302 )
// game.debug( "delObj: "+obj.obj_id+" ind:"+this.objs[obj.obj_id].ind+"/"+this.objs.length );
delete this.objs[obj.obj_id];
// return true;
//}
},
addObj : function( obj ) {
obj.img = this.preloadImage( obj.img_name );
obj.obj_id = this.objs.length;
this.objs.push( obj );
//if( this.objs[obj.obj_id] )
// debug( "addObj: "+obj.obj_id+" ind:"+this.objs[obj.obj_id].ind+" id:"+this.objs[obj.obj_id].obj_id );
//this.objs[id].img = preloadImage( "/media/himesama/img/"+this.objs[id].img_name );
return true;
},
init : function() {
// Start loading
this.loadImages();
this.loadSounds();
},
start : function() {
// Start loop, at around 30 FPS
//setInterval( animAll, 24 );
setInterval( drawAll, 29 );
},
drawAfter : function( ctx ) {
},
drawBefore : function( ctx ) {
// Draw scores, lives, etc...
ctx.fillStyle = "#fff";
ctx.font = '14px Arial';
ctx.fillText( this.score, 40, 120 );
ctx.fillText( this.lives, 65, 310 );
},
debug : function( txt ) {
// Call an external debug functional for the sake of simplicity
debug( txt );
},
animAll : function() {
for( var id in this.objs ) {
if( this.objs[id] )
this.objs[id].doAnim();
}
this.cursor.doAnim();
},
drawAll : function() {
/**
* Animation loop
*/
ctx = this.ctx;
ctx.clearRect(0,0,800,600);
ctx.save();
this.drawBefore( ctx );
if( this.state=="play" ) {
this.animAll();
/*
for( var id in this.explosions ) {
this.explosions[id].doAnim();
}
*/
for( var id in this.objs ) {
if( this.objs[id] ) {
//if( this.objs[id].ind==110 )
// debug( "HEYYYY" );
//this.objs[id].doAnim();
this.objs[id].render( ctx );
}
}
/*
for( var id in this.explosions ) {
this.explosions[id].render( ctx );
}
*/
} else {
this.drawScreen();
}
this.drawAfter( ctx );
//this.cursor.doAnim();
this.cursor.render( ctx );
frame++;
if( frame%50==0 ) {
var curtime = (new Date()).getTime();
fps = Math.floor( frame/((curtime-lasttime)/1000) );
lasttime = curtime;
frame = 0;
//debug( fps );
}
ctx.fillStyle = "#fff";
ctx.font = '14px Arial';
ctx.fillText( fps+" fps", 10, 360 );
ctx.restore();
},
loadImages : function() {
//alert( imagesLoaded+" >= "+imagesToPreload );
for( var i in this.all_imgs ) {
this.load_image( this.all_imgs[i][1], this.all_imgs[i][0] );
}
},
load_image : function(img,uri) {
//var img = new Image();
img.onload = on_image_load_event;
img.onerror = on_image_load_event;
img.onabort = on_image_load_event;
img.src = this.media_path+"/img/"+uri;
return img;
},
preloadImage : function(uri) {
var i=0;
for( i in this.all_imgs ) {
if( this.all_imgs[i][0]==uri )
break;
}
if( this.all_imgs[i] && this.all_imgs[i][0]==uri ) {
var img = this.all_imgs[i][1];
//game.debug( "LOAD IMG [CACHED]: "+uri );
} else {
this.imagesToPreload++;
//game.debug( "LOAD IMG: "+uri );
var img = new Image();
this.all_imgs.push( [uri,img] );
}
return img;
},
loadSounds : function() {
this.toloadAudioElements = this.audios.length-1;
for( var ii in this.audios ) {
var audioElement = this.audios[ii][1];
audioElement.addEventListener("error", function() {
//alert( "error"+this.src );
}, true);
audioElement.addEventListener("load", function() {
game.loadedAudioElements++;
}, true);
var src = this.media_path + "/snd/" + this.audios[ii][0];
src = src.replace( "#", "%23" );
audioElement.src = src;
audioElement.load();
}
},
playSound : function( audioname ) {
},
preloadSound : function( audioname ) {
var ii;
for( ii in this.audios )
if( this.audios[ii][0]==audioname )
break;
if( this.audios[ii] && this.audios[ii][0]==audioname ) {
var audioElement = this.audios[ii][1];
//debug( "LOAD SOUND [CACHED]: "+src );
} else {
var audioElement = new Audio();
//debug( "LOAD SOUND: "+src );
this.audios.push( [audioname,audioElement] );
//this.audios_index[
}
return audioElement;
}
});
// Static methods
// TODO: find a clean way to do that
drawAll = function() {
game.drawAll();
}
animAll = function() {
game.animAll();
}
function on_image_load_event() {
game.imagesLoaded++;
if (game.imagesLoaded >= game.imagesToPreload) {
game.start();
}
}
function getAngle( p1, p2 ) {
alpha = Math.atan( (p1.y - p2.y) / (p1.x - p2.x) );
return alpha;
}
function getAdjacent( a, h ) {
return Math.cos( a )*h;
}
function getOppose( a, h ) {
return Math.sin( a )*h;
}
function rand( min, max ) {
return Math.floor( Math.random()*(max-min+1) )+min;
}