summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/rooms/builder.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles/engine/rooms/builder.js')
-rw-r--r--public/assets/javascripts/rectangles/engine/rooms/builder.js86
1 files changed, 41 insertions, 45 deletions
diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js
index 6b565c2..492a8c6 100644
--- a/public/assets/javascripts/rectangles/engine/rooms/builder.js
+++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js
@@ -1,22 +1,18 @@
(function(){
- var vec2, Rect, Room, sort, UidGenerator, _
+ var Rooms, sort
if ('window' in this) {
- vec2 = window.vec2
- Rect = window.Rect
- Room = window.Room
+ Rooms = window.Rooms
sort = window.sort
- UidGenerator = window.UidGenerator
- _ = window._
}
else {
- vec2 = require('../../models/vec2')
- Rect = require('../../models/rect')
- Room = require('../../models/room')
+ MX = require('../../../../../../test/mocks/mx.js')
+ scene = MX.scene
+ Rooms = require('./_rooms')
sort = require('../../util/sort')
- UidGenerator = require('../../util/uid')
- _ = require('lodash')
FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
+ PI = Math.PI
+ HALF_PI = PI/2
TOP = CEILING, BOTTOM = FLOOR
function sidesToString(sides){
var s = ""
@@ -30,7 +26,6 @@
}
}
-
Rooms.builder = new function(){
var base = this
@@ -41,33 +36,34 @@
}
base.bind = function(){
- app.on("clip", rebuild)
+ app.on("clip", base.rebuild.bind(base))
}
- function rebuild(){
+ base.rebuild = function(){
if (window.scene) {
- clear()
- build()
- bind()
+ base.clear()
+ base.build()
+ base.bind_walls()
}
}
- function build (){
+
+ base.build = function (){
Rooms.regions.forEach(function(region){
- build_walls(region).forEach(function(el){
+ this.build_walls(region).forEach(function(el){
els.push(el)
scene.add(el)
})
- })
+ }.bind(this))
Rooms.sorted_by_height().forEach(function(room){
- build_floors(room).forEach(function(el){
+ this.build_floors(room).forEach(function(el){
els.push(el)
scene.add(el)
})
- })
+ }.bind(this))
}
- function bind (){
+ base.bind_walls = function (){
Rooms.forEach(function(room){
room.walls = room.group_mx_walls()
room.walls.forEach(function(wall){
@@ -78,7 +74,7 @@
})
}
- function clear (){
+ base.clear = function (){
els.forEach(function(el){
scene.remove(el)
el.destroy && el.destroy()
@@ -86,7 +82,7 @@
els = []
}
- function build_walls (region){
+ this.build_walls = function (region) {
var room = Rooms.list[ region.id ]
var list = [], el = null
@@ -96,7 +92,7 @@
var height = room.height
if (region.sides & FRONT) {
- el = make_wall('.front')
+ el = this.make_wall('.front')
el.width = width
el.height = height
el.rotationY = PI
@@ -109,7 +105,7 @@
list.push(el)
}
if (region.sides & BACK) {
- var el = make_wall('.back')
+ var el = this.make_wall('.back')
el.width = width
el.height = height
el.rotationY = 0
@@ -122,7 +118,7 @@
list.push(el)
}
if (region.sides & LEFT) {
- el = make_wall('.left')
+ el = this.make_wall('.left')
el.rotationY = HALF_PI
el.height = height
el.width = depth
@@ -135,7 +131,7 @@
list.push(el)
}
if (region.sides & RIGHT) {
- el = make_wall('.right')
+ el = this.make_wall('.right')
el.rotationY = -HALF_PI
el.height = height
el.width = depth
@@ -151,7 +147,7 @@
return list
}
- function build_floors(room){
+ this.build_floors = function (room){
var list = [], el = null
var constructed = room.intersects.filter(function(room){ return room.constructed })
@@ -171,17 +167,17 @@
else if (constructed[i].rect.intersects(region)) {
intersected = true
if (room.height < constructed[i].height) {
- var ceiling_walls = make_ceiling_walls( room, constructed[i], region )
+ var ceiling_walls = this.make_ceiling_walls( room, constructed[i], region )
list = list.concat(ceiling_walls)
}
}
}
if (! intersected) {
- el = make_floor(room, region)
+ el = this.make_floor(room, region)
list.push( el )
room.mx_floor.push(el)
- el = make_ceiling(room, region)
+ el = this.make_ceiling(room, region)
list.push( el )
room.mx_ceiling.push(el)
}
@@ -190,11 +186,11 @@
}
else {
// render floor and ceiling for the entire rectangle
- el = make_floor(room, room.rect)
+ el = this.make_floor(room, room.rect)
list.push( el )
room.mx_floor.push(el)
- el = make_ceiling(room, room.rect)
+ el = this.make_ceiling(room, room.rect)
list.push( el )
room.mx_ceiling.push(el)
}
@@ -203,7 +199,7 @@
return list
}
- function make_ceiling_walls( lo, hi, region ){
+ this.make_ceiling_walls = function ( lo, hi, region ){
var list = []
var width = region.x.length()
@@ -211,7 +207,7 @@
var height = hi.height - lo.height
if (! (region.half_sides & LEFT) && region.x.a == hi.rect.x.a) {
- el = make_wall('.left')
+ el = this.make_wall('.left')
el.rotationY = HALF_PI
el.height = height
el.width = depth
@@ -226,7 +222,7 @@
}
if (! (region.half_sides & RIGHT) && region.x.b == hi.rect.x.b) {
- el = make_wall('.right')
+ el = this.make_wall('.right')
el.rotationY = -HALF_PI
el.height = height
el.width = depth
@@ -241,7 +237,7 @@
}
if (! (region.half_sides & FRONT) && region.y.a == hi.rect.y.a) {
- el = make_wall('.front')
+ el = this.make_wall('.front')
el.width = width
el.height = height
el.rotationY = PI
@@ -256,7 +252,7 @@
}
if (! (region.half_sides & BACK) && region.y.b == hi.rect.y.b) {
- el = make_wall('.back')
+ el = this.make_wall('.back')
el.width = width
el.height = height
el.rotationY = 0
@@ -272,11 +268,11 @@
return list
}
- function make_floor(room, region){
+ this.make_floor = function (room, region) {
var width = region.x.length()
var depth = region.y.length()
- var el = make_wall('.floor')
+ var el = this.make_wall('.floor')
el.height = depth
el.width = width
el.x = region.x.a + width/2
@@ -287,12 +283,12 @@
el.side = FLOOR
return el
}
- function make_ceiling(room, region){
+ this.make_ceiling = function (room, region) {
var width = region.x.length()
var depth = region.y.length()
var height = room.height
- var el = make_wall('.ceiling')
+ var el = this.make_wall('.ceiling')
el.height = depth
el.width = width
el.x = region.x.a + width/2
@@ -304,7 +300,7 @@
return el
}
- function make_wall(klass){
+ this.make_wall = function (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