mirror of
https://github.com/photonstorm/phaser
synced 2024-11-23 21:24:09 +00:00
Added Camera and fixed path issues.
This commit is contained in:
parent
a081ae8904
commit
14ee4c2d1b
8 changed files with 367 additions and 33 deletions
|
@ -110,8 +110,7 @@ Game.prototype = {
|
|||
// timestamp = DOMHighResTimeStamp
|
||||
update: function (timestamp)
|
||||
{
|
||||
// console.log(timestamp);
|
||||
// this.state.step(timestamp);
|
||||
this.state.step(timestamp);
|
||||
}
|
||||
|
||||
};
|
||||
|
|
341
v3/src/camera/Camera.js
Normal file
341
v3/src/camera/Camera.js
Normal 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;
|
|
@ -1,4 +1,4 @@
|
|||
var CHECKSUM = {
|
||||
build: 'f1712ef0-bc24-11e6-a204-b96b8023346c'
|
||||
build: '66ef5980-bc26-11e6-b62b-c7bd5e19f5c7'
|
||||
};
|
||||
module.exports = CHECKSUM;
|
|
@ -383,3 +383,6 @@ MultiTextureBatch.prototype.destroy = function ()
|
|||
|
||||
this.manager = null;
|
||||
};
|
||||
|
||||
module.exports = MultiTextureBatch;
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ var SingleTextureBatch = function (manager, batchSize)
|
|||
|
||||
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;
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
var CONST = require('../../../const');
|
||||
|
||||
var CreateEmptyTexture = function (gl, width, height, scaleMode, textureIndex)
|
||||
{
|
||||
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.bindTexture(gl.TEXTURE_2D, texture);
|
||||
|
|
|
@ -10,7 +10,7 @@ var State = require('./State');
|
|||
var Settings = require('./Settings');
|
||||
var Systems = require('./Systems');
|
||||
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.
|
||||
|
|
|
@ -6,10 +6,12 @@
|
|||
|
||||
var EventDispatcher = require('../events/EventDispatcher');
|
||||
var GameObjectFactory = require('./systems/GameObjectFactory');
|
||||
var GameObjectCreator = require('./systems/GameObjectCreator');
|
||||
// var GameObjectCreator = require('./systems/GameObjectCreator');
|
||||
var Loader = require('./systems/Loader');
|
||||
var MainLoop = require('./systems/MainLoop');
|
||||
var UpdateManager = require('./systems/UpdateManager');
|
||||
var Component = require('../components');
|
||||
var Camera = require('../camera/Camera');
|
||||
|
||||
var Systems = function (state, config)
|
||||
{
|
||||
|
@ -68,11 +70,11 @@ Systems.prototype = {
|
|||
// this.physics = new Phaser.Physics.Arcade(this.state, 800, 600);
|
||||
|
||||
// State specific properties (transform, data, children, etc)
|
||||
// this.camera = new Phaser.Camera(this.state, 0, 0, 800, 600);
|
||||
// this.children = new Phaser.Component.Children(this.state);
|
||||
// this.color = new Phaser.Component.Color(this.state);
|
||||
// this.data = new Phaser.Component.Data(this.state);
|
||||
// this.transform = this.camera.transform;
|
||||
this.camera = new Camera(this.state, 0, 0, 800, 600);
|
||||
this.children = new Component.Children(this.state);
|
||||
this.color = new Component.Color(this.state);
|
||||
this.data = new Component.Data(this.state);
|
||||
this.transform = this.camera.transform;
|
||||
|
||||
// Boot
|
||||
|
||||
|
@ -83,28 +85,19 @@ Systems.prototype = {
|
|||
this.state.events = this.events;
|
||||
this.state.add = this.add;
|
||||
this.state.load = this.load;
|
||||
// this.state.children = this.children;
|
||||
// this.state.color = this.color;
|
||||
// this.state.data = this.data;
|
||||
// this.state.camera = this.camera;
|
||||
this.state.children = this.children;
|
||||
this.state.color = this.color;
|
||||
this.state.data = this.data;
|
||||
this.state.camera = this.camera;
|
||||
this.state.transform = this.camera.transform;
|
||||
|
||||
|
||||
|
||||
// this.state.input = this.input;
|
||||
// this.state.transform = this.camera.transform;
|
||||
// this.state.state = this.state.game.state;
|
||||
|
||||
// Here we can check which Systems to install as properties into the State object
|
||||
// (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)
|
||||
|
@ -113,13 +106,10 @@ Systems.prototype = {
|
|||
|
||||
update: function (timestep, physicsStep)
|
||||
{
|
||||
// this.tweens.update(timestep);
|
||||
// this.physics.preUpdate(physicsStep);
|
||||
},
|
||||
|
||||
preRender: function ()
|
||||
{
|
||||
// this.physics.update();
|
||||
},
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports = Systems;
|
||||
|
|
Loading…
Reference in a new issue