+ // shortcuts for animation
+ this.block = () => this.animate("block" );
+ this.cast = () => this.animate("cast" );
+ this.die = () => this.animate("die" );
+ this.hit = () => this.animate("hit" );
+ this.run = () => this.animate("run" );
+ this.shoot = () => this.animate("shoot" );
+ this.stance = () => this.animate("stance");
+ this.swing = () => this.animate("swing" );
+}
+
+function Controls(hero){
+ // processes keyboard / touch / mouse
+ // causes according player actions
+ // single player only, conrol will be assigned to server in multi player
+ var kbdmap = {
+ up: 87, altup: 38,
+ down: 83, altdown: 40,
+ left: 65, altleft: 37,
+ right: 68, altright: 39,
+ }
+ 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) ] );
+ setInterval(() => input(), 33.33)
+
+ // 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);
+
+ 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 ( ev.set_status ) for (item of ev.set_status) {
+ qstatus[item] = true;
+ }
+ if ( ev.unset_status ) for (item of ev.unset_status) {
+ qstatus[item] = false;
+ }
+ if ( item = ev.msg ) {
+ console.log(item);
+ }
+ // intramap (i.e. teleporters)
+ 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 ( 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 = 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
+ }
+ }
+ }
+
+ // process input and decide on according action
+ function input(){
+ // facing directions, indexed by OR of key press combinations
+ const dir = [ -1, 0, 4, -1, 2, 1, 3, 2, 6, 7, 5, 6, -1, 0, 4, -1 ];
+ // translation speed and direction, indexed by facing direction of movement
+ const trans = [ [-1.4,0], [-1,-1], [0,-1.4], [1,-1], [1.4,0], [1,1], [0,1.4], [-1,1] ];
+ var k = 0;
+
+ // OR of direction key presses
+ k += (keys[kbdmap.left] || keys[kbdmap.altleft]) ? 1 : 0;
+ k += (keys[kbdmap.right] || keys[kbdmap.altright]) ? 2 : 0;
+ k += (keys[kbdmap.up] || keys[kbdmap.altup]) ? 4 : 0;
+ k += (keys[kbdmap.down] || keys[kbdmap.altdown]) ? 8 : 0;
+
+ if (~dir[k]) {
+ hero.direct(dir[k]).run();
+ translate(trans[dir[k]][0], trans[dir[k]][1]);
+ } else hero.stance();
+
+ if (click[0]) {
+ let hx = map.tileX(hero.position[0], hero.position[1]);
+ let hy = map.tileY(hero.position[0], hero.position[1]);
+ let cx = map.tileX(click[0], click[1]);
+ 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))
+ triggers(click[0], click[1]);
+ click = [];
+ }
+ }