Renamed folder camera to cameras and split up the contents into proper namespaces. Merged inc files back into classes.

This commit is contained in:
Richard Davey 2018-01-16 15:06:47 +00:00
parent c77c412183
commit be756ed7fc
61 changed files with 1221 additions and 1173 deletions

View file

@ -1,100 +0,0 @@
var Class = require('../../utils/Class');
var Rectangle = require('../../geom/rectangle/Rectangle');
var TransformMatrix = require('../../gameobjects/components/TransformMatrix');
var ValueToColor = require('../../display/color/ValueToColor');
var Camera = new Class({
initialize:
function Camera (x, y, width, height)
{
this.scene;
this.name = '';
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.roundPixels = false;
// Bounds
this.useBounds = false;
this._bounds = new Rectangle();
this.inputEnabled = true;
this.scrollX = 0.0;
this.scrollY = 0.0;
this.zoom = 1.0;
this.rotation = 0.0;
this.matrix = new TransformMatrix(1, 0, 0, 1, 0, 0);
this.transparent = true;
this.clearBeforeRender = true;
this.backgroundColor = ValueToColor('rgba(0,0,0,0)');
this.disableCull = false;
this.culledObjects = [];
// Shake
this._shakeDuration = 0;
this._shakeIntensity = 0;
this._shakeOffsetX = 0;
this._shakeOffsetY = 0;
// Fade
this._fadeDuration = 0;
this._fadeRed = 0;
this._fadeGreen = 0;
this._fadeBlue = 0;
this._fadeAlpha = 0;
// Flash
this._flashDuration = 0;
this._flashRed = 1;
this._flashGreen = 1;
this._flashBlue = 1;
this._flashAlpha = 0;
// Follow
this._follow = null;
this._id = 0;
},
centerToBounds: require('./inc/CenterToBounds'),
centerToSize: require('./inc/CenterToSize'),
cull: require('./inc/Cull'),
cullHitTest: require('./inc/CullHitTest'),
cullTilemap: require('./inc/CullTilemap'),
destroy: require('./inc/Destroy'),
fade: require('./inc/Fade'),
flash: require('./inc/Flash'),
getWorldPoint: require('./inc/GetWorldPoint'),
ignore: require('./inc/Ignore'),
preRender: require('./inc/PreRender'),
removeBounds: require('./inc/RemoveBounds'),
setAngle: require('./inc/SetAngle'),
setBackgroundColor: require('./inc/SetBackgroundColor'),
setBounds: require('./inc/SetBounds'),
setName: require('./inc/SetName'),
setPosition: require('./inc/SetPosition'),
setRotation: require('./inc/SetRotation'),
setRoundPixels: require('./inc/SetRoundPixels'),
setScene: require('./inc/SetScene'),
setScroll: require('./inc/SetScroll'),
setSize: require('./inc/SetSize'),
setViewport: require('./inc/SetViewport'),
setZoom: require('./inc/SetZoom'),
shake: require('./inc/Shake'),
startFollow: require('./inc/StartFollow'),
stopFollow: require('./inc/StopFollow'),
toJSON: require('./inc/ToJSON'),
update: require('./inc/Update')
});
module.exports = Camera;

View file

@ -1,9 +0,0 @@
var CenterToBounds = function ()
{
this.scrollX = (this._bounds.width * 0.5) - (this.width * 0.5);
this.scrollY = (this._bounds.height * 0.5) - (this.height * 0.5);
return this;
};
module.exports = CenterToBounds;

View file

@ -1,9 +0,0 @@
var CenterToSize = function ()
{
this.scrollX = this.width * 0.5;
this.scrollY = this.height * 0.5;
return this;
};
module.exports = CenterToSize;

View file

@ -1,68 +0,0 @@
var Cull = function (renderableObjects)
{
if (this.disableCull)
{
return renderableObjects;
}
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
/* First Invert Matrix */
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
return renderableObjects;
}
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var cameraW = this.width;
var cameraH = this.height;
var culledObjects = this.culledObjects;
var length = renderableObjects.length;
determinant = 1 / determinant;
culledObjects.length = 0;
for (var index = 0; index < length; ++index)
{
var object = renderableObjects[index];
if (!object.hasOwnProperty('width'))
{
culledObjects.push(object);
continue;
}
var objectW = object.width;
var objectH = object.height;
var objectX = (object.x - (scrollX * object.scrollFactorX)) - (objectW * object.originX);
var objectY = (object.y - (scrollY * object.scrollFactorY)) - (objectH * object.originY);
var tx = (objectX * mva + objectY * mvc + mve);
var ty = (objectX * mvb + objectY * mvd + mvf);
var tw = ((objectX + objectW) * mva + (objectY + objectH) * mvc + mve);
var th = ((objectX + objectW) * mvb + (objectY + objectH) * mvd + mvf);
var cullW = cameraW + objectW;
var cullH = cameraH + objectH;
if (tx > -objectW || ty > -objectH || tx < cullW || ty < cullH ||
tw > -objectW || th > -objectH || tw < cullW || th < cullH)
{
culledObjects.push(object);
}
}
return culledObjects;
};
module.exports = Cull;

View file

@ -1,67 +0,0 @@
var CullHitTest = function (interactiveObjects)
{
if (this.disableCull)
{
return interactiveObjects;
}
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
/* First Invert Matrix */
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
return interactiveObjects;
}
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var cameraW = this.width;
var cameraH = this.height;
var length = interactiveObjects.length;
determinant = 1 / determinant;
var culledObjects = [];
for (var index = 0; index < length; ++index)
{
var object = interactiveObjects[index].gameObject;
if (!object.hasOwnProperty('width'))
{
culledObjects.push(interactiveObjects[index]);
continue;
}
var objectW = object.width;
var objectH = object.height;
var objectX = (object.x - (scrollX * object.scrollFactorX)) - (objectW * object.originX);
var objectY = (object.y - (scrollY * object.scrollFactorY)) - (objectH * object.originY);
var tx = (objectX * mva + objectY * mvc + mve);
var ty = (objectX * mvb + objectY * mvd + mvf);
var tw = ((objectX + objectW) * mva + (objectY + objectH) * mvc + mve);
var th = ((objectX + objectW) * mvb + (objectY + objectH) * mvd + mvf);
var cullW = cameraW + objectW;
var cullH = cameraH + objectH;
if (tx > -objectW || ty > -objectH || tx < cullW || ty < cullH ||
tw > -objectW || th > -objectH || tw < cullW || th < cullH)
{
culledObjects.push(interactiveObjects[index]);
}
}
return culledObjects;
};
module.exports = CullHitTest;

View file

@ -1,57 +0,0 @@
var CullTilemap = function (tilemap)
{
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
/* First Invert Matrix */
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
return tiles;
}
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
var tiles = tilemap.tiles;
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var cameraW = this.width;
var cameraH = this.height;
var culledObjects = this.culledObjects;
var length = tiles.length;
var tileW = tilemap.tileWidth;
var tileH = tilemap.tileHeight;
var cullW = cameraW + tileW;
var cullH = cameraH + tileH;
var scrollFactorX = tilemap.scrollFactorX;
var scrollFactorY = tilemap.scrollFactorY;
determinant = 1 / determinant;
culledObjects.length = 0;
for (var index = 0; index < length; ++index)
{
var tile = tiles[index];
var tileX = (tile.x - (scrollX * scrollFactorX));
var tileY = (tile.y - (scrollY * scrollFactorY));
var tx = (tileX * mva + tileY * mvc + mve);
var ty = (tileX * mvb + tileY * mvd + mvf);
var tw = ((tileX + tileW) * mva + (tileY + tileH) * mvc + mve);
var th = ((tileX + tileW) * mvb + (tileY + tileH) * mvd + mvf);
if (tx > -tileW && ty > -tileH && tw < cullW && th < cullH)
{
culledObjects.push(tile);
}
}
return culledObjects;
};
module.exports = CullTilemap;

View file

@ -1,9 +0,0 @@
var Destroy = function ()
{
this._bounds = undefined;
this.matrix = undefined;
this.culledObjects = [];
this.scene = undefined;
};
module.exports = Destroy;

View file

@ -1,25 +0,0 @@
var Fade = function (duration, red, green, blue, force)
{
if (red === undefined) { red = 0.0; }
if (green === undefined) { green = 0.0; }
if (blue === undefined) { blue = 0.0; }
if (!force && this._fadeAlpha > 0.0)
{
return;
}
this._fadeRed = red;
this._fadeGreen = green;
this._fadeBlue = blue;
if (duration <= 0)
{
duration = Number.MIN_VALUE;
}
this._fadeDuration = duration;
this._fadeAlpha = Number.MIN_VALUE;
};
module.exports = Fade;

View file

@ -1,25 +0,0 @@
var Flash = function (duration, red, green, blue, force)
{
if (!force && this._flashAlpha > 0.0)
{
return;
}
if (red === undefined) { red = 1.0; }
if (green === undefined) { green = 1.0; }
if (blue === undefined) { blue = 1.0; }
this._flashRed = red;
this._flashGreen = green;
this._flashBlue = blue;
if (duration <= 0)
{
duration = Number.MIN_VALUE;
}
this._flashDuration = duration;
this._flashAlpha = 1.0;
};
module.exports = Flash;

View file

@ -1,54 +0,0 @@
var Vector2 = require('../../../math/Vector2');
var GetWorldPoint = function (x, y, output)
{
if (output === undefined) { output = new Vector2(); }
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
/* First Invert Matrix */
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
output.x = x;
output.y = y;
return output;
}
determinant = 1 / determinant;
var ima = mvd * determinant;
var imb = -mvb * determinant;
var imc = -mvc * determinant;
var imd = mva * determinant;
var ime = (mvc * mvf - mvd * mve) * determinant;
var imf = (mvb * mve - mva * mvf) * determinant;
var c = Math.cos(this.rotation);
var s = Math.sin(this.rotation);
var zoom = this.zoom;
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var sx = x + ((scrollX * c - scrollY * s) * zoom);
var sy = y + ((scrollX * s + scrollY * c) * zoom);
/* Apply transform to point */
output.x = (sx * ima + sy * imc + ime);
output.y = (sx * imb + sy * imd + imf);
return output;
};
module.exports = GetWorldPoint;

View file

@ -1,16 +0,0 @@
var Ignore = function (gameObjectOrArray)
{
if (gameObjectOrArray instanceof Array)
{
for (var index = 0; index < gameObjectOrArray.length; ++index)
{
gameObjectOrArray[index].cameraFilter |= this._id;
}
}
else
{
gameObjectOrArray.cameraFilter |= this._id;
}
};
module.exports = Ignore;

View file

@ -1,60 +0,0 @@
var PreRender = function ()
{
var width = this.width;
var height = this.height;
var zoom = this.zoom;
var matrix = this.matrix;
var originX = width / 2;
var originY = height / 2;
var follow = this._follow;
if (follow !== null)
{
originX = follow.x;
originY = follow.y;
this.scrollX = originX - width * 0.5;
this.scrollY = originY - height * 0.5;
}
if (this.useBounds)
{
var bounds = this._bounds;
var bw = Math.max(0, bounds.right - width);
var bh = Math.max(0, bounds.bottom - height);
if (this.scrollX < bounds.x)
{
this.scrollX = bounds.x;
}
else if (this.scrollX > bw)
{
this.scrollX = bw;
}
if (this.scrollY < bounds.y)
{
this.scrollY = bounds.y;
}
else if (this.scrollY > bh)
{
this.scrollY = bh;
}
}
if (this.roundPixels)
{
this.scrollX = Math.round(this.scrollX);
this.scrollY = Math.round(this.scrollY);
}
matrix.loadIdentity();
matrix.translate(this.x + originX, this.y + originY);
matrix.rotate(this.rotation);
matrix.scale(zoom, zoom);
matrix.translate(-originX, -originY);
matrix.translate(this._shakeOffsetX, this._shakeOffsetY);
};
module.exports = PreRender;

View file

@ -1,10 +0,0 @@
var RemoveBounds = function ()
{
this.useBounds = false;
this._bounds.setEmpty();
return this;
};
module.exports = RemoveBounds;

View file

@ -1,12 +0,0 @@
var DegToRad = require('../../../math/DegToRad');
var SetAngle = function (value)
{
if (value === undefined) { value = 0; }
this.rotation = DegToRad(value);
return this;
};
module.exports = SetAngle;

View file

@ -1,14 +0,0 @@
var ValueToColor = require('../../../display/color/ValueToColor');
var SetBackgroundColor = function (color)
{
if (color === undefined) { color = 'rgba(0,0,0,0)'; }
this.backgroundColor = ValueToColor(color);
this.transparent = (this.backgroundColor.alpha === 0);
return this;
};
module.exports = SetBackgroundColor;

View file

@ -1,10 +0,0 @@
var SetBounds = function (x, y, width, height)
{
this._bounds.setTo(x, y, width, height);
this.useBounds = true;
return this;
};
module.exports = SetBounds;

View file

@ -1,10 +0,0 @@
var SetName = function (value)
{
if (value === undefined) { value = ''; }
this.name = value;
return this;
};
module.exports = SetName;

View file

@ -1,11 +0,0 @@
var SetPosition = function (x, y)
{
if (y === undefined) { y = x; }
this.x = x;
this.y = y;
return this;
};
module.exports = SetPosition;

View file

@ -1,10 +0,0 @@
var SetRotation = function (value)
{
if (value === undefined) { value = 0; }
this.rotation = value;
return this;
};
module.exports = SetRotation;

View file

@ -1,8 +0,0 @@
var SetRoundPixels = function (value)
{
this.roundPixels = value;
return this;
};
module.exports = SetRoundPixels;

View file

@ -1,8 +0,0 @@
var SetScene = function (scene)
{
this.scene = scene;
return this;
};
module.exports = SetScene;

View file

@ -1,11 +0,0 @@
var SetScroll = function (x, y)
{
if (y === undefined) { y = x; }
this.scrollX = x;
this.scrollY = y;
return this;
};
module.exports = SetScroll;

View file

