Skip to content

Instantly share code, notes, and snippets.

Created August 6, 2017 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6e07a81e42faffff52f4f904262d4eed to your computer and use it in GitHub Desktop.
Save anonymous/6e07a81e42faffff52f4f904262d4eed to your computer and use it in GitHub Desktop.
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