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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }
  15. collideTop(bounds, velocity) {
  16. if (!this.groundBounds || bounds.top < this.groundBounds.top) {
  17. this.groundBounds = bounds;
  18. this.groundVelocity = velocity;
  19. }
  20. }
  21. collideWall(bounds, velocity) {
  22. let side = this.entity.bounds.intersectSide(bounds);
  23. if (side === "top") {
  24. this.collideTop(bounds, velocity);
  25. } else if (side === "left" && this.velocity.x > velocity.x) {
  26. this.velocity.x = velocity.x;
  27. this.entity.bounds.right = bounds.left;
  28. } else if (side === "right" && this.velocity.x < velocity.x) {
  29. this.velocity.x = velocity.x;
  30. this.entity.bounds.left = bounds.right;
  31. }
  32. }
  33. collidePlatform(bounds, velocity) {
  34. let side = this.entity.bounds.intersectSide(bounds);
  35. if (side === "top")
  36. return this.collideTop(bounds, velocity);
  37. }
  38. update(dt) {
  39. // Collide
  40. if (this.entity.has("collider")) {
  41. let collider = this.entity.t.collider;
  42. this.groundBounds = null;
  43. this.groundVelocity = null;
  44. collider.entities.forEach(e => {
  45. let vel = Vec2.zero;
  46. if (e.has("physics"))
  47. vel = e.t.physics.velocity;
  48. if (e.has("wall"))
  49. this.collideWall(e.bounds, vel);
  50. else if (e.has("platform"))
  51. this.collidePlatform(e.bounds, vel);
  52. });
  53. collider.structures.forEach((s) => {
  54. if (s.attrs.wall)
  55. this.collideWall(s.bounds, Vec2.zero);
  56. else if (s.attrs.platform)
  57. this.collidePlatform(s.bounds, Vec2.zero);
  58. });
  59. }
  60. this.onGround =
  61. this.groundVelocity && this.velocity.y >= this.groundVelocity.y;
  62. if (this.onGround) {
  63. this.timeLastOnGround = this.entity.time;
  64. this.velocity.y = this.groundVelocity.y;
  65. }
  66. if (!this.onGround)
  67. this.velocity.y += this.gravity * dt;
  68. // Apply friction
  69. var fric = this.onGround ? this.groundFriction : this.airFriction;
  70. var xRatio = 1 / (1 + (dt * fric));
  71. this.velocity.x *= xRatio;
  72. }
  73. postUpdate(dt) {
  74. // Move
  75. this.entity.pos.x += this.velocity.x * dt;
  76. this.entity.pos.y += this.velocity.y * dt;
  77. if (this.onGround)
  78. this.entity.bounds.bottom = this.groundBounds.top;
  79. }
  80. }