Tilemap collision is now working but all Camera following seems to be broken as a result. Awesome.

This commit is contained in:
Richard Davey
2013-04-28 12:25:02 +01:00
parent 2087b2d76e
commit c09cff336d
15 changed files with 12407 additions and 4729 deletions
+46 -85
View File
@@ -92,6 +92,17 @@ module Phaser {
*/
public static OVERLAP_BIAS: number = 4;
/**
* This holds the result of the tile separation check, true if the object was moved, otherwise false
* @type {boolean}
*/
public static TILE_OVERLAP: bool = false;
/**
* A temporary Quad used in the separation process to help avoid gc spikes
* @type {Quad}
*/
public static _tempBounds: Quad;
/**
* Checks for Line to Line intersection and returns an IntersectResult object containing the results of the intersection.
@@ -634,10 +645,10 @@ module Phaser {
* @param tile The Tile to separate
* @returns {boolean} Whether the objects in fact touched and were separated
*/
public static separateTile(object:GameObject, tile): bool {
public static separateTile(object:GameObject, x: number, y: number, width: number, height: number, mass: number, collideLeft: bool, collideRight: bool, collideUp: bool, collideDown: bool): bool {
var separatedX: bool = Collision.separateTileX(object, tile);
var separatedY: bool = Collision.separateTileY(object, tile);
var separatedX: bool = Collision.separateTileX(object, x, y, width, height, mass, collideLeft, collideRight);
var separatedY: bool = Collision.separateTileY(object, x, y, width, height, mass, collideUp, collideDown);
return separatedX || separatedY;
@@ -649,37 +660,34 @@ module Phaser {
* @param tile The Tile to separate
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
*/
public static separateTileX(object, tile): bool {
public static separateTileX(object:GameObject, x: number, y: number, width: number, height: number, mass: number, collideLeft: bool, collideRight: bool): bool {
// Can't separate two immovable objects
if (object.immovable && tile.immovable)
// Can't separate two immovable objects (tiles are always immovable)
if (object.immovable)
{
return false;
}
// First, get the two object deltas
// First, get the object delta
var overlap: number = 0;
var objDelta: number = object.x - object.last.x;
var tileDelta: number = 0;
if (objDelta != tileDelta)
if (objDelta != 0)
{
// Check if the X hulls actually overlap
var objDeltaAbs: number = (objDelta > 0) ? objDelta : -objDelta;
var tileDeltaAbs: number = (tileDelta > 0) ? tileDelta : -tileDelta;
var objBounds: Quad = new Quad(object.x - ((objDelta > 0) ? objDelta : 0), object.last.y, object.width + ((objDelta > 0) ? objDelta : -objDelta), object.height);
var tileBounds: Quad = new Quad(tile.x - ((tileDelta > 0) ? tileDelta : 0), tile.y, tile.width + ((tileDelta > 0) ? tileDelta : -tileDelta), tile.height);
if ((objBounds.x + objBounds.width > tileBounds.x) && (objBounds.x < tileBounds.x + tileBounds.width) && (objBounds.y + objBounds.height > tileBounds.y) && (objBounds.y < tileBounds.y + tileBounds.height))
if ((objBounds.x + objBounds.width > x) && (objBounds.x < x + width) && (objBounds.y + objBounds.height > y) && (objBounds.y < y + height))
{
var maxOverlap: number = objDeltaAbs + tileDeltaAbs + Collision.OVERLAP_BIAS;
var maxOverlap: number = objDeltaAbs + Collision.OVERLAP_BIAS;
// If they did overlap (and can), figure out by how much and flip the corresponding flags
if (objDelta > tileDelta)
if (objDelta > 0)
{
overlap = object.x + object.width - tile.x;
overlap = object.x + object.width - x;
if ((overlap > maxOverlap) || !(object.allowCollisions & Collision.RIGHT) || !(tile.allowCollisions & Collision.LEFT))
if ((overlap > maxOverlap) || !(object.allowCollisions & Collision.RIGHT) || collideLeft == false)
{
overlap = 0;
}
@@ -688,11 +696,11 @@ module Phaser {
object.touching |= Collision.RIGHT;
}
}
else if (objDelta < tileDelta)
else if (objDelta < 0)
{
overlap = object.x - tile.width - tile.x;
overlap = object.x - width - x;
if ((-overlap > maxOverlap) || !(object.allowCollisions & Collision.LEFT) || !(tile.allowCollisions & Collision.RIGHT))
if ((-overlap > maxOverlap) || !(object.allowCollisions & Collision.LEFT) || collideRight == false)
{
overlap = 0;
}
@@ -709,26 +717,9 @@ module Phaser {
// Then adjust their positions and velocities accordingly (if there was any overlap)
if (overlap != 0)
{
var objVelocity: number = object.velocity.x;
var tileVelocity: number = 0;
if (!object.immovable && !tile.immovable)
{
overlap *= 0.5;
object.x = object.x - overlap;
var objNewVelocity: number = Math.sqrt((tileVelocity * tileVelocity * tile.mass) / object.mass) * ((tileVelocity > 0) ? 1 : -1);
var tileNewVelocity: number = Math.sqrt((objVelocity * objVelocity * object.mass) / tile.mass) * ((objVelocity > 0) ? 1 : -1);
var average: number = (objNewVelocity + tileNewVelocity) * 0.5;
objNewVelocity -= average;
object.velocity.x = average + objNewVelocity * object.elasticity;
}
else if (!object.immovable)
{
object.x = object.x - overlap;
object.velocity.x = tileVelocity - objVelocity * object.elasticity;
}
object.x = object.x - overlap;
object.velocity.x = -(object.velocity.x * object.elasticity);
Collision.TILE_OVERLAP = true;
return true;
}
else
@@ -744,57 +735,53 @@ module Phaser {
* @param tile The second GameObject to separate
* @returns {boolean} Whether the objects in fact touched and were separated along the Y axis.
*/
public static separateTileY(object, tile): bool {
public static separateTileY(object: GameObject, x: number, y: number, width: number, height: number, mass: number, collideUp: bool, collideDown: bool): bool {
// Can't separate two immovable objects
if (object.immovable && tile.immovable) {
// Can't separate two immovable objects (tiles are always immovable)
if (object.immovable)
{
return false;
}
// First, get the two object deltas
var overlap: number = 0;
var objDelta: number = object.y - object.last.y;
var tileDelta: number = 0;
if (objDelta != tileDelta)
if (objDelta != 0)
{
// Check if the Y hulls actually overlap
var objDeltaAbs: number = (objDelta > 0) ? objDelta : -objDelta;
var tileDeltaAbs: number = (tileDelta > 0) ? tileDelta : -tileDelta;
var objBounds: Quad = new Quad(object.x, object.y - ((objDelta > 0) ? objDelta : 0), object.width, object.height + objDeltaAbs);
var tileBounds: Quad = new Quad(tile.x, tile.y - ((tileDelta > 0) ? tileDelta : 0), tile.width, tile.height + tileDeltaAbs);
if ((objBounds.x + objBounds.width > tileBounds.x) && (objBounds.x < tileBounds.x + tileBounds.width) && (objBounds.y + objBounds.height > tileBounds.y) && (objBounds.y < tileBounds.y + tileBounds.height))
if ((objBounds.x + objBounds.width > x) && (objBounds.x < x + width) && (objBounds.y + objBounds.height > y) && (objBounds.y < y + height))
{
var maxOverlap: number = objDeltaAbs + tileDeltaAbs + Collision.OVERLAP_BIAS;
var maxOverlap: number = objDeltaAbs + Collision.OVERLAP_BIAS;
// If they did overlap (and can), figure out by how much and flip the corresponding flags
if (objDelta > tileDelta)
if (objDelta > 0)
{
overlap = object.y + object.height - tile.y;
overlap = object.y + object.height - y;
if ((overlap > maxOverlap) || !(object.allowCollisions & Collision.DOWN) || !(tile.allowCollisions & Collision.UP))
if ((overlap > maxOverlap) || !(object.allowCollisions & Collision.DOWN) || collideUp == false)
{
overlap = 0;
}
else
{
object.touching |= Collision.DOWN;
//tile.touching |= Collision.UP;
}
}
else if (objDelta < tileDelta)
else if (objDelta < 0)
{
overlap = object.y - tile.height - tile.y;
overlap = object.y - height - y;
if ((-overlap > maxOverlap) || !(object.allowCollisions & Collision.UP) || !(tile.allowCollisions & Collision.DOWN))
if ((-overlap > maxOverlap) || !(object.allowCollisions & Collision.UP) || collideDown == false)
{
overlap = 0;
}
else
{
object.touching |= Collision.UP;
//tile.touching |= Collision.DOWN;
}
}
}
@@ -805,35 +792,9 @@ module Phaser {
// Then adjust their positions and velocities accordingly (if there was any overlap)
if (overlap != 0)
{
var objVelocity: number = object.velocity.y;
var tileVelocity: number = 0;
if (!object.immovable && !tile.immovable)
{
overlap *= 0.5;
object.y = object.y - overlap;
//tile.y += overlap;
var objNewVelocity: number = Math.sqrt((tileVelocity * tileVelocity * tile.mass) / object.mass) * ((tileVelocity > 0) ? 1 : -1);
var tileNewVelocity: number = Math.sqrt((objVelocity * objVelocity * object.mass) / tile.mass) * ((objVelocity > 0) ? 1 : -1);
var average: number = (objNewVelocity + tileNewVelocity) * 0.5;
objNewVelocity -= average;
//tileNewVelocity -= average;
object.velocity.y = average + objNewVelocity * object.elasticity;
//tile.velocity.y = average + tileNewVelocity * tile.elasticity;
}
else if (!object.immovable)
{
//console.log('y sep', overlap, object.y);
object.y = object.y - overlap;
object.velocity.y = tileVelocity - objVelocity * object.elasticity;
// This is special case code that handles things like horizontal moving platforms you can ride
if (tile.active && tile.moves && (objDelta > tileDelta))
{
//object.x += tile.x - tile.x;
}
}
object.y = object.y - overlap;
object.velocity.y = -(object.velocity.y * object.elasticity);
Collision.TILE_OVERLAP = true;
return true;
}
else
+3 -3
View File
@@ -474,7 +474,7 @@ module Phaser {
}
public forEachAlive(callback, recursive: bool = false) {
public forEachAlive(context, callback, recursive: bool = false) {
var basic;
var i: number = 0;
@@ -487,11 +487,11 @@ module Phaser {
{
if (recursive && (basic.isGroup == true))
{
basic.forEachAlive(callback, true);
basic.forEachAlive(context, callback, true);
}
else
{
callback.call(this, basic);
callback.call(context, basic);
}
}
}
+5 -3
View File
@@ -174,7 +174,7 @@ module Phaser {
*
* @return This Emitter instance (nice for chaining stuff together, if you're into that).
*/
public makeParticles(Graphics, Quantity: number = 50, BakedRotations: number = 16, Multiple: bool = false, Collide: number = 0.8): Emitter {
public makeParticles(Graphics, Quantity: number = 50, BakedRotations: number = 16, Multiple: bool = false, Collide: number = 0): Emitter {
this.maxSize = Quantity;
@@ -236,8 +236,10 @@ module Phaser {
if (Collide > 0)
{
particle.width *= Collide;
particle.height *= Collide;
particle.allowCollisions = Collision.ANY;
particle.elasticity = Collide;
//particle.width *= Collide;
//particle.height *= Collide;
//particle.centerOffsets();
}
else
+3 -28
View File
@@ -29,8 +29,8 @@ module Phaser {
this.last = new MicroPoint(x, y);
this.origin = new MicroPoint(this.bounds.halfWidth, this.bounds.halfHeight);
this.align = GameObject.ALIGN_TOP_LEFT;
this.mass = 1.0;
this.elasticity = 0.0;
this.mass = 1;
this.elasticity = 0;
this.health = 1;
this.immovable = false;
this.moves = true;
@@ -52,7 +52,7 @@ module Phaser {
this.maxAngular = 10000;
this.cameraBlacklist = [];
this.scrollFactor = new MicroPoint(1.0, 1.0);
this.scrollFactor = new MicroPoint(1, 1);
}
@@ -245,17 +245,6 @@ module Phaser {
}
/*
if (typeof ObjectOrGroup === 'Tilemap')
{
//Since tilemap's have to be the caller, not the target, to do proper tile-based collisions,
// we redirect the call to the tilemap overlap here.
return ObjectOrGroup.overlaps(this, InScreenSpace, Camera);
}
*/
//var object: GameObject = ObjectOrGroup;
if (!InScreenSpace)
{
return (ObjectOrGroup.x + ObjectOrGroup.width > this.x) && (ObjectOrGroup.x < this.x + this.width) &&
@@ -308,20 +297,6 @@ module Phaser {
return results;
}
/*
if (typeof ObjectOrGroup === 'Tilemap')
{
//Since tilemap's have to be the caller, not the target, to do proper tile-based collisions,
// we redirect the call to the tilemap overlap here.
//However, since this is overlapsAt(), we also have to invent the appropriate position for the tilemap.
//So we calculate the offset between the player and the requested position, and subtract that from the tilemap.
var tilemap: Tilemap = ObjectOrGroup;
return tilemap.overlapsAt(tilemap.x - (X - this.x), tilemap.y - (Y - this.y), this, InScreenSpace, Camera);
}
*/
//var object: GameObject = ObjectOrGroup;
if (!InScreenSpace)
{
return (ObjectOrGroup.x + ObjectOrGroup.width > X) && (ObjectOrGroup.x < X + this.width) &&
+1
View File
@@ -43,6 +43,7 @@ module Phaser {
* be dead yet, and then has some special bounce behavior if there is some gravity on it.
*/
public update() {
//lifespan behavior
if (this.lifespan <= 0)
{
+27 -10
View File
@@ -49,6 +49,7 @@ module Phaser {
public tiles : Tile[];
public layers : TilemapLayer[];
public currentLayer: TilemapLayer;
public collisionLayer: TilemapLayer;
public mapFormat: number;
public update() {
@@ -90,11 +91,14 @@ module Phaser {
var tileQuantity = layer.parseTileOffsets();
this.currentLayer = layer;
this.collisionLayer = layer;
this.layers.push(layer);
this.generateTiles(tileQuantity);
console.log('generate layer csv');
}
private parseTiledJSON(data: string, key: string) {
@@ -132,6 +136,7 @@ module Phaser {
layer.addColumn(row);
c = 0;
}
console.log('generate layer json');
}
layer.updateBounds();
@@ -139,6 +144,7 @@ module Phaser {
var tileQuantity = layer.parseTileOffsets();
this.currentLayer = layer;
this.collisionLayer = layer;
this.layers.push(layer);
@@ -167,20 +173,20 @@ module Phaser {
// Tile Collision
public setCollisionRange(start: number, end: number, collision?:number = Collision.ANY) {
public setCollisionRange(start: number, end: number, collision?:number = Collision.ANY, resetCollisions: bool = false) {
for (var i = start; i < end; i++)
{
this.tiles[i].allowCollisions = collision;
this.tiles[i].setCollision(collision, resetCollisions);
}
}
public setCollisionByIndex(values:number[], collision?:number = Collision.ANY) {
public setCollisionByIndex(values:number[], collision?:number = Collision.ANY, resetCollisions: bool = false) {
for (var i = 0; i < values.length; i++)
{
this.tiles[values[i]].allowCollisions = collision;
this.tiles[values[i]].setCollision(collision, resetCollisions);
}
}
@@ -222,21 +228,32 @@ module Phaser {
// Group?
if (objectOrGroup.isGroup == false)
{
if (objectOrGroup.exists && objectOrGroup.allowCollisions != Collision.NONE)
{
this.currentLayer.getTileOverlaps(objectOrGroup);
}
return this.collideGameObject(objectOrGroup);
}
else
{
// todo
objectOrGroup.forEachAlive(this.currentLayer.getTileOverlaps);
objectOrGroup.forEachAlive(this, this.collideGameObject, true);
}
return true;
}
public collideGameObject(object: GameObject): bool {
if (object == this) { return false; }
if (object.immovable == false && object.exists == true && object.allowCollisions != Collision.NONE)
{
return this.collisionLayer.getTileOverlaps(object);
}
else
{
return false;
}
}
// Set current layer
// Set layer order?
+9 -2
View File
@@ -221,6 +221,7 @@ module Phaser {
public follow(target: Sprite, style?: number = Camera.STYLE_LOCKON) {
this._target = target;
var helper: number;
switch (style)
@@ -229,18 +230,22 @@ module Phaser {
var w: number = this.width / 8;
var h: number = this.height / 3;
this.deadzone = new Rectangle((this.width - w) / 2, (this.height - h) / 2 - h * 0.25, w, h);
console.log('follow 1');
break;
case Camera.STYLE_TOPDOWN:
helper = Math.max(this.width, this.height) / 4;
this.deadzone = new Rectangle((this.width - helper) / 2, (this.height - helper) / 2, helper, helper);
console.log('follow 2');
break;
case Camera.STYLE_TOPDOWN_TIGHT:
helper = Math.max(this.width, this.height) / 8;
this.deadzone = new Rectangle((this.width - helper) / 2, (this.height - helper) / 2, helper, helper);
console.log('follow 3');
break;
case Camera.STYLE_LOCKON:
default:
this.deadzone = null;
console.log('follow 4');
break;
}
@@ -248,12 +253,16 @@ module Phaser {
public focusOnXY(x: number, y: number) {
console.log('focusOn', x, y);
x += (x > 0) ? 0.0000001 : -0.0000001;
y += (y > 0) ? 0.0000001 : -0.0000001;
this.scroll.x = Math.round(x - this.worldView.halfWidth);
this.scroll.y = Math.round(y - this.worldView.halfHeight);
console.log('focusOn scroll',this.scroll.x, this.scroll.y);
}
public focusOn(point: Point) {
@@ -494,7 +503,6 @@ module Phaser {
this._game.stage.context.translate(-(this._sx + this.worldView.halfWidth), -(this._sy + this.worldView.halfHeight));
}
// Background
if (this.opaque == true)
{
@@ -560,7 +568,6 @@ module Phaser {
if (this._rotation !== 0 || this._clip)
{
this._game.stage.context.translate(0, 0);
//this._game.stage.context.restore();
}
// maybe just do this every frame regardless?
+56 -1
View File
@@ -27,12 +27,17 @@ module Phaser {
// You can give this Tile a friendly name to help with debugging. Never used internally.
public name: string;
public mass: number = 1.0;
public width: number;
public height: number;
public allowCollisions: number;
public collideLeft: bool = false;
public collideRight: bool = false;
public collideUp: bool = false;
public collideDown: bool = false;
/**
* A reference to the tilemap this tile object belongs to.
*/
@@ -54,6 +59,56 @@ module Phaser {
}
public setCollision(collision: number, resetCollisions: bool) {
if (resetCollisions)
{
this.resetCollision();
}
this.allowCollisions = collision;
if (collision & Collision.ANY)
{
this.collideLeft = true;
this.collideRight = true;
this.collideUp = true;
this.collideDown = true;
return;
}
if (collision & Collision.LEFT || collision & Collision.WALL)
{
this.collideLeft = true;
}
if (collision & Collision.RIGHT || collision & Collision.WALL)
{
this.collideRight = true;
}
if (collision & Collision.UP || collision & Collision.CEILING)
{
this.collideUp = true;
}
if (collision & Collision.DOWN || collision & Collision.CEILING)
{
this.collideDown = true;
}
}
public resetCollision() {
this.allowCollisions = Collision.NONE;
this.collideLeft = false;
this.collideRight = false;
this.collideUp = false;
this.collideDown = false;
}
/**
* Returns a string representation of this object.
* @method toString
+43 -55
View File
@@ -43,6 +43,11 @@ module Phaser {
private _oldCameraY: number = 0;
private _columnData;
private _tempTileX: number;
private _tempTileY: number;
private _tempTileW: number;
private _tempTileH: number;
public name: string;
public alpha: number = 1;
public exists: bool = true;
@@ -78,84 +83,67 @@ module Phaser {
public getTileOverlaps(object: GameObject) {
//var result: bool = false;
//var x: number = object.x;
//var y: number = object.y;
// If the object is outside of the world coordinates then abort the check (tilemap has to exist within world bounds)
if (object.bounds.x < 0 || object.bounds.x > this.widthInPixels || object.bounds.y < 0 || object.bounds.bottom > this.heightInPixels)
{
return;
}
// What tiles do we need to check against?
var mapX:number = this._game.math.snapToFloor(object.bounds.x, this.tileWidth);
var mapY:number = this._game.math.snapToFloor(object.bounds.y, this.tileHeight);
var mapW:number = this._game.math.snapToCeil(object.bounds.width, this.tileWidth) + this.tileWidth;
var mapH:number = this._game.math.snapToCeil(object.bounds.height, this.tileHeight) + this.tileHeight;
var tileX = mapX / this.tileWidth;
var tileY = mapY / this.tileHeight;
var tileW = mapW / this.tileWidth;
var tileH = mapH / this.tileHeight;
if (tileX < 0)
{
tileX = 0;
}
if (tileY < 0)
{
tileY = 0;
}
if (tileW > this.widthInTiles)
{
tileW = this.widthInTiles;
}
if (tileH > this.heightInTiles)
{
tileH = this.heightInTiles;
}
this._tempTileX = this._game.math.snapToFloor(object.bounds.x, this.tileWidth) / this.tileWidth;
this._tempTileY = this._game.math.snapToFloor(object.bounds.y, this.tileHeight) / this.tileHeight;
this._tempTileW = (this._game.math.snapToCeil(object.bounds.width, this.tileWidth) + this.tileWidth) / this.tileWidth;
this._tempTileH = (this._game.math.snapToCeil(object.bounds.height, this.tileHeight) + this.tileHeight) / this.tileHeight;
// Loop through the tiles we've got and check overlaps accordingly
var tiles = this.getTileBlock(tileX, tileY, tileW, tileH);
var tiles = this.getTileBlock(this._tempTileX, this._tempTileY, this._tempTileW, this._tempTileH);
var result = [];
var tempBounds = new Quad();
Collision.TILE_OVERLAP = false;
for (var r = 0; r < tiles.length; r++)
{
if (tiles[r].tile.allowCollisions != Collision.NONE)
{
tempBounds.setTo(tiles[r].x * this.tileWidth, tiles[r].y * this.tileHeight, this.tileWidth, this.tileHeight);
if (tempBounds.intersects(object.bounds))
{
result.push(Collision.separateTile(object, { x: tempBounds.x, y: tempBounds.y, width: tempBounds.width, height: tempBounds.height, mass: 1.0, immovable: true, allowCollisions: Collision.ANY }));
}
else
{
result.push(false);
}
}
else
{
result.push(false);
Collision.separateTile(object, tiles[r].x * this.tileWidth, tiles[r].y * this.tileHeight, this.tileWidth, this.tileHeight, tiles[r].tile.mass, tiles[r].tile.collideLeft, tiles[r].tile.collideRight, tiles[r].tile.collideUp, tiles[r].tile.collideDown);
}
}
//return { x: mapX, y: mapY, w: mapW, h: mapH, collision: result };
return { x: tileX, y: tileY, w: tileW, h: tileH, collision: result };
return Collision.TILE_OVERLAP;
}
//public checkTileOverlap(object:GameObject,
public getTileBlock(x: number, y: number, width: number, height: number) {
if (x < 0)
{
x = 0;
}
if (y < 0)
{
y = 0;
}
if (width > this.widthInTiles)
{
width = this.widthInTiles;
}
if (height > this.heightInTiles)
{
height = this.heightInTiles;
}
var output = [];
for (var ty = y; ty < y + height; ty++)
{
for (var tx = x; tx < x + width; tx++)
{
output.push({ x: tx, y: ty, tile: this._parent.tiles[this.mapData[ty][tx]] });
if (this.mapData[ty] && this.mapData[ty][tx])
{
output.push({ x: tx, y: ty, tile: this._parent.tiles[this.mapData[ty][tx]] });
}
}
}
@@ -203,7 +191,7 @@ module Phaser {
this.boundsInTiles.setTo(0, 0, this.widthInTiles, this.heightInTiles);
console.log('layer bounds', this.boundsInTiles);
//console.log('layer bounds', this.boundsInTiles);
}
+6 -5
View File
@@ -12,22 +12,23 @@
myGame.createSprite(0, 0, 'grid');
car = myGame.createSprite(400, 300, 'car');
myGame.camera.follow(car);
myGame.onRenderCallback = render;
}
function update() {
myGame.camera.renderDebugInfo(32, 32);
car.renderDebugInfo(200, 32);
car.velocity.x = 0;
car.velocity.y = 0;
car.angularVelocity = 0;
car.angularAcceleration = 0;
if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
car.angularVelocity = -200;
} else if(myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
car.angularVelocity = 200;
}
if(myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) {
var motion = myGame.motion.velocityFromAngle(car.angle, 300);
car.velocity.copyFrom(motion);
car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
}
}
function render() {
myGame.camera.renderDebugInfo(32, 32);
car.renderDebugInfo(200, 32);
}
})();
+10 -6
View File
@@ -25,17 +25,16 @@
myGame.camera.follow(car);
myGame.onRenderCallback = render;
}
function update() {
myGame.camera.renderDebugInfo(32, 32);
car.renderDebugInfo(200, 32);
car.velocity.x = 0;
car.velocity.y = 0;
car.angularVelocity = 0;
car.angularAcceleration = 0;
if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
@@ -48,11 +47,16 @@
if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP))
{
var motion:Phaser.Point = myGame.motion.velocityFromAngle(car.angle, 300);
car.velocity.copyFrom(motion);
car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
}
}
function render() {
myGame.camera.renderDebugInfo(32, 32);
car.renderDebugInfo(200, 32);
}
})();
+6067 -2213
View File
File diff suppressed because it is too large Load Diff
+36 -43
View File
@@ -1,22 +1,43 @@
var __extends = this.__extends || function (d, b) {
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/Game.ts" />
var customParticle2 = (function (_super) {
__extends(customParticle2, _super);
function customParticle2(game) {
_super.call(this, game);
var s = [
'carrot',
'melon',
'eggplant',
'mushroom',
'pineapple'
];
this.loadGraphic(game.math.getRandom(s));
this.elasticity = 0.8;
}
return customParticle2;
})(Phaser.Particle);
(function () {
var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
function init() {
myGame.loader.addTextFile('platform', 'assets/maps/platform-test-1.json');
myGame.loader.addImageFile('tiles', 'assets/tiles/platformer_tiles.png');
myGame.loader.addImageFile('ufo', 'assets/sprites/ufo.png');
myGame.loader.addImageFile('ilkke', 'assets/sprites/ilkke.png');
myGame.loader.addImageFile('chunk', 'assets/sprites/chunk.png');
myGame.loader.addImageFile('healthbar', 'assets/sprites/healthbar.png');
myGame.loader.addImageFile('carrot', 'assets/sprites/carrot.png');
myGame.loader.addImageFile('melon', 'assets/sprites/melon.png');
myGame.loader.addImageFile('eggplant', 'assets/sprites/eggplant.png');
myGame.loader.addImageFile('mushroom', 'assets/sprites/mushroom.png');
myGame.loader.addImageFile('pineapple', 'assets/sprites/pineapple.png');
myGame.loader.load();
}
var map;
var car;
var marker;
var tile;
var emitter;
var mo;
function create() {
map = myGame.createTilemap('tiles', 'platform', Phaser.Tilemap.FORMAT_TILED_JSON);
map.setCollisionRange(21, 53);
@@ -28,27 +49,19 @@
Phaser.Keyboard.UP,
Phaser.Keyboard.DOWN
]);
emitter = myGame.createEmitter(32, 32);
emitter = myGame.createEmitter(32, 80);
emitter.width = 700;
emitter.makeParticles(null, 50, 0, false, 0);
emitter.particleClass = customParticle2;
emitter.makeParticles(null, 100, 0, false, 0.7);
emitter.gravity = 100;
emitter.setRotation(0, 0);
emitter.start(false);
emitter.start(false, 10, 0.05);
car = myGame.createSprite(250, 64, 'ufo');
car.renderRotation = false;
//car.renderDebug = true;
car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
//car.velocity.y = 10;
marker = myGame.createGeomSprite(0, 0);
marker.createRectangle(16, 16);
marker.renderFill = false;
marker.visible = false;
//myGame.onRenderCallback = render;
}
}
function update() {
marker.x = myGame.math.snapToFloor(myGame.input.worldX, 16);
marker.y = myGame.math.snapToFloor(myGame.input.worldY, 16);
//myGame.collide(car, map.currentLayer);
car.velocity.x = 0;
car.velocity.y = 0;
if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
@@ -61,30 +74,10 @@
} else if(myGame.input.keyboard.isDown(Phaser.Keyboard.DOWN)) {
car.velocity.y = 200;
}
mo = map.collide(car);
//map.getTileOverlaps()
}
function render() {
tile = map.getTileFromInputXY();
//var b = map.getTileOverlaps(car);
myGame.stage.context.font = '18px Arial';
myGame.stage.context.fillStyle = 'rgb(255,255,255)';
//myGame.stage.context.fillText(tile.toString(), 32, 32);
myGame.input.renderDebugInfo(32, 64, 'rgb(255,255,255)');
myGame.stage.context.fillStyle = 'rgb(255,255,255)';
myGame.stage.context.fillText(mo.x + ' ' + mo.y + ' ' + mo.w + ' ' + mo.h, 32, 200);
myGame.stage.context.fillText(car.bounds.x + ' ' + car.bounds.y + ' ' + car.bounds.width + ' ' + car.bounds.height, 32, 232);
var i = 0;
for(var y = mo.y; y < mo.y + mo.h; y++) {
for(var x = mo.x; x < mo.x + mo.w; x++) {
if(mo.collision[i] == true) {
myGame.stage.context.fillStyle = 'rgba(255,0,0,0.5)';
} else {
myGame.stage.context.fillStyle = 'rgba(0,255,0,0.5)';
}
myGame.stage.context.fillRect(x * 16, y * 16, 16, 16);
i++;
}
}
// Collide the space ship with the particles
myGame.collide(car, emitter);
// Collide everything with the map
//map.collide();
map.collide(emitter);
}
})();
+28 -62
View File
@@ -1,6 +1,20 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/Game.ts" />
class customParticle2 extends Phaser.Particle {
constructor(game:Phaser.Game) {
super(game);
var s = ['carrot', 'melon', 'eggplant', 'mushroom', 'pineapple'];
this.loadGraphic(game.math.getRandom(s));
this.elasticity = 0.8;
}
}
(function () {
var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
@@ -10,9 +24,11 @@
myGame.loader.addTextFile('platform', 'assets/maps/platform-test-1.json');
myGame.loader.addImageFile('tiles', 'assets/tiles/platformer_tiles.png');
myGame.loader.addImageFile('ufo', 'assets/sprites/ufo.png');
myGame.loader.addImageFile('ilkke', 'assets/sprites/ilkke.png');
myGame.loader.addImageFile('chunk', 'assets/sprites/chunk.png');
myGame.loader.addImageFile('healthbar', 'assets/sprites/healthbar.png');
myGame.loader.addImageFile('carrot', 'assets/sprites/carrot.png');
myGame.loader.addImageFile('melon', 'assets/sprites/melon.png');
myGame.loader.addImageFile('eggplant', 'assets/sprites/eggplant.png');
myGame.loader.addImageFile('mushroom', 'assets/sprites/mushroom.png');
myGame.loader.addImageFile('pineapple', 'assets/sprites/pineapple.png');
myGame.loader.load();
@@ -20,12 +36,9 @@
var map: Phaser.Tilemap;
var car: Phaser.Sprite;
var marker: Phaser.GeomSprite;
var tile: Phaser.Tile;
var emitter: Phaser.Emitter;
var mo;
function create() {
map = myGame.createTilemap('tiles', 'platform', Phaser.Tilemap.FORMAT_TILED_JSON);
@@ -36,38 +49,24 @@
myGame.input.keyboard.addKeyCapture([Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.UP, Phaser.Keyboard.DOWN]);
emitter = myGame.createEmitter(32, 32);
emitter = myGame.createEmitter(32, 80);
emitter.width = 700;
emitter.makeParticles(null, 50, 0, false, 0);
emitter.particleClass = customParticle2;
emitter.makeParticles(null, 100, 0, false, 0.7);
emitter.gravity = 100;
emitter.setRotation(0,0);
emitter.start(false);
emitter.start(false, 10, 0.05);
car = myGame.createSprite(250, 64, 'ufo');
car.renderRotation = false;
//car.renderDebug = true;
car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
//car.velocity.y = 10;
marker = myGame.createGeomSprite(0, 0);
marker.createRectangle(16, 16);
marker.renderFill = false;
marker.visible = false;
//myGame.onRenderCallback = render;
}
function update() {
marker.x = myGame.math.snapToFloor(myGame.input.worldX, 16);
marker.y = myGame.math.snapToFloor(myGame.input.worldY, 16);
//myGame.collide(car, map.currentLayer);
car.velocity.x = 0;
car.velocity.y = 0;
@@ -89,45 +88,12 @@
car.velocity.y = 200;
}
mo = map.collide(car);
//map.getTileOverlaps()
// Collide the space ship with the particles
myGame.collide(car, emitter);
}
function render() {
tile = map.getTileFromInputXY();
//var b = map.getTileOverlaps(car);
myGame.stage.context.font = '18px Arial';
myGame.stage.context.fillStyle = 'rgb(255,255,255)';
//myGame.stage.context.fillText(tile.toString(), 32, 32);
myGame.input.renderDebugInfo(32, 64, 'rgb(255,255,255)');
myGame.stage.context.fillStyle = 'rgb(255,255,255)';
myGame.stage.context.fillText(mo.x + ' ' + mo.y + ' ' + mo.w + ' ' + mo.h, 32, 200);
myGame.stage.context.fillText(car.bounds.x + ' ' + car.bounds.y + ' ' + car.bounds.width + ' ' + car.bounds.height, 32, 232);
var i = 0;
for (var y = mo.y; y < mo.y + mo.h; y++)
{
for (var x = mo.x; x < mo.x + mo.w; x++)
{
if (mo.collision[i] == true)
{
myGame.stage.context.fillStyle = 'rgba(255,0,0,0.5)';
}
else
{
myGame.stage.context.fillStyle = 'rgba(0,255,0,0.5)';
}
myGame.stage.context.fillRect(x * 16, y * 16, 16, 16);
i++;
}
}
// Collide everything with the map
//map.collide();
map.collide(emitter);
}
+6067 -2213
View File
File diff suppressed because it is too large Load Diff