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/vec2.js14
-rw-r--r--assets/javascripts/rectangles/models/wall.js75
2 files changed, 81 insertions, 8 deletions
diff --git a/assets/javascripts/rectangles/models/vec2.js b/assets/javascripts/rectangles/models/vec2.js
index 7307fca..9b0447c 100644
--- a/assets/javascripts/rectangles/models/vec2.js
+++ b/assets/javascripts/rectangles/models/vec2.js
@@ -13,12 +13,16 @@ vec2.prototype.clone = function(){
}
vec2.prototype.abs = function(){
if (this.b < this.a) {
- this.a = this.a ^ this.b
- this.b = this.a ^ this.b
- this.a = this.a ^ this.b
+ this.invert()
}
return this
}
+vec2.prototype.invert = function(){
+ this.a = this.a ^ this.b
+ this.b = this.a ^ this.b
+ this.a = this.a ^ this.b
+ return this
+}
vec2.prototype.midpoint = function(){
return lerp(0.5, this.a, this.b)
}
@@ -45,7 +49,7 @@ vec2.prototype.div = function(n){
this.b /= n
return this
}
-vec2.normalize = function(){
+vec2.prototype.normalize = function(){
var dim = max(this.a, this.b)
this.a = this.a/dim
this.b = this.b/dim
@@ -85,7 +89,7 @@ vec2.prototype.intersection = function(v){
}
}
vec2.prototype.toString = function(){
- return "[" + this.a + " " + this.b + "]"
+ return "[" + ~~this.a + " " + ~~this.b + "]"
}
vec2.prototype.quantize = function(n){
n = n || 10
diff --git a/assets/javascripts/rectangles/models/wall.js b/assets/javascripts/rectangles/models/wall.js
index baaac43..97083c7 100644
--- a/assets/javascripts/rectangles/models/wall.js
+++ b/assets/javascripts/rectangles/models/wall.js
@@ -2,6 +2,7 @@ window.Wall = (function(){
var Wall = function(opt){
this.id = opt.id
+ this.uid = Uid()
this.room = opt.room
this.rect = opt.rect || new Rect (0,0,0,0)
this.rect.sides = opt.side
@@ -32,6 +33,9 @@ window.Wall = (function(){
base.$walls.bind({
mouseover: function(){
},
+ mouseenter: function(e){
+ scenery.mouse.mouseenter(e, base)
+ },
mousemove: function(e){
},
mousedown: function(){
@@ -45,17 +49,82 @@ window.Wall = (function(){
return new Rect( new vec2( coord.a + img.width/2, coord.b - img.width/2 ),
new vec2( img.height/2, clipper.rooms[this.room].height - img.height/2 ) )
}
+ Wall.prototype.fits = function(img){
+ if (this.side & FRONT_BACK && this.rect.x.length() < img.width) {
+ return false
+ }
+ if (this.side & LEFT_RIGHT && this.rect.y.length() < img.width) {
+ return false
+ }
+ return true
+ }
+
+ Wall.prototype.center_for = function(img, gravity){
+ var left
+ gravity = gravity || 0
+
+ var major_axis, minor_axis
+ if (this.side & FRONT_BACK) {
+ major_axis = this.rect.x
+ minor_axis = this.rect.y
+ }
+ else {
+ minor_axis = this.rect.x
+ major_axis = this.rect.y
+ }
+
+ // left side addition
+ if (gravity > 0) {
+ console.log("gravity left")
+ left = major_axis.b - img.width/2
+ }
+ // right side addition
+ else if (gravity < 0) {
+ console.log("gravity right")
+ left = major_axis.a + img.width/2
+ }
+ else {
+ left = major_axis.midpoint()
+ }
+
+ switch (this.side) {
+ case FRONT:
+ x = left
+ z = minor_axis.a + 10
+ break
+ case BACK:
+ x = left
+ z = minor_axis.b - 10
+ break
+ case LEFT:
+ x = minor_axis.a + 10
+ z = left
+ break
+ case RIGHT:
+ x = minor_axis.b - 10
+ z = left
+ break
+ }
+
+ return new vec2 (x, z)
+ }
Wall.prototype.color = function(color){
this.$walls && this.$walls.css("background-color", color)
}
- Wall.prototype.randomize_colors = function(){
+ Wall.prototype.siblings = function(){
var base = this
var match = base.side | base.half_side
- var walls = clipper.rooms[this.room].walls.filter(function(w){ return (w.side | w.half_side) & match })
+ var walls = clipper.rooms[this.room].walls.filter(function(w){
+ return (w.side | w.half_side) & match
+ })
+ return walls;
+ }
+
+ Wall.prototype.randomize_colors = function(){
var color = choice(window.colors)
- walls.forEach(function(w){ w.color(color) })
+ this.siblings().forEach(function(w){ w.color(color) })
}
return Wall