@ -1,11 +0,0 @@
var SetSize = function (width, height)
{
if (height === undefined) { height = width; }
this.width = width;
this.height = height;
return this;
};
module.exports = SetSize;

View file

@ -1,11 +0,0 @@
var SetViewport = function (x, y, width, height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
return this;
};
module.exports = SetViewport;

View file

@ -1,10 +0,0 @@
var SetZoom = function (value)
{
if (value === undefined) { value = 1; }
this.zoom = value;
return this;
};
module.exports = SetZoom;

View file

@ -1,16 +0,0 @@
var Shake = function (duration, intensity, force)
{
if (intensity === undefined) { intensity = 0.05; }
if (!force && (this._shakeOffsetX !== 0.0 || this._shakeOffsetY !== 0.0))
{
return;
}
this._shakeDuration = duration;
this._shakeIntensity = intensity;
this._shakeOffsetX = 0;
this._shakeOffsetY = 0;
};
module.exports = Shake;

View file

@ -1,18 +0,0 @@
var StartFollow = function (gameObjectOrPoint, roundPx)
{
if (this._follow !== null)
{
this.stopFollow();
}
this._follow = gameObjectOrPoint;
if (roundPx !== undefined)
{
this.roundPixels = roundPx;
}
return this;
};
module.exports = StartFollow;

View file

@ -1,7 +0,0 @@
var StopFollow = function ()
{
/* do unfollow work here */
this._follow = null;
};
module.exports = StopFollow;

View file

@ -1,50 +0,0 @@
/*
camera: {
x: int
y: int
width: int
height: int
zoom: float
rotation: float
roundPixels: bool
scrollX: float
scrollY: float
backgroundColor: string
bounds: {
x: int
y: int
width: int
height: int
}
}
*/
var ToJSON = function ()
{
var output = {
name: this.name,
x: this.x,
y: this.y,
width: this.width,
height: this.height,
zoom: this.zoom,
rotation: this.rotation,
roundPixels: this.roundPixels,
scrollX: this.scrollX,
scrollY: this.scrollY,
backgroundColor: this.backgroundColor.rgba
};
if (this.useBounds)
{
output['bounds'] = {
x: this._bounds.x,
y: this._bounds.y,
width: this._bounds.width,
height: this._bounds.height
};
}
return output;
};
module.exports = ToJSON;

View file

@ -1,42 +0,0 @@
var Update = function (timestep, delta)
{
if (this._flashAlpha > 0.0)
{
this._flashAlpha -= delta / this._flashDuration;
if (this._flashAlpha < 0.0)
{
this._flashAlpha = 0.0;
}
}
if (this._fadeAlpha > 0.0 && this._fadeAlpha < 1.0)
{
this._fadeAlpha += delta / this._fadeDuration;
if (this._fadeAlpha >= 1.0)
{
this._fadeAlpha = 1.0;
}
}
if (this._shakeDuration > 0.0)
{
var intensity = this._shakeIntensity;
this._shakeDuration -= delta;
if (this._shakeDuration <= 0.0)
{
this._shakeOffsetX = 0.0;
this._shakeOffsetY = 0.0;
}
else
{
this._shakeOffsetX = (Math.random() * intensity * this.width * 2 - intensity * this.width) * this.zoom;
this._shakeOffsetY = (Math.random() * intensity * this.height * 2 - intensity * this.height) * this.zoom;
}
}
};
module.exports = Update;

View file

@ -1,12 +0,0 @@
// Phaser.Cameras
module.exports = {
Camera: require('./2d/Camera'),
PerspectiveCamera: require('./3d/PerspectiveCamera'),
OrthographicCamera: require('./3d/OrthographicCamera'),
KeyControl: require('./controls/KeyControl'),
SmoothedKeyControl: require('./controls/SmoothedKeyControl')
};

View file

@ -1,73 +0,0 @@
var Class = require('../../utils/Class');
var PluginManager = require('../../plugins/PluginManager');
var CameraManager = new Class({
initialize:
function CameraManager (scene)
{
// The Scene that owns this plugin
this.scene = scene;
this.systems = scene.sys;
this.mapping = 'cameras';
this.systems.events.on('boot', this.boot, this);
this.currentCameraId = 1;
this.cameras = [];
this.cameraPool = [];
if (scene.sys.settings.cameras)
{
// We have cameras to create
this.fromJSON(scene.sys.settings.cameras);
}
else
{
// Make one
this.add();
}
// Set the default camera
this.main = this.cameras[0];
},
boot: function ()
{
this.systems.inject(this);
this.systems.events.on('update', this.update, this);
this.systems.events.on('shutdown', this.shutdown, this);
this.systems.events.on('destroy', this.destroy, this);
},
add3D: require('./inc/AddPerspectiveCamera'),
add: require('./inc/Add2DCamera'),
addExisting: require('./inc/AddExisting'),
addKeyControl: require('./inc/AddKeyControl'),
addOrthographicCamera: require('./inc/AddOrthographicCamera'),
addPerspectiveCamera: require('./inc/AddPerspectiveCamera'),
addSmoothedKeyControl: require('./inc/AddSmoothedKeyControl'),
destroy: require('./inc/Destroy'),
fromJSON: require('./inc/FromJSON'),
getCamera: require('./inc/GetCamera'),
getCameraBelowPointer: require('./inc/GetCameraBelowPointer'),
remove: require('./inc/RemoveCamera'),
render: require('./inc/Render'),
resetAll: require('./inc/ResetAll'),
update: require('./inc/Update'),
shutdown: function ()
{
}
});
PluginManager.register('cameras', CameraManager);
module.exports = CameraManager;

View file

@ -1,42 +0,0 @@
var Camera = require('../../2d/Camera');
var Add2DCamera = function (x, y, width, height, makeMain, name)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.scene.sys.game.config.width; }
if (height === undefined) { height = this.scene.sys.game.config.height; }
if (makeMain === undefined) { makeMain = false; }
if (name === undefined) { name = ''; }
var camera = null;
if (this.cameraPool.length > 0)
{
camera = this.cameraPool.pop();
camera.setViewport(x, y, width, height);
}
else
{
camera = new Camera(x, y, width, height);
}
camera.setName(name);
camera.setScene(this.scene);
this.cameras.push(camera);
if (makeMain)
{
this.main = camera;
}
camera._id = this.currentCameraId;
this.currentCameraId = this.currentCameraId << 1;
return camera;
};
module.exports = Add2DCamera;

View file

@ -1,16 +0,0 @@
var AddExisting = function (camera)
{
var index = this.cameras.indexOf(camera);
var poolIndex = this.cameraPool.indexOf(camera);
if (index < 0 && poolIndex >= 0)
{
this.cameras.push(camera);
this.cameraPool.slice(poolIndex, 1);
return camera;
}
return null;
};
module.exports = AddExisting;

View file

@ -1,8 +0,0 @@
var KeyControl = require('../../controls/KeyControl');
var AddKeyControl = function (config)
{
return new KeyControl(config);
};
module.exports = AddKeyControl;

View file

