Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // We define a Component, just a regular class with some data, no functions!
- class PositionComponent {
- constructor(canMove, pos = {}){
- this.canMove = canMove;
- this.pos = {
- x: pos.x,
- y: pos.y
- };
- this.dest = {
- x: pos.x,
- y: pos.y
- };
- }
- }
- // An entity is just an object with Components
- let entity = {
- 'POS_COMP': new PositionComponent(true, {x: 10, y: 20})
- };
- // An array of entities which we can easily extend
- let entities = [entity];
- // A move system
- function moveSystem(entities){
- entities.forEach((entity) => {
- // only if a POS_COMP exists, else we do nothing
- if (entity.POS_COMP) {
- let distX = entity.POS_COMP.dest.x - entity.POS_COMP.pos.x;
- let distY = entity.POS_COMP.dest.y - entity.POS_COMP.pos.y;
- if (distX > 0) {
- entity.POS_COMP.pos.x = Math.min(entity.POS_COMP.dest.x, entity.POS_COMP.pos.x + 1);
- } else {
- entity.POS_COMP.pos.x = Math.max(entity.POS_COMP.dest.x, entity.POS_COMP.pos.x - 1);
- }
- if (distY > 0) {
- entity.POS_COMP.pos.y = Math.min(entity.POS_COMP.dest.y, entity.POS_COMP.pos.y + 1);
- } else {
- entity.POS_COMP.pos.y = Math.max(entity.POS_COMP.dest.y, entity.POS_COMP.pos.y - 1);
- }
- }
- });
- }
- // We loop over the system every second, trying to perform some logic
- setInterval(() => {
- moveSystem(entities);
- }, 1000);
Add Comment
Please, Sign In to add comment