Added Camera and fixed path issues.

This commit is contained in:
Richard Davey 2016-12-07 02:40:07 +00:00
parent a081ae8904
commit 14ee4c2d1b
8 changed files with 367 additions and 33 deletions

View file

@ -110,8 +110,7 @@ Game.prototype = {
// timestamp = DOMHighResTimeStamp // timestamp = DOMHighResTimeStamp
update: function (timestamp) update: function (timestamp)
{ {
// console.log(timestamp); this.state.step(timestamp);
// this.state.step(timestamp);
} }
}; };

341
v3/src/camera/Camera.js Normal file
View file

@ -0,0 +1,341 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Component = require('../components');
/**
* A Camera is your view into the game world. It has a position and size and renders only those objects within its field of view.
* The game automatically creates a single Stage sized camera on boot. Move the camera around the world with Phaser.Camera.x/y
*
* @class Phaser.Camera
* @constructor
* @param {Phaser.Game} game - Game reference to the currently running game.
* @param {number} id - Not being used at the moment, will be when Phaser supports multiple camera
* @param {number} x - Position of the camera on the X axis
* @param {number} y - Position of the camera on the Y axis
* @param {number} width - The width of the view rectangle
* @param {number} height - The height of the view rectangle
*/
var Camera = function (state, x, y, viewportWidth, viewportHeight)
{
/**
* The State that this Camera belongs to. A Camera can only belong to one State, and a State only
* has one Camera.
* @property {Phaser.State} state
*/
this.state = state;
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
*/
this.game = state.game;
this.viewportWidth = viewportWidth;
this.viewportHeight = viewportHeight;
this.transform = new Component.Transform(this, x, y);
/**
* The Camera is bound to this Rectangle and cannot move outside of it. By default it is enabled and set to the size of the World.
* The Rectangle can be located anywhere in the world and updated as often as you like. If you don't wish the Camera to be bound
* at all then set this to null. The values can be anything and are in World coordinates, with 0,0 being the top-left of the world.
*
* @property {Phaser.Rectangle} bounds - The Rectangle in which the Camera is bounded. Set to null to allow for movement anywhere.
*/
// this.bounds = new Phaser.Rectangle(x, y, width, height);
// this.bounds = new Phaser.Circle(x, y)
/**
* @property {boolean} atLimit - Whether this camera is flush with the World Bounds or not.
*/
this.atLimit = { x: false, y: false };
};
Camera.prototype.constructor = Camera;
Camera.prototype = {
/**
* Method called to ensure the camera doesn't venture outside of the game world.
* Called automatically by Camera.update.
*
* @method Phaser.Camera#checkBounds
* @protected
*/
checkBounds: function ()
{
this.atLimit.x = false;
this.atLimit.y = false;
// var vx = this.view.x + this._shake.x;
// var vw = this.view.right + this._shake.x;
// var vy = this.view.y + this._shake.y;
// var vh = this.view.bottom + this._shake.y;
var vx = this.x;
var vw = this.x + this.viewportWidth;
var vy = this.y;
var vh = this.y + this.viewportHeight;
// Make sure we didn't go outside the cameras bounds
if (vx <= this.bounds.x * this.scale.x)
{
this.atLimit.x = true;
this.view.x = this.bounds.x * this.scale.x;
if (!this._shake.shakeBounds)
{
// The camera is up against the bounds, so reset the shake
this._shake.x = 0;
}
}
if (vw >= this.bounds.right * this.scale.x)
{
this.atLimit.x = true;
this.view.x = (this.bounds.right * this.scale.x) - this.width;
if (!this._shake.shakeBounds)
{
// The camera is up against the bounds, so reset the shake
this._shake.x = 0;
}
}
if (vy <= this.bounds.top * this.scale.y)
{
this.atLimit.y = true;
this.view.y = this.bounds.top * this.scale.y;
if (!this._shake.shakeBounds)
{
// The camera is up against the bounds, so reset the shake
this._shake.y = 0;
}
}
if (vh >= this.bounds.bottom * this.scale.y)
{
this.atLimit.y = true;
this.view.y = (this.bounds.bottom * this.scale.y) - this.height;
if (!this._shake.shakeBounds)
{
// The camera is up against the bounds, so reset the shake
this._shake.y = 0;
}
}
}
};
Object.defineProperties(Camera.prototype, {
// Transform getters / setters
x: {
enumerable: true,
get: function ()
{
return this.transform._posX;
},
set: function (value)
{
this.transform._posX = value;
this.transform.dirty = true;
}
},
y: {
enumerable: true,
get: function ()
{
return this.transform._posY;
},
set: function (value)
{
this.transform._posY = value;
this.transform.dirty = true;
}
},
right: {
enumerable: true,
get: function ()
{
return this.transform._posX + (this.viewportWidth * this.transform._scaleX);
}
},
bottom: {
enumerable: true,
get: function ()
{
return this.transform._posY + (this.viewportHeight * this.transform._scaleY);
}
},
scale: {
enumerable: true,
get: function ()
{
return this.transform._scaleX;
},
set: function (value)
{
this.transform._scaleX = value;
this.transform._scaleY = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
scaleX: {
enumerable: true,
get: function ()
{
return this.transform._scaleX;
},
set: function (value)
{
this.transform._scaleX = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
scaleY: {
enumerable: true,
get: function ()
{
return this.transform._scaleY;
},
set: function (value)
{
this.transform._scaleY = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
pivotX: {
enumerable: true,
get: function ()
{
return this.transform._pivotX;
},
set: function (value)
{
this.transform._pivotX = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
pivotY: {
enumerable: true,
get: function ()
{
return this.transform._pivotY;
},
set: function (value)
{
this.transform._pivotY = value;
this.transform.dirty = true;
this.transform.updateCache();
}
},
angle: {
enumerable: true,
get: function ()
{
return Phaser.Math.wrapAngle(this.rotation * Phaser.Math.RAD_TO_DEG);
},
set: function (value)
{
this.rotation = Phaser.Math.wrapAngle(value) * Phaser.Math.DEG_TO_RAD;
}
},
rotation: {
enumerable: true,
get: function ()
{
return this.transform._rotation;
},
set: function (value)
{
if (this.transform._rotation === value)
{
return;
}
this.transform._rotation = value;
this.transform.dirty = true;
if (this.transform._rotation % Phaser.Math.PI2)
{
this.transform.cache.sr = Math.sin(this.transform._rotation);
this.transform.cache.cr = Math.cos(this.transform._rotation);
this.transform.updateCache();
this.transform.hasLocalRotation = true;
}
else
{
this.transform.hasLocalRotation = false;
}
}
}
});
module.exports = Camera;

View file

@ -1,4 +1,4 @@
var CHECKSUM = { var CHECKSUM = {
build: 'f1712ef0-bc24-11e6-a204-b96b8023346c' build: '66ef5980-bc26-11e6-b62b-c7bd5e19f5c7'
}; };
module.exports = CHECKSUM; module.exports = CHECKSUM;

View file

@ -383,3 +383,6 @@ MultiTextureBatch.prototype.destroy = function ()
this.manager = null; this.manager = null;
}; };
module.exports = MultiTextureBatch;

View file

@ -27,7 +27,7 @@ var SingleTextureBatch = function (manager, batchSize)
var vertSize = (4 * 2) + (4 * 2) + (4) + (4) + (4); var vertSize = (4 * 2) + (4 * 2) + (4) + (4) + (4);
Phaser.Renderer.WebGL.Batch.call(this, manager, batchSize, vertSize); BaseBatch.call(this, manager, batchSize, vertSize);
this.type = 1; this.type = 1;

View file

@ -1,7 +1,9 @@
var CONST = require('../../../const');
var CreateEmptyTexture = function (gl, width, height, scaleMode, textureIndex) var CreateEmptyTexture = function (gl, width, height, scaleMode, textureIndex)
{ {
var texture = gl.createTexture(); var texture = gl.createTexture();
var glScaleMode = (scaleMode === Phaser.scaleModes.LINEAR) ? gl.LINEAR : gl.NEAREST; var glScaleMode = (scaleMode === CONST.scaleModes.LINEAR) ? gl.LINEAR : gl.NEAREST;
gl.activeTexture(gl.TEXTURE0 + textureIndex); gl.activeTexture(gl.TEXTURE0 + textureIndex);
gl.bindTexture(gl.TEXTURE_2D, texture); gl.bindTexture(gl.TEXTURE_2D, texture);

View file

@ -10,7 +10,7 @@ var State = require('./State');
var Settings = require('./Settings'); var Settings = require('./Settings');
var Systems = require('./Systems'); var Systems = require('./Systems');
var GetObjectValue = require('../utils/GetObjectValue'); var GetObjectValue = require('../utils/GetObjectValue');
var LoaderEvent = require('../loader/events/'); // var LoaderEvent = require('../loader/events/');
/** /**
* The State Manager is responsible for loading, setting up and switching game states. * The State Manager is responsible for loading, setting up and switching game states.

View file

@ -6,10 +6,12 @@
var EventDispatcher = require('../events/EventDispatcher'); var EventDispatcher = require('../events/EventDispatcher');
var GameObjectFactory = require('./systems/GameObjectFactory'); var GameObjectFactory = require('./systems/GameObjectFactory');
var GameObjectCreator = require('./systems/GameObjectCreator'); // var GameObjectCreator = require('./systems/GameObjectCreator');
var Loader = require('./systems/Loader'); var Loader = require('./systems/Loader');
var MainLoop = require('./systems/MainLoop'); var MainLoop = require('./systems/MainLoop');
var UpdateManager = require('./systems/UpdateManager'); var UpdateManager = require('./systems/UpdateManager');
var Component = require('../components');
var Camera = require('../camera/Camera');
var Systems = function (state, config) var Systems = function (state, config)
{ {
@ -68,11 +70,11 @@ Systems.prototype = {
// this.physics = new Phaser.Physics.Arcade(this.state, 800, 600); // this.physics = new Phaser.Physics.Arcade(this.state, 800, 600);
// State specific properties (transform, data, children, etc) // State specific properties (transform, data, children, etc)
// this.camera = new Phaser.Camera(this.state, 0, 0, 800, 600); this.camera = new Camera(this.state, 0, 0, 800, 600);
// this.children = new Phaser.Component.Children(this.state); this.children = new Component.Children(this.state);
// this.color = new Phaser.Component.Color(this.state); this.color = new Component.Color(this.state);
// this.data = new Phaser.Component.Data(this.state); this.data = new Component.Data(this.state);
// this.transform = this.camera.transform; this.transform = this.camera.transform;
// Boot // Boot
@ -83,28 +85,19 @@ Systems.prototype = {
this.state.events = this.events; this.state.events = this.events;
this.state.add = this.add; this.state.add = this.add;
this.state.load = this.load; this.state.load = this.load;
// this.state.children = this.children; this.state.children = this.children;
// this.state.color = this.color; this.state.color = this.color;
// this.state.data = this.data; this.state.data = this.data;
// this.state.camera = this.camera; this.state.camera = this.camera;
this.state.transform = this.camera.transform;
// this.state.input = this.input; // this.state.input = this.input;
// this.state.transform = this.camera.transform;
// this.state.state = this.state.game.state; // this.state.state = this.state.game.state;
// Here we can check which Systems to install as properties into the State object // Here we can check which Systems to install as properties into the State object
// (default systems always exist in here, regardless) // (default systems always exist in here, regardless)
/*
var config = this.config;
var t = typeof config;
if (t !== 'object' || (t === 'object' && !t.hasOwnProperty('systems')))
{
return;
}
*/
// this.key = (config.hasOwnProperty('key')) ? config.key : '';
}, },
begin: function (timestamp, frameDelta) begin: function (timestamp, frameDelta)
@ -113,13 +106,10 @@ Systems.prototype = {
update: function (timestep, physicsStep) update: function (timestep, physicsStep)
{ {
// this.tweens.update(timestep);
// this.physics.preUpdate(physicsStep);
}, },
preRender: function () preRender: function ()
{ {
// this.physics.update();
}, },
end: function (fps, panic) end: function (fps, panic)
@ -135,7 +125,6 @@ Systems.prototype = {
console.warn('Main loop panicked, probably because the browser tab was put in the background. Discarding ' + discardedTime + 'ms'); console.warn('Main loop panicked, probably because the browser tab was put in the background. Discarding ' + discardedTime + 'ms');
} }
} }
}; };
module.exports = Systems; module.exports = Systems;