From: Paul Hänsch Date: Sat, 15 Feb 2020 23:44:04 +0000 (+0100) Subject: enable onload events (allowing persistent map changes, etc.) X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=50f16ca8d19bc89de4a35b4271484e9ae4df562e;p=flarejs enable onload events (allowing persistent map changes, etc.) --- diff --git a/engine.js b/engine.js index 459a9dd..5fe7b6e 100644 --- a/engine.js +++ b/engine.js @@ -410,6 +410,8 @@ function Controls(hero){ } var keys = [], click = []; + loadevents(); + window.addEventListener("keydown", e => keys[e.keyCode] = true ); window.addEventListener("keyup" , e => keys[e.keyCode] = false); window.addEventListener("click", m => click = [ map.mapX(m.clientX), map.mapY(m.clientY) ] ); @@ -446,36 +448,50 @@ function Controls(hero){ // Process on_trigger events at pixel position x/y 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 => qstatus[s]) - ).filter( t => (!t.requires_not_status) ? true : !t.requires_not_status.find(s => qstatus[s]) ) - ) { + if ( map.triggers[i] ) events ( + map.triggers[i].filter( t => (!t.requires_status) ? true : t.requires_status.find(s => qstatus[s]) + ).filter( t => (!t.requires_not_status) ? true : !t.requires_not_status.find(s => qstatus[s]) ) + ); + } + + function loadevents() { + if ( map.info.event ) events( + map.info.event.filter( ev => ev.activate == "on_load" + ).filter( t => (!t.requires_status) ? true : t.requires_status.find(s => qstatus[s]) + ).filter( t => (!t.requires_not_status) ? true : !t.requires_not_status.find(s => qstatus[s]) ) + ) + } + + function events( events ) { + var ev, item; + + for ( ev of events ) { // game status modification - if ( trigger.set_status ) for (item of trigger.set_status) { + if ( ev.set_status ) for (item of ev.set_status) { qstatus[item] = true; } - if ( trigger.unset_status ) for (item of trigger.unset_status) { + if ( ev.unset_status ) for (item of ev.unset_status) { qstatus[item] = false; } - if ( item = trigger.msg ) { + if ( item = ev.msg ) { console.log(item); } // intramap (i.e. teleporters) - if ( item = trigger.intramap ){ + if ( item = ev.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, ...) - if ( trigger.mapmod ) for (item of trigger.mapmod) { + if ( ev.mapmod ) for (item of ev.mapmod) { map.info.layer.find(l => l.type == item[0]).data[item[2] * map.info.header.width + item[1]] = item[3]; } // intermap (i.e. enter new area) - if ( item = trigger.intermap ){ + if ( item = ev.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]) ); + loadevents(); break; // further events would not be valid on new map } }