Skip to content

Instantly share code, notes, and snippets.

Created November 30, 2012 01:23
Show Gist options
  • Save anonymous/4173130 to your computer and use it in GitHub Desktop.
Save anonymous/4173130 to your computer and use it in GitHub Desktop.
Player
function Player(world) {
var WALK_VX = 0.2;
var WALK_VY = 0.2;
var vx = 0;
var vy = 0;
//0- olhando para esquerda; 1 - olhando para direita
var face = 0;
var pos = this.pos = [gs.width/2, gs.height / 2 ];
var p = new Sprite( ["center","bottom"],
{"stand_left": [["{{url_for('static', filename='sprite/boneco_left1.png')}}",0],],
"stand_right": [["{{url_for('static', filename='sprite/boneco_right1.png')}}",0],],
"stand_down": [["{{url_for('static', filename='sprite/boneco_down1.png')}}",0],],
"stand_up": [["{{url_for('static', filename='sprite/boneco_up1.png')}}",0],],
"walk_left" : [["{{url_for('static', filename='sprite/boneco_left1.png')}}",3],
["{{url_for('static', filename='sprite/boneco_left2.png')}}",3],
["{{url_for('static', filename='sprite/boneco_left3.png')}}",3],
["{{url_for('static', filename='sprite/boneco_left4.png')}}",3],],
"walk_right" : [["{{url_for('static', filename='sprite/boneco_right1.png')}}",3],
["{{url_for('static', filename='sprite/boneco_right2.png')}}",3],
["{{url_for('static', filename='sprite/boneco_right3.png')}}",3],
["{{url_for('static', filename='sprite/boneco_right4.png')}}",3],],
"walk_down" : [["{{url_for('static', filename='sprite/boneco_down1.png')}}",3],
["{{url_for('static', filename='sprite/boneco_down2.png')}}",3],
["{{url_for('static', filename='sprite/boneco_down3.png')}}",3],
["{{url_for('static', filename='sprite/boneco_down4.png')}}",3],
],
"walk_up" : [["{{url_for('static', filename='sprite/boneco_up1.png')}}",3],
["{{url_for('static', filename='sprite/boneco_up2.png')}}",3],
["{{url_for('static', filename='sprite/boneco_up3.png')}}",3],
["{{url_for('static', filename='sprite/boneco_up4.png')}}",3],
],},
function(){
p.action("stand_left");
});
this.init = function() {
};
this.draw = function(c) {
p.draw(c, world.camera(pos));
r = p.aabb(world.camera(pos));
c.beginPath();
c.strokeStyle = 'black';
c.rect(r[0], r[1], r[2], r[3]);
c.stroke();
};
this.update = function() {
this._updateanimation();
p.update();
pos[0] += vx;
pos[1] += vy;
};
this._updateanimation = function(){
if (vx >= WALK_VX){
face = 1;
p.action("walk_right");
}
else if (vx <= -WALK_VX) {
face = 0;
p.action("walk_left");
}
else if (vy >= WALK_VY){
//p.action("walk_"+((face )?"right":"left"));
face = 2;
p.action("walk_down");
}
else if (vy <= -WALK_VY) {
// p.action("walk_"+((face )?"right":"left"));
face = 3;
p.action("walk_up");
}
else{
var f = "";
switch (face){
case 0: f = "left";
break;
case 1: f = "right";
break;
case 2: f = "down";
break;
case 3: f = "up";
break;
}
p.action("stand_"+f);
}
};
/*** input events stuff ***/
this.keyDown_37 = function () {
this._updateanimation();
vx = -WALK_VX;
};
this.keyUp_37 = this.keyUp_39 = function() {
this._updateanimation();
vx = 0;
};
this.keyUp_38 = this.keyUp_40 = function() {
this._updateanimation();
vy = 0;
};
this.keyDown_39 = function () {
this._updateanimation();
vx = WALK_VX;
};
this.keyDown_38 = function(){
this._updateanimation();
vy = -WALK_VY;
}
this.keyDown_40 = function(){
this._updateanimation();
vy = WALK_VY;
}
this.keyUp_32 = function(){
alert('to aqui');
}
this.get_collision_aabb = function(){
return p.aabb(pos);
}
// comparison function
var cmp = function(x, y){ return x[0] < y[0] ? 1 : x[0] > y[0] ? -1 : 0;};
this.collide_aabb = function(who){
//alert(who.type);
//alert(this.get_collision_aabb());
if (who.type == 'estrela'){
}
if (who.type == 'parede'){
var ab = this.get_collision_aabb();
var bb = who.get_collision_aabb();
var sides = [
[bb[1] - (ab[1] + ab[3]), 1, 1], //colisão em cima
[bb[0] - (ab[0] + ab[2]), 0, 1], //colisao pela esquerda
[ab[0] - (bb[0] + bb[2]), 0, -1], //colisão pela direita
[ab[1] - (bb[1] + bb[3]), 1, -1] //colisao em baixo
];
sides.sort(cmp);
var d = sides[0];
// alert(d);
// hit a vertical side
if (d[1]) {
if (d[2] == 1 && vy > 0){
vy = 0;
} else if(d[2] == -1 && vy < 0) {
vy = 0;
}
this._updateanimation();
} else {
// horizontal side
if (d[2] == 1 && vx > 0){
vx = 0;
} else if(d[2] == -1 && vx < 0) {
vx = 0;
}
this._updateanimation();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment