Benutzer:Schüler/Jump ’n’ Run collision
Erscheinungsbild
// Collision detection
var steps = 32;
for (var i=0;i<steps;i++) {
for each(var creature in Creatures) {
if(creature.awake && creature.y < (Map.Height*32) + 32) {
var speedX = creature.speedX/steps;
var speedY = creature.speedY/steps;
var x = Math.round(creature.x);
var y = Math.round(creature.y);
// Bricket collision
if(x%32===0) {
var Bx = x/32;
try { var LeftCollision = ((Map.Data[Bx-1][Math.floor(y/32)]>7||Map.Data[Bx-1][Math.ceil(y/32)]>7) && creature.speedX<0); } catch(e) { var LeftCollision = false; }
try { var RightCollision = ((Map.Data[Bx+1][Math.floor(y/32)]>7||Map.Data[Bx+1][Math.ceil(y/32)]>7) && creature.speedX>0); } catch(e) { var RightCollision = false; }
if(LeftCollision||RightCollision) { creature.speedX = 0; }
}
if(y%32===0) {
var By = y/32;
try { var TopCollision = ((Map.Data[Math.floor(x/32)][By-1]>7||Map.Data[Math.ceil(x/32)][By-1]>7) && creature.speedY<0); } catch(e) { var TopCollision = false; }
try { var BottomCollision = ((Map.Data[Math.floor(x/32)][By+1]>7||Map.Data[Math.ceil(x/32)][By+1]>7) && creature.speedY>0); } catch(e) { var BottomCollision = false; }
if(TopCollision||BottomCollision) { creature.speedY = 0; }
if(BottomCollision) creature.AllowJump = 1;
}
// Creature collision
for each(var creature2 in Creatures) {
if(creature2 != creature && !creature2.dead && !creature.dead ) {
var LeftCollision = (x == (Math.round(creature2.x) + 32) && (y + 32) > Math.round(creature2.y) && y < (Math.round(creature2.y) + 32) && creature.speedX<0);
var RightCollision = ( (x+32) == Math.round(creature2.x) && y + 32 > Math.round(creature2.y) && y < Math.round(creature2.y) + 32 && creature.speedX>0);
var TopCollision = ( x < Math.round(creature2.x) + 32 && x > Math.round(creature2.x) - 32 && y == (Math.round(creature2.y)+32) && creature.speedY<0);
var BottomCollision = (x < Math.round(creature2.x) + 32 && x > Math.round(creature2.x) - 32 && (y+32) == Math.round(creature2.y) && creature.speedY>0);
// Handle collision
if(BottomCollision) creature.AllowJump = 1;
// Enemy
if((LeftCollision||RightCollision) && creature.type == "player" && creature2.type <= 1) { creature.kill = 1; }
if((LeftCollision||RightCollision) && creature.type <= 1 && creature2.type == "player") { creature2.kill = 1; }
if(BottomCollision && creature.type <= 1 && creature2.type == "player") { creature2.kill = 1; }
if(BottomCollision && creature.type == "player" && creature2.type <= 1) {
creature.NeedJump = 1;
creature2.dead = 1;
creature2.frame = 2;
}
// Player/Player collision
if(creature.type=="player" && creature2.type=="player") {
if(LeftCollision||RightCollision) {
creature.x -= speedX;
var s = creature.speedX; creature.speedX = creature2.speedX; creature2.speedX=s;
if(creature.speedX>0&&creature2.speedX<0) { creature.speedX--; creature2.speedX++; }
if(creature.speedX<0&&creature2.speedX>0) { creature.speedX++; creature2.speedX--; }
LeftCollision=false; RightCollision=false;
}
if(TopCollision||BottomCollision) {
creature.y -= speedY;
var s = creature.speedY; creature.speedY = creature2.speedY; creature2.speedY=s;
if(creature.speedY>0&&creature2.speedY<0) { creature.speedY--; creature2.speedY++; }
if(creature.speedY<0&&creature2.speedY>0) { creature.speedY++; creature2.speedY--; }
TopCollision=false; BottomCollision=false;
}
}
if(LeftCollision||RightCollision) { creature.speedX = 0; }
if(TopCollision||BottomCollision) { creature.speedY = 0; }
}
}
var speedX = creature.speedX/steps;
var speedY = creature.speedY/steps;
creature.x += speedX; creature.y += speedY;
}
}
}