summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/rooms/grouper.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles/engine/rooms/grouper.js')
-rw-r--r--public/assets/javascripts/rectangles/engine/rooms/grouper.js73
1 files changed, 44 insertions, 29 deletions
diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js
index 4ad3bd8..cde9fbb 100644
--- a/public/assets/javascripts/rectangles/engine/rooms/grouper.js
+++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js
@@ -1,6 +1,6 @@
(function(){
- var vec2, Rect, Rooms, UidGenerator, Wall, Surface, sort
+ var vec2, Rect, Rooms, UidGenerator, Wall, Surface, sort, _
if ('window' in this) {
vec2 = window.vec2
Rect = window.Rect
@@ -9,6 +9,7 @@
UidGenerator = window.UidGenerator
Wall = window.Wall
sort = window.sort
+ _ = window._
}
else {
Rooms = require('./_rooms')
@@ -18,6 +19,7 @@
Wall = require('../../models/wall')
Surface = require('../../models/surface')
sort = require('../../util/sort')
+ _ = require('lodash')
FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
PI = Math.PI
HALF_PI = PI/2
@@ -82,6 +84,7 @@
collection.sort( useX ? sort.compare_zx : sort.compare_xz )
collection.forEach(function(mx){
if (last_mx && last_mx.rect.eq(mx.rect)) {
+ // culls half-walls
if (last_mx.rect.id == mx.rect.id) {
last_mx.height += mx.height/2
last_mx.y += mx.height/4
@@ -103,41 +106,53 @@
}
base.group = function(walls, collections, side){
var collection = collections[side]
- var wall
var useX = side & FRONT_BACK
- var useA = side & (FRONT | RIGHT)
+ var useA = side & (FRONT | LEFT)
// collection.sort( useX ? sort.compare_zx : sort.compare_xz )
+ var planes = {}
+
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)
+ var edge = mx.rect[useX ? 'y': 'x'][ useA ? 'a': 'b']
+ planes[edge] = planes[edge] || []
+ planes[edge].push(mx)
})
-
+
+ var edges = _.keys(planes)
+ edges.forEach(function(edge){
+
+ var wall
+
+ planes[edge].forEach(function(mx){
+
+ if (wall) {
+ 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
}