Lots of Tilemap updates, moved the renderer out, added components and new tests.

This commit is contained in:
Richard Davey
2013-07-02 23:41:25 +01:00
parent c647792c12
commit c81cf0c882
34 changed files with 2395 additions and 1563 deletions
+7 -1
View File
@@ -79,7 +79,7 @@ module Phaser.Components {
public cameraBlacklist: number[];
/**
* Whether the Sprite background is opaque or not. If set to true the Sprite is filled with
* Whether the texture background is opaque or not. If set to true the object is filled with
* the value of Texture.backgroundColor every frame. Normally you wouldn't enable this but
* for some effects it can be handy.
* @type {boolean}
@@ -136,6 +136,12 @@ module Phaser.Components {
*/
public renderRotation: bool = true;
/**
* The direction the animation frame is facing (can be Phaser.Types.RIGHT, LEFT, UP, DOWN).
* Very useful when hooking animation to Sprite directions.
*/
public facing: number;
/**
* Flip the graphic horizontally (defaults to false)
* @type {boolean}
+68 -193
View File
@@ -26,53 +26,36 @@ module Phaser {
*/
constructor(game: Game, parent:Tilemap, key: string, mapFormat: number, name: string, tileWidth: number, tileHeight: number) {
this._game = game;
this._parent = parent;
this.game = game;
this.parent = parent;
this.name = name;
this.mapFormat = mapFormat;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.boundsInTiles = new Rectangle();
//this.scrollFactor = new MicroPoint(1, 1);
this.canvas = game.stage.canvas;
this.context = game.stage.context;
this.texture = new Phaser.Components.Texture(this);
this.transform = new Phaser.Components.Transform(this);
if (key !== null)
{
this.texture.loadImage(key, false);
}
else
{
this.texture.opaque = true;
}
// Handy proxies
this.alpha = this.texture.alpha;
this.mapData = [];
this._tempTileBlock = [];
this._texture = this._game.cache.getImage(key);
}
/**
* Local private reference to game.
*/
private _game: Game;
/**
* The tilemap that contains this layer.
* @type {Tilemap}
*/
private _parent: Tilemap;
/**
* Tileset of this layer.
*/
private _texture;
private _tileOffsets;
private _startX: number = 0;
private _startY: number = 0;
private _maxX: number = 0;
private _maxY: number = 0;
private _tx: number = 0;
private _ty: number = 0;
private _dx: number = 0;
private _dy: number = 0;
private _oldCameraX: number = 0;
private _oldCameraY: number = 0;
private _columnData;
// Private vars to help avoid gc spikes
private _tempTileX: number;
private _tempTileY: number;
private _tempTileW: number;
@@ -80,30 +63,40 @@ module Phaser {
private _tempTileBlock;
private _tempBlockResults;
/**
* Local private reference to game.
*/
public game: Game;
/**
* The tilemap that contains this layer.
* @type {Tilemap}
*/
public parent: Tilemap;
/**
* The texture used to render the Sprite.
*/
public texture: Phaser.Components.Texture;
/**
* The Sprite transform component.
*/
public transform: Phaser.Components.Transform;
public tileOffsets;
/**
* The alpha of the Sprite between 0 and 1, a value of 1 being fully opaque.
*/
public alpha: number;
/**
* Name of this layer, so you can get this layer by its name.
* @type {string}
*/
public name: string;
/**
* A reference to the Canvas this GameObject will render to
* @type {HTMLCanvasElement}
*/
public canvas: HTMLCanvasElement;
/**
* A reference to the Canvas Context2D this GameObject will render to
* @type {CanvasRenderingContext2D}
*/
public context: CanvasRenderingContext2D;
/**
* Opacity of this layer.
* @type {number}
*/
public alpha: number = 1;
/**
* Controls whether update() and draw() are automatically called.
* @type {boolean}
@@ -116,11 +109,10 @@ module Phaser {
*/
public visible: bool = true;
//public scrollFactor: MicroPoint;
/**
* @type {string}
*/
public orientation: string;
//public orientation: string;
/**
* Properties of this map layer. (normally set by map editors)
@@ -202,8 +194,8 @@ module Phaser {
*/
public putTile(x: number, y: number, index: number) {
x = this._game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
y = this._game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
x = this.game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
y = this.game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
if (y >= 0 && y < this.mapData.length)
{
@@ -287,7 +279,7 @@ module Phaser {
for (var r = 0; r < this._tempTileBlock.length; r++)
{
this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = this._game.math.getRandom(tiles);
this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = this.game.math.getRandom(tiles);
}
}
@@ -344,8 +336,8 @@ module Phaser {
*/
public getTileFromWorldXY(x: number, y: number): number {
x = this._game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
y = this._game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
x = this.game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
y = this.game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
return this.getTileIndex(x, y);
@@ -365,10 +357,10 @@ module Phaser {
}
// What tiles do we need to check against?
this._tempTileX = this._game.math.snapToFloor(object.body.bounds.x, this.tileWidth) / this.tileWidth;
this._tempTileY = this._game.math.snapToFloor(object.body.bounds.y, this.tileHeight) / this.tileHeight;
this._tempTileW = (this._game.math.snapToCeil(object.body.bounds.width, this.tileWidth) + this.tileWidth) / this.tileWidth;
this._tempTileH = (this._game.math.snapToCeil(object.body.bounds.height, this.tileHeight) + this.tileHeight) / this.tileHeight;
this._tempTileX = this.game.math.snapToFloor(object.body.bounds.x, this.tileWidth) / this.tileWidth;
this._tempTileY = this.game.math.snapToFloor(object.body.bounds.y, this.tileHeight) / this.tileHeight;
this._tempTileW = (this.game.math.snapToCeil(object.body.bounds.width, this.tileWidth) + this.tileWidth) / this.tileWidth;
this._tempTileH = (this.game.math.snapToCeil(object.body.bounds.height, this.tileHeight) + this.tileHeight) / this.tileHeight;
// Loop through the tiles we've got and check overlaps accordingly (the results are stored in this._tempTileBlock)
@@ -378,7 +370,7 @@ module Phaser {
/*
for (var r = 0; r < this._tempTileBlock.length; r++)
{
if (this._game.world.physics.separateTile(object, this._tempTileBlock[r].x * this.tileWidth, this._tempTileBlock[r].y * this.tileHeight, this.tileWidth, this.tileHeight, this._tempTileBlock[r].tile.mass, this._tempTileBlock[r].tile.collideLeft, this._tempTileBlock[r].tile.collideRight, this._tempTileBlock[r].tile.collideUp, this._tempTileBlock[r].tile.collideDown, this._tempTileBlock[r].tile.separateX, this._tempTileBlock[r].tile.separateY) == true)
if (this.game.world.physics.separateTile(object, this._tempTileBlock[r].x * this.tileWidth, this._tempTileBlock[r].y * this.tileHeight, this.tileWidth, this.tileHeight, this._tempTileBlock[r].tile.mass, this._tempTileBlock[r].tile.collideLeft, this._tempTileBlock[r].tile.collideRight, this._tempTileBlock[r].tile.collideUp, this._tempTileBlock[r].tile.collideDown, this._tempTileBlock[r].tile.separateX, this._tempTileBlock[r].tile.separateY) == true)
{
this._tempBlockResults.push({ x: this._tempTileBlock[r].x, y: this._tempTileBlock[r].y, tile: this._tempTileBlock[r].tile });
}
@@ -428,16 +420,16 @@ module Phaser {
if (collisionOnly)
{
// We only want to consider the tile for checking if you can actually collide with it
if (this.mapData[ty] && this.mapData[ty][tx] && this._parent.tiles[this.mapData[ty][tx]].allowCollisions != Types.NONE)
if (this.mapData[ty] && this.mapData[ty][tx] && this.parent.tiles[this.mapData[ty][tx]].allowCollisions != Types.NONE)
{
this._tempTileBlock.push({ x: tx, y: ty, tile: this._parent.tiles[this.mapData[ty][tx]] });
this._tempTileBlock.push({ x: tx, y: ty, tile: this.parent.tiles[this.mapData[ty][tx]] });
}
}
else
{
if (this.mapData[ty] && this.mapData[ty][tx])
{
this._tempTileBlock.push({ x: tx, y: ty, tile: this._parent.tiles[this.mapData[ty][tx]] });
this._tempTileBlock.push({ x: tx, y: ty, tile: this.parent.tiles[this.mapData[ty][tx]] });
}
}
}
@@ -502,34 +494,35 @@ module Phaser {
/**
* Parse tile offsets from map data.
* @return {number} length of _tileOffsets array.
* @return {number} length of tileOffsets array.
*/
public parseTileOffsets():number {
this._tileOffsets = [];
this.tileOffsets = [];
var i = 0;
if (this.mapFormat == Tilemap.FORMAT_TILED_JSON)
{
// For some reason Tiled counts from 1 not 0
this._tileOffsets[0] = null;
this.tileOffsets[0] = null;
i = 1;
}
for (var ty = this.tileMargin; ty < this._texture.height; ty += (this.tileHeight + this.tileSpacing))
for (var ty = this.tileMargin; ty < this.texture.height; ty += (this.tileHeight + this.tileSpacing))
{
for (var tx = this.tileMargin; tx < this._texture.width; tx += (this.tileWidth + this.tileSpacing))
for (var tx = this.tileMargin; tx < this.texture.width; tx += (this.tileWidth + this.tileSpacing))
{
this._tileOffsets[i] = { x: tx, y: ty };
this.tileOffsets[i] = { x: tx, y: ty };
i++;
}
}
return this._tileOffsets.length;
return this.tileOffsets.length;
}
/*
public renderDebugInfo(x: number, y: number, color?: string = 'rgb(255,255,255)') {
this.context.fillStyle = color;
@@ -539,125 +532,7 @@ module Phaser {
this.context.fillText('dx: ' + this._dx + ' dy: ' + this._dy, x, y + 42);
}
/**
* Render this layer to a specific camera with offset to camera.
* @param camera {Camera} The camera the layer is going to be rendered.
* @param dx {number} X offset to the camera.
* @param dy {number} Y offset to the camera.
* @return {boolean} Return false if layer is invisible or has a too low opacity(will stop rendering), return true if succeed.
*/
public render(camera: Camera, dx, dy): bool {
if (this.visible === false || this.alpha < 0.1)
{
return false;
}
// Work out how many tiles we can fit into our camera and round it up for the edges
this._maxX = this._game.math.ceil(camera.width / this.tileWidth) + 1;
this._maxY = this._game.math.ceil(camera.height / this.tileHeight) + 1;
// And now work out where in the tilemap the camera actually is
this._startX = this._game.math.floor(camera.worldView.x / this.tileWidth);
this._startY = this._game.math.floor(camera.worldView.y / this.tileHeight);
// Tilemap bounds check
if (this._startX < 0)
{
this._startX = 0;
}
if (this._startY < 0)
{
this._startY = 0;
}
if (this._maxX > this.widthInTiles)
{
this._maxX = this.widthInTiles;
}
if (this._maxY > this.heightInTiles)
{
this._maxY = this.heightInTiles;
}
if (this._startX + this._maxX > this.widthInTiles)
{
this._startX = this.widthInTiles - this._maxX;
}
if (this._startY + this._maxY > this.heightInTiles)
{
this._startY = this.heightInTiles - this._maxY;
}
// Finally get the offset to avoid the blocky movement
this._dx = dx;
this._dy = dy;
this._dx += -(camera.worldView.x - (this._startX * this.tileWidth));
this._dy += -(camera.worldView.y - (this._startY * this.tileHeight));
this._tx = this._dx;
this._ty = this._dy;
// Apply camera difference
/*
if (this.scrollFactor.x !== 1.0 || this.scrollFactor.y !== 1.0)
{
this._dx -= (camera.worldView.x * this.scrollFactor.x);
this._dy -= (camera.worldView.y * this.scrollFactor.y);
}
*/
// Alpha
if (this.alpha !== 1)
{
var globalAlpha = this.context.globalAlpha;
this.context.globalAlpha = this.alpha;
}
for (var row = this._startY; row < this._startY + this._maxY; row++)
{
this._columnData = this.mapData[row];
for (var tile = this._startX; tile < this._startX + this._maxX; tile++)
{
if (this._tileOffsets[this._columnData[tile]])
{
this.context.drawImage(
this._texture, // Source Image
this._tileOffsets[this._columnData[tile]].x, // Source X (location within the source image)
this._tileOffsets[this._columnData[tile]].y, // Source Y
this.tileWidth, // Source Width
this.tileHeight, // Source Height
this._tx, // Destination X (where on the canvas it'll be drawn)
this._ty, // Destination Y
this.tileWidth, // Destination Width (always same as Source Width unless scaled)
this.tileHeight // Destination Height (always same as Source Height unless scaled)
);
}
this._tx += this.tileWidth;
}
this._tx = this._dx;
this._ty += this.tileHeight;
}
if (globalAlpha > -1)
{
this.context.globalAlpha = globalAlpha;
}
return true;
}
*/
}
}
+12
View File
@@ -24,10 +24,13 @@ module Phaser.Components.Sprite {
this.onKilled = new Phaser.Signal;
this.onRevived = new Phaser.Signal;
// Only create these if Sprite input is enabled?
this.onInputOver = new Phaser.Signal;
this.onInputOut = new Phaser.Signal;
this.onInputDown = new Phaser.Signal;
this.onInputUp = new Phaser.Signal;
this.onDragStart = new Phaser.Signal;
this.onDragStop = new Phaser.Signal;
}
@@ -81,6 +84,15 @@ module Phaser.Components.Sprite {
*/
public onInputUp: Phaser.Signal;
/**
* Dispatched by the Input component when the Sprite starts being dragged
*/
public onDragStart: Phaser.Signal;
/**
* Dispatched by the Input component when the Sprite stops being dragged
*/
public onDragStop: Phaser.Signal;
+3 -1
View File
@@ -595,6 +595,8 @@ module Phaser.Components.Sprite {
this.sprite.group.bringToTop(this.sprite);
}
this.sprite.events.onDragStart.dispatch(this.sprite, pointer);
}
/**
@@ -612,7 +614,7 @@ module Phaser.Components.Sprite {
this.sprite.y = Math.floor(this.sprite.y / this.snapY) * this.snapY;
}
//pointer.draggedObject = null;
this.sprite.events.onDragStop.dispatch(this.sprite, pointer);
}
/**
+45 -1
View File
@@ -618,17 +618,58 @@ module Phaser {
public bringToTop(child): bool {
//console.log('bringToTop', child.name,'current z', child.z);
var oldZ = child.z;
// If child not in this group, or is already at the top of the group, return false
if (!child || child.group == null || child.group.ID != this.ID || child.z == this._zCounter)
//if (!child || child.group == null || child.group.ID != this.ID || child.z == this._zCounter)
if (!child || child.group == null || child.group.ID != this.ID)
{
//console.log('If child not in this group, or is already at the top of the group, return false');
return false;
}
// Find out the largest z index
var topZ: number = -1;
for (var i = 0; i < this.length; i++)
{
if (this.members[i] && this.members[i].z > topZ)
{
topZ = this.members[i].z;
}
}
// Child is already at the top
if (child.z == topZ)
{
return false;
}
child.z = topZ + 1;
// Sort them out based on the current z indexes
this.sort();
// Now tidy-up the z indexes, removing gaps, etc
for (var i = 0; i < this.length; i++)
{
if (this.members[i])
{
this.members[i].z = i;
}
}
//console.log('bringToTop', child.name, 'old z', oldZ, 'new z', child.z);
return true;
// What's the z index of the top most child?
/*
var childIndex: number = this._zCounter;
console.log('childIndex', childIndex);
this._i = 0;
while (this._i < this.length)
@@ -649,10 +690,13 @@ module Phaser {
}
}
console.log('child inserted at index', child.z);
// Maybe redundant?
this.sort();
return true;
*/
}
+5
View File
@@ -19,6 +19,11 @@ module Phaser {
*/
group: Group;
/**
* The name of the Game Object. Typically not set by Phaser, but extremely useful for debugging / logic.
*/
name: string;
/**
* x value of the object.
*/
+15 -2
View File
@@ -23,7 +23,7 @@ module Phaser {
* @param [x] {number} the initial x position of the sprite.
* @param [y] {number} the initial y position of the sprite.
* @param [key] {string} Key of the graphic you want to load for this sprite.
* @param [bodyType] {number} The physics body type of the object (defaults to BODY_DISABLED)
* @param [bodyType] {number} The physics body type of the object (defaults to BODY_DISABLED, i.e. no physics)
* @param [shapeType] {number} The physics shape the body will consist of (either Box (0) or Circle (1), for custom types see body.addShape)
*/
constructor(game: Game, x?: number = 0, y?: number = 0, key?: string = null, frame? = null, bodyType?: number = Phaser.Types.BODY_DISABLED, shapeType?:number = 0) {
@@ -40,6 +40,7 @@ module Phaser {
this.y = y;
this.z = -1;
this.group = null;
this.name = '';
this.animations = new Phaser.Components.AnimationManager(this);
this.input = new Phaser.Components.Sprite.Input(this);
@@ -97,6 +98,11 @@ module Phaser {
*/
public type: number;
/**
* The name of game object.
*/
public name: string;
/**
* The Group this Sprite belongs to.
*/
@@ -183,7 +189,7 @@ module Phaser {
/**
* z order value of the object.
*/
public z: number = 0;
public z: number = -1;
/**
* Render iteration counter
@@ -202,7 +208,14 @@ module Phaser {
* The value is automatically wrapped to be between 0 and 360.
*/
public set rotation(value: number) {
this.transform.rotation = this.game.math.wrap(value, 360, 0);
if (this.body)
{
this.body.angle = this.game.math.degreesToRadians(this.game.math.wrap(value, 360, 0));
}
}
/**
+59 -27
View File
@@ -35,9 +35,15 @@ module Phaser {
this.visible = true;
this.alive = true;
this.z = -1;
this.group = null;
this.name = '';
this.texture = new Phaser.Components.Texture(this);
this.transform = new Phaser.Components.Transform(this);
this.tiles = [];
this.layers = [];
this.cameraBlacklist = [];
this.mapFormat = format;
@@ -71,6 +77,16 @@ module Phaser {
*/
public type: number;
/**
* The name of game object.
*/
public name: string;
/**
* The Group this Sprite belongs to.
*/
public group: Group;
/**
* Controls if both <code>update</code> and render are called by the core game loop.
*/
@@ -87,10 +103,44 @@ module Phaser {
public visible: bool;
/**
*
* A useful state for many game objects. Kill and revive both flip this switch.
*/
public alive: bool;
/**
* The texture used to render the Sprite.
*/
public texture: Phaser.Components.Texture;
/**
* The Sprite transform component.
*/
public transform: Phaser.Components.Transform;
/**
* The Input component
*/
//public input: Phaser.Components.Sprite.Input;
/**
* The Events component
*/
//public events: Phaser.Components.Sprite.Events;
/**
* z order value of the object.
*/
public z: number = -1;
/**
* Render iteration counter
*/
public renderOrderID: number = 0;
/**
* Tilemap data format enum: CSV.
* @type {number}
@@ -145,34 +195,15 @@ module Phaser {
public mapFormat: number;
/**
* An Array of Cameras to which this GameObject won't render
* @type {Array}
* Inherited methods for overriding.
*/
public cameraBlacklist: number[];
/**
* Inherited update method.
*/
public update() {
public preUpdate() {
}
/**
* Render this tilemap to a specific camera with specific offset.
* @param camera {Camera} The camera this tilemap will be rendered to.
* @param cameraOffsetX {number} X offset of the camera.
* @param cameraOffsetY {number} Y offset of the camera.
*/
public render(camera: Camera, cameraOffsetX: number, cameraOffsetY: number) {
if (this.cameraBlacklist.indexOf(camera.ID) == -1)
{
// Loop through the layers
for (var i = 0; i < this.layers.length; i++)
{
this.layers[i].render(camera, cameraOffsetX, cameraOffsetY);
}
}
public update() {
}
public postUpdate() {
}
/**
@@ -202,6 +233,7 @@ module Phaser {
}
layer.updateBounds();
var tileQuantity = layer.parseTileOffsets();
this.currentLayer = layer;
+16
View File
@@ -1,3 +1,19 @@
/**
* Phaser
*
* v1.0.0 - June XX 2013
*
* A small and feature-packed 2D canvas game framework born from the firey pits of Flixel and Kiwi.
*
* Richard Davey (@photonstorm)
*
* Many thanks to Adam Saltsman (@ADAMATOMIC) for releasing Flixel, from both which Phaser
* and my love of game development took a lot of inspiration.
*
* "If you want your children to be intelligent, read them fairy tales."
* "If you want them to be more intelligent, read them more fairy tales."
* -- Albert Einstein
*/
var Phaser;
(function (Phaser) {
Phaser.VERSION = 'Phaser version 1.0.0';
+63 -50
View File
@@ -35,7 +35,7 @@ module Phaser.Physics {
this.sprite = sprite;
this.game = sprite.game;
this.position = new Phaser.Vec2(Phaser.Physics.Manager.pixelsToMeters(sprite.x), Phaser.Physics.Manager.pixelsToMeters(sprite.y));
this.angle = sprite.rotation;
this.angle = this.game.math.degreesToRadians(sprite.rotation);
}
else
{
@@ -61,7 +61,6 @@ module Phaser.Physics {
this.bounds = new Bounds;
this.allowCollisions = Phaser.Types.ANY;
this.fixedRotation = false;
this.categoryBits = 0x0001;
this.maskBits = 0xFFFF;
@@ -82,11 +81,8 @@ module Phaser.Physics {
}
public toString(): string {
return "[{Body (name=" + this.name + " velocity=" + this.velocity.toString() + " angularVelocity: " + this.angularVelocity + ")}]";
}
private _tempVec2: Phaser.Vec2 = new Phaser.Vec2;
private _fixedRotation: bool = false;
/**
* Reference to Phaser.Game
@@ -118,8 +114,26 @@ module Phaser.Physics {
*/
public type: number;
/**
* The angle of the body in radians. Used by all of the internal physics methods.
*/
public angle: number;
/**
* The rotation of the body in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
*/
public get rotation(): number {
return this.game.math.radiansToDegrees(this.angle);
}
/**
* Set the rotation of the body in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
* The value is automatically wrapped to be between 0 and 360.
*/
public set rotation(value: number) {
this.angle = this.game.math.degreesToRadians(this.game.math.wrap(value, 360, 0));
}
// Local to world transform
public transform: Phaser.Transform;
@@ -174,17 +188,16 @@ module Phaser.Physics {
public inertia: number;
public inertiaInverted: number;
public fixedRotation = false;
public categoryBits = 0x0001;
public maskBits = 0xFFFF;
public stepCount = 0;
public categoryBits: number = 0x0001;
public maskBits: number = 0xFFFF;
public stepCount: number = 0;
public space: Space;
public duplicate() {
console.log('body duplicate called');
//var body = new Body(this.type, this.transform.t, this.angle);
//var body = new Body(this.type, this.transform.t, this.rotation);
//for (var i = 0; i < this.shapes.length; i++)
//{
@@ -313,28 +326,28 @@ module Phaser.Physics {
}
private setMass(mass) {
private setMass(mass:number) {
this.mass = mass;
this.massInverted = mass > 0 ? 1 / mass : 0;
}
private setInertia(inertia) {
private setInertia(inertia:number) {
this.inertia = inertia;
this.inertiaInverted = inertia > 0 ? 1 / inertia : 0;
}
public setTransform(pos, angle) {
public setTransform(pos:Phaser.Vec2, angle:number) {
// inject the transform into this.position
this.transform.setTo(pos, angle);
Manager.write('setTransform: ' + this.position.toString());
Manager.write('centroid: ' + this.centroid.toString());
//Manager.write('setTransform: ' + this.position.toString());
//Manager.write('centroid: ' + this.centroid.toString());
Phaser.TransformUtils.transform(this.transform, this.centroid, this.position);
Manager.write('post setTransform: ' + this.position.toString());
//Manager.write('post setTransform: ' + this.position.toString());
//this.position.copyFrom(this.transform.transform(this.centroid));
this.angle = angle;
@@ -342,17 +355,17 @@ module Phaser.Physics {
public syncTransform() {
Manager.write('syncTransform:');
Manager.write('p: ' + this.position.toString());
Manager.write('centroid: ' + this.centroid.toString());
Manager.write('xf: ' + this.transform.toString());
Manager.write('a: ' + this.angle);
//Manager.write('syncTransform:');
//Manager.write('p: ' + this.position.toString());
//Manager.write('centroid: ' + this.centroid.toString());
//Manager.write('xf: ' + this.transform.toString());
//Manager.write('a: ' + this.angle);
this.transform.setRotation(this.angle);
// OPTIMISE: Creating new vector
Phaser.Vec2Utils.subtract(this.position, Phaser.TransformUtils.rotate(this.transform, this.centroid), this.transform.t);
Manager.write('--------------------');
Manager.write('xf: ' + this.transform.toString());
Manager.write('--------------------');
//Manager.write('--------------------');
//Manager.write('xf: ' + this.transform.toString());
//Manager.write('--------------------');
}
@@ -376,9 +389,16 @@ module Phaser.Physics {
return Phaser.TransformUtils.unrotate(this.transform, v);
}
public setFixedRotation(flag) {
this.fixedRotation = flag;
public set fixedRotation(value:bool) {
this._fixedRotation = value;
this.resetMassData();
}
public get fixedRotation(): bool {
return this._fixedRotation;
}
public resetMassData() {
@@ -392,7 +412,6 @@ module Phaser.Physics {
if (this.isDynamic == false)
{
Phaser.TransformUtils.transform(this.transform, this.centroid, this.position);
//this.position.copyFrom(this.transform.transform(this.centroid));
return;
}
@@ -407,14 +426,11 @@ module Phaser.Physics {
var mass = shape.area() * shape.density;
var inertia = shape.inertia(mass);
//console.log('rmd', centroid, shape);
totalMassCentroid.multiplyAddByScalar(centroid, mass);
totalMass += mass;
totalInertia += inertia;
}
//this.centroid.copy(vec2.scale(totalMassCentroid, 1 / totalMass));
Phaser.Vec2Utils.scale(totalMassCentroid, 1 / totalMass, this.centroid);
this.setMass(totalMass);
@@ -455,9 +471,9 @@ module Phaser.Physics {
public cacheData(source:string = '') {
Manager.write('cacheData -- start');
Manager.write('p: ' + this.position.toString());
Manager.write('xf: ' + this.transform.toString());
//Manager.write('cacheData -- start');
//Manager.write('p: ' + this.position.toString());
//Manager.write('xf: ' + this.transform.toString());
this.bounds.clear();
@@ -468,11 +484,11 @@ module Phaser.Physics {
this.bounds.addBounds(shape.bounds);
}
Manager.write('bounds: ' + this.bounds.toString());
//Manager.write('bounds: ' + this.bounds.toString());
Manager.write('p: ' + this.position.toString());
Manager.write('xf: ' + this.transform.toString());
Manager.write('cacheData -- stop');
//Manager.write('p: ' + this.position.toString());
//Manager.write('xf: ' + this.transform.toString());
//Manager.write('cacheData -- stop');
}
@@ -538,15 +554,16 @@ module Phaser.Physics {
{
this.sprite.x = this.position.x * 50;
this.sprite.y = this.position.y * 50;
// Obey fixed rotation?
this.sprite.rotation = this.game.math.radiansToDegrees(this.angle);
this.sprite.transform.rotation = this.game.math.radiansToDegrees(this.angle);
}
}
public resetForce() {
this.force.setTo(0, 0);
this.torque = 0;
}
public applyForce(force:Phaser.Vec2, p:Phaser.Vec2) {
@@ -638,11 +655,8 @@ module Phaser.Physics {
}
public kineticEnergy() {
var vsq = this.velocity.dot(this.velocity);
var wsq = this.angularVelocity * this.angularVelocity;
return 0.5 * (this.mass * vsq + this.inertia * wsq);
return 0.5 * (this.mass * this.velocity.dot(this.velocity) + this.inertia * (this.angularVelocity * this.angularVelocity));
}
@@ -670,12 +684,7 @@ module Phaser.Physics {
public isCollidable(other:Body) {
if (this == other)
{
return false;
}
if (this.isDynamic == false && other.isDynamic == false)
if ((this.isDynamic == false && other.isDynamic == false) || this == other)
{
return false;
}
@@ -699,6 +708,10 @@ module Phaser.Physics {
}
public toString(): string {
return "[{Body (name=" + this.name + " velocity=" + this.velocity.toString() + " angularVelocity: " + this.angularVelocity + ")}]";
}
}
}
-13
View File
@@ -313,12 +313,8 @@ module Phaser.Physics {
var r2 = new Phaser.Vec2;
// Transformed r1, r2
Phaser.Vec2Utils.rotate(con.r1_local, body1.angle, r1);
//var r1 = vec2.rotate(con.r1_local, body1.a);
Phaser.Vec2Utils.rotate(con.r2_local, body2.angle, r2);
//var r2 = vec2.rotate(con.r2_local, body2.a);
Manager.write('r1_local.x = ' + con.r1_local.x + ' r1_local.y = ' + con.r1_local.y + ' angle: ' + body1.angle);
Manager.write('r1 rotated: r1.x = ' + r1.x + ' r1.y = ' + r1.y);
@@ -330,10 +326,7 @@ module Phaser.Physics {
var p2 = new Phaser.Vec2;
Phaser.Vec2Utils.add(body1.position, r1, p1);
//var p1 = vec2.add(body1.p, r1);
Phaser.Vec2Utils.add(body2.position, r2, p2);
//var p2 = vec2.add(body2.p, r2);
Manager.write('body1.pos.x=' + body1.position.x + ' y=' + body1.position.y);
Manager.write('body2.pos.x=' + body2.position.x + ' y=' + body2.position.y);
@@ -341,7 +334,6 @@ module Phaser.Physics {
// Corrected delta vector
var dp = new Phaser.Vec2;
Phaser.Vec2Utils.subtract(p2, p1, dp);
//var dp = vec2.sub(p2, p1);
// Position constraint
var c = Phaser.Vec2Utils.dot(dp, n) + con.depth;
@@ -368,16 +360,11 @@ module Phaser.Physics {
// Apply correction impulses
var impulse_dt = new Phaser.Vec2;
Phaser.Vec2Utils.scale(n, lambda_dt, impulse_dt);
//var impulse_dt = vec2.scale(n, lambda_dt);
body1.position.multiplyAddByScalar(impulse_dt, -m1_inv);
//body1.p.mad(impulse_dt, -m1_inv);
body1.angle -= sn1 * lambda_dt * i1_inv;
body2.position.multiplyAddByScalar(impulse_dt, m2_inv);
//body2.p.mad(impulse_dt, m2_inv);
body2.angle += sn2 * lambda_dt * i2_inv;
Manager.write('body1.pos.x=' + body1.position.x + ' y=' + body1.position.y);
+130 -2
View File
@@ -17,7 +17,7 @@ module Phaser {
*/
private _game: Phaser.Game;
// local rendering related temp vars to help avoid gc spikes with var creation
// Local rendering related temp vars to help avoid gc spikes through var creation
private _ga: number = 1;
private _sx: number = 0;
private _sy: number = 0;
@@ -29,9 +29,15 @@ module Phaser {
private _dh: number = 0;
private _fx: number = 1;
private _fy: number = 1;
private _tx: number = 0;
private _ty: number = 0;
private _sin: number = 0;
private _cos: number = 1;
private _maxX: number = 0;
private _maxY: number = 0;
private _startX: number = 0;
private _startY: number = 0;
private _columnData;
private _cameraList;
private _camera: Camera;
private _groupLength: number;
@@ -74,6 +80,10 @@ module Phaser {
{
this.renderScrollZone(this._camera, object);
}
else if (object.type == Types.TILEMAP)
{
this.renderTilemap(this._camera, object);
}
}
@@ -668,6 +678,124 @@ module Phaser {
}
/**
* Render a tilemap to a specific camera.
* @param camera {Camera} The camera this tilemap will be rendered to.
*/
public renderTilemap(camera: Camera, tilemap: Tilemap): bool {
// Loop through the layers
for (var i = 0; i < tilemap.layers.length; i++)
{
var layer: TilemapLayer = tilemap.layers[i];
if (layer.visible == false || layer.alpha < 0.1)
{
continue;
}
// Work out how many tiles we can fit into our camera and round it up for the edges
this._maxX = this._game.math.ceil(camera.width / layer.tileWidth) + 1;
this._maxY = this._game.math.ceil(camera.height / layer.tileHeight) + 1;
// And now work out where in the tilemap the camera actually is
this._startX = this._game.math.floor(camera.worldView.x / layer.tileWidth);
this._startY = this._game.math.floor(camera.worldView.y / layer.tileHeight);
// Tilemap bounds check
if (this._startX < 0)
{
this._startX = 0;
}
if (this._startY < 0)
{
this._startY = 0;
}
if (this._maxX > layer.widthInTiles)
{
this._maxX = layer.widthInTiles;
}
if (this._maxY > layer.heightInTiles)
{
this._maxY = layer.heightInTiles;
}
if (this._startX + this._maxX > layer.widthInTiles)
{
this._startX = layer.widthInTiles - this._maxX;
}
if (this._startY + this._maxY > layer.heightInTiles)
{
this._startY = layer.heightInTiles - this._maxY;
}
// Finally get the offset to avoid the blocky movement
//this._dx = (camera.screenView.x * layer.transform.scrollFactor.x) - (camera.worldView.x * layer.transform.scrollFactor.x);
//this._dy = (camera.screenView.y * layer.transform.scrollFactor.y) - (camera.worldView.y * layer.transform.scrollFactor.y);
//this._dx = (camera.screenView.x * this.scrollFactor.x) + this.x - (camera.worldView.x * this.scrollFactor.x);
//this._dy = (camera.screenView.y * this.scrollFactor.y) + this.y - (camera.worldView.y * this.scrollFactor.y);
this._dx = 0;
this._dy = 0;
this._dx += -(camera.worldView.x - (this._startX * layer.tileWidth));
this._dy += -(camera.worldView.y - (this._startY * layer.tileHeight));
this._tx = this._dx;
this._ty = this._dy;
// Alpha
if (layer.texture.alpha !== 1)
{
this._ga = layer.texture.context.globalAlpha;
layer.texture.context.globalAlpha = layer.texture.alpha;
}
for (var row = this._startY; row < this._startY + this._maxY; row++)
{
this._columnData = layer.mapData[row];
for (var tile = this._startX; tile < this._startX + this._maxX; tile++)
{
if (layer.tileOffsets[this._columnData[tile]])
{
layer.texture.context.drawImage(
layer.texture.texture,
layer.tileOffsets[this._columnData[tile]].x,
layer.tileOffsets[this._columnData[tile]].y,
layer.tileWidth,
layer.tileHeight,
this._tx,
this._ty,
layer.tileWidth,
layer.tileHeight
);
}
this._tx += layer.tileWidth;
}
this._tx = this._dx;
this._ty += layer.tileHeight;
}
if (this._ga > -1)
{
layer.texture.context.globalAlpha = this._ga;
}
}
return true;
}
}
}
+10
View File
@@ -187,6 +187,8 @@ module Phaser {
}
public isRunning: bool = false;
/**
* Start to tween.
*/
@@ -205,6 +207,7 @@ module Phaser {
}
this._startTime = this._game.time.now + this._delayTime;
this.isRunning = true;
for (var property in this._valuesEnd)
{
@@ -289,6 +292,8 @@ module Phaser {
this._manager.remove(this);
}
this.isRunning = false;
this.onComplete.dispose();
return this;
@@ -432,6 +437,11 @@ module Phaser {
this._chainedTweens[i].start();
}
if (this._chainedTweens.length == 0)
{
this.isRunning = false;
}
return false;
}
+7
View File
@@ -91,6 +91,13 @@ module Phaser {
}
static renderRectangle(rect: Phaser.Rectangle, fillStyle: string = 'rgba(0,255,0,0.3)') {
DebugUtils.context.fillStyle = fillStyle;
DebugUtils.context.fillRect(rect.x, rect.y, rect.width, rect.height);
}
static renderPhysicsBody(body: Phaser.Physics.Body, lineWidth: number = 1, fillStyle: string = 'rgba(0,255,0,0.2)', sleepStyle: string = 'rgba(100,100,100,0.2)') {
for (var s = 0; s < body.shapesLength; s++)
+1 -1
View File
@@ -11,7 +11,7 @@
module Phaser {
class PointUtils {
export class PointUtils {
/**
* Adds the coordinates of two points together to create a new point.