mirror of
https://github.com/wassname/phaser.git
synced 2026-08-14 12:40:17 +08:00
Tilemap collision is now working but all Camera following seems to be broken as a result. Awesome.
This commit is contained in:
@@ -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
@@ -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,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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user