@ -1,15 +0,0 @@
var OrthographicCamera = require('../../3d/OrthographicCamera');
var AddOrthographicCamera = function (width, height)
{
var config = this.scene.sys.game.config;
if (width === undefined) { width = config.width; }
if (height === undefined) { height = config.height; }
var camera = new OrthographicCamera(this.scene, width, height);
return camera;
};
module.exports = AddOrthographicCamera;

View file

@ -1,16 +0,0 @@
var PerspectiveCamera = require('../../3d/PerspectiveCamera');
var AddPerspectiveCamera = function (fieldOfView, width, height)
{
var config = this.scene.sys.game.config;
if (fieldOfView === undefined) { fieldOfView = 80; }
if (width === undefined) { width = config.width; }
if (height === undefined) { height = config.height; }
var camera = new PerspectiveCamera(this.scene, fieldOfView, width, height);
return camera;
};
module.exports = AddPerspectiveCamera;

View file

@ -1,8 +0,0 @@
var SmoothedKeyControl = require('../../controls/SmoothedKeyControl');
var AddSmoothedKeyControl = function (config)
{
return new SmoothedKeyControl(config);
};
module.exports = AddSmoothedKeyControl;

View file

@ -1,20 +0,0 @@
var Destroy = function ()
{
this.main = undefined;
for (var i = 0; i < this.cameras.length; i++)
{
this.cameras[i].destroy();
}
for (i = 0; i < this.cameraPool.length; i++)
{
this.cameraPool[i].destroy();
}
this.cameras = [];
this.cameraPool = [];
this.scene = undefined;
};
module.exports = Destroy;

View file

@ -1,85 +0,0 @@
var GetFastValue = require('../../../utils/object/GetFastValue');
/*
{
cameras: [
{
name: string
x: int
y: int
width: int
height: int
zoom: float
rotation: float
roundPixels: bool
scrollX: float
scrollY: float
backgroundColor: string
bounds: {
x: int
y: int
width: int
height: int
}
}
]
}
*/
var FromJSON = function (config)
{
if (!Array.isArray(config))
{
config = [ config ];
}
var gameWidth = this.scene.sys.game.config.width;
var gameHeight = this.scene.sys.game.config.height;
for (var i = 0; i < config.length; i++)
{
var cameraConfig = config[i];
var x = GetFastValue(cameraConfig, 'x', 0);
var y = GetFastValue(cameraConfig, 'y', 0);
var width = GetFastValue(cameraConfig, 'width', gameWidth);
var height = GetFastValue(cameraConfig, 'height', gameHeight);
var camera = this.add(x, y, width, height);
// Direct properties
camera.name = GetFastValue(cameraConfig, 'name', '');
camera.zoom = GetFastValue(cameraConfig, 'zoom', 1);
camera.rotation = GetFastValue(cameraConfig, 'rotation', 0);
camera.scrollX = GetFastValue(cameraConfig, 'scrollX', 0);
camera.scrollY = GetFastValue(cameraConfig, 'scrollY', 0);
camera.roundPixels = GetFastValue(cameraConfig, 'roundPixels', false);
// Background Color
var backgroundColor = GetFastValue(cameraConfig, 'backgroundColor', false);
if (backgroundColor)
{
camera.setBackgroundColor(backgroundColor);
}
// Bounds
var boundsConfig = GetFastValue(cameraConfig, 'bounds', null);
if (boundsConfig)
{
var bx = GetFastValue(boundsConfig, 'x', 0);
var by = GetFastValue(boundsConfig, 'y', 0);
var bwidth = GetFastValue(boundsConfig, 'width', gameWidth);
var bheight = GetFastValue(boundsConfig, 'height', gameHeight);
camera.setBounds(bx, by, bwidth, bheight);
}
}
return this;
};
module.exports = FromJSON;

View file

@ -1,14 +0,0 @@
var GetCamera = function (name)
{
this.cameras.forEach(function (camera)
{
if (camera.name === name)
{
return camera;
}
});
return null;
};
module.exports = GetCamera;

View file

@ -1,19 +0,0 @@
var RectangleContains = require('../../../geom/rectangle/Contains');
var GetCameraBelowPointer = function (pointer)
{
var cameras = this.cameras;
// Start from the most recently added camera (the 'top' camera)
for (var i = cameras.length - 1; i >= 0; i--)
{
var camera = cameras[i];
if (camera.inputEnabled && RectangleContains(camera, pointer.x, pointer.y))
{
return camera;
}
}
};
module.exports = GetCameraBelowPointer;

View file

@ -1,17 +0,0 @@
var RemoveCamera = function (camera)
{
var cameraIndex = this.cameras.indexOf(camera);
if (cameraIndex >= 0 && this.cameras.length > 1)
{
this.cameraPool.push(this.cameras[cameraIndex]);
this.cameras.splice(cameraIndex, 1);
if (this.main === camera)
{
this.main = this.cameras[0];
}
}
};
module.exports = RemoveCamera;

View file

@ -1,15 +0,0 @@
var Render = function (renderer, children, interpolation)
{
var cameras = this.cameras;
for (var i = 0, l = cameras.length; i < l; ++i)
{
var camera = cameras[i];
camera.preRender();
renderer.render(this.scene, children, interpolation, camera);
}
};
module.exports = Render;

View file

@ -1,13 +0,0 @@
var ResetAll = function ()
{
while (this.cameras.length > 0)
{
this.cameraPool.push(this.cameras.pop());
}
this.main = this.add();
return this.main;
};
module.exports = ResetAll;

View file

@ -1,9 +0,0 @@
var Update = function (timestep, delta)
{
for (var i = 0, l = this.cameras.length; i < l; ++i)
{
this.cameras[i].update(timestep, delta);
}
};
module.exports = Update;

707
src/cameras/2d/Camera.js Normal file
View file

