fixed timer functions

This commit is contained in:
2025-11-23 18:41:52 +01:00
parent d5fdae397d
commit adec55526e
7 changed files with 100 additions and 22 deletions

View File

@ -56,11 +56,53 @@ void Game::reset(int startLevel_) {
_score = 0; _lines = 0; _level = startLevel_; startLevel = startLevel_;
// Initialize gravity using NES timing table (ms per cell by level)
gravityMs = gravityMsForLevel(_level, gravityGlobalMultiplier);
fallAcc = 0; _elapsedSec = 0; gameOver=false; paused=false;
fallAcc = 0; gameOver=false; paused=false;
_startTime = SDL_GetPerformanceCounter();
_pausedTime = 0;
_lastPauseStart = 0;
hold = Piece{}; hold.type = PIECE_COUNT; canHold=true;
refillBag(); spawn();
}
double Game::elapsed() const {
if (!_startTime) return 0.0;
Uint64 currentTime = SDL_GetPerformanceCounter();
Uint64 totalPausedTime = _pausedTime;
// If currently paused, add time since pause started
if (paused && _lastPauseStart > 0) {
totalPausedTime += (currentTime - _lastPauseStart);
}
Uint64 activeTime = currentTime - _startTime - totalPausedTime;
double seconds = (double)activeTime / (double)SDL_GetPerformanceFrequency();
return seconds;
}
void Game::updateElapsedTime() {
// This method is now just for API compatibility
// Actual elapsed time is calculated on-demand in elapsed()
}
void Game::setPaused(bool p) {
if (p == paused) return; // No change
if (p) {
// Pausing - record when pause started
_lastPauseStart = SDL_GetPerformanceCounter();
} else {
// Unpausing - add elapsed pause time to total
if (_lastPauseStart > 0) {
Uint64 currentTime = SDL_GetPerformanceCounter();
_pausedTime += (currentTime - _lastPauseStart);
_lastPauseStart = 0;
}
}
paused = p;
}
void Game::refillBag() {
bag.clear();
for (int i=0;i<PIECE_COUNT;++i) bag.push_back(static_cast<PieceType>(i));
@ -241,13 +283,15 @@ bool Game::tryMoveDown() {
void Game::tickGravity(double frameMs) {
if (paused) return; // Don't tick gravity when paused
// Soft drop: 20x faster for rapid continuous dropping
double effectiveGravityMs = softDropping ? (gravityMs / 5.0) : gravityMs;
fallAcc += frameMs;
while (fallAcc >= gravityMs) {
while (fallAcc >= effectiveGravityMs) {
// Attempt to move down by one row
if (tryMoveDown()) {
// Award soft drop points only if player is actively holding Down
// JS: POINTS.SOFT_DROP = 1 per cell for soft drop
if (softDropping) {
_score += 1;
}
@ -256,13 +300,14 @@ void Game::tickGravity(double frameMs) {
lockPiece();
if (gameOver) break;
}
fallAcc -= gravityMs;
fallAcc -= effectiveGravityMs;
}
}
void Game::softDropBoost(double frameMs) {
// Reduce soft drop speed multiplier from 10.0 to 3.0 to make it less aggressive
if (!paused) fallAcc += frameMs * 3.0;
// This method is now deprecated - soft drop is handled in tickGravity
// Kept for API compatibility but does nothing
(void)frameMs;
}
void Game::hardDrop() {