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 2.5KB

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