From fa7f340b2d859e7330ab4e6bb16a8589ae8c7953 Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 19 Jun 2024 02:02:14 -0500 Subject: [PATCH] refactor: control --- app/constants.js | 7 ------- app/ecs-components/controlled.js | 8 ++++---- app/ecs-components/speed.js | 3 +++ app/ecs-systems/apply-control-movement.js | 19 +++++++++++++++++++ app/ecs-systems/control-direction.js | 10 +++++----- app/ecs-systems/control-movement.js | 21 --------------------- app/ecs-systems/sprite-direction.js | 4 ++-- app/engine/engine.js | 11 +++++------ 8 files changed, 38 insertions(+), 45 deletions(-) create mode 100644 app/ecs-components/speed.js create mode 100644 app/ecs-systems/apply-control-movement.js delete mode 100644 app/ecs-systems/control-movement.js diff --git a/app/constants.js b/app/constants.js index ac991af..e063bf2 100644 --- a/app/constants.js +++ b/app/constants.js @@ -17,10 +17,3 @@ export const ACTION_MAP = { s: 'moveDown', a: 'moveLeft', }; - -export const MOVE_MAP = { - 'moveUp': 'up', - 'moveRight': 'right', - 'moveDown': 'down', - 'moveLeft': 'left', -}; diff --git a/app/ecs-components/controlled.js b/app/ecs-components/controlled.js index a663d5c..da57ca6 100644 --- a/app/ecs-components/controlled.js +++ b/app/ecs-components/controlled.js @@ -1,6 +1,6 @@ export default { - up: {type: 'float32'}, - right: {type: 'float32'}, - down: {type: 'float32'}, - left: {type: 'float32'}, + moveUp: {type: 'float32'}, + moveRight: {type: 'float32'}, + moveDown: {type: 'float32'}, + moveLeft: {type: 'float32'}, }; diff --git a/app/ecs-components/speed.js b/app/ecs-components/speed.js new file mode 100644 index 0000000..6c1ecbb --- /dev/null +++ b/app/ecs-components/speed.js @@ -0,0 +1,3 @@ +export default { + speed: {type: 'float32'}, +}; diff --git a/app/ecs-systems/apply-control-movement.js b/app/ecs-systems/apply-control-movement.js new file mode 100644 index 0000000..423a0f2 --- /dev/null +++ b/app/ecs-systems/apply-control-movement.js @@ -0,0 +1,19 @@ +import {System} from '@/ecs/index.js'; + +export default class ApplyControlMovement extends System { + + static queries() { + return { + default: ['Controlled', 'Momentum', 'Speed'], + }; + } + + tick() { + for (const [Controlled, Momentum, Speed] of this.select('default')) { + Momentum.x = Speed.speed * (Controlled.moveRight - Controlled.moveLeft); + Momentum.y = Speed.speed * (Controlled.moveDown - Controlled.moveUp); + } + } + +} + diff --git a/app/ecs-systems/control-direction.js b/app/ecs-systems/control-direction.js index 4994987..b181684 100644 --- a/app/ecs-systems/control-direction.js +++ b/app/ecs-systems/control-direction.js @@ -7,17 +7,17 @@ export default class ControlDirection extends System { for (const id in diff) { const {Controlled} = diff[id]; if (Controlled) { - const {Controlled: {up, right, down, left}, Direction} = this.ecs.get(id); - if (up > 0) { + const {Controlled: {moveUp, moveRight, moveDown, moveLeft}, Direction} = this.ecs.get(id); + if (moveUp > 0) { Direction.direction = 0; } - if (down > 0) { + if (moveDown > 0) { Direction.direction = 2; } - if (left > 0) { + if (moveLeft > 0) { Direction.direction = 3; } - if (right > 0) { + if (moveRight > 0) { Direction.direction = 1; } } diff --git a/app/ecs-systems/control-movement.js b/app/ecs-systems/control-movement.js deleted file mode 100644 index bc6a2f6..0000000 --- a/app/ecs-systems/control-movement.js +++ /dev/null @@ -1,21 +0,0 @@ -import {System} from '@/ecs/index.js'; - -const SPEED = 100; - -export default class ControlMovement extends System { - - static queries() { - return { - default: ['Controlled', 'Momentum'], - }; - } - - tick() { - for (const [controlled, momentum] of this.select('default')) { - momentum.x = SPEED * (controlled.right - controlled.left); - momentum.y = SPEED * (controlled.down - controlled.up); - } - } - -} - diff --git a/app/ecs-systems/sprite-direction.js b/app/ecs-systems/sprite-direction.js index e061782..9462847 100644 --- a/app/ecs-systems/sprite-direction.js +++ b/app/ecs-systems/sprite-direction.js @@ -13,8 +13,8 @@ export default class SpriteDirection extends System { const entity = this.ecs.get(entityId); const parts = []; if (entity.Controlled) { - const {up, right, down, left} = entity.Controlled; - if (up > 0 || right > 0 || down > 0 || left > 0) { + const {moveUp, moveRight, moveDown, moveLeft} = entity.Controlled; + if (moveUp > 0 || moveRight > 0 || moveDown > 0 || moveLeft > 0) { parts.push('moving'); } else { diff --git a/app/engine/engine.js b/app/engine/engine.js index 8ac2488..779a864 100644 --- a/app/engine/engine.js +++ b/app/engine/engine.js @@ -1,5 +1,4 @@ import { - MOVE_MAP, TPS, } from '@/constants.js'; import Ecs from '@/ecs/ecs.js'; @@ -82,7 +81,7 @@ export default class Engine { }, }); const defaultSystems = [ - 'ControlMovement', + 'ApplyControlMovement', 'ApplyMomentum', 'ClampPositions', 'FollowCamera', @@ -105,12 +104,13 @@ export default class Engine { async createPlayer(id) { const player = { Camera: {}, - Controlled: {up: 0, right: 0, down: 0, left: 0}, + Controlled: {moveUp: 0, moveRight: 0, moveDown: 0, moveLeft: 0}, Direction: {direction: 2}, Ecs: {path: join('homesteads', `${id}`)}, Momentum: {}, Position: {x: 368, y: 368}, VisibleAabb: {}, + Speed: {speed: 100}, Sprite: { animation: 'moving:down', frame: 0, @@ -181,10 +181,9 @@ export default class Engine { for (const i in this.ecses) { this.ecses[i].setClean(); } + // TODO: SANITIZE for (const [{Controlled}, payload] of this.incomingActions) { - if (payload.type in MOVE_MAP) { - Controlled[MOVE_MAP[payload.type]] = payload.value; - } + Controlled[payload.type] = payload.value; } this.incomingActions = []; for (const i in this.ecses) {