cubeforge 0.4.12 → 0.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +39 -7
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -18227,6 +18227,9 @@ function usePlatformerController(entityId, opts = {}) {
18227
18227
  coyoteTime = 0.08,
18228
18228
  jumpBuffer = 0.08,
18229
18229
  jumpCooldown = 0.18,
18230
+ acceleration = Infinity,
18231
+ groundFriction = 0.6,
18232
+ airFriction = 0.92,
18230
18233
  gamepadIndex = 0,
18231
18234
  gamepadDeadZone = 0.2,
18232
18235
  bindings
@@ -18264,9 +18267,18 @@ function usePlatformerController(entityId, opts = {}) {
18264
18267
  else if (!jumpKeys.some((k) => input.isDown(k)) && !gpJump) state.jumpBuffer = Math.max(0, state.jumpBuffer - dt);
18265
18268
  const left = leftKeys.some((k) => input.isDown(k)) || gpLeft;
18266
18269
  const right = rightKeys.some((k) => input.isDown(k)) || gpRight;
18267
- if (left) rb.vx = -speed;
18268
- else if (right) rb.vx = speed;
18269
- else rb.vx *= rb.onGround ? 0.6 : 0.92;
18270
+ if (left || right) {
18271
+ const targetVx = left ? -speed : speed;
18272
+ if (acceleration === Infinity) {
18273
+ rb.vx = targetVx;
18274
+ } else {
18275
+ const diff = targetVx - rb.vx;
18276
+ const maxDelta = acceleration * dt;
18277
+ rb.vx += Math.abs(diff) <= maxDelta ? diff : Math.sign(diff) * maxDelta;
18278
+ }
18279
+ } else {
18280
+ rb.vx *= rb.onGround ? groundFriction : airFriction;
18281
+ }
18270
18282
  const sprite = world.getComponent(id, "Sprite");
18271
18283
  if (sprite) {
18272
18284
  if (left) sprite.flipX = true;
@@ -18508,11 +18520,16 @@ function useIDBSave(key, defaultValue, opts = {}) {
18508
18520
 
18509
18521
  // ../gameplay/src/hooks/useTopDownMovement.ts
18510
18522
  import { useContext as useContext60, useEffect as useEffect65 } from "react";
18523
+ function moveToward(current, target, maxDelta) {
18524
+ const diff = target - current;
18525
+ if (Math.abs(diff) <= maxDelta) return target;
18526
+ return current + Math.sign(diff) * maxDelta;
18527
+ }
18511
18528
  function useTopDownMovement(entityId, opts = {}) {
18512
18529
  const engine = useContext60(EngineContext);
18513
- const { speed = 200, normalizeDiagonal = true } = opts;
18530
+ const { speed = 200, normalizeDiagonal = true, acceleration = Infinity, deceleration = acceleration } = opts;
18514
18531
  useEffect65(() => {
18515
- const updateFn = (id, world, input) => {
18532
+ const updateFn = (id, world, input, dt) => {
18516
18533
  if (!world.hasEntity(id)) return;
18517
18534
  const rb = world.getComponent(id, "RigidBody");
18518
18535
  if (!rb) return;
@@ -18527,8 +18544,23 @@ function useTopDownMovement(entityId, opts = {}) {
18527
18544
  dx /= len2;
18528
18545
  dy /= len2;
18529
18546
  }
18530
- rb.vx = dx * speed;
18531
- rb.vy = dy * speed;
18547
+ const targetVx = dx * speed;
18548
+ const targetVy = dy * speed;
18549
+ if (acceleration === Infinity && deceleration === Infinity) {
18550
+ rb.vx = targetVx;
18551
+ rb.vy = targetVy;
18552
+ } else {
18553
+ const hasInput = dx !== 0 || dy !== 0;
18554
+ const rate = hasInput ? acceleration : deceleration;
18555
+ if (rate === Infinity) {
18556
+ rb.vx = targetVx;
18557
+ rb.vy = targetVy;
18558
+ } else {
18559
+ const maxDelta = rate * dt;
18560
+ rb.vx = moveToward(rb.vx, targetVx, maxDelta);
18561
+ rb.vy = moveToward(rb.vy, targetVy, maxDelta);
18562
+ }
18563
+ }
18532
18564
  };
18533
18565
  engine.ecs.addComponent(entityId, createScript(updateFn));
18534
18566
  return () => engine.ecs.removeComponent(entityId, "Script");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cubeforge",
3
- "version": "0.4.12",
3
+ "version": "0.4.13",
4
4
  "description": "React-first 2D browser game engine",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",