fixed timer functions
This commit is contained in:
@ -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() {
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <SDL3/SDL.h>
|
||||
#include "../../core/GravityManager.h"
|
||||
|
||||
enum PieceType { I, O, T, S, Z, J, L, PIECE_COUNT };
|
||||
@ -40,13 +41,14 @@ public:
|
||||
bool canHoldPiece() const { return canHold; }
|
||||
bool isGameOver() const { return gameOver; }
|
||||
bool isPaused() const { return paused; }
|
||||
void setPaused(bool p) { paused = p; }
|
||||
void setPaused(bool p);
|
||||
int score() const { return _score; }
|
||||
int lines() const { return _lines; }
|
||||
int level() const { return _level; }
|
||||
int startLevelBase() const { return startLevel; }
|
||||
double elapsed() const { return _elapsedSec; }
|
||||
void addElapsed(double frameMs) { if (!paused) _elapsedSec += frameMs/1000.0; }
|
||||
double elapsed() const; // Now calculated from start time
|
||||
void updateElapsedTime(); // Update elapsed time from system clock
|
||||
bool isSoftDropping() const { return softDropping; }
|
||||
|
||||
// Block statistics
|
||||
const std::array<int, PIECE_COUNT>& getBlockCounts() const { return blockCounts; }
|
||||
@ -69,6 +71,7 @@ public:
|
||||
void setGravityGlobalMultiplier(double m) { gravityGlobalMultiplier = m; }
|
||||
double getGravityGlobalMultiplier() const;
|
||||
double getGravityMs() const;
|
||||
double getFallAccumulator() const { return fallAcc; } // Debug: time accumulated toward next drop
|
||||
void setLevelGravityMultiplier(int level, double m);
|
||||
|
||||
private:
|
||||
@ -85,7 +88,9 @@ private:
|
||||
int _level{1};
|
||||
double gravityMs{800.0};
|
||||
double fallAcc{0.0};
|
||||
double _elapsedSec{0.0};
|
||||
Uint64 _startTime{0}; // Performance counter at game start
|
||||
Uint64 _pausedTime{0}; // Time spent paused (in performance counter ticks)
|
||||
Uint64 _lastPauseStart{0}; // When the current pause started
|
||||
bool gameOver{false};
|
||||
int startLevel{0};
|
||||
bool softDropping{false}; // true while player holds Down key
|
||||
|
||||
Reference in New Issue
Block a user