Permalink
Browse files

Collisions, pipe logic

  • Loading branch information...
1 parent 85d36e9 commit 23d64909d86766e5d5cf04ccfe81fc113b72bcfe @davidgovea committed Aug 6, 2011
Showing with 138 additions and 62 deletions.
  1. +138 −62 main.js
View
200 main.js
@@ -20,9 +20,10 @@
bg = new Game.bg('images/bg.png', 3, "S", {x:0,y:0}),
floor = 75,
+ mzone = [20, 40, 12, 40]
mheight = 200,
mwidth = 200,
- mzero = HEIGHT -floor - mheight+15,
+ mzero = HEIGHT -floor - mheight + mzone[2],
mleft = 0,
mright = WIDTH-mwidth,
pipeh = 125,
@@ -31,13 +32,13 @@
pipey = HEIGHT - floor - pipeh,
pipetop = mzero - pipeh,
- xacc = 10,
+ xacc = 5,
max_vx = 50,
jumpv = 160,
gravity = 11,
vscale = 0.1;
- mario.zone(20,20,20,20);
+ mario.zone(mzone[0],mzone[1],mzone[2],mzone[3]);
_.extend(mario, {
vel: {
@@ -54,12 +55,13 @@
height: mheight,
up: 0,
dir: 1,
+ under: false,
mode: function(mode, direction){
var start = false,
- dir = direction || mario.dir,
+ dir = direction,
num;
- mario.dir = dir;
+ //mario.dir = dir;
switch(mode){
case 'stand':
if(mario.state !== 0){
@@ -90,80 +92,99 @@
if(start){
mario.change('images/'+mode+'.png', 200, 200, num, 1);
+ mario.zone(mzone[0],mzone[1],mzone[2],mzone[3]);
}
- // if(dir !== mario.dir){
- mario.animation((mario.dir<0) ? 0 : 1);
- //mario.dir = dir;
- // }
- mario.position(mario.x, mario.y);
- // console.log(mario.dir);
- // console.log(dir);
+ if(dir !== 0 && dir !== mario.dir){
+ mario.dir = dir;
+ }
+ mario.animation((mario.dir<0) ? 0 : 1);
+ //mario.position(mario.pos.x, mario.pos.y);
+
+
return mario;
}
});
- console.log(mario);
- mario.size(mario.width,mario.height);
- mario.position(mario.pos.x, mario.pos.y, 6).speed(4);
+ mario.position(mario.pos.x, mario.pos.y, 1).speed(4);
//pipe.size(pipew, pipeh);
- pipe.position(pipex, pipey, 2).speed(0);
+ pipe.position(pipex, HEIGHT-floor-15, 2).speed(0).noHits();
- boxes[0].position(45, 45).speed(8);
- boxes[1].position(180, 45).speed(8);
- boxes[2].position(610, 45).speed(8);
+ boxes[0].position(50, 55).speed(8);
+ boxes[1].position(250, 55).speed(8);
+ boxes[2].position(650, 55).speed(8);
- pow.position(pipex+36, 2).speed(8);
+ pow.position(450, 55).speed(8);
- ground.position(0, HEIGHT-79).speed(0);
+ ground.position(0, HEIGHT-79, 2).speed(0);
bg.speed(0).dir(180).on();
function moveHandler(){
var state = "stand",
bgspeed = 0;
-
- if(mario.sides){
- state = "walk";
- mario.dir = mario.sides;
+
+ if(!mario.under){
+ if(mario.sides){
+ state = "walk";
+ //mario.dir = mario.sides;
+
+ if(mario.pos.x <= mleft-80 || mario.pos.x >= mright+80){
+ mario.pos.x = (mario.pos.x <= mleft-80) ? mleft -79 : mright +79;
+ mario.vel.x = 0;
+ bgspeed = 7;
+ } else {
+ mario.vel.x += mario.sides * xacc;
+ mario.vel.x = (Math.abs(mario.vel.x) > max_vx) ? mario.sides*max_vx : mario.vel.x;
+ }
- if(mario.pos.x <= mleft-80 || mario.pos.x >= mright+80){
- mario.pos.x = (mario.pos.x <= mleft-80) ? mleft -79 : mright +79;
- mario.vel.x = 0;
- bgspeed = 7;
} else {
- mario.vel.x += mario.dir * xacc;
- mario.vel.x = (Math.abs(mario.vel.x) > max_vx) ? mario.sides*max_vx : mario.vel.x;
+ if(mario.vel.x !== 0) {
+ mario.vel.x *= 0.8;
+ }
}
- } else {
- if(mario.vel.x !== 0) {
- mario.vel.x *= 0.8;
+
+
+ if(mario.pos.y < mzero){
+ mario.vel.y += gravity;
+ state = "jump"
+ } else {
+ mario.vel.y = 0;
+ mario.pos.y = mzero;
+ if(mario.up === 1) mario.state = 0;
}
- }
-
-
-
- if(mario.pos.y < mzero){
- mario.vel.y += gravity;
- state = "jump"
+ if(mario.up == 1){
+ if(mario.state !== 2){
+ mario.vel.y = -jumpv;
+ state = "jump";
+ }
+ }
+
+ if(mario.up == -1 && mario.pos.y == mzero && mario.pos.x > pipex-mzone[3] && mario.pos.x < pipex+pipew-mwidth+mzone[1]){
+ state = "stand";
+ mario.under = true;
+ mario.vel.y = 4;
+ }
+
+ mario.pos.x += mario.vel.x * vscale;
+ mario.pos.y += mario.vel.y * vscale;
+ mario.mode(state, mario.sides).position(mario.pos.x, mario.pos.y);
+ bg.speed(bgspeed * mario.sides);
} else {
- mario.vel.y = 0;
- mario.pos.y = mzero;
- }
-
- if(mario.up){
- if(mario.state !== 2){
- mario.vel.y = -jumpv;
- state = "jump";
+ if(mario.up == 1){
+ mario.vel.y = -4;
+ }
+ if(mario.pos.y < mzero+mheight || mario.vel.y < 0){
+ mario.pos.y += mario.vel.y;
}
+ if(mario.vel.y < 0 && mario.pos.y <= mzero){
+ mario.under = false;
+ mario.pos.y = mzero;
+ }
+ mario.position(mario.pos.x, mario.pos.y);
}
- mario.pos.x += mario.vel.x * vscale;
- mario.pos.y += mario.vel.y * vscale;
- mario.mode(state, mario.dir).position(mario.pos.x, mario.pos.y);
- bg.speed(bgspeed * mario.dir);
-
}
@@ -181,6 +202,9 @@
case 39: //right
mario.sides = 1;
break;
+ case 40: //right
+ mario.up = -1;
+ break;
}
e.stopPropagation();
}
@@ -189,11 +213,14 @@
case 38: //up
if (mario.up===1) mario.up = 0;
break;
+ case 40: //up
+ mario.up = 0;
+ break;
case 37: //left
- if (mario.sides===-1) mario.sides = 0;
+ mario.sides = 0;
break;
case 39: //right
- if (mario.sides===1) mario.sides = 0;
+ mario.sides = 0;
break;
}
//e.stopPropagation();
@@ -211,13 +238,62 @@
-
- mario.hit(pipe, function(){
- // mario.x = pipe.position().x-mario.size().width-1;
- //mario.position(mario.x, mario.y);
- // mario.touch = mario.sides;
- //mario.sides = 0;
+ mario.hit(boxes[0], function(){
+ mario.vel.y = 0;
+ boxes[0].position(50, 48);
+ setTimeout(function(){
+ boxes[0].position(50,55);
+ }, 200)
+ });
+ mario.hit(boxes[1], function(){
+ mario.vel.y = 0;
+ boxes[1].position(250, 48);
+ setTimeout(function(){
+ boxes[1].position(250,55);
+ }, 200)
+ });
+ mario.hit(boxes[2], function(){
+ mario.vel.y = 0;
+ boxes[2].position(650, 48);
+ setTimeout(function(){
+ boxes[2].position(650,55);
+ }, 200)
+ });
+
+ pow.step = 0;
+ mario.hit(pow, function(){
+ var h;
+ pow.step++;
+ switch(pow.step){
+ case 1:
+ h = 80;
+ break;
+ case 2:
+ h = 50;
+ break;
+ case 3:
+ h = 100;
+ break;
+ default:
+ }
+ mario.vel.y = 0;
+ pow.size(118, h);
+ if(pow.step != 3){
+ pow.position(450, 55);
+ if(pow.step == 2){
+ setTimeout(function(){
+ pow.position(450,55+28);
+ }, 200)
+ }
+ } else {
+ pow.position(450, 20)
+ setTimeout(function(){
+ pow.position(450,55);
+ }, 150)
+ }
+
+ pow.step %= 3;
});
Game.on().hook(moveHandler).hitsOn();

0 comments on commit 23d6490

Please sign in to comment.