Created
August 6, 2017 03:42
-
-
Save anonymous/6e07a81e42faffff52f4f904262d4eed to your computer and use it in GitHub Desktop.
This file contains 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
import * as p from 'pixi.js' | |
const DEFAULT_TILE_WIDTH = 48 | |
const DEFAULT_TILE_HEIGHT = 48 | |
const DEFAULT_MAP_DIMENSIONS = 16 | |
/** | |
* Bootstrap the application | |
*/ | |
const application = new p.Application( | |
window.innerWidth, | |
window.innerWidth, | |
{ | |
backgroundColor : 0x303539 | |
} | |
) | |
document | |
.getElementById('viewport') | |
.appendChild(application.view) | |
/** | |
* Create a map for our agent to navigate through | |
** | |
* 0: grass/empty tile | |
* 1: wall/blocked tile | |
* 2: agent | |
* 3: goal | |
*/ | |
const mapData = [ | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
] | |
/** | |
* Render the tilemap | |
*/ | |
const mapContainer = new p.Container() | |
for (let xi = 0; xi < DEFAULT_MAP_DIMENSIONS; xi++) { | |
for (let yi = 0; yi < DEFAULT_MAP_DIMENSIONS; yi++) { | |
let tileData = mapData[xi][yi] | |
let sprite = new p.Sprite(p.Texture.fromImage(`/assets/graphics/${tileData}.png`)) | |
sprite.anchor.set(0.5) | |
sprite.x = (xi % DEFAULT_MAP_DIMENSIONS) * 48 | |
sprite.y = Math.floor(yi / DEFAULT_MAP_DIMENSIONS) * 48 | |
mapContainer.addChild(sprite) | |
} | |
} | |
mapContainer.x = (application.renderer.width - mapContainer.width) | |
mapContainer.y = (application.renderer.height - mapContainer.height) | |
application.stage.addChild(mapContainer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment