Skip to content

Commit

Permalink
Moved Camera debug code to debug module
Browse files Browse the repository at this point in the history
Added scale property to Camera
Renamed follow method to setTarget on Camera
  • Loading branch information
Eemeli Kelokorpi authored and Eemeli Kelokorpi committed Feb 16, 2015
1 parent bab330a commit 56434d5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 57 deletions.
95 changes: 39 additions & 56 deletions src/engine/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ game.createClass('Camera', {
/**
Sprite, that camera follows.
@property {game.Sprite} target
@default null
**/
target: null,
/**
Container, that the camera is moving.
@property {game.Container} container
@default null
**/
container: null,
/**
Current speed of camera.
@property {game.Point} speed
**/
speed: null,
/**
Scale value of camera.
@property {Number} scale
@default 1
**/
scale: 1,

sensorPosition: null,
sensorWidth: 0,
Expand All @@ -65,27 +69,9 @@ game.createClass('Camera', {
this.sensorPosition = new game.Point(this.offset.x, this.offset.y);
this.sensorWidth = 200 * game.scale;
this.sensorHeight = 200 * game.scale;
if (x && y) this.setPosition(x, y);
if (typeof x === 'number' && typeof y === 'number') this.setPosition(x, y);

game.scene.addObject(this);

if (game.debugDraw && game.Camera.debug) {
this.debugBox = new game.Graphics();
this.debugBox.beginFill(game.Camera.debugColor);
this.debugBox.alpha = game.Camera.debugAlpha;
this.debugBox.drawRect(-this.sensorWidth / 2, -this.sensorHeight / 2, this.sensorWidth, this.sensorHeight);
game.system.stage.addChild(this.debugBox);
}
},

/**
Set target for camera.
@method target
@param {game.Sprite} target
**/
follow: function(target) {
this.target = target;
this.sensorPosition.set(this.target.position.x, this.target.position.y);
},

/**
Expand All @@ -99,6 +85,16 @@ game.createClass('Camera', {
return this;
},

/**
Set target for camera.
@method setTarget
@param {game.Sprite} target
**/
setTarget: function(target) {
this.target = target;
this.sensorPosition.set(this.target.position.x * this.scale, this.target.position.y * this.scale);
},

setPosition: function(x, y) {
this.position.set(x - this.offset.x, y - this.offset.y);

Expand All @@ -125,34 +121,33 @@ game.createClass('Camera', {
}
},

setSensor: function(width, height) {
this.sensorWidth = width;
this.sensorHeight = height;
},

moveSensor: function() {
var targetWidth = Math.abs(this.target.width);
var targetHeight = Math.abs(this.target.height);
if (!this.target) return;

var targetWidth = Math.abs(this.target.width) * this.scale;
var targetHeight = Math.abs(this.target.height) * this.scale;
var targetPosX = (this.target.position.x + this.target.width / 2) * this.scale;
var targetPosY = (this.target.position.y + this.target.height / 2) * this.scale;

if (this.sensorWidth < targetWidth || this.sensorHeight < targetHeight) this.setSensor(targetWidth, targetHeight);

if (this.target.position.x < this.sensorPosition.x - this.sensorWidth / 2 + targetWidth / 2) {
this.sensorPosition.x = this.target.position.x + this.sensorWidth / 2 - targetWidth / 2;
if (targetPosX < this.sensorPosition.x - this.sensorWidth / 2 + targetWidth / 2) {
this.sensorPosition.x = targetPosX + this.sensorWidth / 2 - targetWidth / 2;
}
else if (this.target.position.x + (this.sensorWidth / 2 + targetWidth / 2) > this.sensorPosition.x + this.sensorWidth) {
this.sensorPosition.x = this.target.position.x + (this.sensorWidth / 2 + targetWidth / 2) - this.sensorWidth;
else if (targetPosX + (this.sensorWidth / 2 + targetWidth / 2) > this.sensorPosition.x + this.sensorWidth) {
this.sensorPosition.x = targetPosX + (this.sensorWidth / 2 + targetWidth / 2) - this.sensorWidth;
}

if (this.target.position.y < this.sensorPosition.y - this.sensorHeight / 2 + targetHeight / 2) {
this.sensorPosition.y = this.target.position.y + this.sensorHeight / 2 - targetHeight / 2;
if (targetPosY < this.sensorPosition.y - this.sensorHeight / 2 + targetHeight / 2) {
this.sensorPosition.y = targetPosY + this.sensorHeight / 2 - targetHeight / 2;
}
else if (this.target.position.y + (this.sensorHeight / 2 + targetHeight / 2) > this.sensorPosition.y + this.sensorHeight) {
this.sensorPosition.y = this.target.position.y + (this.sensorHeight / 2 + targetHeight / 2) - this.sensorHeight;
}
},

setSensor: function(width, height) {
this.sensorWidth = width;
this.sensorHeight = height;

if (this.debugBox) {
this.debugBox.clear();
this.debugBox.beginFill(game.Camera.debugColor);
this.debugBox.drawRect(-this.sensorWidth / 2, -this.sensorHeight / 2, this.sensorWidth, this.sensorHeight);
else if (targetPosY + (this.sensorHeight / 2 + targetHeight / 2) > this.sensorPosition.y + this.sensorHeight) {
this.sensorPosition.y = targetPosY + (this.sensorHeight / 2 + targetHeight / 2) - this.sensorHeight;
}
},

Expand All @@ -169,28 +164,16 @@ game.createClass('Camera', {
this.position.x + this.offset.x - this.speed.x * this.acceleration * game.system.delta,
this.position.y + this.offset.y - this.speed.y * this.acceleration * game.system.delta
);
if (this.debugBox) this.debugBox.alpha = game.Camera.debugAlpha * 2;
}
else {
this.speed.x = 0;
this.speed.y = 0;
if (this.debugBox) this.debugBox.alpha = game.Camera.debugAlpha;
this.speed.set(0, 0);
}
},

update: function() {
if (this.target) this.moveSensor();

this.moveSensor();
this.moveCamera();

if (this.debugBox) this.debugBox.position.set(this.sensorPosition.x - this.position.x, this.sensorPosition.y - this.position.y);
}
});

game.addAttributes('Camera', {
debug: false,
debugColor: 0xff00ff,
debugAlpha: 0.2
});

});
43 changes: 42 additions & 1 deletion src/engine/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ game.module(
)
.require(
'engine.pixi',
'engine.physics'
'engine.physics',
'engine.camera'
)
.body(function() {
'use strict';
Expand Down Expand Up @@ -300,6 +301,46 @@ game.World.inject({
}
});

game.Camera.inject({
init: function(x, y) {
this._super(x, y);

if (game.debugDraw && game.Camera.debug) {
this.debugBox = new game.Graphics();
this.debugBox.beginFill(game.Camera.debugColor);
this.debugBox.alpha = game.Camera.debugAlpha;
this.debugBox.drawRect(-this.sensorWidth / 2, -this.sensorHeight / 2, this.sensorWidth, this.sensorHeight);
game.system.stage.addChild(this.debugBox);
}
},

setSensor: function(width, height) {
this._super(width, height);

if (this.debugBox) {
this.debugBox.clear();
this.debugBox.beginFill(game.Camera.debugColor);
this.debugBox.drawRect(-this.sensorWidth / 2, -this.sensorHeight / 2, this.sensorWidth, this.sensorHeight);
}
},

moveCamera: function() {
this._super();
if (this.debugBox) this.debugBox.alpha = game.Camera.debugAlpha * ((this.speed.x === 0 && this.speed.y === 0) ? 1 : 2);
},

update: function() {
this._super();
if (this.debugBox) this.debugBox.position.set(this.sensorPosition.x - this.position.x, this.sensorPosition.y - this.position.y);
}
});

game.addAttributes('Camera', {
debug: false,
debugColor: 0xff00ff,
debugAlpha: 0.2
});

game.onStart = function() {
if (game.Debug && game.Debug.enabled) {
console.log('Panda.js ' + game.version);
Expand Down

0 comments on commit 56434d5

Please sign in to comment.