summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/models/wall.js
blob: 97083c7f507d6361a24cfb1ffe544cc87cbd0680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
		this.side = opt.side
		this.mx = []
 		this.els = []
 		if (opt.el) {
 			this.mx.push(opt.el)
 		}
	}
	
	Wall.prototype.toString = function(){
		return this.rect.toString()
	}

	Wall.prototype.reset = function(){
	}
	Wall.prototype.destroy = function(){
		this.mx.forEach(function(mx){
			mx.destroy && mx.destroy()
		})
		this.room = this.rect = this.mx = this.els = null
	}
	
	Wall.prototype.bind = function(){
		var base = this
		base.$walls = $( this.mx.map(function(mx){ return mx.el }) )
		base.$walls.bind({
			mouseover: function(){
			},
			mouseenter: function(e){
				scenery.mouse.mouseenter(e, base)
			},
			mousemove: function(e){
			},
			mousedown: function(){
				base.randomize_colors()
			}
		})
	}
	
	Wall.prototype.bounds_for = function(img) {
		var coord = this.side & FRONT_BACK ? this.rect.x : this.rect.y
		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.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
		})
		return walls;
	}

	Wall.prototype.randomize_colors = function(){
		var color = choice(window.colors)
		this.siblings().forEach(function(w){ w.color(color) })
	}

	return Wall

})()