From 93bc3ed05429f36d6eff08eac29dcba5142d3325 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Sat, 15 Feb 2020 09:42:11 +0100 Subject: [PATCH] quest status support --- engine.js | 72 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/engine.js b/engine.js index 3b36331..f3481c7 100644 --- a/engine.js +++ b/engine.js @@ -2,6 +2,7 @@ var gamedata = { // associative array for gamedata files load: function(def) { return this[def] ? this[def] : this[def] = new Textfile(def); } } + var gfx = { // associative array for game graphics load: function(def) { @@ -11,9 +12,14 @@ var gfx = { } return this[def]; } -}; +} + +// map object is global var map; +// quest status +var qstatus = {}; + function Textfile(txtfile) { // fetch and parse gamedata file into structured object var lines, fetch = new XMLHttpRequest(); @@ -90,6 +96,11 @@ function Textfile(txtfile) { case /^mapmod=.*$/.test(line): ref.mapmod = line.split(/[=]/)[1].split(/[;]/).map( m => m.split(/[,]/).map( n => n * 1 ? n * 1 : n) ); break; + // status array used in events + case /^(requires_status|requires_not_status|set_status|unset_status)=.*$/.test(line): + key = line.split(/[=]/)[0]; value = line.split(/[=]/)[1]; + ref[key] = value.split(/[,]/); + break; // general array type, will not be aggregated case /^[^#].*=.*,.*$/.test(line): key = line.split(/[=]/)[0]; value = line.split(/[=]/)[1].split(/,/); @@ -137,13 +148,13 @@ function Map(textdef) { this.info.layer.find(l => l.type == "collision").data[loc] = 1; } - this.events = []; - for (let ev of this.info.event) { + this.triggers = []; + for ( let ev of this.info.event.filter(e => e.activate == "on_trigger") ) { for (let x = ev.location[0]; x < ev.location[0] + ev.location[2]; x++) for (let y = ev.location[1]; y < ev.location[1] + ev.location[3]; y++) { let loc = y * w + x; - if (!this.events[loc]) this.events[loc] = []; - this.events[loc].push(ev); + if (!this.triggers[loc]) this.triggers[loc] = []; + this.triggers[loc].push(ev); } } @@ -429,30 +440,43 @@ function Controls(hero){ else player.stance(); map.center(hero.position[0], hero.position[1]); - events(hero.position[0], hero.position[1]); + triggers(hero.position[0], hero.position[1]); } // Process on_trigger events at pixel position x/y - function events(x, y) { - const i = map.tileAt(x,y); let ev, e, n; - if (map.events[i]) { - // intramap (teleporters) - if ( ev = map.events[i].find(e => (e.activate == "on_trigger" && e.intramap)) ){ - ev = ev.intramap; - hero.place( map.xOf(ev[0], ev[1]), map.yOf(ev[0], ev[1]) ); - map.center( map.xOf(ev[0], ev[1]), map.yOf(ev[0], ev[1]) ); + function triggers(x, y) { + const i = map.tileAt(x,y); + var trigger, item; + + if (map.triggers[i]) for ( trigger of map.triggers[i].filter( + t => (!t.requires_status) ? true : t.requires_status.find(s => s in qstatus) + ).filter( t => (!t.requires_not_status) ? true : !t.requires_not_status.find(s => s in qstatus) ) + ) { + // game status modification + if ( trigger.set_status ) for (item of trigger.set_status) { + qstatus[item] = true; + } + if ( trigger.unset_status ) for (item of trigger.unset_status) { + qstatus[item] = false; + } + if ( item = trigger.msg ) { + console.log(item); + } + // intramap (i.e. teleporters) + if ( item = trigger.intramap ){ + hero.place( map.xOf(item[0], item[1]), map.yOf(item[0], item[1]) ); + map.center( map.xOf(item[0], item[1]), map.yOf(item[0], item[1]) ); } // mapmod (e.g. opening doors, activating platforms, changing terrain, ...) - for (ev of map.events[i].filter(e => (e.activate == "on_trigger" && e.mapmod)) ){ - for (ev of ev.mapmod) - map.info.layer.find(l => l.type == ev[0]).data[ev[2] * map.info.header.width + ev[1]] = ev[3]; + if ( trigger.mapmod ) for (item of trigger.mapmod) { + map.info.layer.find(l => l.type == item[0]).data[item[2] * map.info.header.width + item[1]] = item[3]; } - // intermap (must be last because loading new map breaks further event search) - if ( ev = map.events[i].find(e => (e.activate == "on_trigger" && e.intermap)) ){ - ev = ev.intermap; - map = new Map(ev[0]); - hero.place( map.xOf(ev[1], ev[2]), map.yOf(ev[1], ev[2]) ); - map.center( map.xOf(ev[1], ev[2]), map.yOf(ev[1], ev[2]) ); + // intermap (i.e. enter new area) + if ( item = trigger.intermap ){ + map = new Map(item[0]); + hero.place( map.xOf(item[1], item[2]), map.yOf(item[1], item[2]) ); + map.center( map.xOf(item[1], item[2]), map.yOf(item[1], item[2]) ); + break; // further events would not be valid on new map } } } @@ -483,7 +507,7 @@ function Controls(hero){ let cy = map.tileY(click[0], click[1]); if ( (cx == hx - 1 || cx == hx + 1 || cx == hx) && (cy == hy - 1 || cy == hy + 1 || cy == hy)) - events(click[0], click[1]); + triggers(click[0], click[1]); click = []; } } -- 2.39.2