@ -0,0 +1,707 @@
var Class = require('../../utils/Class');
var DegToRad = require('../../math/DegToRad');
var Rectangle = require('../../geom/rectangle/Rectangle');
var TransformMatrix = require('../../gameobjects/components/TransformMatrix');
var ValueToColor = require('../../display/color/ValueToColor');
var Vector2 = require('../../math/Vector2');
// Phaser.Cameras.Scene2D.Camera
var Camera = new Class({
initialize:
function Camera (x, y, width, height)
{
this.scene;
this.name = '';
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.roundPixels = false;
// Bounds
this.useBounds = false;
this._bounds = new Rectangle();
this.inputEnabled = true;
this.scrollX = 0.0;
this.scrollY = 0.0;
this.zoom = 1.0;
this.rotation = 0.0;
this.matrix = new TransformMatrix(1, 0, 0, 1, 0, 0);
this.transparent = true;
this.clearBeforeRender = true;
this.backgroundColor = ValueToColor('rgba(0,0,0,0)');
this.disableCull = false;
this.culledObjects = [];
// Shake
this._shakeDuration = 0;
this._shakeIntensity = 0;
this._shakeOffsetX = 0;
this._shakeOffsetY = 0;
// Fade
this._fadeDuration = 0;
this._fadeRed = 0;
this._fadeGreen = 0;
this._fadeBlue = 0;
this._fadeAlpha = 0;
// Flash
this._flashDuration = 0;
this._flashRed = 1;
this._flashGreen = 1;
this._flashBlue = 1;
this._flashAlpha = 0;
// Follow
this._follow = null;
this._id = 0;
},
centerToBounds: function ()
{
this.scrollX = (this._bounds.width * 0.5) - (this.width * 0.5);
this.scrollY = (this._bounds.height * 0.5) - (this.height * 0.5);
return this;
},
centerToSize: function ()
{
this.scrollX = this.width * 0.5;
this.scrollY = this.height * 0.5;
return this;
},
cull: function (renderableObjects)
{
if (this.disableCull)
{
return renderableObjects;
}
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
/* First Invert Matrix */
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
return renderableObjects;
}
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var cameraW = this.width;
var cameraH = this.height;
var culledObjects = this.culledObjects;
var length = renderableObjects.length;
determinant = 1 / determinant;
culledObjects.length = 0;
for (var index = 0; index < length; ++index)
{
var object = renderableObjects[index];
if (!object.hasOwnProperty('width'))
{
culledObjects.push(object);
continue;
}
var objectW = object.width;
var objectH = object.height;
var objectX = (object.x - (scrollX * object.scrollFactorX)) - (objectW * object.originX);
var objectY = (object.y - (scrollY * object.scrollFactorY)) - (objectH * object.originY);
var tx = (objectX * mva + objectY * mvc + mve);
var ty = (objectX * mvb + objectY * mvd + mvf);
var tw = ((objectX + objectW) * mva + (objectY + objectH) * mvc + mve);
var th = ((objectX + objectW) * mvb + (objectY + objectH) * mvd + mvf);
var cullW = cameraW + objectW;
var cullH = cameraH + objectH;
if (tx > -objectW || ty > -objectH || tx < cullW || ty < cullH ||
tw > -objectW || th > -objectH || tw < cullW || th < cullH)
{
culledObjects.push(object);
}
}
return culledObjects;
},
cullHitTest: function (interactiveObjects)
{
if (this.disableCull)
{
return interactiveObjects;
}
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
/* First Invert Matrix */
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
return interactiveObjects;
}
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var cameraW = this.width;
var cameraH = this.height;
var length = interactiveObjects.length;
determinant = 1 / determinant;
var culledObjects = [];
for (var index = 0; index < length; ++index)
{
var object = interactiveObjects[index].gameObject;
if (!object.hasOwnProperty('width'))
{
culledObjects.push(interactiveObjects[index]);
continue;
}
var objectW = object.width;
var objectH = object.height;
var objectX = (object.x - (scrollX * object.scrollFactorX)) - (objectW * object.originX);
var objectY = (object.y - (scrollY * object.scrollFactorY)) - (objectH * object.originY);
var tx = (objectX * mva + objectY * mvc + mve);
var ty = (objectX * mvb + objectY * mvd + mvf);
var tw = ((objectX + objectW) * mva + (objectY + objectH) * mvc + mve);
var th = ((objectX + objectW) * mvb + (objectY + objectH) * mvd + mvf);
var cullW = cameraW + objectW;
var cullH = cameraH + objectH;
if (tx > -objectW || ty > -objectH || tx < cullW || ty < cullH ||
tw > -objectW || th > -objectH || tw < cullW || th < cullH)
{
culledObjects.push(interactiveObjects[index]);
}
}
return culledObjects;
},
cullTilemap: function (tilemap)
{
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
/* First Invert Matrix */
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
return tiles;
}
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
var tiles = tilemap.tiles;
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var cameraW = this.width;
var cameraH = this.height;
var culledObjects = this.culledObjects;
var length = tiles.length;
var tileW = tilemap.tileWidth;
var tileH = tilemap.tileHeight;
var cullW = cameraW + tileW;
var cullH = cameraH + tileH;
var scrollFactorX = tilemap.scrollFactorX;
var scrollFactorY = tilemap.scrollFactorY;
determinant = 1 / determinant;
culledObjects.length = 0;
for (var index = 0; index < length; ++index)
{
var tile = tiles[index];
var tileX = (tile.x - (scrollX * scrollFactorX));
var tileY = (tile.y - (scrollY * scrollFactorY));
var tx = (tileX * mva + tileY * mvc + mve);
var ty = (tileX * mvb + tileY * mvd + mvf);
var tw = ((tileX + tileW) * mva + (tileY + tileH) * mvc + mve);
var th = ((tileX + tileW) * mvb + (tileY + tileH) * mvd + mvf);
if (tx > -tileW && ty > -tileH && tw < cullW && th < cullH)
{
culledObjects.push(tile);
}
}
return culledObjects;
},
destroy: function ()
{
this._bounds = undefined;
this.matrix = undefined;
this.culledObjects = [];
this.scene = undefined;
},
fade: function (duration, red, green, blue, force)
{
if (red === undefined) { red = 0.0; }
if (green === undefined) { green = 0.0; }
if (blue === undefined) { blue = 0.0; }
if (!force && this._fadeAlpha > 0.0)
{
return;
}
this._fadeRed = red;
this._fadeGreen = green;
this._fadeBlue = blue;
if (duration <= 0)
{
duration = Number.MIN_VALUE;
}
this._fadeDuration = duration;
this._fadeAlpha = Number.MIN_VALUE;
},
flash: function (duration, red, green, blue, force)
{
if (!force && this._flashAlpha > 0.0)
{
return;
}
if (red === undefined) { red = 1.0; }
if (green === undefined) { green = 1.0; }
if (blue === undefined) { blue = 1.0; }
this._flashRed = red;
this._flashGreen = green;
this._flashBlue = blue;
if (duration <= 0)
{
duration = Number.MIN_VALUE;
}
this._flashDuration = duration;
this._flashAlpha = 1.0;
},
getWorldPoint: function (x, y, output)
{
if (output === undefined) { output = new Vector2(); }
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
/* First Invert Matrix */
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
output.x = x;
output.y = y;
return output;
}
determinant = 1 / determinant;
var ima = mvd * determinant;
var imb = -mvb * determinant;
var imc = -mvc * determinant;
var imd = mva * determinant;
var ime = (mvc * mvf - mvd * mve) * determinant;
var imf = (mvb * mve - mva * mvf) * determinant;
var c = Math.cos(this.rotation);
var s = Math.sin(this.rotation);
var zoom = this.zoom;
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var sx = x + ((scrollX * c - scrollY * s) * zoom);
var sy = y + ((scrollX * s + scrollY * c) * zoom);
/* Apply transform to point */
output.x = (sx * ima + sy * imc + ime);
output.y = (sx * imb + sy * imd + imf);
return output;
},
ignore: function (gameObjectOrArray)
{
if (gameObjectOrArray instanceof Array)
{
for (var index = 0; index < gameObjectOrArray.length; ++index)
{
gameObjectOrArray[index].cameraFilter |= this._id;
}
}
else
{
gameObjectOrArray.cameraFilter |= this._id;
}
},
preRender: function ()
{
var width = this.width;
var height = this.height;
var zoom = this.zoom;
var matrix = this.matrix;
var originX = width / 2;
var originY = height / 2;
var follow = this._follow;
if (follow !== null)
{
originX = follow.x;
originY = follow.y;
this.scrollX = originX - width * 0.5;
this.scrollY = originY - height * 0.5;
}
if (this.useBounds)
{
var bounds = this._bounds;
var bw = Math.max(0, bounds.right - width);
var bh = Math.max(0, bounds.bottom - height);
if (this.scrollX < bounds.x)
{
this.scrollX = bounds.x;
}
else if (this.scrollX > bw)
{
this.scrollX = bw;
}
if (this.scrollY < bounds.y)
{
this.scrollY = bounds.y;
}
else if (this.scrollY > bh)
{
this.scrollY = bh;
}
}
if (this.roundPixels)
{
this.scrollX = Math.round(this.scrollX);
this.scrollY = Math.round(this.scrollY);
}
matrix.loadIdentity();
matrix.translate(this.x + originX, this.y + originY);
matrix.rotate(this.rotation);
matrix.scale(zoom, zoom);
matrix.translate(-originX, -originY);
matrix.translate(this._shakeOffsetX, this._shakeOffsetY);
},
removeBounds: function ()
{
this.useBounds = false;
this._bounds.setEmpty();
return this;
},
setAngle: function (value)
{
if (value === undefined) { value = 0; }
this.rotation = DegToRad(value);
return this;
},
setBackgroundColor: function (color)
{
if (color === undefined) { color = 'rgba(0,0,0,0)'; }
this.backgroundColor = ValueToColor(color);
this.transparent = (this.backgroundColor.alpha === 0);
return this;
},
setBounds: function (x, y, width, height)
{
this._bounds.setTo(x, y, width, height);
this.useBounds = true;
return this;
},
setName: function (value)
{
if (value === undefined) { value = ''; }
this.name = value;
return this;
},
setPosition: function (x, y)
{
if (y === undefined) { y = x; }
this.x = x;
this.y = y;
return this;
},
setRotation: function (value)
{
if (value === undefined) { value = 0; }
this.rotation = value;
return this;
},
setRoundPixels: function (value)
{
this.roundPixels = value;
return this;
},
setScene: function (scene)
{
this.scene = scene;
return this;
},
setScroll: function (x, y)
{
if (y === undefined) { y = x; }
this.scrollX = x;
this.scrollY = y;
return this;
},
setSize: function (width, height)
{
if (height === undefined) { height = width; }
this.width = width;
this.height = height;
return this;
},
setViewport: function (x, y, width, height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
return this;
},
setZoom: function (value)
{
if (value === undefined) { value = 1; }
this.zoom = value;
return this;
},
shake: function (duration, intensity, force)
{
if (intensity === undefined) { intensity = 0.05; }
if (!force && (this._shakeOffsetX !== 0.0 || this._shakeOffsetY !== 0.0))
{
return;
}
this._shakeDuration = duration;
this._shakeIntensity = intensity;
this._shakeOffsetX = 0;
this._shakeOffsetY = 0;
},
startFollow: function (gameObjectOrPoint, roundPx)
{
if (this._follow !== null)
{
this.stopFollow();
}
this._follow = gameObjectOrPoint;
if (roundPx !== undefined)
{
this.roundPixels = roundPx;
}
return this;
},
stopFollow: function ()
{
/* do unfollow work here */
this._follow = null;
},
/*
camera: {
x: int
y: int
width: int
height: int
zoom: float
rotation: float
roundPixels: bool
scrollX: float
scrollY: float
backgroundColor: string
bounds: {
x: int
y: int
width: int
height: int
}
}
*/
toJSON: function ()
{
var output = {
name: this.name,
x: this.x,
y: this.y,
width: this.width,
height: this.height,
zoom: this.zoom,
rotation: this.rotation,
roundPixels: this.roundPixels,
scrollX: this.scrollX,
scrollY: this.scrollY,
backgroundColor: this.backgroundColor.rgba
};
if (this.useBounds)
{
output['bounds'] = {
x: this._bounds.x,
y: this._bounds.y,
width: this._bounds.width,
height: this._bounds.height
};
}
return output;
},
update: function (timestep, delta)
{
if (this._flashAlpha > 0.0)
{
this._flashAlpha -= delta / this._flashDuration;
if (this._flashAlpha < 0.0)
{
this._flashAlpha = 0.0;
}
}
if (this._fadeAlpha > 0.0 && this._fadeAlpha < 1.0)
{
this._fadeAlpha += delta / this._fadeDuration;
if (this._fadeAlpha >= 1.0)
{
this._fadeAlpha = 1.0;
}
}
if (this._shakeDuration > 0.0)
{
var intensity = this._shakeIntensity;
this._shakeDuration -= delta;
if (this._shakeDuration <= 0.0)
{
this._shakeOffsetX = 0.0;
this._shakeOffsetY = 0.0;
}
else
{
this._shakeOffsetX = (Math.random() * intensity * this.width * 2 - intensity * this.width) * this.zoom;
this._shakeOffsetY = (Math.random() * intensity * this.height * 2 - intensity * this.height) * this.zoom;
}
}
}
});
module.exports = Camera;

