Little clean-up

This commit is contained in:
Evert Prants 2018-08-19 18:41:53 +03:00
parent 0b4bb54441
commit 29205d9298
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 32 additions and 25 deletions

2
dist/dwelibs.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,28 +1,24 @@
DWE.File = {}
DWE.File.httpGET = function (url, xml) {
DWE.File.httpGET = function (url) {
return new Promise(function (resolve, reject) {
var xmlHttp = new XMLHttpRequest()
xmlHttp.onreadystatechange = function() {
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) {
reject(xmlHttp.responseText)
}
}
xmlHttp.open('GET', url, true)
xmlHttp.responseType = xml ? 'xml' : 'text'
xmlHttp.responseType = 'text'
xmlHttp.send(null)
})
}
DWE.File.loadXML = function (url) {
return DWE.File.httpGET(url, true)
}
DWE.File.loadJson = function (url) {
return new Promise(function (resolve, reject) {
var promise = DWE.File.httpGET(url)

View File

@ -19,9 +19,9 @@ DWE.Tiled.Map = class {
}
done () {
if (this['onready']) {
if (this['onready'])
this.onready.apply(this, [this])
}
this.loaded = true
}
@ -44,15 +44,19 @@ DWE.Tiled.Map = class {
// Load map layers
function loadMapLayers () {
let width = mapData.width
let height = mapData.height
var width = mapData.width
var height = mapData.height
for (let i in mapData.layers) {
let layer = mapData.layers[i]
let lw = layer.width || width
let lh = layer.height || height
let tiles = []
if (layer.type != 'tilelayer' || !layer.visible) continue
for(let i = 0; i < width; i++) {
for(let j = 0; j < height; j++) {
for(let i = 0; i < lw; i++) {
for(let j = 0; j < lh; j++) {
let value = layer.data[(j * width) + i]
if (value == 0) continue
@ -84,25 +88,29 @@ DWE.Tiled.Map = class {
var xtile = mapData.tilesets[index]
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 doc = parser.parseFromString(data, 'application/xml')
var tilesets = doc.getElementsByTagName('tileset')
var dataf = {}
for (let i in tilesets) {
let d = tilesets[i]
if (!(d instanceof Element)) continue
let tw = parseInt(d.getAttribute('tilewidth'))
let th = parseInt(d.getAttribute('tileheight'))
let imgfile = d.getElementsByTagName('image')[0]
if (imgfile) {
let img = new DWE.Image.TileMap(map.dir + '/' + imgfile.getAttribute('source'), tw, th)
img.onready = function () {
loadNextTileMap(index + 1)
}
img.offset = xtile.firstgid
img.tileCount = parseInt(d.getAttribute('tilecount'))
map.tileMaps.push(img)
img.onready = function () {
loadNextTileMap(index + 1)
}
}
}
}, function (e) {
@ -118,14 +126,17 @@ DWE.Tiled.Map = class {
drawLayer (ctx, origin, viewport, name) {
if (!this.layers[name]) return null
let layer = this.layers[name]
var layer = this.layers[name]
for (let i in this.tileMaps) {
let t = this.tileMaps[i]
let tileMap = this.tileMaps[i]
for (let j in layer.tiles) {
let tile = layer.tiles[j]
let tcount = t.tileCount || t.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,
let tcount = tileMap.tileCount || tileMap.tiles.length
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,
this.tileHeight * this.scale)
}
@ -135,7 +146,7 @@ DWE.Tiled.Map = class {
collideLayer (object, name) {
if (!this.layers[name]) return null
let layer = this.layers[name]
var layer = this.layers[name]
// TODO: Handle object collisions with map tiles
return null
}