-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCamera.as
187 lines (154 loc) · 4.67 KB
/
Camera.as
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package sentinel.gameplay.world
{
import sentinel.framework.BaseGame;
import sentinel.framework.graphics.Viewport;
import sentinel.gameplay.IPositionProvider;
import sentinel.gameplay.physics.Vector2D;
/**
* The Camera used to view a World, typically centred on the main character. It allows you to
* move, rotate and scale the world around a given point.
* @author Marty Wallace.
*/
public class Camera implements IPositionProvider
{
private var _world:BaseWorld;
private var _offsetX:Number = 0;
private var _offsetY:Number = 0;
private var _position:Vector2D;
private var _snap:Boolean = false;
public function Camera(world:BaseWorld)
{
_world = world;
_set(0, 0, 0, 1);
}
/**
* Move the camera ontop of a Being within the World.
* @param being The Being to look at.
*
* @csharp Overloading will let us do things like lookAt(being), lookAt(vector).
*/
public function lookAt(being:Being):void
{
if (being !== null)
{
position = being.position;
}
else
{
// Ignore this call.
// Being forgiving to those who potentially nullify the game character but let this
// continue running.
}
}
/**
* Reset the position, zoom and rotation of the camera.
*/
public function reset():void
{
_set(0, 0, 0, 1);
}
/**
* Move the Camera to the specified coordinates within the World.
* @param x The new x coordinate.
* @param y The new y coordinate.
*/
public function moveTo(x:Number, y:Number):void
{
_set(x, y, rotation, zoom);
}
private function _set(x:Number, y:Number, rotation:Number, zoom:Number):void
{
if (_snap)
{
x = Math.round(x);
y = Math.round(y);
}
_world.graphics.x = viewport.width / 2 + _offsetX;
_world.graphics.y = viewport.height / 2 + _offsetY;
_world.graphics.rotation = -rotation;
_world.graphics.scaleX = _world.graphics.scaleY = zoom;
_world.__content.x = -x;
_world.__content.y = -y;
if (_world.engine !== null && _world.engine.debugging)
{
_world.engine.debug.graphics.x = _world.__content.x;
_world.engine.debug.graphics.y = _world.__content.y;
_world.engine.debug.wrapper.x = _world.graphics.x;
_world.engine.debug.wrapper.y = _world.graphics.y;
_world.engine.debug.wrapper.scaleX = _world.graphics.scaleX;
_world.engine.debug.wrapper.scaleY = _world.graphics.scaleY;
_world.engine.debug.wrapper.rotation = _world.graphics.rotation * 180 / Math.PI;
}
}
/**
* A reference to the game Viewport service.
*/
public function get viewport():Viewport { return BaseGame.getInstance().viewport; }
/**
* The position of the camera within the world along the x axis.
*/
public function get x():Number { return -_world.__content.x; }
public function set x(value:Number):void{ _set(value, y, rotation, zoom); }
/**
* The position of the camera within the world along the y axis.
*/
public function get y():Number { return -_world.__content.y; }
public function set y(value:Number):void{ _set(x, value, rotation, zoom); }
/**
* The position of the camera represented as a Vector2D instance.
*/
public function get position():Vector2D
{
_position.x = x;
_position.y = y;
return _position;
}
public function set position(value:Vector2D):void
{
moveTo(value.x, value.y);
}
/**
* The camera rotation.
*/
public function get rotation():Number { return -_world.graphics.rotation; }
public function set rotation(value:Number):void { _set(x, y, value, zoom); }
/**
* The camera zoom.
*/
public function get zoom():Number { return _world.graphics.scaleX; }
public function set zoom(value:Number):void{ _set(x, y, rotation, value); }
/**
* The camera offset along the x axis, added to the <code>x</code> value.
*/
public function get offsetX():Number { return _offsetX; }
public function set offsetX(value:Number):void
{
_offsetX = value;
_set(x, y, rotation, zoom);
}
/**
* The camera offset along the y axis, added to the <code>y</code> value.
*/
public function get offsetY():Number { return _offsetY; }
public function set offsetY(value:Number):void
{
_offsetY = value;
_set(x, y, rotation, zoom);
}
/**
* Whether or not the camera should snap to whole-pixels. Snapping to whole pixels may
* produce a slightly choppier appearance when moving the camera, but may supress rendering
* anomalies.
*/
public function get snap():Boolean { return _snap; }
public function set snap(value:Boolean):void
{
_snap = value;
if(_snap)
{
// Force the camera to apply snapping.
_set(x, y, rotation, zoom);
}
}
}
}