View file

@ -0,0 +1,307 @@
var Camera = require('./Camera');
var Class = require('../../utils/Class');
var GetFastValue = require('../../utils/object/GetFastValue');
var PluginManager = require('../../plugins/PluginManager');
var RectangleContains = require('../../geom/rectangle/Contains');
// Phaser.Cameras.Scene2D.CameraManager
var CameraManager = new Class({
initialize:
function CameraManager (scene)
{
// The Scene that owns this plugin
this.scene = scene;
this.systems = scene.sys;
this.mapping = 'cameras';
this.systems.events.on('boot', this.boot, this);
this.currentCameraId = 1;
this.cameras = [];
this.cameraPool = [];
if (scene.sys.settings.cameras)
{
// We have cameras to create
this.fromJSON(scene.sys.settings.cameras);
}
else
{
// Make one
this.add();
}
// Set the default camera
this.main = this.cameras[0];
},
boot: function ()
{
this.systems.inject(this);
this.systems.events.on('update', this.update, this);
this.systems.events.on('shutdown', this.shutdown, this);
this.systems.events.on('destroy', this.destroy, this);
},
add: function (x, y, width, height, makeMain, name)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.scene.sys.game.config.width; }
if (height === undefined) { height = this.scene.sys.game.config.height; }
if (makeMain === undefined) { makeMain = false; }
if (name === undefined) { name = ''; }
var camera = null;
if (this.cameraPool.length > 0)
{
camera = this.cameraPool.pop();
camera.setViewport(x, y, width, height);
}
else
{
camera = new Camera(x, y, width, height);
}
camera.setName(name);
camera.setScene(this.scene);
this.cameras.push(camera);
if (makeMain)
{
this.main = camera;
}
camera._id = this.currentCameraId;
this.currentCameraId = this.currentCameraId << 1;
return camera;
},
addExisting: function (camera)
{
var index = this.cameras.indexOf(camera);
var poolIndex = this.cameraPool.indexOf(camera);
if (index < 0 && poolIndex >= 0)
{
this.cameras.push(camera);
this.cameraPool.slice(poolIndex, 1);
return camera;
}
return null;
},
/*
addKeyControl: function (config)
{
return new KeyControl(config);
},
addSmoothedKeyControl: function (config)
{
return new SmoothedKeyControl(config);
},
*/
destroy: function ()
{
this.main = undefined;
for (var i = 0; i < this.cameras.length; i++)
{
this.cameras[i].destroy();
}
for (i = 0; i < this.cameraPool.length; i++)
{
this.cameraPool[i].destroy();
}
this.cameras = [];
this.cameraPool = [];
this.scene = undefined;
},
/*
{
cameras: [
{
name: string
x: int
y: int
width: int
height: int
zoom: float
rotation: float
roundPixels: bool
scrollX: float
scrollY: float
backgroundColor: string
bounds: {
x: int
y: int
width: int
height: int
}
}
]
}
*/
fromJSON: function (config)
{
if (!Array.isArray(config))
{
config = [ config ];
}
var gameWidth = this.scene.sys.game.config.width;
var gameHeight = this.scene.sys.game.config.height;
for (var i = 0; i < config.length; i++)
{
var cameraConfig = config[i];
var x = GetFastValue(cameraConfig, 'x', 0);
var y = GetFastValue(cameraConfig, 'y', 0);
var width = GetFastValue(cameraConfig, 'width', gameWidth);
var height = GetFastValue(cameraConfig, 'height', gameHeight);
var camera = this.add(x, y, width, height);
// Direct properties
camera.name = GetFastValue(cameraConfig, 'name', '');
camera.zoom = GetFastValue(cameraConfig, 'zoom', 1);
camera.rotation = GetFastValue(cameraConfig, 'rotation', 0);
camera.scrollX = GetFastValue(cameraConfig, 'scrollX', 0);
camera.scrollY = GetFastValue(cameraConfig, 'scrollY', 0);
camera.roundPixels = GetFastValue(cameraConfig, 'roundPixels', false);
// Background Color
var backgroundColor = GetFastValue(cameraConfig, 'backgroundColor', false);
if (backgroundColor)
{
camera.setBackgroundColor(backgroundColor);
}
// Bounds
var boundsConfig = GetFastValue(cameraConfig, 'bounds', null);
if (boundsConfig)
{
var bx = GetFastValue(boundsConfig, 'x', 0);
var by = GetFastValue(boundsConfig, 'y', 0);
var bwidth = GetFastValue(boundsConfig, 'width', gameWidth);
var bheight = GetFastValue(boundsConfig, 'height', gameHeight);
camera.setBounds(bx, by, bwidth, bheight);
}
}
return this;
},
getCamera: function (name)
{
this.cameras.forEach(function (camera)
{
if (camera.name === name)
{
return camera;
}
});
return null;
},
getCameraBelowPointer: function (pointer)
{
var cameras = this.cameras;
// Start from the most recently added camera (the 'top' camera)
for (var i = cameras.length - 1; i >= 0; i--)
{
var camera = cameras[i];
if (camera.inputEnabled && RectangleContains(camera, pointer.x, pointer.y))
{
return camera;
}
}
},
removeCamera: function (camera)
{
var cameraIndex = this.cameras.indexOf(camera);
if (cameraIndex >= 0 && this.cameras.length > 1)
{
this.cameraPool.push(this.cameras[cameraIndex]);
this.cameras.splice(cameraIndex, 1);
if (this.main === camera)
{
this.main = this.cameras[0];
}
}
},
render: function (renderer, children, interpolation)
{
var cameras = this.cameras;
for (var i = 0, l = cameras.length; i < l; ++i)
{
var camera = cameras[i];
camera.preRender();
renderer.render(this.scene, children, interpolation, camera);
}
},
resetAll: function ()
{
while (this.cameras.length > 0)
{
this.cameraPool.push(this.cameras.pop());
}
this.main = this.add();
return this.main;
},
update: function (timestep, delta)
{
for (var i = 0, l = this.cameras.length; i < l; ++i)
{
this.cameras[i].update(timestep, delta);
}
},
shutdown: function ()
{
}
});
PluginManager.register('cameras', CameraManager);
module.exports = CameraManager;

