You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Entity.js 792B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import Rect from './Rect';
  2. import Vec2 from './Vec2';
  3. export class Trait {
  4. constructor(entity, name) {
  5. this.name = name;
  6. this.entity = entity;
  7. this.enabled = true;
  8. }
  9. init() {}
  10. update(dt) {}
  11. }
  12. export default class Entity {
  13. constructor(level) {
  14. this.level = level;
  15. this.bounds = new Rect();
  16. this.velocity = new Vec2();
  17. this.t = {};
  18. this.traits = [];
  19. }
  20. init() {
  21. for (let trait of this.traits) {
  22. trait.init();
  23. }
  24. }
  25. update(dt) {
  26. for (let trait of this.traits) {
  27. if (trait.enabled)
  28. trait.update(dt);
  29. }
  30. this.bounds.pos.x += this.velocity.x * dt;
  31. this.bounds.pos.y += this.velocity.y * dt;
  32. }
  33. draw(ctx) {}
  34. has(name) {
  35. let t = this.t[name];
  36. return t != null && t.enabled;
  37. }
  38. addTrait(t) {
  39. this.t[t.name] = t;
  40. this.traits.push(t);
  41. }
  42. }