mirror of
https://github.com/wassname/phaser.git
synced 2026-06-27 16:10:15 +08:00
Making some major changes to Camera and World.
This commit is contained in:
@@ -80,8 +80,8 @@ Version 1.0.7 (in progress in the dev branch)
|
||||
* Phaser.Loader.Parser is now Phaser.LoaderParser (also the file has renamed from Parser.js to LoaderParser.js)
|
||||
* Fixed Cache.addDefaultImage so the default image works in Canvas as well as WebGL. Updated to a new image (32x32 black square with green outline)
|
||||
* Extended the Loader 404 error to display the url of the file that didn't load as well as the key.
|
||||
|
||||
|
||||
* Change: We've removed the scrollFactor property from all Game Objects. Sorry, but the new Camera system doesn't work with it and it caused all kinds of issues anyway. We will sort out a replacement for it at a later date.
|
||||
* Change: The Camera has been completely revamped.
|
||||
|
||||
|
||||
* TODO: addMarker hh:mm:ss:ms
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
$title = "Custom Sprite";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
MonsterBunny = function (game, rotateSpeed) {
|
||||
|
||||
// We call the Phaser.Sprite passing in the game reference
|
||||
// We're giving it a random X/Y position here, just for the sake of this demo - you could also pass the x/y in the constructor
|
||||
Phaser.Sprite.call(this, game, game.world.randomX, game.world.randomY, 'bunny');
|
||||
|
||||
this.anchor.setTo(0.5, 0.5);
|
||||
|
||||
this.rotateSpeed = rotateSpeed;
|
||||
|
||||
var randomScale = 0.1 + Math.random();
|
||||
|
||||
this.scale.setTo(randomScale, randomScale)
|
||||
|
||||
game.add.existing(this);
|
||||
|
||||
};
|
||||
|
||||
MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);
|
||||
MonsterBunny.prototype.constructor = MonsterBunny;
|
||||
|
||||
MonsterBunny.prototype.update = function() {
|
||||
|
||||
// Automatically called by World.update
|
||||
this.angle += this.rotateSpeed;
|
||||
|
||||
};
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('bunny', 'assets/sprites/bunny.png');
|
||||
|
||||
}
|
||||
|
||||
function create() {
|
||||
|
||||
for (var i = 0.1; i < 2; i += 0.1)
|
||||
{
|
||||
new MonsterBunny(game, i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require('../foot.php');
|
||||
?>
|
||||
@@ -6,13 +6,15 @@
|
||||
<script type="text/javascript">
|
||||
|
||||
// Here is a custom game object
|
||||
MonsterBunny = function (game, x, y) {
|
||||
MonsterBunny = function (game, x, y, rotateSpeed) {
|
||||
|
||||
Phaser.Sprite.call(this, game, x, y, 'bunny');
|
||||
|
||||
this.rotateSpeed = rotateSpeed;
|
||||
|
||||
};
|
||||
|
||||
MonsterBunny.prototype = Phaser.Utils.extend(true, Phaser.Sprite.prototype, PIXI.Sprite.prototype);
|
||||
MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);
|
||||
MonsterBunny.prototype.constructor = MonsterBunny;
|
||||
|
||||
/**
|
||||
@@ -20,7 +22,7 @@ MonsterBunny.prototype.constructor = MonsterBunny;
|
||||
*/
|
||||
MonsterBunny.prototype.update = function() {
|
||||
|
||||
this.angle++;
|
||||
this.angle += this.rotateSpeed;
|
||||
|
||||
};
|
||||
|
||||
@@ -36,11 +38,14 @@ MonsterBunny.prototype.update = function() {
|
||||
|
||||
function create() {
|
||||
|
||||
var wabbit = new MonsterBunny(game, 400, 300);
|
||||
wabbit.lifespan = 3000;
|
||||
var wabbit = new MonsterBunny(game, 200, 300, 1);
|
||||
wabbit.anchor.setTo(0.5, 0.5);
|
||||
|
||||
var wabbit2 = new MonsterBunny(game, 600, 300, 0.5);
|
||||
wabbit2.anchor.setTo(0.5, 0.5);
|
||||
|
||||
game.add.existing(wabbit);
|
||||
game.add.existing(wabbit2);
|
||||
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -29,8 +29,7 @@ PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject)
|
||||
continue;
|
||||
}
|
||||
|
||||
// if(!displayObject.renderable || displayObject.alpha == 0)
|
||||
if(!displayObject.renderable)
|
||||
if(!displayObject.renderable || displayObject.alpha == 0)
|
||||
{
|
||||
displayObject = displayObject._iNext;
|
||||
continue;
|
||||
|
||||
+91
-56
@@ -17,7 +17,6 @@
|
||||
* @param {number} width - The width of the view rectangle
|
||||
* @param {number} height - The height of the view rectangle
|
||||
*/
|
||||
|
||||
Phaser.Camera = function (game, id, x, y, width, height) {
|
||||
|
||||
/**
|
||||
@@ -50,6 +49,14 @@ Phaser.Camera = function (game, id, x, y, width, height) {
|
||||
*/
|
||||
this.screenView = new Phaser.Rectangle(x, y, width, height);
|
||||
|
||||
/**
|
||||
* 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 center 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);
|
||||
|
||||
/**
|
||||
* @property {Phaser.Rectangle} deadzone - Moving inside this Rectangle will not cause camera moving.
|
||||
*/
|
||||
@@ -168,49 +175,62 @@ Phaser.Camera.prototype = {
|
||||
*/
|
||||
update: function () {
|
||||
|
||||
return;
|
||||
|
||||
// Add dirty flag
|
||||
|
||||
if (this.target !== null)
|
||||
if (this.target)
|
||||
{
|
||||
if (this.deadzone)
|
||||
{
|
||||
this._edge = this.target.x - this.deadzone.x;
|
||||
|
||||
if (this.view.x > this._edge)
|
||||
{
|
||||
this.view.x = this._edge;
|
||||
}
|
||||
|
||||
this._edge = this.target.x + this.target.width - this.deadzone.x - this.deadzone.width;
|
||||
|
||||
if (this.view.x < this._edge)
|
||||
{
|
||||
this.view.x = this._edge;
|
||||
}
|
||||
|
||||
this._edge = this.target.y - this.deadzone.y;
|
||||
|
||||
if (this.view.y > this._edge)
|
||||
{
|
||||
this.view.y = this._edge;
|
||||
}
|
||||
|
||||
this._edge = this.target.y + this.target.height - this.deadzone.y - this.deadzone.height;
|
||||
|
||||
if (this.view.y < this._edge)
|
||||
{
|
||||
this.view.y = this._edge;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.focusOnXY(this.target.x, this.target.y);
|
||||
}
|
||||
this.updateTarget();
|
||||
}
|
||||
|
||||
this.checkWorldBounds();
|
||||
this.updateTarget();
|
||||
|
||||
if (this.bounds)
|
||||
{
|
||||
this.checkBounds();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
updateTarget: function () {
|
||||
|
||||
if (this.deadzone)
|
||||
{
|
||||
this._edge = this.target.x - this.deadzone.x;
|
||||
|
||||
if (this.view.x > this._edge)
|
||||
{
|
||||
this.view.x = this._edge;
|
||||
}
|
||||
|
||||
this._edge = this.target.x + this.target.width - this.deadzone.x - this.deadzone.width;
|
||||
|
||||
if (this.view.x < this._edge)
|
||||
{
|
||||
this.view.x = this._edge;
|
||||
}
|
||||
|
||||
this._edge = this.target.y - this.deadzone.y;
|
||||
|
||||
if (this.view.y > this._edge)
|
||||
{
|
||||
this.view.y = this._edge;
|
||||
}
|
||||
|
||||
this._edge = this.target.y + this.target.height - this.deadzone.y - this.deadzone.height;
|
||||
|
||||
if (this.view.y < this._edge)
|
||||
{
|
||||
this.view.y = this._edge;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.focusOnXY(this.target.x, this.target.y);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
setBoundsToWorld: function () {
|
||||
|
||||
this.bounds.setTo(this.game.world.x, this.game.world.y, this.game.world.width, this.game.world.height);
|
||||
|
||||
},
|
||||
|
||||
@@ -218,36 +238,34 @@ Phaser.Camera.prototype = {
|
||||
* Method called to ensure the camera doesn't venture outside of the game world.
|
||||
* @method Phaser.Camera#checkWorldBounds
|
||||
*/
|
||||
checkWorldBounds: function () {
|
||||
|
||||
return;
|
||||
checkBounds: function () {
|
||||
|
||||
this.atLimit.x = false;
|
||||
this.atLimit.y = false;
|
||||
|
||||
// Make sure we didn't go outside the cameras worldBounds
|
||||
if (this.view.x < this.world.bounds.left)
|
||||
// Make sure we didn't go outside the cameras bounds
|
||||
if (this.view.x < this.bounds.left)
|
||||
{
|
||||
this.atLimit.x = true;
|
||||
this.view.x = this.world.bounds.left;
|
||||
this.view.x = this.bounds.left;
|
||||
}
|
||||
|
||||
if (this.view.x > this.world.bounds.right - this.width)
|
||||
if (this.view.x > this.bounds.right - this.width)
|
||||
{
|
||||
this.atLimit.x = true;
|
||||
this.view.x = (this.world.bounds.right - this.width) + 1;
|
||||
this.view.x = (this.bounds.right - this.width) + 1;
|
||||
}
|
||||
|
||||
if (this.view.y < this.world.bounds.top)
|
||||
if (this.view.y < this.bounds.top)
|
||||
{
|
||||
this.atLimit.y = true;
|
||||
this.view.y = this.world.bounds.top;
|
||||
this.view.y = this.bounds.top;
|
||||
}
|
||||
|
||||
if (this.view.y > this.world.bounds.bottom - this.height)
|
||||
if (this.view.y > this.bounds.bottom - this.height)
|
||||
{
|
||||
this.atLimit.y = true;
|
||||
this.view.y = (this.world.bounds.bottom - this.height) + 1;
|
||||
this.view.y = (this.bounds.bottom - this.height) + 1;
|
||||
}
|
||||
|
||||
this.view.floor();
|
||||
@@ -266,7 +284,14 @@ Phaser.Camera.prototype = {
|
||||
|
||||
this.view.x = x;
|
||||
this.view.y = y;
|
||||
this.checkWorldBounds();
|
||||
|
||||
this.displayObject.x = -x;
|
||||
this.displayObject.y = -y;
|
||||
|
||||
if (this.bounds)
|
||||
{
|
||||
this.checkBounds();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
@@ -298,9 +323,14 @@ Object.defineProperty(Phaser.Camera.prototype, "x", {
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
this.view.x = value;
|
||||
this.displayObject.x = -value;
|
||||
this.checkWorldBounds();
|
||||
|
||||
if (this.bounds)
|
||||
{
|
||||
this.checkBounds();
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
@@ -317,9 +347,14 @@ Object.defineProperty(Phaser.Camera.prototype, "y", {
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
this.view.y = value;
|
||||
this.displayObject.y = -value;
|
||||
this.checkWorldBounds();
|
||||
|
||||
if (this.bounds)
|
||||
{
|
||||
this.checkBounds();
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
* @param {*} parent - The parent Group or DisplayObjectContainer that will hold this group, if any.
|
||||
* @param {string} [name=group] - A name for this Group. Not used internally but useful for debugging.
|
||||
* @param {boolean} [useStage=false] - Only the root World Group should use this value.
|
||||
* @param {boolean} [useStage=false] - Should the DisplayObjectContainer this Group creates be added to the World (default, false) or direct to the Stage (true).
|
||||
*/
|
||||
Phaser.Group = function (game, parent, name, useStage) {
|
||||
|
||||
|
||||
+19
-21
@@ -5,16 +5,16 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* "This world is but a canvas to our imagination." - Henry David Thoreau
|
||||
* <p>
|
||||
* A game has only one world. The world is an abstract place in which all game objects live. It is not bound
|
||||
* by stage limits and can be any size. You look into the world via cameras. All game objects live within
|
||||
* the world at world-based coordinates. By default a world is created the same size as your Stage.
|
||||
*
|
||||
* @class Phaser.World
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - Reference to the current game instance.
|
||||
*/
|
||||
* "This world is but a canvas to our imagination." - Henry David Thoreau
|
||||
*
|
||||
* A game has only one world. The world is an abstract place in which all game objects live. It is not bound
|
||||
* by stage limits and can be any size. You look into the world via cameras. All game objects live within
|
||||
* the world at world-based coordinates. By default a world is created the same size as your Stage.
|
||||
*
|
||||
* @class Phaser.World
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - Reference to the current game instance.
|
||||
*/
|
||||
Phaser.World = function (game) {
|
||||
|
||||
/**
|
||||
@@ -23,6 +23,10 @@ Phaser.World = function (game) {
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* The World has no fixed size, but it does have a bounds outside of which objects are no longer considered as being "in world" and you should use this to clean-up the display list and purge dead objects.
|
||||
* By default we set the Bounds to be from 0,0 to Game.width,Game.height. I.e. it will match the size given to the game constructor with 0,0 representing the top-left of the display.
|
||||
* However 0,0 is actually the center of the world, and if you rotate or scale the world all of that will happen from 0,0.
|
||||
* So if you want to make a game in which the world itself will rotate you should adjust the bounds so that 0,0 is the center point, i.e. set them to -1000,-1000,2000,2000 for a 2000x2000 sized world centered around 0,0.
|
||||
* @property {Phaser.Rectangle} bounds - Bound of this world that objects can not escape from.
|
||||
*/
|
||||
this.bounds = new Phaser.Rectangle(0, 0, game.width, game.height);
|
||||
@@ -54,7 +58,7 @@ Phaser.World.prototype = {
|
||||
*/
|
||||
boot: function () {
|
||||
|
||||
this.group = new Phaser.Group(this.game, null, '__world', false);
|
||||
this.group = new Phaser.Group(this.game, null, '__world');
|
||||
|
||||
this.camera = new Phaser.Camera(this.game, 0, 0, 0, this.game.width, this.game.height);
|
||||
this.camera.displayObject = this.group;
|
||||
@@ -122,22 +126,16 @@ Phaser.World.prototype = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Updates the size of this world.
|
||||
* Updates the size of this world. Note that this doesn't modify the world x/y coordinates, just the width and height.
|
||||
* If you need to adjust the bounds of the world
|
||||
* @method Phaser.World#setSize
|
||||
* @param {number} width - New width of the world.
|
||||
* @param {number} height - New height of the world.
|
||||
*/
|
||||
setSize: function (width, height) {
|
||||
|
||||
if (width >= this.game.width)
|
||||
{
|
||||
this.bounds.width = width;
|
||||
}
|
||||
|
||||
if (height >= this.game.height)
|
||||
{
|
||||
this.bounds.height = height;
|
||||
}
|
||||
this.bounds.width = width;
|
||||
this.bounds.height = height;
|
||||
|
||||
},
|
||||
|
||||
|
||||
@@ -82,12 +82,6 @@ Phaser.BitmapText = function (game, x, y, text, style) {
|
||||
*/
|
||||
this.scale = new Phaser.Point(1, 1);
|
||||
|
||||
// Influence of camera movement upon the position
|
||||
/**
|
||||
* @property {Phaser.Point} scrollFactor - Description.
|
||||
*/
|
||||
this.scrollFactor = new Phaser.Point(1, 1);
|
||||
|
||||
// A mini cache for storing all of the calculated values
|
||||
/**
|
||||
* @property {function} _cache - Description.
|
||||
@@ -100,7 +94,7 @@ Phaser.BitmapText = function (game, x, y, text, style) {
|
||||
// Transform cache
|
||||
a00: 1, a01: 0, a02: x, a10: 0, a11: 1, a12: y, id: 1,
|
||||
|
||||
// The previous calculated position inc. camera x/y and scrollFactor
|
||||
// The previous calculated position
|
||||
x: -1, y: -1,
|
||||
|
||||
// The actual scale values based on the worldTransform
|
||||
@@ -108,8 +102,8 @@ Phaser.BitmapText = function (game, x, y, text, style) {
|
||||
|
||||
};
|
||||
|
||||
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
this._cache.x = this.x;
|
||||
this._cache.y = this.y;
|
||||
|
||||
/**
|
||||
* @property {boolean} renderable - Description.
|
||||
@@ -136,8 +130,8 @@ Phaser.BitmapText.prototype.update = function() {
|
||||
|
||||
this._cache.dirty = false;
|
||||
|
||||
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
this._cache.x = this.x;
|
||||
this._cache.y = this.y;
|
||||
|
||||
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
|
||||
{
|
||||
|
||||
@@ -1,364 +1,5 @@
|
||||
Phaser.Bullet = function (game, x, y, key, frame) {
|
||||
|
||||
x = x || 0;
|
||||
y = y || 0;
|
||||
key = key || null;
|
||||
frame = frame || null;
|
||||
|
||||
this.game = game;
|
||||
|
||||
// If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all
|
||||
this.exists = true;
|
||||
|
||||
// This is a handy little var your game can use to determine if a sprite is alive or not, it doesn't effect rendering
|
||||
this.alive = true;
|
||||
|
||||
this.group = null;
|
||||
|
||||
this.name = '';
|
||||
|
||||
this.renderOrderID = -1;
|
||||
|
||||
// If you would like the Sprite to have a lifespan once 'born' you can set this to a positive value. Handy for particles, bullets, etc.
|
||||
// The lifespan is decremented by game.time.elapsed each update, once it reaches zero the kill() function is called.
|
||||
this.lifespan = 0;
|
||||
|
||||
this.key = key;
|
||||
|
||||
if (key instanceof Phaser.RenderTexture)
|
||||
{
|
||||
PIXI.Sprite.call(this, key);
|
||||
|
||||
this.currentFrame = this.game.cache.getTextureFrame(key.name);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (key == null || this.game.cache.checkImageKey(key) == false)
|
||||
{
|
||||
key = '__default';
|
||||
}
|
||||
|
||||
PIXI.Sprite.call(this, PIXI.TextureCache[key]);
|
||||
|
||||
if (this.game.cache.isSpriteSheet(key))
|
||||
{
|
||||
if (frame !== null)
|
||||
{
|
||||
if (typeof frame === 'string')
|
||||
{
|
||||
this.currentFrame = this.game.cache.getFrameByName(key, frame);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.currentFrame = this.game.cache.getFrameByIndex(key, frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.currentFrame = this.game.cache.getFrame(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The anchor sets the origin point of the texture.
|
||||
* The default is 0,0 this means the textures origin is the top left
|
||||
* Setting than anchor to 0.5,0.5 means the textures origin is centered
|
||||
* Setting the anchor to 1,1 would mean the textures origin points will be the bottom right
|
||||
*
|
||||
* @property anchor
|
||||
* @type Point
|
||||
*/
|
||||
this.anchor = new Phaser.Point();
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
this.position.x = x;
|
||||
this.position.y = y;
|
||||
|
||||
/**
|
||||
* Should this Sprite be automatically culled if out of range of the camera?
|
||||
* A culled sprite has its visible property set to 'false'.
|
||||
* Note that this check doesn't look at this Sprites children, which may still be in camera range.
|
||||
* So you should set autoCull to false if the Sprite will have children likely to still be in camera range.
|
||||
*
|
||||
* @property autoCull
|
||||
* @type Boolean
|
||||
*/
|
||||
this.autoCull = false;
|
||||
|
||||
// Replaces the PIXI.Point with a slightly more flexible one
|
||||
this.scale = new Phaser.Point(1, 1);
|
||||
|
||||
// Influence of camera movement upon the position
|
||||
this.scrollFactor = new Phaser.Point(1, 1);
|
||||
|
||||
// A mini cache for storing all of the calculated values
|
||||
this._cache = {
|
||||
|
||||
dirty: false,
|
||||
|
||||
// Transform cache
|
||||
a00: 1, a01: 0, a02: x, a10: 0, a11: 1, a12: y, id: 1,
|
||||
|
||||
// Input specific transform cache
|
||||
i01: 0, i10: 0, idi: 1,
|
||||
|
||||
// Bounds check
|
||||
left: null, right: null, top: null, bottom: null,
|
||||
|
||||
// The previous calculated position inc. camera x/y and scrollFactor
|
||||
x: -1, y: -1,
|
||||
|
||||
// The actual scale values based on the worldTransform
|
||||
scaleX: 1, scaleY: 1,
|
||||
|
||||
// The width/height of the image, based on the un-modified frame size multiplied by the final calculated scale size
|
||||
width: this.currentFrame.sourceSizeW, height: this.currentFrame.sourceSizeH,
|
||||
|
||||
// The actual width/height of the image if from a trimmed atlas, multiplied by the final calculated scale size
|
||||
halfWidth: Math.floor(this.currentFrame.sourceSizeW / 2), halfHeight: Math.floor(this.currentFrame.sourceSizeH / 2),
|
||||
|
||||
// The current frame details
|
||||
frameID: this.currentFrame.uuid, frameWidth: this.currentFrame.width, frameHeight: this.currentFrame.height,
|
||||
|
||||
boundsX: 0, boundsY: 0,
|
||||
|
||||
// If this sprite visible to the camera (regardless of being set to visible or not)
|
||||
cameraVisible: true
|
||||
|
||||
};
|
||||
|
||||
this.bounds = new Phaser.Rectangle(x, y, this.currentFrame.sourceSizeW, this.currentFrame.sourceSizeH);
|
||||
|
||||
// Set-up the physics body
|
||||
this.body = new Phaser.Physics.Arcade.Body(this);
|
||||
|
||||
// World bounds check
|
||||
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds);
|
||||
this.inWorldThreshold = 0;
|
||||
this._outOfBoundsFired = false;
|
||||
|
||||
};
|
||||
|
||||
// Needed to keep the PIXI.Sprite constructor in the prototype chain (as the core pixi renderer uses an instanceof check sadly)
|
||||
Phaser.Bullet.prototype = Object.create(PIXI.Sprite.prototype);
|
||||
Phaser.Bullet.prototype.constructor = Phaser.Bullet;
|
||||
|
||||
/**
|
||||
* Automatically called by World.update. You can create your own update in Objects that extend Phaser.Bullet.
|
||||
*/
|
||||
Phaser.Bullet.prototype.preUpdate = function() {
|
||||
|
||||
if (!this.exists)
|
||||
{
|
||||
this.renderOrderID = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.lifespan > 0)
|
||||
{
|
||||
this.lifespan -= this.game.time.elapsed;
|
||||
|
||||
if (this.lifespan <= 0)
|
||||
{
|
||||
this.kill();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// this._cache.dirty = false;
|
||||
|
||||
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
|
||||
// If this sprite or the camera have moved then let's update everything
|
||||
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
|
||||
{
|
||||
this.position.x = this._cache.x;
|
||||
this.position.y = this._cache.y;
|
||||
// this._cache.dirty = true;
|
||||
}
|
||||
|
||||
if (this.visible)
|
||||
{
|
||||
this.renderOrderID = this.game.world.currentRenderOrderID++;
|
||||
|
||||
/*
|
||||
|
||||
// Only update the values we need
|
||||
if (this.worldTransform[0] != this._cache.a00 || this.worldTransform[1] != this._cache.a01)
|
||||
{
|
||||
this._cache.a00 = this.worldTransform[0]; // scaleX a
|
||||
this._cache.a01 = this.worldTransform[1]; // skewY c
|
||||
this._cache.i01 = this.worldTransform[1]; // skewY c
|
||||
this._cache.scaleX = Math.sqrt((this._cache.a00 * this._cache.a00) + (this._cache.a01 * this._cache.a01)); // round this off a bit?
|
||||
this._cache.a01 *= -1;
|
||||
this._cache.dirty = true;
|
||||
}
|
||||
|
||||
// Need to test, but probably highly unlikely that a scaleX would happen without effecting the Y skew
|
||||
if (this.worldTransform[3] != this._cache.a10 || this.worldTransform[4] != this._cache.a11)
|
||||
{
|
||||
this._cache.a10 = this.worldTransform[3]; // skewX b
|
||||
this._cache.i10 = this.worldTransform[3]; // skewX b
|
||||
this._cache.a11 = this.worldTransform[4]; // scaleY d
|
||||
this._cache.scaleY = Math.sqrt((this._cache.a10 * this._cache.a10) + (this._cache.a11 * this._cache.a11)); // round this off a bit?
|
||||
this._cache.a10 *= -1;
|
||||
this._cache.dirty = true;
|
||||
}
|
||||
|
||||
if (this.worldTransform[2] != this._cache.a02 || this.worldTransform[5] != this._cache.a12)
|
||||
{
|
||||
this._cache.a02 = this.worldTransform[2]; // translateX tx
|
||||
this._cache.a12 = this.worldTransform[5]; // translateY ty
|
||||
this._cache.dirty = true;
|
||||
}
|
||||
|
||||
if (this._cache.dirty)
|
||||
{
|
||||
this._cache.width = Math.floor(this.currentFrame.sourceSizeW * this._cache.scaleX);
|
||||
this._cache.height = Math.floor(this.currentFrame.sourceSizeH * this._cache.scaleY);
|
||||
this._cache.halfWidth = Math.floor(this._cache.width / 2);
|
||||
this._cache.halfHeight = Math.floor(this._cache.height / 2);
|
||||
|
||||
this._cache.id = 1 / (this._cache.a00 * this._cache.a11 + this._cache.a01 * -this._cache.a10);
|
||||
this._cache.idi = 1 / (this._cache.a00 * this._cache.a11 + this._cache.i01 * -this._cache.i10);
|
||||
|
||||
this.updateBounds();
|
||||
}
|
||||
*/
|
||||
}
|
||||
else
|
||||
{
|
||||
// We still need to work out the bounds in case the camera has moved
|
||||
// but we can't use the local or worldTransform to do it, as Pixi resets that if a Sprite is invisible.
|
||||
// So we'll compare against the cached state + new position.
|
||||
if (this._cache.dirty && this.visible == false)
|
||||
{
|
||||
this.bounds.x -= this._cache.boundsX - this._cache.x;
|
||||
this._cache.boundsX = this._cache.x;
|
||||
|
||||
this.bounds.y -= this._cache.boundsy - this._cache.y;
|
||||
this._cache.boundsY = this._cache.y;
|
||||
}
|
||||
}
|
||||
|
||||
// Re-run the camera visibility check
|
||||
// if (this._cache.dirty)
|
||||
// {
|
||||
this._cache.cameraVisible = Phaser.Rectangle.intersects(this.game.world.camera.screenView, this.bounds, 0);
|
||||
|
||||
if (this.autoCull == true)
|
||||
{
|
||||
this.visible = this._cache.cameraVisible;
|
||||
}
|
||||
|
||||
// Update our physics bounds
|
||||
this.body.updateBounds(this.center.x, this.center.y, this._cache.scaleX, this._cache.scaleY);
|
||||
// }
|
||||
|
||||
this.body.update();
|
||||
|
||||
}
|
||||
|
||||
Phaser.Bullet.prototype.revive = function() {
|
||||
|
||||
this.alive = true;
|
||||
this.exists = true;
|
||||
this.visible = true;
|
||||
// this.events.onRevived.dispatch(this);
|
||||
|
||||
}
|
||||
|
||||
Phaser.Bullet.prototype.kill = function() {
|
||||
|
||||
this.alive = false;
|
||||
this.exists = false;
|
||||
this.visible = false;
|
||||
// this.events.onKilled.dispatch(this);
|
||||
|
||||
}
|
||||
|
||||
Phaser.Bullet.prototype.reset = function(x, y) {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.position.x = x;
|
||||
this.position.y = y;
|
||||
this.alive = true;
|
||||
this.exists = true;
|
||||
this.visible = true;
|
||||
this._outOfBoundsFired = false;
|
||||
this.body.reset();
|
||||
|
||||
}
|
||||
|
||||
Phaser.Bullet.prototype.updateBounds = function() {
|
||||
|
||||
// Update the edge points
|
||||
|
||||
// this.bounds.setTo(this._cache.left, this._cache.top, this._cache.right - this._cache.left, this._cache.bottom - this._cache.top);
|
||||
|
||||
if (this.inWorld == false)
|
||||
{
|
||||
// Sprite WAS out of the screen, is it still?
|
||||
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, this.inWorldThreshold);
|
||||
|
||||
if (this.inWorld)
|
||||
{
|
||||
// It's back again, reset the OOB check
|
||||
this._outOfBoundsFired = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Sprite WAS in the screen, has it now left?
|
||||
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, this.inWorldThreshold);
|
||||
|
||||
if (this.inWorld == false)
|
||||
{
|
||||
this.events.onOutOfBounds.dispatch(this);
|
||||
this._outOfBoundsFired = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Phaser.Bullet.prototype.bringToTop = function() {
|
||||
|
||||
if (this.group)
|
||||
{
|
||||
this.group.bringToTop(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.game.world.bringToTop(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperty(Phaser.Bullet.prototype, 'angle', {
|
||||
|
||||
get: function() {
|
||||
return Phaser.Math.radToDeg(this.rotation);
|
||||
},
|
||||
|
||||
set: function(value) {
|
||||
this.rotation = Phaser.Math.degToRad(value);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Object.defineProperty(Phaser.Bullet.prototype, "inCamera", {
|
||||
|
||||
/**
|
||||
* Is this sprite visible to the camera or not?
|
||||
*/
|
||||
get: function () {
|
||||
return this._cache.cameraVisible;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
+28
-55
@@ -184,11 +184,6 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
*/
|
||||
this.scale = new Phaser.Point(1, 1);
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} scrollFactor - Influence of camera movement upon the position.
|
||||
*/
|
||||
this.scrollFactor = new Phaser.Point(1, 1);
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} _cache - A mini cache for storing all of the calculated values.
|
||||
* @private
|
||||
@@ -206,7 +201,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
// Bounds check
|
||||
left: null, right: null, top: null, bottom: null,
|
||||
|
||||
// The previous calculated position inc. camera x/y and scrollFactor
|
||||
// The previous calculated position
|
||||
x: -1, y: -1,
|
||||
|
||||
// The actual scale values based on the worldTransform
|
||||
@@ -345,24 +340,21 @@ Phaser.Sprite.prototype.preUpdate = function() {
|
||||
// |0 0 1|
|
||||
|
||||
// Only update the values we need
|
||||
if (this.worldTransform[0] != this._cache.a00 || this.worldTransform[1] != this._cache.a01)
|
||||
if (this.worldTransform[0] != this._cache.a00 || this.worldTransform[1] != this._cache.a01 || this.worldTransform[3] != this._cache.a10 || this.worldTransform[4] != this._cache.a11)
|
||||
{
|
||||
this._cache.a00 = this.worldTransform[0]; // scaleX a
|
||||
this._cache.a01 = this.worldTransform[1]; // skewY c
|
||||
this._cache.i01 = this.worldTransform[1]; // skewY c
|
||||
this._cache.scaleX = Math.sqrt((this._cache.a00 * this._cache.a00) + (this._cache.a01 * this._cache.a01)); // round this off a bit?
|
||||
this._cache.a01 *= -1;
|
||||
this._cache.dirty = true;
|
||||
}
|
||||
|
||||
// Need to test, but probably highly unlikely that a scaleX would happen without effecting the Y skew
|
||||
if (this.worldTransform[3] != this._cache.a10 || this.worldTransform[4] != this._cache.a11)
|
||||
{
|
||||
this._cache.a10 = this.worldTransform[3]; // skewX b
|
||||
this._cache.i10 = this.worldTransform[3]; // skewX b
|
||||
this._cache.a11 = this.worldTransform[4]; // scaleY d
|
||||
|
||||
this._cache.scaleX = Math.sqrt((this._cache.a00 * this._cache.a00) + (this._cache.a01 * this._cache.a01)); // round this off a bit?
|
||||
this._cache.scaleY = Math.sqrt((this._cache.a10 * this._cache.a10) + (this._cache.a11 * this._cache.a11)); // round this off a bit?
|
||||
|
||||
this._cache.a01 *= -1;
|
||||
this._cache.a10 *= -1;
|
||||
|
||||
this._cache.dirty = true;
|
||||
}
|
||||
|
||||
@@ -410,7 +402,10 @@ Phaser.Sprite.prototype.preUpdate = function() {
|
||||
this.body.updateBounds(this.center.x, this.center.y, this._cache.scaleX, this._cache.scaleY);
|
||||
}
|
||||
|
||||
this.body.preUpdate();
|
||||
if (this.body)
|
||||
{
|
||||
this.body.preUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -419,16 +414,19 @@ Phaser.Sprite.prototype.postUpdate = function() {
|
||||
if (this.exists)
|
||||
{
|
||||
// The sprite is positioned in this call, after taking into consideration motion updates and collision
|
||||
// this.body.postUpdate();
|
||||
if (this.body)
|
||||
{
|
||||
this.body.postUpdate();
|
||||
}
|
||||
|
||||
// this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
// this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
this._cache.x = this.x;
|
||||
this._cache.y = this.y;
|
||||
|
||||
// if (this.position.x != this._cache.x || this.position.y != this._cache.y)
|
||||
// {
|
||||
// this.position.x = this._cache.x;
|
||||
// this.position.y = this._cache.y;
|
||||
// }
|
||||
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
|
||||
{
|
||||
this.position.x = this._cache.x;
|
||||
this.position.y = this._cache.y;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -501,8 +499,8 @@ Phaser.Sprite.prototype.reset = function(x, y) {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.position.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
this.position.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
this.position.x = this.x;
|
||||
this.position.y = this.y;
|
||||
this.alive = true;
|
||||
this.exists = true;
|
||||
this.visible = true;
|
||||
@@ -618,31 +616,6 @@ Phaser.Sprite.prototype.bringToTop = function() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Description.
|
||||
*
|
||||
* @method Phaser.Sprite.prototype.bringToTop
|
||||
* @param {Phaser.Rectangle} rect - Description.
|
||||
* @return {Phaser.Rectangle} Description.
|
||||
*/
|
||||
Phaser.Sprite.prototype.getBounds = function(rect) {
|
||||
|
||||
rect = rect || new Phaser.Rectangle;
|
||||
|
||||
var left = Phaser.Math.min(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x);
|
||||
var right = Phaser.Math.max(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x);
|
||||
var top = Phaser.Math.min(this.topLeft.y, this.topRight.y, this.bottomLeft.y, this.bottomRight.y);
|
||||
var bottom = Phaser.Math.max(this.topLeft.y, this.topRight.y, this.bottomLeft.y, this.bottomRight.y);
|
||||
|
||||
rect.x = left;
|
||||
rect.y = top;
|
||||
rect.width = right - left;
|
||||
rect.height = bottom - top;
|
||||
|
||||
return rect;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Play an animation based on the given key. The animation should previously have been added via sprite.animations.add()
|
||||
* If the requested animation is already playing this request will be ignored. If you need to reset an already running animation do so directly on the Animation object itself.
|
||||
@@ -748,10 +721,10 @@ Object.defineProperty(Phaser.Sprite.prototype, "crop", {
|
||||
this._cropUUID = this.game.rnd.uuid();
|
||||
|
||||
PIXI.TextureCache[this._cropUUID] = new PIXI.Texture(PIXI.BaseTextureCache[this.key], {
|
||||
x: value.x,
|
||||
y: value.y,
|
||||
width: value.width,
|
||||
height: value.height
|
||||
x: Math.floor(value.x),
|
||||
y: Math.floor(value.y),
|
||||
width: Math.floor(value.width),
|
||||
height: Math.floor(value.height)
|
||||
});
|
||||
}
|
||||
else
|
||||
|
||||
+5
-11
@@ -81,12 +81,6 @@ Phaser.Text = function (game, x, y, text, style) {
|
||||
*/
|
||||
this.scale = new Phaser.Point(1, 1);
|
||||
|
||||
// Influence of camera movement upon the position
|
||||
/**
|
||||
* @property {Phaser.Point} scrollFactor - Description.
|
||||
*/
|
||||
this.scrollFactor = new Phaser.Point(1, 1);
|
||||
|
||||
// A mini cache for storing all of the calculated values
|
||||
/**
|
||||
* @property {Description} _cache - Description.
|
||||
@@ -99,7 +93,7 @@ Phaser.Text = function (game, x, y, text, style) {
|
||||
// Transform cache
|
||||
a00: 1, a01: 0, a02: x, a10: 0, a11: 1, a12: y, id: 1,
|
||||
|
||||
// The previous calculated position inc. camera x/y and scrollFactor
|
||||
// The previous calculated position
|
||||
x: -1, y: -1,
|
||||
|
||||
// The actual scale values based on the worldTransform
|
||||
@@ -107,8 +101,8 @@ Phaser.Text = function (game, x, y, text, style) {
|
||||
|
||||
};
|
||||
|
||||
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
this._cache.x = this.x;
|
||||
this._cache.y = this.y;
|
||||
|
||||
/**
|
||||
* @property {boolean} renderable - Description.
|
||||
@@ -134,8 +128,8 @@ Phaser.Text.prototype.update = function() {
|
||||
|
||||
this._cache.dirty = false;
|
||||
|
||||
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
this._cache.x = this.x;
|
||||
this._cache.y = this.y;
|
||||
|
||||
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
|
||||
{
|
||||
|
||||
@@ -45,22 +45,25 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
updateMotion: function (body) {
|
||||
|
||||
// If you're wondering why the velocity is halved and applied twice, read this: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
|
||||
|
||||
// Rotation
|
||||
this._velocityDelta = (this.computeVelocity(0, false, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity) / 2;
|
||||
body.angularVelocity += this._velocityDelta;
|
||||
body.rotation += body.angularVelocity * this.game.time.physicsElapsed;
|
||||
body.angularVelocity += this._velocityDelta;
|
||||
|
||||
// Horizontal
|
||||
this._velocityDelta = (this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x) - body.velocity.x) / 2;
|
||||
body.velocity.x += this._velocityDelta;
|
||||
this._delta = body.velocity.x * this.game.time.physicsElapsed;
|
||||
body.x += this._delta;
|
||||
body.x += (body.velocity.x * this.game.time.physicsElapsed);
|
||||
body.velocity.x += this._velocityDelta;
|
||||
|
||||
// Vertical
|
||||
this._velocityDelta = (this.computeVelocity(2, body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y) - body.velocity.y) / 2;
|
||||
body.velocity.y += this._velocityDelta;
|
||||
this._delta = body.velocity.y * this.game.time.physicsElapsed;
|
||||
body.y += this._delta;
|
||||
body.y += (body.velocity.y * this.game.time.physicsElapsed);
|
||||
body.velocity.y += this._velocityDelta;
|
||||
|
||||
},
|
||||
|
||||
@@ -109,16 +112,13 @@ Phaser.Physics.Arcade.prototype = {
|
||||
}
|
||||
}
|
||||
|
||||
if (velocity != 0)
|
||||
if (velocity > max)
|
||||
{
|
||||
if (velocity > max)
|
||||
{
|
||||
velocity = max;
|
||||
}
|
||||
else if (velocity < -max)
|
||||
{
|
||||
velocity = -max;
|
||||
}
|
||||
velocity = max;
|
||||
}
|
||||
else if (velocity < -max)
|
||||
{
|
||||
velocity = -max;
|
||||
}
|
||||
|
||||
return velocity;
|
||||
|
||||
Reference in New Issue
Block a user