View file

@ -1,5 +1,5 @@
var Class = require('../../utils/Class'); var Class = require('../../../utils/Class');
var GetValue = require('../../utils/object/GetValue'); var GetValue = require('../../../utils/object/GetValue');
// var camControl = new CameraControl({ // var camControl = new CameraControl({
// camera: this.cameras.main, // camera: this.cameras.main,
@ -8,6 +8,8 @@ var GetValue = require('../../utils/object/GetValue');
// speed: float OR { x: 0, y: 0 } // speed: float OR { x: 0, y: 0 }
// }) // })
// Phaser.Cameras.Scene2D.Controls.KeyControl
var KeyControl = new Class({ var KeyControl = new Class({
initialize: initialize:

View file

@ -1,5 +1,5 @@
var Class = require('../../utils/Class'); var Class = require('../../../utils/Class');
var GetValue = require('../../utils/object/GetValue'); var GetValue = require('../../../utils/object/GetValue');
// var controlConfig = { // var controlConfig = {
// camera: this.cameras.main, // camera: this.cameras.main,
@ -15,6 +15,8 @@ var GetValue = require('../../utils/object/GetValue');
// maxSpeed: 1.0 // maxSpeed: 1.0
// }; // };
// Phaser.Cameras.Scene2D.Controls.SmoothedKeyControl
var SmoothedKeyControl = new Class({ var SmoothedKeyControl = new Class({
initialize: initialize:

View file

@ -0,0 +1,8 @@
// Phaser.Cameras.Scene2D.Controls
module.exports = {
KeyControl: require('./KeyControl'),
SmoothedKeyControl: require('./SmoothedKeyControl')
};

9
src/cameras/2d/index.js Normal file
View file

@ -0,0 +1,9 @@
// Phaser.Cameras.Scene2D
module.exports = {
Camera: require('./Camera'),
CameraManager: require('./CameraManager'),
Controls: require('./controls/')
};

8
src/cameras/index.js Normal file
View file

@ -0,0 +1,8 @@
// Phaser.Cameras
module.exports = {
Scene2D: require('./2d/'),
Sprite3D: require('./sprite3d/')
};

View file

@ -16,11 +16,15 @@ var dirvec = new Vector3();
var rightvec = new Vector3(); var rightvec = new Vector3();
var billboardMatrix = new Matrix4(); var billboardMatrix = new Matrix4();
var Camera3D = new Class({ // @author attribute https://github.com/mattdesl/cam3d/wiki
// Phaser.Cameras.Sprite3D.Camera
var Camera = new Class({
initialize: initialize:
function Camera3D (scene) function Camera (scene)
{ {
this.scene = scene; this.scene = scene;
@ -335,8 +339,8 @@ var Camera3D = new Class({
// TODO: support viewport XY // TODO: support viewport XY
var viewportWidth = this.viewportWidth; var viewportWidth = this.viewportWidth;
var viewportHeight = this.viewportHeight; var viewportHeight = this.viewportHeight;
var n = Camera3D.NEAR_RANGE; var n = Camera.NEAR_RANGE;
var f = Camera3D.FAR_RANGE; var f = Camera.FAR_RANGE;
// For useful Z and W values we should do the usual steps: clip space -> NDC -> window coords // For useful Z and W values we should do the usual steps: clip space -> NDC -> window coords
@ -577,7 +581,7 @@ var Camera3D = new Class({
}); });
Camera3D.FAR_RANGE = 1.0; Camera.FAR_RANGE = 1.0;
Camera3D.NEAR_RANGE = 0.0; Camera.NEAR_RANGE = 0.0;
module.exports = Camera3D; module.exports = Camera;

View file

@ -0,0 +1,140 @@
var Class = require('../../utils/Class');
var GetFastValue = require('../../utils/object/GetFastValue');
var OrthographicCamera = require('./OrthographicCamera');
var PerspectiveCamera = require('./PerspectiveCamera');
var PluginManager = require('../../plugins/PluginManager');
// Phaser.Cameras.Sprite3D.CameraManager
var CameraManager = new Class({
initialize:
function CameraManager (scene)
{
// The Scene that owns this plugin
this.scene = scene;
this.systems = scene.sys;
this.mapping = 'cameras';
this.systems.events.on('boot', this.boot, this);
},
boot: function ()
{
this.systems.inject(this);
this.systems.events.on('update', this.update, this);
this.systems.events.on('shutdown', this.shutdown, this);
this.systems.events.on('destroy', this.destroy, this);
},
add: function (fieldOfView, width, height)
{
return this.addPerspectiveCamera(fieldOfView, width, height);
},
addOrthographicCamera: function (width, height)
{
var config = this.scene.sys.game.config;
if (width === undefined) { width = config.width; }
if (height === undefined) { height = config.height; }
var camera = new OrthographicCamera(this.scene, width, height);
return camera;
},
addPerspectiveCamera: function (fieldOfView, width, height)
{
var config = this.scene.sys.game.config;
if (fieldOfView === undefined) { fieldOfView = 80; }
if (width === undefined) { width = config.width; }
if (height === undefined) { height = config.height; }
var camera = new PerspectiveCamera(this.scene, fieldOfView, width, height);
return camera;
},
destroy: function ()
{
this.scene = undefined;
},
getCamera: function (name)
{
this.cameras.forEach(function (camera)
{
if (camera.name === name)
{
return camera;
}
});
return null;
},
removeCamera: function (camera)
{
var cameraIndex = this.cameras.indexOf(camera);
if (cameraIndex >= 0 && this.cameras.length > 1)
{
this.cameraPool.push(this.cameras[cameraIndex]);
this.cameras.splice(cameraIndex, 1);
if (this.main === camera)
{
this.main = this.cameras[0];
}
}
},
render: function (renderer, children, interpolation)
{
var cameras = this.cameras;
for (var i = 0, l = cameras.length; i < l; ++i)
{
var camera = cameras[i];
camera.preRender();
renderer.render(this.scene, children, interpolation, camera);
}
},
resetAll: function ()
{
while (this.cameras.length > 0)
{
this.cameraPool.push(this.cameras.pop());
}
this.main = this.add();
return this.main;
},
update: function (timestep, delta)
{
for (var i = 0, l = this.cameras.length; i < l; ++i)
{
this.cameras[i].update(timestep, delta);
}
},
shutdown: function ()
{
}
});
// PluginManager.register('cameras', CameraManager);
module.exports = CameraManager;

View file

@ -1,15 +1,15 @@
var Camera3D = require('./Camera3D'); var Camera = require('./Camera');
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var Matrix4 = require('../../math/Matrix4');
var Vector3 = require('../../math/Vector3'); var Vector3 = require('../../math/Vector3');
var Vector4 = require('../../math/Vector4');
// Local cache vars // Local cache vars
var tmpVec3 = new Vector3(); var tmpVec3 = new Vector3();
// Phaser.Cameras.Sprite3D.OrthographicCamera
var OrthographicCamera = new Class({ var OrthographicCamera = new Class({
Extends: Camera3D, Extends: Camera,
initialize: initialize:
@ -18,7 +18,7 @@ var OrthographicCamera = new Class({
if (viewportWidth === undefined) { viewportWidth = 0; } if (viewportWidth === undefined) { viewportWidth = 0; }
if (viewportHeight === undefined) { viewportHeight = 0; } if (viewportHeight === undefined) { viewportHeight = 0; }
Camera3D.call(this, scene); Camera.call(this, scene);
this.viewportWidth = viewportWidth; this.viewportWidth = viewportWidth;
this.viewportHeight = viewportHeight; this.viewportHeight = viewportHeight;
@ -49,7 +49,6 @@ var OrthographicCamera = new Class({
update: function () update: function ()
{ {
//TODO: support x/y offset
var w = this.viewportWidth; var w = this.viewportWidth;
var h = this.viewportHeight; var h = this.viewportHeight;
var near = Math.abs(this.near); var near = Math.abs(this.near);

View file

@ -1,13 +1,15 @@
var Camera3D = require('./Camera3D'); var Camera = require('./Camera');
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var Vector3 = require('../../math/Vector3'); var Vector3 = require('../../math/Vector3');
// Local cache vars // Local cache vars
var tmpVec3 = new Vector3(); var tmpVec3 = new Vector3();
// Phaser.Cameras.Sprite3D.PerspectiveCamera
var PerspectiveCamera = new Class({ var PerspectiveCamera = new Class({
Extends: Camera3D, Extends: Camera,
// FOV is converted to radians automatically // FOV is converted to radians automatically
initialize: initialize:
@ -18,7 +20,7 @@ var PerspectiveCamera = new Class({
if (viewportWidth === undefined) { viewportWidth = 0; } if (viewportWidth === undefined) { viewportWidth = 0; }
if (viewportHeight === undefined) { viewportHeight = 0; } if (viewportHeight === undefined) { viewportHeight = 0; }
Camera3D.call(this, scene); Camera.call(this, scene);
this.viewportWidth = viewportWidth; this.viewportWidth = viewportWidth;
this.viewportHeight = viewportHeight; this.viewportHeight = viewportHeight;

View file

@ -0,0 +1,10 @@
// Phaser.Cameras.Sprite3D
module.exports = {
Camera: require('./Camera'),
CameraManager: require('./CameraManager'),
OrthographicCamera: require('./OrthographicCamera'),
PerspectiveCamera: require('./PerspectiveCamera')
};

View file

@ -1,4 +1,4 @@
var Camera = require('../../camera/2d/Camera.js'); var Camera = require('../../cameras/2d/Camera.js');
var CanvasPool = require('../../display/canvas/CanvasPool'); var CanvasPool = require('../../display/canvas/CanvasPool');
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var Commands = require('./Commands'); var Commands = require('./Commands');

View file

@ -11,7 +11,7 @@ var Phaser = {
Create: require('./create/'), Create: require('./create/'),
Cameras: require('./camera/'), Cameras: require('./cameras/'),
DOM: require('./dom/'), DOM: require('./dom/'),

View file

@ -1,4 +1,4 @@
var CameraManager = require('../../camera/local/CameraManager'); // var CameraManager = require('../../camera/local/CameraManager');
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var Clock = require('../../time/Clock'); var Clock = require('../../time/Clock');
var Data = require('../../data/Data'); var Data = require('../../data/Data');