Skip to content

Commit

Permalink
fix raf loop, world consts
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Tańczyk committed May 12, 2024
1 parent f8f6565 commit 7cf0003
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
15 changes: 12 additions & 3 deletions games/nukes/src/controls/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useCustomEvent } from '../events';
import { EntityType, Explosion, Missile, WorldState } from '../world/world-state-types';
import { distance } from '../math/position-utils';
import { usePointer } from './pointer';
import { useSelectedObject } from './selection';
import { EXPLOSION_RADIUS, EXPLOSION_DURATION, MISSILE_SPEED } from '../world/world-state-constants';

export function Command({
worldState,
Expand All @@ -18,21 +20,28 @@ export function Command({
return;
}

const dist = distance(
selectedObject.position.x,
selectedObject.position.y,
pointer.pointingObjects[0].position.x,
pointer.pointingObjects[0].position.y,
);

const missile: Missile = {
id: Math.random() + '',
launch: selectedObject.position,
launchTimestamp: worldState.timestamp,

target: pointer.pointingObjects[0].position,
targetTimestamp: worldState.timestamp + 10,
targetTimestamp: worldState.timestamp + dist / MISSILE_SPEED,
};

const explosion: Explosion = {
id: Math.random() + '',
startTimestamp: missile.targetTimestamp,
endTimestamp: missile.targetTimestamp + 5,
endTimestamp: missile.targetTimestamp + EXPLOSION_DURATION,
position: missile.target,
radius: 30,
radius: EXPLOSION_RADIUS,
};

setWorldState({
Expand Down
19 changes: 16 additions & 3 deletions games/nukes/src/game-states/state-tech-world.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useState } from 'react';
import { useCallback, useRef, useState } from 'react';
import styled from 'styled-components';
import { useRafLoop } from 'react-use';

Expand Down Expand Up @@ -48,9 +48,22 @@ function TimeControls({
updateWorldTime: (deltaTime: number) => void;
}) {
const [isAutoplay, setAutoplay] = useState(true);
useRafLoop((deltaTime) => {
const timeRef = useRef<number | null>(null);
useRafLoop((time) => {
if (!timeRef.current) {
timeRef.current = time;
return;
}

const deltaTime = time - timeRef.current;
timeRef.current = time;

if (deltaTime <= 0) {
return;
}

if (isAutoplay) {
updateWorldTime(deltaTime / 100000);
updateWorldTime(deltaTime / 1000);
}
}, true);

Expand Down
11 changes: 11 additions & 0 deletions games/nukes/src/world/world-state-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** How fast are the missiles */
export const MISSILE_SPEED = 10;

/** What is the radius of explosions */
export const EXPLOSION_RADIUS = 20;

/** How fast is the explosion */
export const EXPLOSION_SPEED = 5;

/** How long does the explosion take */
export const EXPLOSION_DURATION = EXPLOSION_RADIUS / EXPLOSION_SPEED;

0 comments on commit 7cf0003

Please sign in to comment.