From 387bfba38d95b5a252a0529000de4817c0c461ab Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 29 Feb 2024 16:07:24 +0100 Subject: [PATCH] game: fix: support horizontal player --- frontend/static/js/api/game/MyPlayer.js | 41 ++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/frontend/static/js/api/game/MyPlayer.js b/frontend/static/js/api/game/MyPlayer.js index 5cd1ddd..0702f58 100644 --- a/frontend/static/js/api/game/MyPlayer.js +++ b/frontend/static/js/api/game/MyPlayer.js @@ -20,7 +20,35 @@ class MyPlayer extends Player */ this.client = client; - this.increment_sign = this.rail.start.y > this.rail.stop.y ? 1 : -1; + this.upKeys = []; + this.downKeys = []; + console.log(rail.start.x, rail.stop.x) + if (rail.start.x != rail.stop.x) + { + if (rail.start.x < rail.stop.x) + { + this.upKeys.push("a"); + this.downKeys.push("d"); + } + else + { + this.upKeys.push("d"); + this.downKeys.push("a"); + } + } + if (rail.start.y != rail.stop.y) + { + if (rail.start.y > rail.stop.y) + { + this.upKeys.push("w"); + this.downKeys.push("s"); + } + else + { + this.upKeys.push("s"); + this.downKeys.push("w"); + } + } } /** @@ -30,10 +58,13 @@ class MyPlayer extends Player { let new_pos = this.position; - if (keys_pressed.includes("s")) - new_pos -= this.game.config.paddle_speed_per_second_max * this.game.time.deltaTimeSecond() * 1.0 * this.increment_sign; - if (keys_pressed.includes("w")) - new_pos += this.game.config.paddle_speed_per_second_max * this.game.time.deltaTimeSecond() * 1.0 * this.increment_sign; + keys_pressed.forEach(key => { + if (this.downKeys.includes(key)) + new_pos += this.game.config.paddle_speed_per_second_max * this.game.time.deltaTimeSecond(); + if (this.upKeys.includes(key)) + new_pos -= this.game.config.paddle_speed_per_second_max * this.game.time.deltaTimeSecond(); + }); + new_pos = Math.max(0 + this.game.config.paddle_ratio / 2, new_pos); new_pos = Math.min(1 - this.game.config.paddle_ratio / 2, new_pos);