Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Speed up game every frame #14

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/Obstacles.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const Obstacles = React.forwardRef(({ mode, videos, setScore, hitPoints, setHitP
const [obstacles, setObstacles] = useState([])
const [lastBonusToastId, setLastBonusToastId] = useState(null)
const [lastDamageToastId, setLastDamageToastId] = useState(null)
const [gameSpeed, setGameSpeed] = useState(0.1)

const visibleObstacles = 5
const clockRef = useRef({ elapsedTime: 0, delta: 0 })
Expand Down Expand Up @@ -100,7 +101,7 @@ const Obstacles = React.forwardRef(({ mode, videos, setScore, hitPoints, setHitP

// Near bonus box
const bonusScaleFator = 1.35
const bonusZScaleFator = 1.75
const bonusZScaleFator = 1.75 / (gameSpeed * 10)
const bonusCenter = new THREE.Vector3()
const bonusSize = new THREE.Vector3()
bonusBox.getCenter(bonusCenter)
Expand Down Expand Up @@ -187,7 +188,7 @@ const Obstacles = React.forwardRef(({ mode, videos, setScore, hitPoints, setHitP

if (canaryRef && canaryRef.current) {
// Move obstacles and clean up old ones
if (clockRef.current.delta >= 0.1) {
if (clockRef.current.delta >= gameSpeed) {
clockRef.current.elapsedTime = clock.getElapsedTime()
setObstacles(
prevObstacles =>
Expand Down Expand Up @@ -217,6 +218,9 @@ const Obstacles = React.forwardRef(({ mode, videos, setScore, hitPoints, setHitP

setObstacles(prevObstacles => [...prevObstacles, newObstacle])
}

// Increase game speed
setGameSpeed(gameSpeed => gameSpeed * 0.9992)
}
})

Expand Down