Created
July 6, 2013 00:45
-
-
Save Buildstarted/5938076 to your computer and use it in GitHub Desktop.
Basic Tiled map object for EndGate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Tiled { | |
| export class Tileset { | |
| firstgid: number; | |
| image: string; | |
| imageheight: number; | |
| imagewidth: number; | |
| margin: number; | |
| name: string; | |
| properties: {}; | |
| spacing: number; | |
| tileheight: number; | |
| tilewidth: number; | |
| } | |
| export class Layer { | |
| public data: number[]; | |
| public height: number; | |
| public name: string; | |
| public opacity: number; | |
| public type: string; | |
| public visible: bool; | |
| public width: number; | |
| public x: number; | |
| public y: number; | |
| public objects: any[]; | |
| public properties: {}; | |
| } | |
| export class Map { | |
| public height: number; | |
| public width: number; | |
| public orientation: string; | |
| public properties: {}; | |
| public tileheight: number; | |
| public tilewidth: number; | |
| public version: number; | |
| public tilesets: any[]; | |
| public layers: Layer[]; | |
| public static Extract(json: string) { | |
| var map: Map = JSON.parse(json); | |
| return map; | |
| } | |
| } | |
| export class TiledMap { | |
| private _zIndex: number = 0; | |
| public Map: Tiled.Map; | |
| constructor(map: Tiled.Map, position: eg.Vector2d, sceneryHandler: eg.Map.SceneryHandler, game: eg.Game, options?: any) { | |
| //find player layer | |
| console.log(options); | |
| options = options || { | |
| ShowCollisionBoundingBoxes: false | |
| }; | |
| this.Map = map; | |
| for (var i in map.layers) { | |
| var layer = map.layers[i]; | |
| if (layer.type && layer.type === "objectgroup" && layer.name === "player") { | |
| break; | |
| } | |
| //decrement the zIndex until we find a layer named player | |
| //this is the convention required to properly zIndex | |
| //the player in relation to the world | |
| this._zIndex--; | |
| } | |
| for (var i in map.layers) { | |
| var layer = map.layers[i]; | |
| var layerRect = { | |
| x: position.X * (map.width / 2) * map.tilewidth, | |
| y: position.Y * (map.height / 2) * map.tileheight, | |
| width: map.width * map.tilewidth, | |
| height: map.height * map.tileheight | |
| }; | |
| //look for a specific layer called collision | |
| //that we'll use to fill out the collision manager | |
| if (layer.name === "collision") { | |
| var i = 0; | |
| for (var y = 0; y < map.height; y++) { | |
| for (var x = 0; x < map.width; x++) { | |
| if (layer.data[i] !== 0) { | |
| if (options.ShowCollisionBoundingBoxes) { | |
| var rectangle = new eg.Graphics.Rectangle( | |
| x * map.tilewidth + (map.tilewidth / 2) + (layerRect.x * 2), | |
| y * map.tileheight + (map.tileheight / 2) + (layerRect.y * 2), | |
| map.tilewidth, | |
| map.tileheight | |
| ); | |
| rectangle.BorderColor = "#f00"; | |
| rectangle.BorderThickness = 1; | |
| game.Scene.Add(rectangle); | |
| } | |
| var offset = new eg.Vector2d( | |
| x * map.tilewidth + (map.tilewidth / 2) + (layerRect.x * 2), | |
| y * map.tileheight + (map.tileheight / 2) + (layerRect.y * 2) | |
| ); | |
| var boundingRectangle = new eg.Bounds.BoundingRectangle(offset, new eg.Size2d(map.tilewidth)); | |
| game.CollisionManager.Monitor(new eg.Collision.Collidable(boundingRectangle)); | |
| } | |
| i++; | |
| } | |
| } | |
| } | |
| else | |
| //standard tile layer. there are updates that need to be made though | |
| if (layer.type === "tilelayer") | |
| { | |
| var data = []; | |
| var i = 0; | |
| for (var y = 0; y < map.height; y++) { | |
| data[y] = []; | |
| for (var x = 0; x < map.width; x++) { | |
| data[y][x] = layer.data[i] - 1; | |
| i++; | |
| } | |
| } | |
| this.LoadTileMap(sceneryHandler, layerRect, data, map.tilesets[0].image, map.tilewidth, map.tileheight, this._zIndex); | |
| } | |
| this._zIndex++; | |
| } | |
| } | |
| private LoadTileMap(sceneryHandler : eg.Map.SceneryHandler, offset, data, tileset: string, tileWidth: number, tileHeight: number, zIndex: number) { | |
| var resourceSheet: eg.Graphics.Assets.ImageSource = new eg.Graphics.Assets.ImageSource(tileset); | |
| resourceSheet.OnLoaded.Bind(() => { | |
| this.AddTileMap(sceneryHandler, offset, data, resourceSheet, tileWidth, tileHeight, zIndex); | |
| }); | |
| } | |
| private AddTileMap(sceneryHandler, offset, data, resourceSheet, tileWidth, tileHeight, zIndex) { | |
| var resources = eg.Map.SquareTileMap.ExtractTiles(resourceSheet, tileWidth, tileHeight); | |
| var tileMap = new eg.Map.SquareTileMap( | |
| (offset.x * 2) + (offset.width / 2), | |
| (offset.y * 2) + (offset.height / 2), | |
| tileWidth, | |
| tileHeight, | |
| resources, | |
| data, | |
| true | |
| ); | |
| tileMap.ZIndex = zIndex; | |
| sceneryHandler.AddLayer(tileMap); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment