summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/rooms/mover.js
blob: 98f80c5d82da6a6406894e6431f8a11b676a503e (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
Rooms.mover = new function(){

	var base = this
	base.room = null
	base.noclip = false

	base.init = function(){
		base.bind()
		base.update(scene.camera)
	}
	
	base.bind = function(){
		app.on("move", base.update)
		keys.on("backslash", function(){
			base.noclip = ! base.noclip
			base.room = null
			app.movements.gravity( ! base.noclip )
		})
	}
	
	base.update = function(pos){
		var radius = scene.camera.radius
		
		cam.y = pos.y

		// if we were in a room already..
		if (base.room) {
			// check if we're still in the room
			if (base.room.rect.containsDisc(pos.x, pos.z, radius)) {
				cam.x = pos.x
				cam.z = pos.z
				return
			}

			// check if we've breached one of the walls.. clamp position if so
			var collision = base.room.collidesDisc(pos.x, pos.z, radius)

			if (collision && ! base.noclip) {
				cam.x = (collision & LEFT_RIGHT) ? base.room.rect.x.clampDisc(pos.x, radius) : pos.x
				cam.z = (collision & FRONT_BACK) ? base.room.rect.y.clampDisc(pos.z, radius) : pos.z
				return
			}

			// in this case, we appear to have left the room..
			// $(".face.active").removeClass("active")
			$("body").css("background-color", "transparent")
			base.room = null
		}

		// collision test failed, so update position
		cam.x = pos.x
		cam.z = pos.z
		
		// determine what room we are in now
		var intersects = Rooms.filter(function(r){
			return r.rect.contains(pos.x, pos.z)
		})
		
		// did we actually enter a room?
		if (intersects.length) {
			base.room = intersects[0]
			$("body").css("background-color", rgb_string( Walls.colors.wall ))
      app.tube("change-room", { room: base.room })
		}

	}

}