Skip to content

Commit

Permalink
Merge master camera to release-prep
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Dec 22, 2015
2 parents cfbd5cb + 380d128 commit 76db33e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
49 changes: 40 additions & 9 deletions src/engine/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ module ex {
protected _follow: Actor;
public focus: Point = new Point(0, 0);
public lerp: boolean = false;

// camera physical quantities
public x: number = 0;
public y: number = 0;
public z: number = 1;

public dx: number = 0;
public dy: number = 0;
public dz: number = 0;

public ax: number = 0;
public ay: number = 0;
public az: number = 0;

public rotation: number = 0;
public rx: number = 0;


private _cameraMoving: boolean = false;
private _currentLerpTime: number = 0;
private _lerpDuration: number = 1 * 1000; // 5 seconds
Expand Down Expand Up @@ -101,25 +119,26 @@ module ex {
}

/**
* Returns the focal point of the camera
* Returns the focal point of the camera, a new point giving the x and y position of the camera
*/
public getFocus() {
return this.focus;
return new ex.Point(this.x, this.y);
}

/**
* Sets the focal point of the camera. This value can only be set if there is no actor to be followed.
* @param x The x coordinate of the focal point
* @param y The y coordinate of the focal point
* @deprecated
*/
public setFocus(x: number, y: number) {
if (!this._follow && !this.lerp) {
this.focus.x = x;
this.focus.y = y;
this.x = x;
this.y = y;
}

if (this.lerp) {
this._lerpStart = this.focus.clone();
this._lerpStart = this.getFocus().clone();
this._lerpEnd = new Point(x, y);
this._currentLerpTime = 0;
this._cameraMoving = true;
Expand Down Expand Up @@ -172,18 +191,30 @@ module ex {
* Gets the current zoom scale
*/
public getZoom() {
return this._currentZoomScale;
return this.z;
}

private _setCurrentZoomScale(zoomScale: number) {
this._currentZoomScale = zoomScale;
this.z = zoomScale;
}

/**
* Applies the relevant transformations to the game canvas to "move" or apply effects to the Camera
* @param delta The number of milliseconds since the last update
*/
public update(ctx: CanvasRenderingContext2D, delta: number) {
// Update placements based on linear algebra
this.x += this.dx * delta / 1000;
this.y += this.dy * delta / 1000;
this.z += this.dz * delta / 1000;

this.dx += this.ax * delta / 1000;
this.dy += this.ay * delta / 1000;
this.dz += this.az * delta / 1000;

this.rotation += this.rx * delta / 1000;


var focus = this.getFocus();

var xShake = 0;
Expand Down Expand Up @@ -236,7 +267,7 @@ module ex {
yShake = (Math.random() * this._shakeMagnitudeY | 0) + 1;
}

if (this._isDoneZooming()) {
/*if (this._isDoneZooming()) {
this._isZooming = false;
this._elapsedZoomTime = 0;
this._zoomDuration = 0;
Expand All @@ -246,7 +277,7 @@ module ex {
this._elapsedZoomTime += delta;
this._setCurrentZoomScale(this.getZoom() + this._zoomIncrement * delta / 1000);
}
}*/

ctx.scale(this.getZoom(), this.getZoom());
ctx.translate(-focus.x + newCanvasWidth / 2 + xShake, -focus.y + newCanvasHeight / 2 + yShake);
Expand Down
19 changes: 14 additions & 5 deletions src/spec/CameraSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('A camera', () => {

var sideCamera;
var lockedCamera;
var baseCamera;
var actor;
var engine;
var scene;
Expand All @@ -25,6 +26,7 @@ describe('A camera', () => {

sideCamera = new ex.SideCamera();
lockedCamera = new ex.LockedCamera();
baseCamera = new ex.BaseCamera();
});

it('can follow an actor if it is a lockedCamera', () => {
Expand Down Expand Up @@ -75,13 +77,20 @@ describe('A camera', () => {
});

it('can focus on a point', () => {
engine.camera = lockedCamera;
lockedCamera.setFocus(10, 20);
// set the focus with positional attributes
baseCamera.x = 10;
baseCamera.y = 20;

expect(lockedCamera.getFocus().x).toBe(10);
expect(lockedCamera.getFocus().y).toBe(20);
expect(baseCamera.getFocus().x).toBe(10);
expect(baseCamera.getFocus().y).toBe(20);

});
// set the focus with the legacy api
baseCamera.setFocus(20, 10);

expect(baseCamera.getFocus().x).toBe(20);
expect(baseCamera.getFocus().y).toBe(10);

});

it('cannot focus on a point if it has an actor to follow', () => {
//TODO
Expand Down

0 comments on commit 76db33e

Please sign in to comment.