mirror of
https://github.com/wassname/phaser.git
synced 2026-08-02 13:00:25 +08:00
Finish document for ScrollRegion, ScrollZone, Tilemap, Device, TilemapLayer.
This commit is contained in:
@@ -12,6 +12,17 @@ module Phaser {
|
||||
|
||||
export class ScrollRegion{
|
||||
|
||||
/**
|
||||
* ScrollRegion constructor
|
||||
* Create a new <code>ScrollRegion</code>.
|
||||
*
|
||||
* @param x {number} X position in world coordinate.
|
||||
* @param y {number} Y position in world coordinate.
|
||||
* @param width {number} Width of this object.
|
||||
* @param height {number} Height of this object.
|
||||
* @param speedX {number} X-axis scrolling speed.
|
||||
* @param speedY {number} Y-axis scrolling speed.
|
||||
*/
|
||||
constructor(x: number, y: number, width: number, height: number, speedX:number, speedY:number) {
|
||||
|
||||
// Our seamless scrolling quads
|
||||
@@ -38,9 +49,21 @@ module Phaser {
|
||||
private _inverseWidth: number = 0;
|
||||
private _inverseHeight: number = 0;
|
||||
|
||||
/**
|
||||
* Will this region be rendered? (default to true)
|
||||
* @type {boolean}
|
||||
*/
|
||||
public visible: bool = true;
|
||||
/**
|
||||
* Region scrolling speed.
|
||||
* @type {MicroPoint}
|
||||
*/
|
||||
public scrollSpeed: MicroPoint;
|
||||
|
||||
/**
|
||||
* Update region scrolling with tick time.
|
||||
* @param delta {number} Elapsed time since last update.
|
||||
*/
|
||||
public update(delta: number) {
|
||||
|
||||
this._scroll.x += this.scrollSpeed.x;
|
||||
@@ -102,6 +125,15 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Render this region to specific context.
|
||||
* @param context {CanvasRenderingContext2D} Canvas context this region will be rendered to.
|
||||
* @param texture {object} The texture to be rendered.
|
||||
* @param dx {number} X position in world coordinate.
|
||||
* @param dy {number} Y position in world coordinate.
|
||||
* @param width {number} Width of this region to be rendered.
|
||||
* @param height {number} Height of this region to be rendered.
|
||||
*/
|
||||
public render(context:CanvasRenderingContext2D, texture, dx: number, dy: number, dw: number, dh: number) {
|
||||
|
||||
if (this.visible == false)
|
||||
@@ -126,6 +158,21 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Crop part of the texture and render it to the given context.
|
||||
* @param context {CanvasRenderingContext2D} Canvas context the texture will be rendered to.
|
||||
* @param texture {object} Texture to be rendered.
|
||||
* @param srcX {number} Target region top-left x coordinate in the texture.
|
||||
* @param srcX {number} Target region top-left y coordinate in the texture.
|
||||
* @param srcW {number} Target region width in the texture.
|
||||
* @param srcH {number} Target region height in the texture.
|
||||
* @param destX {number} Render region top-left x coordinate in the context.
|
||||
* @param destX {number} Render region top-left y coordinate in the context.
|
||||
* @param destW {number} Target region width in the context.
|
||||
* @param destH {number} Target region height in the context.
|
||||
* @param offsetX {number} X offset to the context.
|
||||
* @param offsetY {number} Y offset to the context.
|
||||
*/
|
||||
private crop(context, texture, srcX, srcY, srcW, srcH, destX, destY, destW, destH, offsetX, offsetY) {
|
||||
|
||||
offsetX += destX;
|
||||
|
||||
@@ -15,6 +15,17 @@ module Phaser {
|
||||
|
||||
export class ScrollZone extends GameObject {
|
||||
|
||||
/**
|
||||
* ScrollZone constructor
|
||||
* Create a new <code>ScrollZone</code>.
|
||||
*
|
||||
* @param game {Phaser.Game} Current game instance.
|
||||
* @param key {string} Asset key for image texture of this object.
|
||||
* @param x {number} X position in world coordinate.
|
||||
* @param y {number} Y position in world coordinate.
|
||||
* @param width {number} Optional, width of this object.
|
||||
* @param height {number} Optional, height of this object.
|
||||
*/
|
||||
constructor(game: Game, key:string, x: number = 0, y: number = 0, width?: number = 0, height?: number = 0) {
|
||||
|
||||
super(game, x, y, width, height);
|
||||
@@ -49,19 +60,63 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Texture of this object.
|
||||
*/
|
||||
private _texture;
|
||||
/**
|
||||
* If this zone is larger than texture image, this will be filled with a pattern of texture.
|
||||
* @type {DynamicTexture}
|
||||
*/
|
||||
private _dynamicTexture: DynamicTexture = null;
|
||||
|
||||
// local rendering related temp vars to help avoid gc spikes
|
||||
/**
|
||||
* Local rendering related temp vars to help avoid gc spikes.
|
||||
* @type {number}
|
||||
*/
|
||||
private _dx: number = 0;
|
||||
/**
|
||||
* Local rendering related temp vars to help avoid gc spikes.
|
||||
* @type {number}
|
||||
*/
|
||||
private _dy: number = 0;
|
||||
/**
|
||||
* Local rendering related temp vars to help avoid gc spikes.
|
||||
* @type {number}
|
||||
*/
|
||||
private _dw: number = 0;
|
||||
/**
|
||||
* Local rendering related temp vars to help avoid gc spikes.
|
||||
* @type {number}
|
||||
*/
|
||||
private _dh: number = 0;
|
||||
|
||||
/**
|
||||
* Current region this zone is scrolling.
|
||||
* @type {ScrollRegion}
|
||||
*/
|
||||
public currentRegion: ScrollRegion;
|
||||
/**
|
||||
* Array contains all added regions.
|
||||
* @type {ScrollRegion[]}
|
||||
*/
|
||||
public regions: ScrollRegion[];
|
||||
/**
|
||||
* Flip this zone vertically? (default to false)
|
||||
* @type {boolean}
|
||||
*/
|
||||
public flipped: bool = false;
|
||||
|
||||
/**
|
||||
* Add a new region to this zone.
|
||||
* @param x {number} X position of the new region.
|
||||
* @param y {number} Y position of the new region.
|
||||
* @param width {number} Width of the new region.
|
||||
* @param height {number} Height of the new region.
|
||||
* @param speedX {number} Optional, x-axis scrolling speed.
|
||||
* @param speedY {number} Optional, y-axis scrolling speed.
|
||||
* @return {ScrollRegion} The newly added region.
|
||||
*/
|
||||
public addRegion(x: number, y: number, width: number, height: number, speedX?:number = 0, speedY?:number = 0):ScrollRegion {
|
||||
|
||||
if (x > this.width || y > this.height || x < 0 || y < 0 || (x + width) > this.width || (y + height) > this.height)
|
||||
@@ -78,6 +133,11 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set scrolling speed of current region.
|
||||
* @param x {number} X speed of current region.
|
||||
* @param y {number} Y speed of current region.
|
||||
*/
|
||||
public setSpeed(x: number, y: number) {
|
||||
|
||||
if (this.currentRegion)
|
||||
@@ -89,6 +149,9 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Update regions.
|
||||
*/
|
||||
public update() {
|
||||
|
||||
for (var i = 0; i < this.regions.length; i++)
|
||||
@@ -98,8 +161,13 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this zone is visible in a specific camera rectangle.
|
||||
* @param camera {Rectangle} The rectangle you want to check.
|
||||
* @return {boolean} Return true if bound of this zone intersects the given rectangle, otherwise return false.
|
||||
*/
|
||||
public inCamera(camera: Rectangle): bool {
|
||||
|
||||
|
||||
if (this.scrollFactor.x !== 1.0 || this.scrollFactor.y !== 1.0)
|
||||
{
|
||||
this._dx = this.bounds.x - (camera.x * this.scrollFactor.x);
|
||||
@@ -116,6 +184,13 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Render this zone object to a specific camera.
|
||||
* @param camera {Camera} The camera this object will be render to.
|
||||
* @param cameraOffsetX {number} X offset of camera.
|
||||
* @param cameraOffsetY {number} Y offset of camera.
|
||||
* @return Return false if not rendered, otherwise return true.
|
||||
*/
|
||||
public render(camera: Camera, cameraOffsetX: number, cameraOffsetY: number) {
|
||||
|
||||
// Render checks
|
||||
@@ -189,6 +264,10 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create repeating texture with _texture, and store it into the _dynamicTexture.
|
||||
* Used to create texture when texture image is small than size of the zone.
|
||||
*/
|
||||
private createRepeatingTexture(regionWidth: number, regionHeight: number) {
|
||||
|
||||
// Work out how many we'll need of the source image to make it tile properly
|
||||
|
||||
@@ -14,6 +14,18 @@ module Phaser {
|
||||
|
||||
export class Tilemap extends GameObject {
|
||||
|
||||
/**
|
||||
* Tilemap constructor
|
||||
* Create a new <code>Tilemap</code>.
|
||||
*
|
||||
* @param game {Phaser.Game} Current game instance.
|
||||
* @param key {string} Asset key for this map.
|
||||
* @param mapData {string} Data of this map. (a big 2d array, normally in csv)
|
||||
* @param format {number} Format of this map data, available: Tilemap.FORMAT_CSV or Tilemap.FORMAT_TILED_JSON.
|
||||
* @param resizeWorld {boolean} Resize the world bound automatically based on this tilemap?
|
||||
* @param tileWidth {number} Width of tiles in this map.
|
||||
* @param tileHeight {number} Height of tiles in this map.
|
||||
*/
|
||||
constructor(game: Game, key: string, mapData: string, format: number, resizeWorld: bool = true, tileWidth?: number = 0, tileHeight?: number = 0) {
|
||||
|
||||
super(game);
|
||||
@@ -45,20 +57,64 @@ module Phaser {
|
||||
|
||||
private _tempCollisionData;
|
||||
|
||||
/**
|
||||
* Tilemap data format enum: CSV.
|
||||
* @type {number}
|
||||
*/
|
||||
public static FORMAT_CSV: number = 0;
|
||||
/**
|
||||
* Tilemap data format enum: Tiled JSON.
|
||||
* @type {number}
|
||||
*/
|
||||
public static FORMAT_TILED_JSON: number = 1;
|
||||
|
||||
/**
|
||||
* Array contains tile objects of this map.
|
||||
* @type {Tile[]}
|
||||
*/
|
||||
public tiles : Tile[];
|
||||
/**
|
||||
* Array contains tilemap layer objects of this map.
|
||||
* @type {TilemapLayer[]}
|
||||
*/
|
||||
public layers : TilemapLayer[];
|
||||
/**
|
||||
* Current tilemap layer.
|
||||
* @type {TilemapLayer}
|
||||
*/
|
||||
public currentLayer: TilemapLayer;
|
||||
/**
|
||||
* The tilemap layer for collision.
|
||||
* @type {TilemapLayer}
|
||||
*/
|
||||
public collisionLayer: TilemapLayer;
|
||||
/**
|
||||
* Tilemap collision callback.
|
||||
* @type {function}
|
||||
*/
|
||||
public collisionCallback = null;
|
||||
/**
|
||||
* Context for the collision callback called with.
|
||||
*/
|
||||
public collisionCallbackContext;
|
||||
/**
|
||||
* Format of this tilemap data. Available values: Tilemap.FORMAT_CSV or Tilemap.FORMAT_TILED_JSON.
|
||||
* @type {number}
|
||||
*/
|
||||
public mapFormat: number;
|
||||
|
||||
/**
|
||||
* Inherited update method.
|
||||
*/
|
||||
public update() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
@@ -72,6 +128,13 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Parset csv map data and generate tiles.
|
||||
* @param data {string} CSV map data.
|
||||
* @param key {string} Asset key for tileset image.
|
||||
* @param tileWidth {number} Width of its tile.
|
||||
* @param tileHeight {number} Height of its tile.
|
||||
*/
|
||||
private parseCSV(data: string, key: string, tileWidth: number, tileHeight: number) {
|
||||
|
||||
var layer: TilemapLayer = new TilemapLayer(this._game, this, key, Tilemap.FORMAT_CSV, 'TileLayerCSV' + this.layers.length.toString(), tileWidth, tileHeight);
|
||||
@@ -103,6 +166,11 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Parset JSON map data and generate tiles.
|
||||
* @param data {string} JSON map data.
|
||||
* @param key {string} Asset key for tileset image.
|
||||
*/
|
||||
private parseTiledJSON(data: string, key: string) {
|
||||
|
||||
// Trim any rogue whitespace from the data
|
||||
@@ -113,7 +181,7 @@ module Phaser {
|
||||
for (var i = 0; i < json.layers.length; i++)
|
||||
{
|
||||
var layer: TilemapLayer = new TilemapLayer(this._game, this, key, Tilemap.FORMAT_TILED_JSON, json.layers[i].name, json.tilewidth, json.tileheight);
|
||||
|
||||
|
||||
layer.alpha = json.layers[i].opacity;
|
||||
layer.visible = json.layers[i].visible;
|
||||
layer.tileMargin = json.tilesets[0].margin;
|
||||
@@ -155,6 +223,10 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create tiles of given quantity.
|
||||
* @param qty {number} Quentity of tiles to be generated.
|
||||
*/
|
||||
private generateTiles(qty:number) {
|
||||
|
||||
for (var i = 0; i < qty; i++)
|
||||
@@ -174,6 +246,11 @@ module Phaser {
|
||||
|
||||
// Tile Collision
|
||||
|
||||
/**
|
||||
* Set callback to be called when this tilemap collides.
|
||||
* @param context {object} Callback will be called with this context.
|
||||
* @param callback {function} Callback function.
|
||||
*/
|
||||
public setCollisionCallback(context, callback) {
|
||||
|
||||
this.collisionCallbackContext = context;
|
||||
@@ -181,6 +258,15 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set collision configs of tiles in a range index.
|
||||
* @param start {number} First index of tiles.
|
||||
* @param end {number} Last index of tiles.
|
||||
* @param collision {number} Bit field of flags. (see Tile.allowCollision)
|
||||
* @param resetCollisions {boolean} Reset collision flags before set.
|
||||
* @param separateX {boolean} Enable seprate at x-axis.
|
||||
* @param separateY {boolean} Enable seprate at y-axis.
|
||||
*/
|
||||
public setCollisionRange(start: number, end: number, collision?:number = Collision.ANY, resetCollisions?: bool = false, separateX?: bool = true, separateY?: bool = true) {
|
||||
|
||||
for (var i = start; i < end; i++)
|
||||
@@ -190,6 +276,14 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set collision configs of tiles with given index.
|
||||
* @param values {number[]} Index array which contains all tile indexes. The tiles with those indexes will be setup with rest parameters.
|
||||
* @param collision {number} Bit field of flags. (see Tile.allowCollision)
|
||||
* @param resetCollisions {boolean} Reset collision flags before set.
|
||||
* @param separateX {boolean} Enable seprate at x-axis.
|
||||
* @param separateY {boolean} Enable seprate at y-axis.
|
||||
*/
|
||||
public setCollisionByIndex(values:number[], collision?:number = Collision.ANY, resetCollisions?: bool = false, separateX?: bool = true, separateY?: bool = true) {
|
||||
|
||||
for (var i = 0; i < values.length; i++)
|
||||
@@ -201,6 +295,11 @@ module Phaser {
|
||||
|
||||
// Tile Management
|
||||
|
||||
/**
|
||||
* Get the tile by its index.
|
||||
* @param value {number} Index of the tile you want to get.
|
||||
* @return {Tile} The tile with given index.
|
||||
*/
|
||||
public getTileByIndex(value: number):Tile {
|
||||
|
||||
if (this.tiles[value])
|
||||
@@ -212,12 +311,26 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the tile located at specific position and layer.
|
||||
* @param x {number} X position of this tile located.
|
||||
* @param y {number} Y position of this tile located.
|
||||
* @param layer {number} Optional, layer of this tile located.
|
||||
* @return {Tile} The tile with specific properties.
|
||||
*/
|
||||
public getTile(x: number, y: number, layer?: number = 0):Tile {
|
||||
|
||||
return this.tiles[this.layers[layer].getTileIndex(x, y)];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the tile located at specific position (in world coordinate) and layer. (thus you give a position of a point which is within the tile)
|
||||
* @param x {number} X position of the point in target tile.
|
||||
* @param x {number} Y position of the point in target tile.
|
||||
* @param layer {number} Optional, layer of this tile located.
|
||||
* @return {Tile} The tile with specific properties.
|
||||
*/
|
||||
public getTileFromWorldXY(x: number, y: number, layer?: number = 0):Tile {
|
||||
|
||||
return this.tiles[this.layers[layer].getTileFromWorldXY(x, y)];
|
||||
@@ -230,6 +343,11 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tiles overlaps the given object.
|
||||
* @param object {GameObject} Tiles you want to get that overlaps this.
|
||||
* @return {array} Array with tiles informations. (Each contains x, y and the tile.)
|
||||
*/
|
||||
public getTileOverlaps(object: GameObject) {
|
||||
|
||||
return this.currentLayer.getTileOverlaps(object);
|
||||
@@ -237,6 +355,13 @@ module Phaser {
|
||||
}
|
||||
|
||||
// COLLIDE
|
||||
/**
|
||||
* Check whether this tilemap collides with the given game object or group of objects.
|
||||
* @param objectOrGroup {function} Target object of group you want to check.
|
||||
* @param callback {function} This is called if objectOrGroup collides the tilemap.
|
||||
* @param context {object} Callback will be called with this context.
|
||||
* @return {boolean} Return true if this collides with given object, otherwise return false.
|
||||
*/
|
||||
public collide(objectOrGroup = null, callback = null, context = null) {
|
||||
|
||||
if (callback !== null && context !== null)
|
||||
@@ -262,6 +387,11 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this tilemap collides with the given game object.
|
||||
* @param object {GameObject} Target object you want to check.
|
||||
* @return {boolean} Return true if this collides with given object, otherwise return false.
|
||||
*/
|
||||
public collideGameObject(object: GameObject): bool {
|
||||
|
||||
if (object !== this && object.immovable == false && object.exists == true && object.allowCollisions != Collision.NONE)
|
||||
@@ -282,6 +412,13 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a tile to a specific layer.
|
||||
* @param x {number} X position of this tile.
|
||||
* @param y {number} Y position of this tile.
|
||||
* @param index {number} The index of this tile type in the core map data.
|
||||
* @param layer {number} Optional, which layer you want to set the tile to.
|
||||
*/
|
||||
public putTile(x: number, y: number, index: number, layer?: number = 0) {
|
||||
|
||||
this.layers[layer].putTile(x, y, index);
|
||||
|
||||
Reference in New Issue
Block a user