summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/models
diff options
context:
space:
mode:
Diffstat (limited to 'assets/javascripts/rectangles/models')
-rw-r--r--assets/javascripts/rectangles/models/rect.js38
-rw-r--r--assets/javascripts/rectangles/models/room.js56
-rw-r--r--assets/javascripts/rectangles/models/tree.js16
-rw-r--r--assets/javascripts/rectangles/models/wall.js31
4 files changed, 90 insertions, 51 deletions
diff --git a/assets/javascripts/rectangles/models/rect.js b/assets/javascripts/rectangles/models/rect.js
index 0664693..2d48e13 100644
--- a/assets/javascripts/rectangles/models/rect.js
+++ b/assets/javascripts/rectangles/models/rect.js
@@ -10,8 +10,8 @@ function sidesToString(sides){
return s
}
-window.rect = (function(){
- var rect = function (x0,y0,x1,y1){
+window.Rect = (function(){
+ var Rect = function (x0,y0,x1,y1){
if (x0 instanceof vec2) {
this.x = x0
this.y = y0
@@ -27,56 +27,56 @@ window.rect = (function(){
this.translation = new vec2(0,0)
this.sides = FRONT | BACK | LEFT | RIGHT
}
- rect.prototype.clone = function(){
- return new rect( this.x.clone(), this.y.clone() )
+ Rect.prototype.clone = function(){
+ return new Rect( this.x.clone(), this.y.clone() )
}
- rect.prototype.center = function(){
+ Rect.prototype.center = function(){
return new vec2(this.x.midpoint(), this.y.midpoint())
}
- rect.prototype.area = function(){
+ Rect.prototype.area = function(){
return this.x.length() * this.y.length()
}
- rect.prototype.mul = function(n){
+ Rect.prototype.mul = function(n){
this.x.mul(n)
this.y.mul(n)
return this
}
- rect.prototype.div = function(n){
+ Rect.prototype.div = function(n){
this.x.div(n)
this.y.div(n)
return this
}
- rect.prototype.translate = function(translation){
+ Rect.prototype.translate = function(translation){
var translation = translation || this.translation
this.x.abs().add(translation.a)
this.y.abs().add(translation.b)
this.translation.a = this.translation.b = 0
return this
}
- rect.prototype.contains = function(x,y){
+ Rect.prototype.contains = function(x,y){
return this.x.contains(x) && this.y.contains(y)
}
- rect.prototype.containsDisc = function(x,y,r){
+ Rect.prototype.containsDisc = function(x,y,r){
return this.x.containsDisc(x,r) && this.y.containsDisc(y,r)
}
- rect.prototype.intersects = function(r){
+ Rect.prototype.intersects = function(r){
return this.x.intersects(r.x) && this.y.intersects(r.y)
}
- rect.prototype.width = function(){ return this.x.length() }
- rect.prototype.height = function(){ return this.y.length() }
- rect.prototype.toString = function(){
+ Rect.prototype.width = function(){ return this.x.length() }
+ Rect.prototype.height = function(){ return this.y.length() }
+ Rect.prototype.toString = function(){
var sides = sidesToString(this.sides)
var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides
return s
}
- rect.prototype.quantize = function(n){
+ Rect.prototype.quantize = function(n){
this.x.quantize(n)
this.y.quantize(n)
return this
}
- rect.prototype.split = function(r){
+ Rect.prototype.split = function(r){
var rz = this
var splits = []
var split_contains = 0
@@ -135,7 +135,7 @@ window.rect = (function(){
x_intervals.forEach(function(x){
y_intervals.forEach(function(y){
- var rn = new rect(x[0], y[0])
+ var rn = new Rect(x[0], y[0])
rn.id = rz.id
rn.sides = ((x[1] | y[1]) & sides)
if (r.intersects(rn)) {
@@ -148,6 +148,6 @@ window.rect = (function(){
return splits
}
- return rect
+ return Rect
})()
diff --git a/assets/javascripts/rectangles/models/room.js b/assets/javascripts/rectangles/models/room.js
index b66d868..c26efa9 100644
--- a/assets/javascripts/rectangles/models/room.js
+++ b/assets/javascripts/rectangles/models/room.js
@@ -1,23 +1,22 @@
-window.room = (function(){
+window.Room = (function(){
- var room = function(opt){
+ var Room = function(opt){
this.id = opt.id || clipper.rooms.length
this.rect = opt.rect
this.regions = []
+ this.walls = []
+ this.floor = []
+ this.ceiling = []
this.height = opt.height || 200
this.focused = false
-
- this.$walls = $([])
- this.$floor = $([])
- this.$ceiling = $([])
}
- room.prototype.toString = function(){
+ Room.prototype.toString = function(){
return this.rect.toString()
}
- room.prototype.reset = function(){
+ Room.prototype.reset = function(){
var copy = this.rect.clone()
copy.id = this.id
copy.sides = FRONT | BACK | LEFT | RIGHT
@@ -26,24 +25,33 @@ window.room = (function(){
this.intersects = []
this.constructed = false
- this.$walls = $([])
- this.$floor = $([])
- this.$ceiling = $([])
+ this.walls = []
+ this.floor = []
+ this.ceiling = []
}
- room.prototype.bind = function(){
- this.$walls.bind({
- mouseover: function(){
- },
- mousemove: function(e){
- },
- mousedown: function(){
- $(this).css("background-color", choice(window.palettes.colors))
- }
+ Room.prototype.bind = function(){
+ var base = this
+ base.walls.forEach(function(wall){
+ $(wall.el).bind({
+ mouseover: function(){
+ },
+ mousemove: function(e){
+ var color = choice(window.palettes.colors)
+ base.walls.forEach(function(wall){
+ $(wall.el).css("background-color", color)
+ })
+ },
+ mousedown: function(){
+ }
+ })
})
}
+
+ Room.prototype.add_wall = function(){
+ }
- room.prototype.clipTo = function(r){
+ Room.prototype.clipTo = function(r){
// for each of this rect's regions split the region if necessary
var regions = this.regions
var splits
@@ -57,7 +65,7 @@ window.room = (function(){
this.regions = regions
}
- room.prototype.collides = function(x,y){
+ Room.prototype.collides = function(x,y){
var collision = 0, wall_collision, contains_x, contains_y
this.regions.forEach(function(r){
if (! r.sides) return
@@ -94,7 +102,7 @@ window.room = (function(){
return collision
}
- room.prototype.collidesDisc = function(x,y,radius){
+ Room.prototype.collidesDisc = function(x,y,radius){
var collision = 0, wall_collision, contains_x, contains_y
this.regions.forEach(function(r){
if (! r.sides) return
@@ -131,7 +139,7 @@ window.room = (function(){
return collision
}
- return room
+ return Room
})()
diff --git a/assets/javascripts/rectangles/models/tree.js b/assets/javascripts/rectangles/models/tree.js
index 577c41a..8193988 100644
--- a/assets/javascripts/rectangles/models/tree.js
+++ b/assets/javascripts/rectangles/models/tree.js
@@ -1,35 +1,35 @@
-var tree = function(n, data){
+var Tree = function(n, data){
this.lo = null
this.hi = null
this.value = n
this.data = data
}
-tree.prototype.find = function(n){
+Tree.prototype.find = function(n){
if (n == this.value) return this
if (n < this.value) return this.lo ? this.lo.find(n) : this
if (n > this.value) return this.hi ? this.hi.find(n) : this
}
-tree.prototype.add = function(n, data){
+Tree.prototype.add = function(n, data){
var closest = this.find(n)
if (n == closest.value) return closest
- if (n < closest.value) return closest.lo = new tree(n, data)
- if (n > closest.value) return closest.hi = new tree(n, data)
+ if (n < closest.value) return closest.lo = new Tree(n, data)
+ if (n > closest.value) return closest.hi = new Tree(n, data)
}
-tree.prototype.toArray = function(){
+Tree.prototype.toArray = function(){
var a = []
if (this.lo) a = a.concat(this.lo.toArray())
a.push(this.data)
if (this.hi) a = a.concat(this.hi.toArray())
return a
}
-tree.prototype.toString = function(){
+Tree.prototype.toString = function(){
var s = "";
if (this.lo) s += this.lo.toString()
s += this.value + ","
if (this.hi) s += this.hi.toString()
return s
}
-tree.prototype.depth = function(){
+Tree.prototype.depth = function(){
if (this.lo && this.hi) return 1 + max(this.lo.depth(), this.hi.depth())
else if (this.lo) return 1 + this.lo.depth()
else if (this.hi) return 1 + this.hi.depth()
diff --git a/assets/javascripts/rectangles/models/wall.js b/assets/javascripts/rectangles/models/wall.js
new file mode 100644
index 0000000..e25d2dd
--- /dev/null
+++ b/assets/javascripts/rectangles/models/wall.js
@@ -0,0 +1,31 @@
+window.wall = (function(){
+
+ var wall = function(opt){
+ this.id = opt.id
+ this.room = opt.room
+ this.rect = opt.rect
+ this.mx = []
+ }
+
+ wall.prototype.toString = function(){
+ return this.rect.toString()
+ }
+
+ wall.prototype.reset = function(){
+ }
+
+ wall.prototype.bind = function(){
+ this.$walls.bind({
+ mouseover: function(){
+ },
+ mousemove: function(e){
+ },
+ mousedown: function(){
+ $(this).css("background-color", choice(window.palettes.colors))
+ }
+ })
+ }
+
+ return wall
+
+})()