Little clean-up
This commit is contained in:
parent
0b4bb54441
commit
29205d9298
2
dist/dwelibs.min.js
vendored
2
dist/dwelibs.min.js
vendored
File diff suppressed because one or more lines are too long
10
src/file.js
10
src/file.js
@ -1,28 +1,24 @@
|
|||||||
|
|
||||||
DWE.File = {}
|
DWE.File = {}
|
||||||
|
|
||||||
DWE.File.httpGET = function (url, xml) {
|
DWE.File.httpGET = function (url) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
var xmlHttp = new XMLHttpRequest()
|
var xmlHttp = new XMLHttpRequest()
|
||||||
|
|
||||||
xmlHttp.onreadystatechange = function() {
|
xmlHttp.onreadystatechange = function() {
|
||||||
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
|
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
|
||||||
resolve(xml === true ? xmlHttp.response : xmlHttp.responseText)
|
resolve(xmlHttp.responseText)
|
||||||
} else if (xmlHttp.readyState == 4 && xmlHttp.status >= 400) {
|
} else if (xmlHttp.readyState == 4 && xmlHttp.status >= 400) {
|
||||||
reject(xmlHttp.responseText)
|
reject(xmlHttp.responseText)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
xmlHttp.open('GET', url, true)
|
xmlHttp.open('GET', url, true)
|
||||||
xmlHttp.responseType = xml ? 'xml' : 'text'
|
xmlHttp.responseType = 'text'
|
||||||
xmlHttp.send(null)
|
xmlHttp.send(null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
DWE.File.loadXML = function (url) {
|
|
||||||
return DWE.File.httpGET(url, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
DWE.File.loadJson = function (url) {
|
DWE.File.loadJson = function (url) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
var promise = DWE.File.httpGET(url)
|
var promise = DWE.File.httpGET(url)
|
||||||
|
45
src/tiled.js
45
src/tiled.js
@ -19,9 +19,9 @@ DWE.Tiled.Map = class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
done () {
|
done () {
|
||||||
if (this['onready']) {
|
if (this['onready'])
|
||||||
this.onready.apply(this, [this])
|
this.onready.apply(this, [this])
|
||||||
}
|
|
||||||
this.loaded = true
|
this.loaded = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,15 +44,19 @@ DWE.Tiled.Map = class {
|
|||||||
|
|
||||||
// Load map layers
|
// Load map layers
|
||||||
function loadMapLayers () {
|
function loadMapLayers () {
|
||||||
let width = mapData.width
|
var width = mapData.width
|
||||||
let height = mapData.height
|
var height = mapData.height
|
||||||
|
|
||||||
for (let i in mapData.layers) {
|
for (let i in mapData.layers) {
|
||||||
let layer = mapData.layers[i]
|
let layer = mapData.layers[i]
|
||||||
|
let lw = layer.width || width
|
||||||
|
let lh = layer.height || height
|
||||||
let tiles = []
|
let tiles = []
|
||||||
|
|
||||||
if (layer.type != 'tilelayer' || !layer.visible) continue
|
if (layer.type != 'tilelayer' || !layer.visible) continue
|
||||||
|
|
||||||
for(let i = 0; i < width; i++) {
|
for(let i = 0; i < lw; i++) {
|
||||||
for(let j = 0; j < height; j++) {
|
for(let j = 0; j < lh; j++) {
|
||||||
let value = layer.data[(j * width) + i]
|
let value = layer.data[(j * width) + i]
|
||||||
if (value == 0) continue
|
if (value == 0) continue
|
||||||
|
|
||||||
@ -84,25 +88,29 @@ DWE.Tiled.Map = class {
|
|||||||
var xtile = mapData.tilesets[index]
|
var xtile = mapData.tilesets[index]
|
||||||
var path = map.dir + '/' + xtile.source
|
var path = map.dir + '/' + xtile.source
|
||||||
|
|
||||||
DWE.File.loadXML(path).then(function (data) {
|
DWE.File.httpGET(path).then(function (data) {
|
||||||
var parser = new DOMParser()
|
var parser = new DOMParser()
|
||||||
var doc = parser.parseFromString(data, 'application/xml')
|
var doc = parser.parseFromString(data, 'application/xml')
|
||||||
var tilesets = doc.getElementsByTagName('tileset')
|
var tilesets = doc.getElementsByTagName('tileset')
|
||||||
var dataf = {}
|
|
||||||
for (let i in tilesets) {
|
for (let i in tilesets) {
|
||||||
let d = tilesets[i]
|
let d = tilesets[i]
|
||||||
if (!(d instanceof Element)) continue
|
if (!(d instanceof Element)) continue
|
||||||
|
|
||||||
let tw = parseInt(d.getAttribute('tilewidth'))
|
let tw = parseInt(d.getAttribute('tilewidth'))
|
||||||
let th = parseInt(d.getAttribute('tileheight'))
|
let th = parseInt(d.getAttribute('tileheight'))
|
||||||
let imgfile = d.getElementsByTagName('image')[0]
|
let imgfile = d.getElementsByTagName('image')[0]
|
||||||
|
|
||||||
if (imgfile) {
|
if (imgfile) {
|
||||||
let img = new DWE.Image.TileMap(map.dir + '/' + imgfile.getAttribute('source'), tw, th)
|
let img = new DWE.Image.TileMap(map.dir + '/' + imgfile.getAttribute('source'), tw, th)
|
||||||
img.onready = function () {
|
|
||||||
loadNextTileMap(index + 1)
|
|
||||||
}
|
|
||||||
img.offset = xtile.firstgid
|
img.offset = xtile.firstgid
|
||||||
img.tileCount = parseInt(d.getAttribute('tilecount'))
|
img.tileCount = parseInt(d.getAttribute('tilecount'))
|
||||||
map.tileMaps.push(img)
|
map.tileMaps.push(img)
|
||||||
|
|
||||||
|
img.onready = function () {
|
||||||
|
loadNextTileMap(index + 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, function (e) {
|
}, function (e) {
|
||||||
@ -118,14 +126,17 @@ DWE.Tiled.Map = class {
|
|||||||
|
|
||||||
drawLayer (ctx, origin, viewport, name) {
|
drawLayer (ctx, origin, viewport, name) {
|
||||||
if (!this.layers[name]) return null
|
if (!this.layers[name]) return null
|
||||||
let layer = this.layers[name]
|
var layer = this.layers[name]
|
||||||
|
|
||||||
for (let i in this.tileMaps) {
|
for (let i in this.tileMaps) {
|
||||||
let t = this.tileMaps[i]
|
let tileMap = this.tileMaps[i]
|
||||||
|
|
||||||
for (let j in layer.tiles) {
|
for (let j in layer.tiles) {
|
||||||
let tile = layer.tiles[j]
|
let tile = layer.tiles[j]
|
||||||
let tcount = t.tileCount || t.tiles.length
|
let tcount = tileMap.tileCount || tileMap.tiles.length
|
||||||
if (tile.tile >= t.offset && tile.tile <= tcount) {
|
|
||||||
t.draw(ctx, tile.tile - 1, ((tile.x * this.tileWidth * this.scale) + origin.x + layer.x) + viewport.x,
|
if (tile.tile >= tileMap.offset && tile.tile <= tcount) {
|
||||||
|
tileMap.draw(ctx, tile.tile - 1, ((tile.x * this.tileWidth * this.scale) + origin.x + layer.x) + viewport.x,
|
||||||
((tile.y * this.tileHeight * this.scale) + origin.y + layer.y) + viewport.y, this.tileWidth * this.scale,
|
((tile.y * this.tileHeight * this.scale) + origin.y + layer.y) + viewport.y, this.tileWidth * this.scale,
|
||||||
this.tileHeight * this.scale)
|
this.tileHeight * this.scale)
|
||||||
}
|
}
|
||||||
@ -135,7 +146,7 @@ DWE.Tiled.Map = class {
|
|||||||
|
|
||||||
collideLayer (object, name) {
|
collideLayer (object, name) {
|
||||||
if (!this.layers[name]) return null
|
if (!this.layers[name]) return null
|
||||||
let layer = this.layers[name]
|
var layer = this.layers[name]
|
||||||
// TODO: Handle object collisions with map tiles
|
// TODO: Handle object collisions with map tiles
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user