summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2016-01-06 08:52:06 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2016-01-06 08:52:06 +0100
commit4fa246628bd9e5911ae998162f0bdc724cc739d2 (patch)
treefade72830f5bc1cc8c76e5a78a8ae65290c37a8b
parent6ed351b495db9e9b86066a419df43dd959ae94d3 (diff)
downloadrpgedit-4fa246628bd9e5911ae998162f0bdc724cc739d2.tar
rpgedit-4fa246628bd9e5911ae998162f0bdc724cc739d2.zip
Add entity rendering
-rw-r--r--src/app.coffee11
-rw-r--r--src/control/MapContext.coffee29
-rw-r--r--src/model/Direction.coffee13
-rw-r--r--src/model/Entity.coffee8
-rw-r--r--src/model/EntityPosition.coffee8
-rw-r--r--src/model/MapData.coffee (renamed from src/model/Map.coffee)5
-rw-r--r--src/model/Position.coffee10
-rw-r--r--src/util.coffee15
-rw-r--r--src/view/MapView.coffee40
9 files changed, 118 insertions, 21 deletions
diff --git a/src/app.coffee b/src/app.coffee
index 6c64c5a..ca3ddf8 100644
--- a/src/app.coffee
+++ b/src/app.coffee
@@ -2,19 +2,20 @@
require './style.css'
+window._ = require 'lodash'
-Map = require './model/Map'
-MapView = require './view/MapView'
+MapData = require './model/MapData'
+MapContext = require './control/MapContext'
-mapView = null
+mapContext = null
window.onload = ->
xhr = new XMLHttpRequest()
xhr.onload = ->
- mapDef = new Map(JSON.parse this.responseText)
- mapView = new MapView mapDef
+ mapDef = new MapData(JSON.parse this.responseText)
+ mapContext = new MapContext mapDef
xhr.open 'GET', 'resources/map/test.json', true
xhr.send()
diff --git a/src/control/MapContext.coffee b/src/control/MapContext.coffee
new file mode 100644
index 0000000..df6efec
--- /dev/null
+++ b/src/control/MapContext.coffee
@@ -0,0 +1,29 @@
+'use strict'
+
+
+Direction = require '../model/Direction'
+Entity = require '../model/Entity'
+EntityPosition = require '../model/EntityPosition'
+Position = require '../model/Position'
+
+MapView = require '../view/MapView'
+
+
+class MapContext
+ constructor: (@map) ->
+ @entities = {}
+
+ @playerEntity = new EntityPosition(
+ new Entity('square'),
+ new Position(8, 8),
+ Direction.EAST)
+
+ @addEntity(@playerEntity)
+
+ @mavView = new MapView @map, @entities
+
+ addEntity: (entity) =>
+ @entities[entity.position.asString()] = entity
+
+
+module.exports = MapContext
diff --git a/src/model/Direction.coffee b/src/model/Direction.coffee
new file mode 100644
index 0000000..5c49c80
--- /dev/null
+++ b/src/model/Direction.coffee
@@ -0,0 +1,13 @@
+'use strict'
+
+
+Direction =
+ NORTH: 0
+ EAST: 1
+ SOUTH: 2
+ WEST: 3
+
+ reverse: (d) -> (d+2)%4
+
+
+module.exports = Direction
diff --git a/src/model/Entity.coffee b/src/model/Entity.coffee
new file mode 100644
index 0000000..70c81fa
--- /dev/null
+++ b/src/model/Entity.coffee
@@ -0,0 +1,8 @@
+'use strict'
+
+
+class Entity
+ constructor: (@name) ->
+
+
+module.exports = Entity
diff --git a/src/model/EntityPosition.coffee b/src/model/EntityPosition.coffee
new file mode 100644
index 0000000..aaf9531
--- /dev/null
+++ b/src/model/EntityPosition.coffee
@@ -0,0 +1,8 @@
+'use strict'
+
+
+class EntityPosition
+ constructor: (@entity, @position, @direction) ->
+
+
+module.exports = EntityPosition
diff --git a/src/model/Map.coffee b/src/model/MapData.coffee
index e97b1ed..39b2140 100644
--- a/src/model/Map.coffee
+++ b/src/model/MapData.coffee
@@ -1,8 +1,9 @@
'use strict'
-class Map
+
+class MapData
constructor: (data) ->
{@tiles, @collition, @layers} = data
-module.exports = Map
+module.exports = MapData
diff --git a/src/model/Position.coffee b/src/model/Position.coffee
new file mode 100644
index 0000000..08bb999
--- /dev/null
+++ b/src/model/Position.coffee
@@ -0,0 +1,10 @@
+'use strict'
+
+
+class Position
+ constructor: (@x, @y) ->
+
+ asString: => "#{@x},#{@y}"
+
+
+module.exports = Position
diff --git a/src/util.coffee b/src/util.coffee
index d2cea04..4e0cd76 100644
--- a/src/util.coffee
+++ b/src/util.coffee
@@ -1,12 +1,13 @@
'use strict'
-_ = require 'lodash'
module.exports =
mapPromises: (promises) ->
- _.reduce promises, ((seq, v, k) ->
- seq.then (acc) ->
- v.then (r) ->
- acc[k] = r
- acc
- ), Promise.resolve {}
+ p = []
+ ret = {}
+
+ for own k, v of promises
+ do (k, v) ->
+ p.push(v.then (r) -> ret[k] = r)
+
+ Promise.all(p).then -> ret
diff --git a/src/view/MapView.coffee b/src/view/MapView.coffee
index cafc0aa..e051d48 100644
--- a/src/view/MapView.coffee
+++ b/src/view/MapView.coffee
@@ -1,6 +1,5 @@
'use strict'
-_ = require 'lodash'
util = require '../util'
@@ -19,12 +18,20 @@ loadImage = (url) ->
loadImages = (imgs) ->
util.mapPromises(_.mapValues imgs, loadImage)
-loadTiles = (tileDef) ->
- loadImages(_.mapValues tileDef, (t) -> "resources/sprite/tile/#{t.file}.png")
+loadTiles = (tiles) ->
+ loadImages(_.mapValues tiles, (t) -> "resources/sprite/tile/#{t.file}.png")
+
+loadEntities = (entities) ->
+ p = {}
+ for e in entities
+ do (e) ->
+ p[e.entity.name] = loadImage "resources/sprite/entity/#{e.entity.name}.png"
+
+ util.mapPromises p
class MapView
- constructor: (@map) ->
+ constructor: (@map, @entities) ->
@redrawPending = false
@canvas = document.createElement 'canvas'
@@ -36,22 +43,38 @@ class MapView
window.addEventListener 'resize', @setSize
@setSize()
- @ready = loadTiles(@map.tiles).then (tiles) =>
+ tilesReady = loadTiles(@map.tiles).then (tiles) =>
@tiles = tiles
+
+ entitiesReady = loadEntities(_.values @entities).then (entities) =>
+ @entitySprites = entities
+
+ tilesReady.then(entitiesReady).then =>
@redraw()
return
drawTile: (x, y, tile) =>
return unless tile
- @ctx.drawImage(tile, x, y)
+ @ctx.drawImage tile, x, y
+
+ drawEntity: (e) =>
+ sprite = @entitySprites[e.entity.name]
+ return unless sprite
+
+ @ctx.drawImage(
+ sprite,
+ e.direction*tileSize, 0,
+ tileSize, tileSize,
+ e.position.x*tileSize, e.position.y*tileSize,
+ tileSize, tileSize)
draw: =>
@redrawPending = false
@ctx.clearRect 0, 0, @canvas.width, @canvas.height
- return unless @tiles
+ return unless @tiles and @entitySprites
for layer in @map.layers
y = 0
@@ -65,6 +88,9 @@ class MapView
y += tileSize
+ for e in _.values @entities
+ @drawEntity e
+
redraw: =>
unless @redrawPending
@redrawPending = true