Add collision handler

This commit is contained in:
RedEnchilada 2016-05-07 12:54:40 -05:00
parent 536dc9e2c0
commit 8b3a671066
5 changed files with 66 additions and 2 deletions

View file

@ -74,7 +74,9 @@ LevelDatabase = {
{x: 1536, y: 1298, z: 0, type: "small"},
{x: 1506, y: 1268, z: 0, type: "large"},
{x: 1536, y: 1238, z: 0, type: "small"},
{x: 1566, y: 1268, z: 0, type: "large"}
{x: 1566, y: 1268, z: 0, type: "large"},
{x: 1024, y: 1024, z: 0, type: "large"}
],
spawn: {

View file

@ -10,6 +10,13 @@ Dolphin = function(level, axis, position, z) {
position: position
};
dolphin.bbox = {
x: 20,
y: 20,
z: 20,
tag: "player"
};
axisMove();
var sprite = Renderer.animation([
@ -61,6 +68,13 @@ Dolphin = function(level, axis, position, z) {
}
positionOnAxis();
if (toucher = dolphin.colliding("geometry")) {
dolphin.momentum.x /= -1.5;
dolphin.axis.current = oldCurrent;
dolphin.axis.position = oldPosition;
positionOnAxis();
}
}
function newAxis(transitionList) {

View file

@ -39,5 +39,9 @@ Entity = function(level, x, y, z) {
currentAnim = name;
}
entity.colliding = function(tag) {
return level.colliding(entity, tag);
}
return entity;
};

View file

@ -110,6 +110,49 @@ Level = function(levelName) {
}
level.colliding = function(object, tag, excludes) {
excludes = excludes || [];
var xoffs = object.position.x % 600 > 300 ? 1 : -1;
var yoffs = object.position.y % 600 > 300 ? 512 : -512;
try {
([0, xoffs, yoffs, xoffs+yoffs]).forEach(function(pos) {
if (entityGrid[pos]) {
entityGrid[pos].forEach(function(target) {
if (!target.bbox || target.bbox.tag != tag) {
return;
}
if (Math.abs(target.position.x - object.position.x)
> target.bbox.x + object.bbox.x) {
return;
}
if (Math.abs(target.position.y - object.position.y)
> target.bbox.y + object.bbox.y) {
return;
}
if (Math.abs(target.position.z - object.position.z)
> target.bbox.z + object.bbox.z) {
return;
}
if (excludes.indexOf(target) != -1) {
return;
}
throw target;
});
}
});
} catch (toucher) {
return toucher;
}
return false;
}
function placeEntityInGrid(entity) {
var grid;
var gridpos = getGridIndex(entity.position.x, entity.position.y);

View file

@ -8,7 +8,8 @@ Rock = function(level, x, y, z, size) {
small: 12,
medium: 31,
large: 47
})[size]
})[size],
tag: "geometry"
};
rock.addSprite("sprite", Renderer.sprite("rock-" + size));