html-tower/static/player.js

56 lines
2 KiB
JavaScript
Raw Normal View History

2024-05-19 04:37:53 +00:00
console.log("loading player system");
const playerSystem = (engine) => {
const entity = document.getElementById("player");
const movementRate = 0.5;
2024-05-19 04:37:53 +00:00
engine("registerEntity", entity);
2024-05-22 16:21:10 +00:00
const playerTransform = engine ("getComponentConstructor", "transform")(entity);
const playerCollision =
engine ("getComponentConstructor", "collision")(entity, {
layers: new Set(["player"]),
mask: new Set(["environment"]),
collisionShape: {
}
});
// const interactionCollision =
// engine ("getComponentConstructor", "collision") (entity, {
// layers: ["player"],
// mask: ["interactable"],
// collisionShape: {
// }
// });
2024-05-19 04:37:53 +00:00
const move = (direction) => (delta, eventData) => {
2024-05-19 22:07:43 +00:00
let translation = [0,0];
2024-05-19 04:37:53 +00:00
switch (direction) {
case "left":
2024-05-19 22:07:43 +00:00
translation [0] = -( movementRate * delta );
2024-05-19 04:37:53 +00:00
break;
case "right":
2024-05-19 22:07:43 +00:00
translation[0] = movementRate * delta;
2024-05-19 04:37:53 +00:00
break;
case "up":
2024-05-19 22:07:43 +00:00
translation[1] = - ( movementRate * delta );
2024-05-19 04:37:53 +00:00
break;
case "down":
2024-05-19 22:07:43 +00:00
translation[1] = movementRate * delta;
2024-05-19 04:37:53 +00:00
break;
}
2024-05-19 22:07:43 +00:00
playerTransform("translate", translation);
2024-05-22 16:21:10 +00:00
playerCollision("checkCollision");
// interactionCollision("checkCollision");
2024-05-19 04:37:53 +00:00
};
2024-05-19 22:07:43 +00:00
engine("registerEntity", entity);
engine("addComponent", entity, "transform", playerTransform);
2024-05-22 16:21:10 +00:00
engine("addComponent", entity, "collision", playerCollision);
// engine("addComponent", entity, "collision", interactionCollision);
2024-05-19 22:07:43 +00:00
// engine("")
2024-05-19 04:37:53 +00:00
engine("subscribeToEvent", "moveLeft", "movePlayerLeft", move("left"));
engine("subscribeToEvent", "moveRight", "movePlayerRight", move("right"));
engine("subscribeToEvent", "moveUp", "movePlayerUp", move("up"));
engine("subscribeToEvent", "moveDown", "movePlayerDown", move("down"));
};