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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.gravity = 40;
  7. this.groundFriction = 6;
  8. this.airFriction = 2;
  9. this.absVelocity = new Vec2();
  10. this.velocity = new Vec2();
  11. this.onGround = false;
  12. this.prevRelativeTo = null;
  13. this.relativeTo = null;
  14. }
  15. update(dt) {
  16. let collider = this.entity.t.collider;
  17. this.prevRelativeTo = this.relativeTo;
  18. this.relativeTo = null;
  19. this.onGround = false;
  20. if (
  21. this.entity.has("collider") &&
  22. collider.collides &&
  23. this.velocity.y >= 0 &&
  24. this.entity.bounds.bottomIntersects(collider.cBounds)) {
  25. // Structures are static; just teleport us to the top of them
  26. if (collider.cStructure) {
  27. this.velocity.y = 0;
  28. this.entity.bounds.bottom = collider.cBounds.top;
  29. this.onGround = true;
  30. // If we're colliding with an entity, and that entity is
  31. // a platform, teleport us to the top of them.
  32. } else if (collider.cEntity.has("platform")) {
  33. this.velocity.y = 0;
  34. this.entity.bounds.bottom = collider.cBounds.top;
  35. this.onGround = true;
  36. if (collider.cEntity.has("physics")) {
  37. let cPhys = collider.cEntity.t.physics;
  38. this.relativeTo = cPhys;
  39. }
  40. }
  41. }
  42. if (!this.onGround)
  43. this.velocity.y += this.gravity * dt;
  44. // If we just started riding something, adjust relative absVelocity
  45. if (!this.prevRelativeTo && this.relativeTo) {
  46. this.velocity.x = this.absVelocity.x - this.relativeTo.absVelocity.x;
  47. this.velocity.y = this.absVelocity.y - this.relativeTo.absVelocity.y;
  48. // If we just stopped riding something, adjust relative absVelocity
  49. } else if (this.prevRelativeTo && !this.relativeTo) {
  50. this.velocity.x += this.prevRelativeTo.absVelocity.x;
  51. this.velocity.y += this.prevRelativeTo.absVelocity.y;
  52. }
  53. // Apply friction
  54. var fric = this.onGround ? this.groundFriction : this.airFriction;
  55. var xRatio = 1 / (1 + (dt * fric));
  56. this.velocity.x *= xRatio;
  57. }
  58. postUpdate(dt) {
  59. // Update absVelocity
  60. if (this.relativeTo) {
  61. this.absVelocity.x = this.velocity.x + this.relativeTo.absVelocity.x;
  62. this.absVelocity.y = this.velocity.y + this.relativeTo.absVelocity.y;
  63. } else {
  64. this.absVelocity.x = this.velocity.x;
  65. this.absVelocity.y = this.velocity.y;
  66. }
  67. // Move
  68. this.entity.bounds.pos.x += this.absVelocity.x * dt;
  69. this.entity.bounds.pos.y += this.absVelocity.y * dt;
  70. }
  71. }