// 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) {
}
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();
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(/,/);
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);
}
}
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
}
}
}
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 = [];
}
}