summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/engine
diff options
context:
space:
mode:
Diffstat (limited to 'assets/javascripts/rectangles/engine')
-rw-r--r--assets/javascripts/rectangles/engine/builder.js95
-rw-r--r--assets/javascripts/rectangles/engine/clipper.js4
-rw-r--r--assets/javascripts/rectangles/engine/mover.js48
3 files changed, 92 insertions, 55 deletions
diff --git a/assets/javascripts/rectangles/engine/builder.js b/assets/javascripts/rectangles/engine/builder.js
index 8e1508a..42ef76b 100644
--- a/assets/javascripts/rectangles/engine/builder.js
+++ b/assets/javascripts/rectangles/engine/builder.js
@@ -19,12 +19,9 @@ var builder = new function(){
}
function build (){
clipper.rooms = sort_rooms_by_id(clipper.rooms)
- clipper.rooms.forEach(function(r){
- r.walls = []
- r.floors = []
- })
+
clipper.regions.forEach(function(r){
- walls(r).forEach(function(el){
+ build_walls(r).forEach(function(el){
els.push(el)
scene.add(el)
})
@@ -32,7 +29,7 @@ var builder = new function(){
clipper.rooms = sort_rooms_by_height(clipper.rooms)
clipper.rooms.forEach(function(r){
- floors(r).forEach(function(el){
+ build_floors(r).forEach(function(el){
els.push(el)
scene.add(el)
})
@@ -45,7 +42,9 @@ var builder = new function(){
els = []
}
- function walls (r){
+ function build_walls (r){
+ var rm = clipper.rooms[ r.id ]
+
var list = [], el = null
var width = r.x.length()
@@ -53,50 +52,54 @@ var builder = new function(){
var height = clipper.rooms[r.id].height
if (r.sides & FRONT) {
- el = wall('.front')
+ el = make_wall('.front')
el.width = width
el.height = height
el.rotationY = PI
el.x = r.x.a + width/2
el.y = height/2
el.z = r.y.a
+ rm.$walls.push(el.el)
list.push(el)
}
if (r.sides & BACK) {
- var el = wall('.back')
+ var el = make_wall('.back')
el.width = width
el.height = height
el.rotationY = 0
el.x = r.x.b - width/2
el.y = height/2
el.z = r.y.b
+ rm.$walls.push(el.el)
list.push(el)
}
if (r.sides & LEFT) {
- el = wall('.left')
+ el = make_wall('.left')
el.rotationY = HALF_PI
el.height = height
el.width = depth
el.x = r.x.a
el.y = height/2
el.z = r.y.a + depth/2
+ rm.$walls.push(el.el)
list.push(el)
}
if (r.sides & RIGHT) {
- el = wall('.right')
+ el = make_wall('.right')
el.rotationY = -HALF_PI
el.height = height
el.width = depth
el.x = r.x.b
el.y = height/2
el.z = r.y.b - depth/2
+ rm.$walls.push(el.el)
list.push(el)
}
return list
}
- function floors(rm){
+ function build_floors(rm){
var list = [], el = null
var already_constructed = rm.intersects.filter(function(rr){ return rr.constructed })
@@ -110,41 +113,52 @@ 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
if (rm.height < already_constructed[i].height) {
- list = list.concat( ceiling_walls( rm, already_constructed[i], r ) )
+ list = list.concat( make_ceiling_walls( rm, already_constructed[i], r ) )
}
}
}
if (! intersected) {
- list.push( ground(rm, r) )
- list.push( ceiling(rm, r) )
+ el = make_floor(rm, r)
+ list.push( el )
+ rm.$floor.push(el.el)
+
+ el = make_ceiling(rm, r)
+ list.push( el )
+ rm.$ceiling.push(el.el)
}
})
}
else {
// render floor and ceiling for the entire rectangle
- list.push( ground(rm, rm.rect) )
- list.push( ceiling(rm, rm.rect) )
+ el = make_floor(rm, rm.rect)
+ list.push( el )
+ rm.$floor.push(el.el)
+
+ el = make_ceiling(rm, rm.rect)
+ list.push( el )
+ rm.$ceiling.push(el.el)
}
rm.constructed = true
return list
}
- function ceiling_walls( lo, hi, r ){
+ function make_ceiling_walls( lo, hi, r ){
var list = []
var width = r.x.length()
var depth = r.y.length()
var height = hi.height - lo.height
- if (! (r.sides & LEFT) && r.x.a == hi.rect.x.a) {
- el = wall('.left')
+ if (! (r.half_sides & LEFT) && r.x.a == hi.rect.x.a) {
+ el = make_wall('.left')
el.rotationY = HALF_PI
el.height = height
el.width = depth
@@ -152,12 +166,12 @@ 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) {
- el = wall('.right')
+ if (! (r.half_sides & RIGHT) && r.x.b == hi.rect.x.b) {
+ el = make_wall('.right')
el.rotationY = -HALF_PI
el.height = height
el.width = depth
@@ -165,11 +179,12 @@ 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) {
- el = wall('.front')
+ if (! (r.half_sides & FRONT) && r.y.a == hi.rect.y.a) {
+ el = make_wall('.front')
el.width = width
el.height = height
el.rotationY = PI
@@ -177,11 +192,12 @@ 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) {
- el = wall('.back')
+ if (! (r.half_sides & BACK) && r.y.b == hi.rect.y.b) {
+ el = make_wall('.back')
el.width = width
el.height = height
el.rotationY = 0
@@ -189,45 +205,42 @@ 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
}
- function ground(rm, r){
+ function make_floor(rm, r){
var width = r.x.length()
var depth = r.y.length()
- var el = wall('.bottom')
+ var el = make_wall('.floor')
el.height = depth
el.width = width
el.x = r.x.a + width/2
el.y = 0
el.z = r.y.a + depth/2
el.rotationX = PI/2
- el.el.style.backgroundColor = "#f00"
-
- rm.floors.push(el)
return el
}
- function ceiling(rm, r){
+ function make_ceiling(rm, r){
var width = r.x.length()
var depth = r.y.length()
var height = rm.height
- var el = wall('.top')
+ var el = make_wall('.ceiling')
el.height = depth
el.width = width
el.x = r.x.a + width/2
el.y = height
el.z = r.y.a + depth/2
el.rotationX = -PI/2
- el.el.style.backgroundColor = "#00f"
return el
}
- function wall(klass){
+ function make_wall(klass){
var el = new MX.Object3D(".face" + (klass || ""))
el.width = el.height = el.scaleX = el.scaleY = el.scaleZ = 1
el.z = el.y = el.x = 0
diff --git a/assets/javascripts/rectangles/engine/clipper.js b/assets/javascripts/rectangles/engine/clipper.js
index bcd0586..43a1bab 100644
--- a/assets/javascripts/rectangles/engine/clipper.js
+++ b/assets/javascripts/rectangles/engine/clipper.js
@@ -97,10 +97,6 @@ var clipper = new function(){
base.regions = sort_rects_by_position(regions)
}
- // generate floor and ceiling for some regions
- // generate walls from surviving regions
- // generate ceiling-walls where ceiling has discontinuity
-
return base
}
diff --git a/assets/javascripts/rectangles/engine/mover.js b/assets/javascripts/rectangles/engine/mover.js
index ebe4447..5a1f7d9 100644
--- a/assets/javascripts/rectangles/engine/mover.js
+++ b/assets/javascripts/rectangles/engine/mover.js
@@ -1,11 +1,11 @@
var mover = new function(){
var base = this
- var last_room = null
+ base.room = null
base.init = function(){
- last_room = clipper.rooms[0]
base.bind()
+ base.update(scene.camera)
}
base.bind = function(){
@@ -13,19 +13,47 @@ var mover = new function(){
}
base.update = function(pos){
- if (last_room && last_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")
if (intersects.length) {
- $(".face.active").removeClass("active")
- last_room = intersects[0]
- }
+ base.room = intersects[0]
+ base.room.$floor.addClass("active")
+ base.room.$ceiling.addClass("active")
+ base.room.$walls.addClass("active")
+ }
+ else {
+ base.room = null
+ }
+
+ cam.x = pos.x
+ cam.z = pos.z
}
}