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.

TPhysics.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {Trait} from "../Entity.js";
  2. import Vec2 from "../Vec2.js";
  3. export default class TPhysics extends Trait {
  4. constructor(entity) {
  5. super(entity, "physics");
  6. this.velocity = new Vec2();
  7. this.gravity = 40;
  8. this.groundFriction = 6;
  9. this.airFriction = 2;
  10. this.timeLastOnGround = 0;
  11. this.onGround = false;
  12. this.groundBounds = null;
  13. this.groundVelocity = null;
  14. this.moved = false;
  15. }
  16. collideTop(bounds, velocity) {
  17. if (!this.groundBounds || bounds.top < this.groundBounds.top) {
  18. this.groundBounds = bounds;
  19. this.groundVelocity = velocity;
  20. }
  21. }
  22. collideWall(bounds, velocity) {
  23. let side = this.entity.bounds.intersectSide(bounds);
  24. if (side === "top") {
  25. this.collideTop(bounds, velocity);
  26. } else if (side === "left" && this.velocity.x > velocity.x) {
  27. this.velocity.x = velocity.x;
  28. this.entity.bounds.right = bounds.left;
  29. } else if (side === "right" && this.velocity.x < velocity.x) {
  30. this.velocity.x = velocity.x;
  31. this.entity.bounds.left = bounds.right;
  32. }
  33. }
  34. collidePlatform(bounds, velocity) {
  35. let side = this.entity.bounds.intersectSide(bounds);
  36. if (side === "top")
  37. return this.collideTop(bounds, velocity);
  38. }
  39. update(dt) {
  40. // Collide
  41. if (this.entity.has("collider") && !this.moved) {
  42. let collider = this.entity.t.collider;
  43. this.groundBounds = null;
  44. this.groundVelocity = null;
  45. collider.entities.forEach(e => {
  46. let vel = Vec2.zero;
  47. if (e.has("physics"))
  48. vel = e.t.physics.velocity;
  49. if (e.has("wall"))
  50. this.collideWall(e.bounds, vel);
  51. else if (e.has("platform"))
  52. this.collidePlatform(e.bounds, vel);
  53. });
  54. collider.structures.forEach((s) => {
  55. if (s.attrs.wall)
  56. this.collideWall(s.bounds, Vec2.zero);
  57. else if (s.attrs.platform)
  58. this.collidePlatform(s.bounds, Vec2.zero);
  59. });
  60. }
  61. this.onGround =
  62. this.groundVelocity && this.velocity.y >= this.groundVelocity.y;
  63. if (this.onGround && !this.moved) {
  64. this.timeLastOnGround = this.entity.time;
  65. this.velocity.y = this.groundVelocity.y;
  66. }
  67. if (!this.onGround)
  68. this.velocity.y += this.gravity * dt;
  69. // Apply friction
  70. var fric = this.onGround ? this.groundFriction : this.airFriction;
  71. var xRatio = 1 / (1 + (dt * fric));
  72. this.velocity.x *= xRatio;
  73. }
  74. postUpdate(dt) {
  75. // Move
  76. this.entity.pos.x += this.velocity.x * dt;
  77. this.entity.pos.y += this.velocity.y * dt;
  78. if (this.onGround && !this.moved)
  79. this.entity.bounds.bottom = this.groundBounds.top;
  80. this.moved = false;
  81. }
  82. }