summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles/engine')
-rw-r--r--public/assets/javascripts/rectangles/engine/rooms/builder.js20
-rw-r--r--public/assets/javascripts/rectangles/engine/rooms/grouper.js153
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/_scenery.js20
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/move.js2
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/types/_object.js4
5 files changed, 174 insertions, 25 deletions
diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js
index dfabc86..f321f71 100644
--- a/public/assets/javascripts/rectangles/engine/rooms/builder.js
+++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js
@@ -7,7 +7,7 @@
}
else {
MX = require('../../../../../../test/mocks/mx.js')
- scene = MX.scene
+ scene = MX.Scene
Rooms = require('./_rooms')
sort = require('../../util/sort')
FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
@@ -43,7 +43,7 @@
if (window.scene) {
base.clear()
base.build()
- base.bind_walls()
+ Rooms.grouper.build()
}
}
@@ -62,18 +62,7 @@
})
}.bind(this))
}
-
- base.bind_walls = function (){
- Rooms.forEach(function(room){
- room.walls = room.group_mx_walls()
- room.walls.forEach(function(wall){
- Rooms.walls[ wall.id ] = wall
- wall.bind()
- wall.randomize_colors()
- })
- })
- }
-
+
base.clear = function (){
els.forEach(function(el){
scene.remove(el)
@@ -84,7 +73,6 @@
this.build_walls = function (region) {
var room = Rooms.list[ region.id ]
-
var list = [], el = null
var width = region.x.length()
@@ -305,7 +293,7 @@
el.side = 0
el.rect = null
el.destroy = function(){
- this.el = this.rect = null
+ this.el = this.rect = this.face = null
}
// possible if walls are opaque
diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js
new file mode 100644
index 0000000..4ad3bd8
--- /dev/null
+++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js
@@ -0,0 +1,153 @@
+(function(){
+
+ var vec2, Rect, Rooms, UidGenerator, Wall, Surface, sort
+ if ('window' in this) {
+ vec2 = window.vec2
+ Rect = window.Rect
+ Surface = window.Surface
+ Rooms = window.Rooms
+ UidGenerator = window.UidGenerator
+ Wall = window.Wall
+ sort = window.sort
+ }
+ else {
+ Rooms = require('./_rooms')
+ UidGenerator = require('../../util/uid')
+ vec2 = require('../../models/vec2')
+ Rect = require('../../models/rect')
+ Wall = require('../../models/wall')
+ Surface = require('../../models/surface')
+ sort = require('../../util/sort')
+ 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 = ""
+ if (sides & FRONT) s += "front "
+ if (sides & BACK) s += "back "
+ if (sides & LEFT) s += "left "
+ if (sides & RIGHT) s += "right "
+ if (sides & TOP) s += "top "
+ if (sides & BOTTOM) s += "bottom "
+ return s
+ }
+ }
+
+ Rooms.grouper = new function(){
+
+ var base = this
+
+ base.list = {}
+
+ base.uid = new UidGenerator(base.list)
+
+ base.build = function (){
+ var walls = []
+ var collections = base.collect()
+ base.cull(collections)
+ base.group(walls, collections, FRONT)
+ base.group(walls, collections, BACK)
+ base.group(walls, collections, LEFT)
+ base.group(walls, collections, RIGHT)
+ Rooms.walls = walls
+ base.bind()
+ }
+ base.collect = function(){
+ var collections = {}
+ collections[FRONT] = []
+ collections[BACK] = []
+ collections[LEFT] = []
+ collections[RIGHT] = []
+
+ Rooms.forEach(function(room){
+ room.mx_walls.forEach(function(mx){
+ var side = mx.side || mx.half_side
+ collections[side].push(mx)
+ })
+ })
+
+ base.cull(collections)
+
+ return collections
+ }
+ base.cull = function(collections){
+ [FRONT, BACK, LEFT, RIGHT].forEach(function(side){
+ var collection = collections[side]
+ var useX = side & FRONT_BACK
+ var useA = side & (FRONT | RIGHT)
+ var last_mx
+ var widthVec, heightVec
+
+ collection.sort( useX ? sort.compare_zx : sort.compare_xz )
+ collection.forEach(function(mx){
+ if (last_mx && last_mx.rect.eq(mx.rect)) {
+ if (last_mx.rect.id == mx.rect.id) {
+ last_mx.height += mx.height/2
+ last_mx.y += mx.height/4
+ last_mx.face.y.b += mx.height/2
+ }
+ last_mx.side = side
+ mx.culled = true
+ mx.destroy()
+ scene.remove(mx)
+ return
+ }
+ widthVec = mx.rect[useX ? 'x' : 'y'].clone()
+ heightVec = new vec2( mx.y - mx.height/2, mx.y + mx.height/2 )
+ mx.face = new Rect( widthVec, heightVec )
+ last_mx = mx
+ })
+ })
+ return collections
+ }
+ base.group = function(walls, collections, side){
+ var collection = collections[side]
+ var wall
+ var useX = side & FRONT_BACK
+ var useA = side & (FRONT | RIGHT)
+
+ // collection.sort( useX ? sort.compare_zx : sort.compare_xz )
+
+ collection.forEach(function(mx){
+ if (mx.culled) return
+ var coplanar = wall && wall.edge == mx.rect[useX ? 'y': 'x'][useA ? 'a': 'b']
+
+ if (wall && coplanar) {
+ if (useX && wall.vec.b == mx.rect.x.a) {
+ wall.vec.b = mx.rect.x.b
+ wall.mx.push(mx)
+ wall.surface.add(mx.face)
+ return
+ }
+ else if (! useX && wall.vec.b == mx.rect.y.a) {
+ wall.vec.b = mx.rect.y.b
+ wall.mx.push(mx)
+ wall.surface.add(mx.face)
+ return
+ }
+ }
+ wall = new Wall ({
+ id: base.uid(),
+ side: side,
+ mx: [ mx ],
+ surface: new Surface( mx.face ),
+ vec: mx.rect[ useX ? 'x' : 'y' ].clone(),
+ edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ],
+ })
+ walls.push(wall)
+ })
+
+ return walls
+ }
+
+ base.bind = function(){
+ Rooms.walls.forEach(function(wall){
+ wall.bind()
+ wall.randomize_colors()
+ })
+ }
+
+ }
+
+})()
diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js
index c96885c..c43ef14 100644
--- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js
+++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js
@@ -12,25 +12,29 @@ var Scenery = new function(){
base.resize.init()
}
- base.add = function(wall, media, id){
+ base.add = function(opt){
var scene_media
switch (media.type) {
case 'image':
- scene_media = new Scenery.types.image ({ media: media, wall: wall, id: id })
+ scene_media = new Scenery.types.image (opt)
break
case 'video':
case 'youtube':
case 'vimeo':
- scene_media = new Scenery.types.video ({ media: media, wall: wall, id: id })
+ scene_media = new Scenery.types.video (opt)
break
}
base.list[scene_media.id] = scene_media
return scene_media
}
- base.addNextToWall = function(wall){
- var media = base.add(wall, base.nextMedia)
+ base.addNextToWall = function(wall, mx){
+ base.add({
+ wall: wall,
+ media: base.nextMedia,
+ mx: mx
+ })
base.nextMedia = null
return media
}
@@ -69,7 +73,11 @@ var Scenery = new function(){
base.deserialize = function(scenery_data){
scenery_data.forEach(function(data){
var wall = Rooms.walls[data.wall_id]
- var scene_media = base.add(wall, data.media, data.id)
+ var scene_media = base.add({
+ wall: wall,
+ media: data.media,
+ id: data.id
+ })
scene_media.deserialize(data)
})
}
diff --git a/public/assets/javascripts/rectangles/engine/scenery/move.js b/public/assets/javascripts/rectangles/engine/scenery/move.js
index edeb24b..f2d37d8 100644
--- a/public/assets/javascripts/rectangles/engine/scenery/move.js
+++ b/public/assets/javascripts/rectangles/engine/scenery/move.js
@@ -93,7 +93,7 @@ Scenery.move = function(base){
function switch_wall (e, new_wall, cursor){
if (! dragging) return
- if (new_wall.uid == base.wall.uid) return
+ if (new_wall.id == base.wall.id) return
if (! new_wall.fits(base.media, base.scale)) return
var old_wall_side = base.wall.side
diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js
index 66e0faf..46bc0e7 100644
--- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js
+++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js
@@ -12,7 +12,7 @@ Scenery.types.base = Fiber.extend(function(base){
this.scale = this.media.scale
if (opt.wall) {
- this.set_wall(opt.wall)
+ this.set_wall(opt.wall, opt.mx)
}
},
@@ -60,7 +60,7 @@ Scenery.types.base = Fiber.extend(function(base){
}
},
- set_wall: function(wall){
+ set_wall: function(wall, mx){
this.wall = wall || this.wall
this.bounds = this.wall.bounds_for(this.media, this.scale)
this.center = this.wall.center()