From 79979dce661fd668fa15d8d9501925ab62c29335 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Tue, 11 Feb 2020 08:46:52 +0100 Subject: [PATCH] some code optimisation --- engine.js | 83 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/engine.js b/engine.js index faeecc2..d0a87c4 100644 --- a/engine.js +++ b/engine.js @@ -78,20 +78,26 @@ function Textfile(txtfile) { } function Map(textdef) { - this.frametime = performance.now(); this.info = gamedata.load(textdef); - this.tileset = gamedata.load(this.info.header.tileset); - gfx.load(this.tileset.img); + const tileset = gamedata.load(this.info.header.tileset); + const frametime = performance.now(); + gfx.load(tileset.img); - var h = this.info.header.height, w = this.info.header.width; - var th = this.info.header.tileheight, tw = this.info.header.tilewidth; + const h = this.info.header.height, w = this.info.header.width; + const th = this.info.header.tileheight, tw = this.info.header.tilewidth; var posx = canvas.canvas.width / 2, posy = canvas.canvas.height / 2 - h * th/2; + const dx = [], dy = []; // precalculated tile positions + for ( let y = 0; y < h; y++ ) for ( let x = 0; x < w; x++ ) { + dx[y * w + x] = (w + x - y) * tw / 2; + dy[y * w + x] = (x + y) * th / 2; + } + canvas.fillStyle = "rgba("+this.info.header.background_color+")"; this.tileAt = function(x, y) { - nx = (y + th / 2) / th + (x - w * tw / 2) / tw |0; - ny = (y + th / 2) / th - (x - w * tw / 2) / tw |0; + var r = (y + th / 2) / th; var c = (x - w * tw /2) / tw; + var nx = r + c |0; var ny = r - c |0; return ny * w + nx; } @@ -102,57 +108,56 @@ function Map(textdef) { } this.draw = function(mobs) { - var x, y, dx, dy, i; - var bg = this.info.layer.find(l => l.type == "background").data; - var ob = this.info.layer.find(l => l.type == "object").data; - var mm = []; + const bg = this.info.layer.find(l => l.type == "background").data; + const ob = this.info.layer.find(l => l.type == "object").data; + var x, y, i, mm = []; + mobs.forEach(m => { - i = this.tileAt(m.position[0], m.position[1]); + let i = this.tileAt(m.position[0], m.position[1]); mm[i] = mobs.filter(m => i == this.tileAt(m.position[0], m.position[1])); }); canvas.fillRect(0,0, canvas.canvas.width, canvas.canvas.height); for ( y = 0; y < h; y++ ) for ( x = 0; x < w; x++ ) { i = y * h + x; - dx = posx + (w + x - y) * tw /2; - dy = posy + (x + y) * th / 2; - this.draw_tile(bg[i], dx, dy); + draw_tile(bg[i], posx + dx[i], posy + dy[i]); } for ( y = 0; y < h; y++ ) for ( x = 0; x < w; x++ ) { i = y * h + x; - dx = posx + (w + x - y) * tw /2; - dy = posy + (x + y) * th / 2; - this.draw_tile(ob[i], dx, dy); - if (mm[i]) mm[i].forEach(m => m.draw(m.position[0] + posx, m.position[1] + posy)); + draw_tile(ob[i], posx + dx[i], posy + dy[i]); + if (mm[i]) mm[i].forEach(m => m.draw(posx + m.position[0], posy + m.position[1])); } } - this.draw_tile = function(tile, x, y) { + function draw_tile(tile, x, y) { + const t = tileset.tile[tile]; + const f = tileset.animation[tile]; x = x |0; y = y |0; - var t = this.tileset.tile[tile]; - var f = this.tileset.animation[tile]; if (t && f) { - frame = ((performance.now() - this.frametime) / f[0][2] |0) % f.length; - canvas.drawImage(gfx[this.tileset.img], f[frame][0], f[frame][1], t[2], t[3], - x - t[4], y - t[5], t[2], t[3]); + frame = ((performance.now() - frametime) / f[0][2] |0) % f.length; + canvas.drawImage(gfx[tileset.img], f[frame][0], f[frame][1], t[2], t[3], + x - t[4], y - t[5], t[2], t[3]); } else if (t) { - canvas.drawImage(gfx[this.tileset.img], t[0], t[1], t[2], t[3], - x - t[4], y - t[5], t[2], t[3]); + canvas.drawImage(gfx[tileset.img], t[0], t[1], t[2], t[3], + x - t[4], y - t[5], t[2], t[3]); } } } function Mob(textdef) { this.position = [0, 0]; - var info = gamedata.load(textdef); + const info = gamedata.load(textdef); var direction = 0; var animation = "stance"; var previous_animation = ""; var frametime = performance.now(); gfx.load(info.image) - this.place = function(x, y) { this.position = [x, y]; return this; } + this.place = function(x, y) { + this.position[0] = x, this.position[1] = y; + return this; + } this.direct = function(d) { direction = d % 8; return this; } this.animate = function(a) { if ( a != animation ) { @@ -165,7 +170,7 @@ function Mob(textdef) { this.draw = function(x, y){ var f, a = info[animation]; - var frame = ( performance.now() - frametime ) * a.frames / a.duration | 0; + var f, frame = ( performance.now() - frametime ) * a.frames / a.duration | 0; switch(a.type){ case "looped": @@ -208,18 +213,18 @@ function Mob(textdef) { function Hero(gender = "female", hair = "short"){ this.position = [0,0]; this.stats = gamedata.load("/engine/stats.txt"); - var layers = gamedata.load("/engine/hero_layers.txt"); - var hair = (gender == "female")?"long":hair; + const layers = gamedata.load("/engine/hero_layers.txt"); var direction = 0, animation = "stance"; + hair = (gender == "female")?"long":hair; - limbs = { + var limbs = { head : new Mob("/animations/avatar/"+gender+"/head_"+hair+".txt"), - chest: new Mob("/animations/avatar/"+gender+"/default_chest.txt"), + chest: new Mob("/animations/avatar/"+gender+"/cloth_shirt.txt"), hands: new Mob("/animations/avatar/"+gender+"/default_hands.txt"), - legs : new Mob("/animations/avatar/"+gender+"/default_legs.txt"), + legs : new Mob("/animations/avatar/"+gender+"/cloth_pants.txt"), feet : new Mob("/animations/avatar/"+gender+"/default_feet.txt"), - main : new Mob("/animations/avatar/"+gender+"/dagger.txt"), - off : new Mob("/animations/avatar/"+gender+"/shield.txt") + main : new Mob("/animations/avatar/"+gender+"/default_hands.txt"), + off : new Mob("/animations/avatar/"+gender+"/default_hands.txt") } this.dress = function(limb, item) { @@ -230,7 +235,7 @@ function Hero(gender = "female", hair = "short"){ } this.place = function(x,y) { - this.position = [x,y]; + this.position[0] = x, this.position[1] = y; for (var limb in limbs) limbs[limb].place(x,y); return this; } @@ -333,4 +338,4 @@ player.direct(5).stance(); map.center(player.position[0], player.position[1]); c = new Controls(player, map); -setInterval (() => map.draw([player]), 16.66 ); +setInterval (() => map.draw([player]), 33.33 ); -- 2.39.2