2012-08-15 08:55:08 +02:00
debug = true ;
2012-08-19 17:11:01 +02:00
player _pos = [ 0.5 * window . innerWidth , 0.9 * window . innerHeight ] ;
enemy _pos = [ 0.5 * window . innerWidth , 0.1 * window . innerHeight ] ;
2012-08-19 17:45:26 +02:00
new _enemy _pos = [ enemy _pos [ 0 ] , enemy _pos [ 1 ] ] ;
2012-08-14 23:08:49 +02:00
pewpew = 0 ;
document . onkeydown = onKeyDown ;
function onKeyDown ( e ) {
e = e || window . event ;
if ( e . keyCode == '37' ) { // left
player _pos [ 0 ] -= 10 ;
}
if ( e . keyCode == '39' ) { // right
player _pos [ 0 ] += 10 ;
}
if ( e . keyCode == '32' ) { // space = fire
2012-08-15 09:02:16 +02:00
shoot ( player _pos ) ;
2012-08-14 23:08:49 +02:00
}
}
shoots = [ ] ;
2012-08-15 08:55:08 +02:00
function shoot ( pos ) {
2012-08-15 09:02:16 +02:00
shoots [ shoots . length ] = [ pos [ 0 ] , pos [ 1 ] ] ;
2012-08-14 23:08:49 +02:00
// pewpew = 5;
}
function isCollided ( a , b ) {
2012-08-14 23:20:12 +02:00
var a _left = a . getClientRects ( ) [ 0 ] . left ;
var a _right = a . getClientRects ( ) [ 0 ] . right ;
var a _top = a . getClientRects ( ) [ 0 ] . top ;
var a _bottom = a . getClientRects ( ) [ 0 ] . bottom ;
var b _left = b . getClientRects ( ) [ 0 ] . left ;
var b _right = b . getClientRects ( ) [ 0 ] . right ;
var b _top = b . getClientRects ( ) [ 0 ] . top ;
var b _bottom = b . getClientRects ( ) [ 0 ] . bottom ;
2012-08-14 23:08:49 +02:00
return ! (
( a _left > b _right ) ||
( a _right < b _left ) ||
( a _top > b _bottom ) ||
( a _bottom < b _top )
) ;
}
2012-08-15 08:55:08 +02:00
function detectCollisions ( ) {
2012-08-14 23:08:49 +02:00
// detect collision
var enemydiv = document . getElementById ( "enemy" ) ;
for ( var i = 0 ; i < shoots . length ; i ++ ) {
if ( shoots [ i ] ) {
var shootdiv = document . getElementById ( 'shoot' + i ) ;
if ( shootdiv && isCollided ( shootdiv , enemydiv ) ) {
pewpew = 5 ;
}
}
}
2012-08-15 08:55:08 +02:00
}
2012-08-14 23:08:49 +02:00
2012-08-15 08:55:08 +02:00
function render ( ) {
2012-08-14 23:08:49 +02:00
// render shoots
for ( var i = 0 ; i < shoots . length ; i ++ ) {
if ( shoots [ i ] ) {
var shootdiv = document . getElementById ( 'shoot' + i ) ;
if ( ! shootdiv ) {
shootdiv = document . createElement ( 'div' ) ;
shootdiv . setAttribute ( 'id' , 'shoot' + i ) ;
shootdiv . setAttribute ( 'class' , 'shoot' ) ;
2012-08-15 08:55:08 +02:00
shootdiv . innerHTML = 'i' ;
2012-08-14 23:08:49 +02:00
var body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
body . appendChild ( shootdiv ) ;
}
2012-08-19 16:59:23 +02:00
shootdiv . style . left = ( shoots [ i ] [ 0 ]
- Math . round ( shootdiv . getClientRects ( ) [ 0 ] . width / 2 ) ) + 'px' ;
2012-08-14 23:08:49 +02:00
shootdiv . style . top = shoots [ i ] [ 1 ] ;
2012-08-15 08:55:08 +02:00
if ( debug )
shootdiv . style . border = "1px solid" ;
2012-08-14 23:08:49 +02:00
} else {
if ( deldiv = document . getElementById ( 'shoot' + i ) ) {
var body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
body . removeChild ( deldiv ) ;
}
}
}
// render pew pew
var pewpewdiv = document . getElementById ( "pewpew" ) ;
if ( pewpew > 0 ) {
pewpewdiv . innerHTML = "pew! pew!" ;
} else {
pewpewdiv . innerHTML = "" ;
}
// render player
var playerdiv = document . getElementById ( "player" ) ;
2012-08-19 17:11:01 +02:00
playerdiv . style . left = ( player _pos [ 0 ]
- Math . round ( playerdiv . getClientRects ( ) [ 0 ] . width / 2 ) ) + 'px' ;
playerdiv . style . top = ( player _pos [ 1 ]
- Math . round ( playerdiv . getClientRects ( ) [ 0 ] . height / 2 ) ) + 'px' ;
2012-08-15 08:55:08 +02:00
if ( debug )
playerdiv . style . border = "1px solid" ;
2012-08-14 23:08:49 +02:00
// render enemy
var enemydiv = document . getElementById ( "enemy" ) ;
2012-08-19 17:11:01 +02:00
enemydiv . style . left = ( enemy _pos [ 0 ]
- Math . round ( enemydiv . getClientRects ( ) [ 0 ] . width / 2 ) ) + 'px' ;
enemydiv . style . top = ( enemy _pos [ 1 ]
- Math . round ( enemydiv . getClientRects ( ) [ 0 ] . height / 2 ) ) + 'px' ;
2012-08-15 08:55:08 +02:00
if ( debug )
enemydiv . style . border = "1px solid" ;
}
2012-08-14 23:08:49 +02:00
2012-08-19 17:45:26 +02:00
function lerp ( t , a , b ) {
return ( a + t * ( b - a ) ) ;
}
var a = 0 ;
2012-08-15 08:55:08 +02:00
function animate ( ) {
2012-08-19 17:45:26 +02:00
// inc loop counter
a ++ ;
2012-08-14 23:08:49 +02:00
// animate shoots
for ( var i = 0 ; i < shoots . length ; i ++ ) {
if ( shoots [ i ] ) {
shoots [ i ] [ 1 ] -= 20 ;
if ( shoots [ i ] [ 1 ] < 0 ) {
delete ( shoots [ i ] ) ;
}
}
}
// animate pew pew
if ( pewpew > 0 ) {
pewpew -= 1 ;
}
// animate enemy
2012-08-19 17:45:26 +02:00
// every now and then change direction
2012-08-14 23:08:49 +02:00
var min = - 50 ;
var max = + 50 ;
2012-08-19 17:45:26 +02:00
if ( a % 15 == 0 ) {
var off = min + parseInt ( Math . random ( ) * ( max - min ) ) ;
new _enemy _pos [ 0 ] += off ;
var off = min + parseInt ( Math . random ( ) * ( max - min ) ) ;
new _enemy _pos [ 1 ] += off ;
if ( new _enemy _pos [ 0 ] < 0 ) new _enemy _pos [ 0 ] = 0 ;
if ( new _enemy _pos [ 0 ] > window . innerWidth ) new _enemy _pos [ 0 ] = window . innerWidth ;
if ( new _enemy _pos [ 1 ] < 0.05 * window . innerHeight ) new _enemy _pos [ 1 ] = 0.05 * window . innerHeight ;
if ( new _enemy _pos [ 1 ] > 0.5 * window . innerHeight ) new _enemy _pos [ 0 ] = 0.5 * window . innerHeight ;
}
// slowly move to desired position
enemy _pos [ 0 ] = lerp ( 0.15 , enemy _pos [ 0 ] , new _enemy _pos [ 0 ] ) ;
enemy _pos [ 1 ] = lerp ( 0.15 , enemy _pos [ 1 ] , new _enemy _pos [ 1 ] ) ;
2012-08-15 08:55:08 +02:00
}
function gameLoop ( ) {
detectCollisions ( ) ;
render ( ) ;
animate ( ) ;
2012-08-14 23:08:49 +02:00
setTimeout ( "gameLoop()" , 100 ) ;
}
2012-08-19 17:11:01 +02:00
function init ( ) {
gameLoop ( ) ;
}