Created
May 12, 2017 02:06
-
-
Save eevee/b9ca47b009198611b1f2d475151b0828 to your computer and use it in GitHub Desktop.
initial crack at potluck with pixi, because someone asked
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
| window.Potluck = {}; | |
| let _index_to_coords = function(index, columns) { | |
| return [index % columns, Math.floor(index / columns)]; | |
| }; | |
| Potluck.begin = function() { | |
| let renderer = PIXI.autoDetectRenderer(800, 600); | |
| document.body.appendChild(renderer.view); | |
| // FIXME custom xhr loader for this? | |
| PIXI.loader.add('maps/test.json'); | |
| PIXI.loader.load(on_map_load); | |
| let mapdata; | |
| function on_map_load() { | |
| console.log(arguments); | |
| mapdata = PIXI.loader.resources['maps/test.json'].data; | |
| // FIXME can i get the tileset constructor to do this or something | |
| PIXI.loader.add('sheet', 'maps/' + mapdata.tilesets[0].image); | |
| PIXI.loader.load(on_pixi_load); | |
| } | |
| function on_pixi_load() { | |
| let stage = new PIXI.Container; | |
| // FIXME can i put the tileset in resources? | |
| let tileset = new Potluck.TiledTileset(mapdata.tilesets[0], PIXI.loader.resources['sheet'].texture); | |
| for (let i = 0; i < mapdata.layers[0].data.length; i++) { | |
| let t = mapdata.layers[0].data[i]; | |
| if (t == 0) | |
| continue; | |
| let sprite = new PIXI.Sprite(tileset.get_subtex(t)); | |
| let [x, y] = _index_to_coords(i, mapdata.layers[0].width); | |
| sprite.x = x * tileset.rawdata.tilewidth; | |
| sprite.y = y * tileset.rawdata.tileheight; | |
| console.log(sprite); | |
| stage.addChild(sprite); | |
| } | |
| renderer.render(stage); | |
| } | |
| }; | |
| Potluck.TiledTileset = function(rawdata, texture) { | |
| this.rawdata = rawdata; | |
| this.texture = texture; | |
| this.rectangles = []; | |
| this.subtextures = []; | |
| }; | |
| Potluck.TiledTileset.prototype.get_rectangle = function(gt) { | |
| let t = gt - this.rawdata.firstgid; | |
| if (! this.rectangles[t]) { | |
| let [x, y] = _index_to_coords(t, this.rawdata.columns); | |
| let tw = this.rawdata.tilewidth; | |
| let th = this.rawdata.tileheight; | |
| this.rectangles[t] = new PIXI.Rectangle(x * tw, y * th, tw, th); | |
| } | |
| return this.rectangles[t]; | |
| }; | |
| Potluck.TiledTileset.prototype.get_subtex = function(gt) { | |
| let t = gt - this.rawdata.firstgid; | |
| if (! this.subtextures[t]) { | |
| this.subtextures[t] = new PIXI.Texture(this.texture, this.get_rectangle(gt)); | |
| } | |
| return this.subtextures[t]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment