Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TPhysics.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {Trait} from "../Entity.js";
  2. import Vec2 from "../Vec2.js";
  3. let zeroVector = new Vec2(0, 0);
  4. export default class TPhysics extends Trait {
  5. constructor(entity) {
  6. super(entity, "physics");
  7. this.gravity = 40;
  8. this.groundFriction = 6;
  9. this.airFriction = 2;
  10. this.absVelocity = new Vec2();
  11. this.velocity = new Vec2();
  12. this.onGround = false;
  13. this.groundBounds = null;
  14. this.groundVelocity = null;
  15. this.oldGroundBounds = null;
  16. this.timeLastOnGround = 0;
  17. }
  18. collideTop(bounds, absVelocity) {
  19. if (this.onGround || bounds === this.oldGroundBounds)
  20. return;
  21. this.onGround = true;
  22. this.groundBounds = bounds;
  23. this.groundVelocity = absVelocity;
  24. this.velocity.x -= absVelocity.x;
  25. this.velocity.y = 0;
  26. this.entity.bounds.bottom = bounds.top;
  27. }
  28. collideWall(bounds, absVelocity) {
  29. let side = this.entity.bounds.intersectSide(bounds);
  30. if (side === "top")
  31. return this.collideTop(bounds, absVelocity);
  32. }
  33. collidePlatform(bounds, absVelocity) {
  34. let side = this.entity.bounds.intersectSide(bounds);
  35. if (side === "top")
  36. return this.collideTop(bounds, absVelocity);
  37. }
  38. update(dt) {
  39. // Gravity
  40. if (!this.onGround)
  41. this.velocity.y += this.gravity * dt;
  42. if (this.entity.has("collider")) {
  43. let jumping =
  44. this.groundVelocity &&
  45. this.velocity.y < this.groundVelocity.y;
  46. // Check if we're still on the same ground
  47. if (this.onGround) {
  48. let stillOnGround =
  49. (this.entity.bounds.intersects(this.groundBounds)) &&
  50. (this.entity.bounds.intersectSide(this.groundBounds) === "top") &&
  51. !jumping;
  52. if (stillOnGround) {
  53. this.entity.bounds.bottom = this.groundBounds.top;
  54. } else {
  55. this.velocity.x += this.groundVelocity.x;
  56. this.velocity.y += this.groundVelocity.y;
  57. this.onGround = false;
  58. this.oldGroundBounds = this.groundBounds;
  59. this.groundBounds = null;
  60. this.groundVelocity = null;
  61. }
  62. }
  63. // Collide with new stuff
  64. if (!jumping) {
  65. let collider = this.entity.t.collider;
  66. collider.entities.forEach(e => {
  67. let vel;
  68. if (e.has("physics"))
  69. vel = e.t.physics.absVelocity;
  70. else
  71. vel = zeroVector;
  72. if (e.has("wall"))
  73. this.collideWall(e.bounds, vel);
  74. else if (e.has("platform"))
  75. this.collidePlatform(e.bounds, vel);
  76. });
  77. collider.structures.forEach((s, i) => {
  78. let bounds = collider.structureBounds[i];
  79. if (s.attrs.wall)
  80. this.collideWall(bounds, zeroVector);
  81. else if (s.attrs.platform)
  82. this.collidePlatform(bounds, zeroVector);
  83. });
  84. }
  85. this.oldGroundBounds = null;
  86. }
  87. // Track the last time we were on the ground
  88. if (this.onGround)
  89. this.timeLastOnGround = this.entity.time;
  90. // Apply friction
  91. var fric = this.onGround ? this.groundFriction : this.airFriction;
  92. var xRatio = 1 / (1 + (dt * fric));
  93. this.velocity.x *= xRatio;
  94. }
  95. postUpdate(dt) {
  96. this.jumped = false;
  97. // Update absVelocity
  98. if (this.onGround) {
  99. this.absVelocity.x = this.velocity.x + this.groundVelocity.x;
  100. this.absVelocity.y = this.velocity.y + this.groundVelocity.y;
  101. } else {
  102. this.absVelocity.x = this.velocity.x;
  103. this.absVelocity.y = this.velocity.y;
  104. }
  105. // Move
  106. this.entity.bounds.pos.x += this.absVelocity.x * dt;
  107. this.entity.bounds.pos.y += this.absVelocity.y * dt;
  108. }
  109. }