feat: animation control
This commit is contained in:
parent
b92f1db744
commit
b40c80caff
3
app/ecs-components/direction.js
Normal file
3
app/ecs-components/direction.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export default {
|
||||||
|
direction: {type: 'uint8'},
|
||||||
|
};
|
27
app/ecs-systems/control-direction.js
Normal file
27
app/ecs-systems/control-direction.js
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import {System} from '@/ecs/index.js';
|
||||||
|
|
||||||
|
export default class ControlDirection extends System {
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
const {diff} = this.ecs;
|
||||||
|
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) {
|
||||||
|
Direction.direction = 0;
|
||||||
|
}
|
||||||
|
if (down > 0) {
|
||||||
|
Direction.direction = 2;
|
||||||
|
}
|
||||||
|
if (left > 0) {
|
||||||
|
Direction.direction = 3;
|
||||||
|
}
|
||||||
|
if (right > 0) {
|
||||||
|
Direction.direction = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
39
app/ecs-systems/sprite-direction.js
Normal file
39
app/ecs-systems/sprite-direction.js
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import {System} from '@/ecs/index.js';
|
||||||
|
|
||||||
|
export default class SpriteDirection extends System {
|
||||||
|
|
||||||
|
static queries() {
|
||||||
|
return {
|
||||||
|
default: ['Sprite'],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
for (const [Sprite, entityId] of this.select('default')) {
|
||||||
|
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) {
|
||||||
|
parts.push('moving');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
parts.push('idle');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (entity.Direction) {
|
||||||
|
const name = {
|
||||||
|
0: 'up',
|
||||||
|
1: 'right',
|
||||||
|
2: 'down',
|
||||||
|
3: 'left',
|
||||||
|
};
|
||||||
|
parts.push(name[entity.Direction.direction]);
|
||||||
|
}
|
||||||
|
if (parts.length > 0) {
|
||||||
|
Sprite.animation = parts.join(':');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -9,6 +9,8 @@ import CalculateAabbs from '@/ecs-systems/calculate-aabbs.js';
|
||||||
import FollowCamera from '@/ecs-systems/follow-camera.js';
|
import FollowCamera from '@/ecs-systems/follow-camera.js';
|
||||||
import UpdateSpatialHash from '@/ecs-systems/update-spatial-hash.js';
|
import UpdateSpatialHash from '@/ecs-systems/update-spatial-hash.js';
|
||||||
import RunAnimations from '@/ecs-systems/run-animations.js';
|
import RunAnimations from '@/ecs-systems/run-animations.js';
|
||||||
|
import ControlDirection from '@/ecs-systems/control-direction.js';
|
||||||
|
import SpriteDirection from '@/ecs-systems/sprite-direction.js';
|
||||||
import Ecs from '@/engine/ecs.js';
|
import Ecs from '@/engine/ecs.js';
|
||||||
import {decode, encode} from '@/packets/index.js';
|
import {decode, encode} from '@/packets/index.js';
|
||||||
|
|
||||||
|
@ -16,16 +18,17 @@ const players = {
|
||||||
0: {
|
0: {
|
||||||
Camera: {},
|
Camera: {},
|
||||||
Controlled: {up: 0, right: 0, down: 0, left: 0},
|
Controlled: {up: 0, right: 0, down: 0, left: 0},
|
||||||
|
Direction: {direction: 2},
|
||||||
Momentum: {},
|
Momentum: {},
|
||||||
Position: {x: 368, y: 368},
|
Position: {x: 368, y: 368},
|
||||||
VisibleAabb: {},
|
VisibleAabb: {},
|
||||||
World: {world: 1},
|
World: {world: 1},
|
||||||
Sprite: {
|
Sprite: {
|
||||||
animation: 'down',
|
animation: 'moving:down',
|
||||||
frame: 0,
|
frame: 0,
|
||||||
frames: 8,
|
frames: 8,
|
||||||
source: '/assets/dude.json',
|
source: '/assets/dude.json',
|
||||||
speed: 0.1,
|
speed: 0.115,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -34,6 +37,8 @@ export default class Engine {
|
||||||
|
|
||||||
static Ecs = Ecs;
|
static Ecs = Ecs;
|
||||||
|
|
||||||
|
incoming = [];
|
||||||
|
|
||||||
constructor(Server) {
|
constructor(Server) {
|
||||||
const ecs = new this.constructor.Ecs();
|
const ecs = new this.constructor.Ecs();
|
||||||
const layerSize = {x: Math.ceil(RESOLUTION.x / 4), y: Math.ceil(RESOLUTION.y / 4)};
|
const layerSize = {x: Math.ceil(RESOLUTION.x / 4), y: Math.ceil(RESOLUTION.y / 4)};
|
||||||
|
@ -57,6 +62,8 @@ export default class Engine {
|
||||||
ecs.addSystem(FollowCamera);
|
ecs.addSystem(FollowCamera);
|
||||||
ecs.addSystem(CalculateAabbs);
|
ecs.addSystem(CalculateAabbs);
|
||||||
ecs.addSystem(UpdateSpatialHash);
|
ecs.addSystem(UpdateSpatialHash);
|
||||||
|
ecs.addSystem(ControlDirection);
|
||||||
|
ecs.addSystem(SpriteDirection);
|
||||||
ecs.addSystem(RunAnimations);
|
ecs.addSystem(RunAnimations);
|
||||||
this.ecses = {
|
this.ecses = {
|
||||||
1: ecs,
|
1: ecs,
|
||||||
|
@ -79,17 +86,8 @@ export default class Engine {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
accept(connection, {payload, type}) {
|
accept(connection, packet) {
|
||||||
switch (type) {
|
this.incoming.push([this.connectedPlayers.get(connection).entity, packet]);
|
||||||
case 'Action': {
|
|
||||||
const {entity} = this.connectedPlayers.get(connection);
|
|
||||||
if (payload.type in MOVE_MAP) {
|
|
||||||
entity.Controlled[MOVE_MAP[payload.type]] = payload.value;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async connectPlayer(connection) {
|
async connectPlayer(connection) {
|
||||||
|
@ -135,6 +133,20 @@ export default class Engine {
|
||||||
tick(elapsed) {
|
tick(elapsed) {
|
||||||
for (const i in this.ecses) {
|
for (const i in this.ecses) {
|
||||||
this.ecses[i].setClean();
|
this.ecses[i].setClean();
|
||||||
|
}
|
||||||
|
for (const [{Controlled}, {payload, type}] of this.incoming) {
|
||||||
|
switch (type) {
|
||||||
|
case 'Action': {
|
||||||
|
if (payload.type in MOVE_MAP) {
|
||||||
|
Controlled[MOVE_MAP[payload.type]] = payload.value;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.incoming = [];
|
||||||
|
for (const i in this.ecses) {
|
||||||
this.ecses[i].tick(elapsed);
|
this.ecses[i].tick(elapsed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
Binary file not shown.
Before Width: | Height: | Size: 4.1 KiB After Width: | Height: | Size: 9.2 KiB |
Loading…
Reference in New Issue
Block a user