summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/map/_map.js
blob: 202803a74bf29ae0bb0a88dfc56c236010bfbc02 (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
/*
*/

var Map = function(opt){
	
	opt = defaults(opt, {
		type: "editor",
		el: document.querySelector("#map"),
		width: window.innerWidth,
		height: window.innerHeight,
		zoom: -2,
		zoom_min: -6.2,
		zoom_max: 0,
	})
	
	var base = this
	base.el = opt.el
	base.$el = $(base.el)
	
	if (! base.el) return
	
	base.dimensions = new vec2(opt.width, opt.height)
	base.center = new vec2(0,0)
	
	base.sides_for_center = function(){
		var sides = base.dimensions.clone().div(2).div(base.zoom)
		return new Rect( base.center.a - sides.a, -base.center.b - sides.b,
										 base.center.a + sides.a, -base.center.b + sides.b )
	}
	base.sides_for_camera = function(){
		var sides = base.dimensions.clone().div(2).div(base.zoom)
		return new Rect( scene.camera.x - sides.a, scene.camera.z - sides.b,
										 scene.camera.x + sides.a, scene.camera.z + sides.b )
	}


	base.set_zoom = function (n) {
		n = clamp(n, opt.zoom_min, opt.zoom_max)
		base.zoom_exponent = n
		base.zoom = pow(2, n)
	}
	base.set_zoom(opt.zoom)

	var canvas = base.canvas = document.createElement("canvas")
	canvas.width = base.dimensions.a
	canvas.height = base.dimensions.b
	
	base.el.appendChild(canvas)
	
	switch (opt.type) {
		case "editor":
			base.draw = new Map.Draw (base)
			base.ui = new Map.UI.Editor (base)
			base.sides = base.sides_for_center
			$(window).resize(base.resize)
			break
		
		case "minimap":
			base.draw = new Map.Draw (base, { center: scene.camera, minimap: true })
			base.ui = new Map.UI.Minimap (base)
			base.sides = base.sides_for_camera
			break
	}
	
	base.resize = function(){
		canvas.width = base.dimensions.a = window.innerWidth
		canvas.height = base.dimensions.b = window.innerHeight
	}
		
	base.toggle = function(state){
		return $(base.el).toggle(state).is(':visible')
	}

}

Map.prototype.update = function(){
	this.draw && this.draw.animate()
}