Skip to content

Commit

Permalink
#61 map layer sort bounds - parsed, 3d wireframe, and map element int…
Browse files Browse the repository at this point in the history
…ersects entity method (Map Export script still has a position bug for these border shapes)
  • Loading branch information
robwalch committed Mar 23, 2013
1 parent 6aba71a commit 105990d
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 21 deletions.
111 changes: 97 additions & 14 deletions app/scripts/models/MapElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,112 @@
}
},

isBehindEntity: function(entity) {
intersectsEntity: function (entity) {
/* extend class for these types */
switch (this.mapType) {
case 'container':
case 'parallax':
return false;
}

var mapBounds = this.getSortBounds();
var entityBounds = entity.getSortBounds();

// -- CHECK IF ENTITY IS BEHIND --

// Entity is under Element
if (entityBounds.top < this.z) {//mapBounds.bottom? or z?
return false;
}

// Entity is behind Element
if (entityBounds.front < mapBounds.back) {
return false;
}

// -- CHECK IF ENTITY IS IN FRONT --

// Entity is above Element
if (entity.z >= mapBounds.top) {
return false;
}

// Entity is in front of Element
if (entityBounds.back > mapBounds.front) {
return false;
}

// -- ENTITY IS TO THE SIDE OF OVERLAPS --

if (entityBounds.right < mapBounds.left || entityBounds.left > mapBounds.right) {
// Entity is outside the left or right of Element
return false;
}
else {
// Entity BB overlaps with Element BB

switch (this.shape) {
case 'box':
// if rotated fall through to polygon check
if (!this.body || this.body.a === 0) {
return false;
}
/*falls through*/
case 'polygons':
// test all shape edges
var point, distance = Infinity;
for (var i = this.shapes.length; i-- > 0;) {
var shape = this.shapes[i];
var cpShape = shape.cpShape;
var info = cpShape.nearestPointQuery(entity.body.p);
if (info.d < distance) {
distance = info.d;
point = info.p;
}
}
if (distance > 0) {
// Entity is outside Element shape
return false;
}
break;

case 'oval':
// TODO: test distance from center
break;

default:
throw('Invalid map shape type: '+ this.shape);
}
}

return true;
},

isBehindEntity: function(entity) {
/* extend class for these types */
switch (this.mapType) {
case 'container':
case 'parallax':
return false;
}

var mapBounds = this.getSortBounds();
var entityBounds = entity.getSortBounds();

// -- CHECK IF ENTITY IS BEHIND --

// Entity is under Element
if (entityBounds.top < this.z) {//mapBounds.bottom? or z?
return false;
}

// Entity is behind Element
if (entityBounds.front < mapBounds.back) {
return false;
}

// -- CHECK IF ENTITY IS IN FRONT --

// Entity is above Element
if (entity.z >= mapBounds.top) {
// Entity is not behind Element
Expand All @@ -127,18 +222,6 @@
return false;
}

// -- CHECK IF ENTITY IS BEHIND --

// Entity is under Element
if (entityBounds.top < this.z) {//mapBounds.bottom? or z?
return false;
}

// Entity is behind Element
if (entityBounds.front < mapBounds.back) {
return false;
}

// -- ENTITY IS TO THE SIDE OF OVERLAPS --

if (entityBounds.right < mapBounds.left || entityBounds.left > mapBounds.right) {
Expand Down Expand Up @@ -242,7 +325,7 @@
this.addBox(pos, shape.width, shape.height, shape);
}

} else if (this.mapType === 'floor') {
} else if (this.mapType === 'floor' || this.mapType === 'bounds') {
if (this.shape === 'polygons') {
this.setBody(new cp.Body(Infinity, Infinity));
//poly to verts... 2d array to flat array
Expand Down
15 changes: 11 additions & 4 deletions app/scripts/models/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,15 @@
layer.layerNum = i;

var elements = layer.elements || [];
var shapes = layer.shapes || [];

//var shapes = layer.shapes || [];
var layerBounds = layer.bounds;
if (layerBounds) {
// ew! updating the data we're parsing
layerBounds.mapType = 'bounds';
layer.bounds = this.initMapElement(layerBounds);
this.addMapElement(layer.bounds);
}

for (j=elements.length; j-- > 0;) {
if (elements[j].mapType === 'entity') {
entities.push(elements.splice(j, 1)[0]);
Expand All @@ -103,7 +110,7 @@
}
}
layer.elements = elements;
layer.shapes = shapes;
//layer.shapes = shapes;
}
// add entities
var entity;
Expand Down Expand Up @@ -181,7 +188,7 @@

addMapElement: function(mapElement) {
//negative depth implies impassable bounds
if (mapElement.depth >= 0) {
if (mapElement.depth >= 0 && mapElement.mapType !== 'bounds') {
this.elements.push(mapElement);
}
for (var i in mapElement.shapes) {
Expand Down
9 changes: 9 additions & 0 deletions app/scripts/views/Canvas2dMapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@
getItemsInLayer: function(layer, entities, inLayer) {
inLayer = inLayer || [];
var elements = layer.elements;
var bounds = layer.bounds;
for (var i = entities.length; i-- > 0;) {
var entity = entities[i];
entity.layerNum = -1;
if (bounds) {
// skip layer if not entity is not intersecting
if (!bounds.intersectsEntity(entity)) {
//continue;
} else {
//console.log('oh hello!', layer.name);
}
}
for (var j=elements.length; j-- > 0;) {
if (elements[j].isBehindEntity(entity)) {
entity.layerNum = layer.layerNum;
Expand Down
14 changes: 11 additions & 3 deletions app/scripts/views/ThreeJsWorldView.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,15 @@
var materials = {};
var shapes = this.shapes = [];
this.updateShapeView = (function(scene) {
var getMaterial = function(name, color) {
materials[name] = materials[name] || //new THREE.MeshBasicMaterial( {color: color, wireframe: true} );
[ new THREE.MeshLambertMaterial( { color: color } ), new THREE.MeshBasicMaterial( { color: color, wireframe: true, transparent: true } ) ];
var getMaterial = function(name, color, wireframeOnly) {
if (!materials[name]) {
var multi = [new THREE.MeshBasicMaterial( { color: color, wireframe: true, transparent: true } )];
if (!wireframeOnly) {
multi.unshift(new THREE.MeshLambertMaterial( { color: color } ));
}
materials[name] = multi;
//materials[name] = new THREE.MeshBasicMaterial( {color: color, wireframe: true} );
}
return materials[name];
};
var getEntityMaterial = function(entity) {
Expand Down Expand Up @@ -128,6 +134,8 @@
return getMaterial('wall', 0x228800);
} else if (entity.mapType === 'steps') {
return getMaterial('steps', 0x009900);
} else if (entity.mapType === 'bounds') {
return getMaterial('map', 0x00ff00, true);
} else {
return getMaterial('map', 0x00ff00);
}
Expand Down

0 comments on commit 105990d

Please sign in to comment.