refactor: control

This commit is contained in:
cha0s 2024-06-19 02:02:14 -05:00
parent 9b2dab91ac
commit fa7f340b2d
8 changed files with 38 additions and 45 deletions

View File

@ -17,10 +17,3 @@ export const ACTION_MAP = {
s: 'moveDown',
a: 'moveLeft',
};
export const MOVE_MAP = {
'moveUp': 'up',
'moveRight': 'right',
'moveDown': 'down',
'moveLeft': 'left',
};

View File

@ -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'},
};

View File

@ -0,0 +1,3 @@
export default {
speed: {type: 'float32'},
};

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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 {

View File

@ -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) {