summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/engine
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-04-23 17:43:09 -0400
committerJules Laplace <jules@okfoc.us>2014-04-23 17:43:09 -0400
commit7c82721449b09496ad4e33c04513e4253482f457 (patch)
tree24b783981dcc0d1b188227d079ea13042895440e /assets/javascripts/rectangles/engine
parent1282559bffc1acbc99a22ecfef44227eefbc9817 (diff)
basic collision detection
Diffstat (limited to 'assets/javascripts/rectangles/engine')
-rw-r--r--assets/javascripts/rectangles/engine/builder.js24
-rw-r--r--assets/javascripts/rectangles/engine/mover.js32
2 files changed, 41 insertions, 15 deletions
diff --git a/assets/javascripts/rectangles/engine/builder.js b/assets/javascripts/rectangles/engine/builder.js
index c48e8e4..42ef76b 100644
--- a/assets/javascripts/rectangles/engine/builder.js
+++ b/assets/javascripts/rectangles/engine/builder.js
@@ -113,7 +113,8 @@ var builder = new function(){
for (var i = 0; i < already_constructed.length; i++) {
if (already_constructed[i].rect.contains(r)) {
intersected = true
- r.sides = 0xf
+ // r.sides = 0xf
+ // half_sides
}
else if (already_constructed[i].rect.intersects(r)) {
intersected = true
@@ -156,7 +157,7 @@ var builder = new function(){
var depth = r.y.length()
var height = hi.height - lo.height
- if (! (r.sides & LEFT) && r.x.a == hi.rect.x.a) {
+ if (! (r.half_sides & LEFT) && r.x.a == hi.rect.x.a) {
el = make_wall('.left')
el.rotationY = HALF_PI
el.height = height
@@ -165,11 +166,11 @@ var builder = new function(){
el.y = lo.height + height/2
el.z = r.y.a + depth/2
list.push(el)
- r.sides |= LEFT
- console.log(hi.height, lo.height)
+ hi.$walls.push(el.el)
+ r.half_sides |= LEFT
}
- if (! (r.sides & RIGHT) && r.x.b == hi.rect.x.b) {
+ if (! (r.half_sides & RIGHT) && r.x.b == hi.rect.x.b) {
el = make_wall('.right')
el.rotationY = -HALF_PI
el.height = height
@@ -178,10 +179,11 @@ var builder = new function(){
el.y = lo.height + height/2
el.z = r.y.b - depth/2
list.push(el)
- r.sides |= RIGHT
+ hi.$walls.push(el.el)
+ r.half_sides |= RIGHT
}
- if (! (r.sides & FRONT) && r.y.a == hi.rect.y.a) {
+ if (! (r.half_sides & FRONT) && r.y.a == hi.rect.y.a) {
el = make_wall('.front')
el.width = width
el.height = height
@@ -190,10 +192,11 @@ var builder = new function(){
el.y = lo.height + height/2
el.z = r.y.a
list.push(el)
- r.sides |= FRONT
+ hi.$walls.push(el.el)
+ r.half_sides |= FRONT
}
- if (! (r.sides & BACK) && r.y.b == hi.rect.y.b) {
+ if (! (r.half_sides & BACK) && r.y.b == hi.rect.y.b) {
el = make_wall('.back')
el.width = width
el.height = height
@@ -202,7 +205,8 @@ var builder = new function(){
el.y = lo.height + height/2
el.z = r.y.b
list.push(el)
- r.sides |= BACK
+ hi.$walls.push(el.el)
+ r.half_sides |= BACK
}
return list
}
diff --git a/assets/javascripts/rectangles/engine/mover.js b/assets/javascripts/rectangles/engine/mover.js
index 90cabdd..5a1f7d9 100644
--- a/assets/javascripts/rectangles/engine/mover.js
+++ b/assets/javascripts/rectangles/engine/mover.js
@@ -13,13 +13,32 @@ var mover = new function(){
}
base.update = function(pos){
- if (base.room && base.room.rect.contains(pos.x, pos.z)) return;
+ cam.y = pos.y
- var intersects = []
- clipper.rooms.forEach(function(r){
- if (r.rect.contains(pos.x, pos.z)) {
- intersects.push(r)
+ // if we were in a room
+ if (base.room) {
+ // check if we're still in the room
+ if (base.room.rect.contains(pos.x, pos.z)) {
+ cam.x = pos.x
+ cam.z = pos.z
+ return
}
+ // check if we've crossed one of the walls.. clamp position if so
+ var collision = base.room.collides(pos.x, pos.z)
+ if (collision) {
+ if (! (collision & LEFT || collision & RIGHT)) {
+ cam.x = pos.x
+ }
+ if (! (collision & FRONT || collision & BACK)) {
+ cam.z = pos.z
+ }
+ return
+ }
+ }
+
+ // otherwise we've either entered into a new room, or broken free
+ var intersects = clipper.rooms.filter(function(r){
+ return r.rect.contains(pos.x, pos.z)
})
$(".face.active").removeClass("active")
@@ -32,6 +51,9 @@ var mover = new function(){
else {
base.room = null
}
+
+ cam.x = pos.x
+ cam.z = pos.z
}
}