+ // cause player to walk, processes blocked terrain and player speed
+ // x/y are factors of speed and direction
+ // i.e. +/-1 for diagonal movement
+ // and +/-1.4 for horizontal/vertical movement
+ function translate(x, y){
+ var sx = map.info.header.tilewidth * hero.stats.speed / 33.33;
+ var sy = map.info.header.tileheight * hero.stats.speed / 33.33;
+ var dx = x * sx, hx = hero.position[0];
+ var dy = y * sy, hy = hero.position[1];
+ var f = 2.1;
+ const col = map.info.layer.find(l => l.type == "collision").data;
+
+ if (col[map.tileAt(hx + dx, hy + dy)] == 0 )
+ hero.place(hx + dx, hy + dy);
+ else if ( dy == 0 && col[map.tileAt(hx + dx / f, hy + sy / 1.5)] == 0 )
+ hero.place(hx + dx / f, hy + sy / 1.5);
+ else if ( dy == 0 && col[map.tileAt(hx + dx / f, hy - sy / 1.5)] == 0 )
+ hero.place(hx + dx / f, hy - sy / 1.5);
+ else if ( dx == 0 && col[map.tileAt(hx + sx / 1.5, hy + dy / f)] == 0 )
+ hero.place(hx + sx / 1.5, hy + dy / f);
+ else if ( dx == 0 && col[map.tileAt(hx - sx / 1.5, hy + dy / f)] == 0 )
+ hero.place(hx - sx / 1.5, hy + dy / f);
+ else player.stance();
+
+ map.center(hero.position[0], hero.position[1]);
+ triggers(hero.position[0], hero.position[1]);
+ }
+
+ // 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]) )
+ ) {
+ // 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, ...)
+ 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 (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
+ }
+ }
+ }
+
+ // process input and decide on according action