From 950d2e8dc6dc34bfef4044f9b089645ced96c119 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Mon, 10 Feb 2020 05:07:44 +0100 Subject: [PATCH] basic input control object --- engine.js | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/engine.js b/engine.js index 4a9eb2e..456a882 100644 --- a/engine.js +++ b/engine.js @@ -148,9 +148,11 @@ function Mob(textdef) { this.place = function(x, y) { this.position = [x, y]; return this; } this.direct = function(d) { this.direction = d % 8; return this; } this.animate = function(a) { - this.previous_animation = this.animation; - this.animation = a; - this.frametime = performance.now(); + if ( a != this.animation ) { + this.previous_animation = this.animation; + this.animation = a; + this.frametime = performance.now(); + } return this; } @@ -250,6 +252,25 @@ function Hero(gender = "female", hair = "short"){ this.swing = function() { this.animate("swing" ); return this; }; } +function Controls(hero, map){ + var keys = []; + + function input(){ + var d = {}; + d.l = keys[65] || keys[37]; d.r = keys[68] || keys[39]; + d.u = keys[87] || keys[38]; d.d = keys[83] || keys[40]; + if ( d.l ) hero.direct(0).run(); if ( d.r ) hero.direct(4).run(); + if ( d.u ) hero.direct(2).run(); if ( d.d ) hero.direct(6).run(); + if ( d.u && d.l ) hero.direct(1).run(); + if ( d.u && d.r ) hero.direct(3).run(); + if ( d.d && d.l ) hero.direct(7).run(); + if ( d.d && d.r ) hero.direct(5).run(); + if ( ! (d.l || d.r || d.u || d.d) ) hero.stance(); + } + window.addEventListener("keydown", function(e){ keys[e.keyCode] = true ; input();} ); + window.addEventListener("keyup" , function(e){ keys[e.keyCode] = false; input();} ); +} + document.querySelector("body").setAttribute("style", "margin: 0; padding: 0;"); canvas = document.createElement("canvas").getContext("2d"); canvas.canvas.setAttribute("style", "display: block; border: 0; margin: 0; padding: 0;"); @@ -262,19 +283,11 @@ window.addEventListener("resize", function(){ canvas.canvas.height = window.innerHeight; }); -player = new Hero().place(20 * 64 , 20 * 32).direct(5); +player = new Hero(); map = new Map("/maps/arrival.txt"); +player.place(map.info.header.hero_pos.split(/,/)[0] * map.info.header.tilewidth, + map.info.header.hero_pos.split(/,/)[1] * map.info.header.tileheight); +player.direct(5).stance(); +c = new Controls(player, map); setInterval (function() { map.center(player.position[0], player.position[1]).draw([player]);}, 33.33 ); - -// window.addEventListener("click", function(e){ -// tile = map.tileAt(e.pageX + map.posx, e.pageY - map.posy ); -// console.log( "X: " + (e.pageX + map.posx) + ", Y:" + (e.pageY - map.posy) + -// ", Tile: " + tile + " ( " + (tile % map.info.header.width) + ", " + (tile / map.info.header.width |0) + " )" ); -// }); -// -// mark = [17,17]; -// window.addEventListener("mousemove", function(e){ -// tile = map.tileAt(e.pageX + map.posx, e.pageY - map.posy); -// mark = [(tile % map.info.header.width), (tile / map.info.header.width |0)]; -// }); -- 2.39.2