Animal: fix step times so that current time cannot drift too far away from step times. animate time also is checked always against current interval

This commit is contained in:
jettis
2024-12-18 23:35:47 +02:00
parent 9cedba02b0
commit c68deabd09

View File

@@ -304,30 +304,48 @@ void Animal::resetTimes(Uint64 t) {
} }
Uint64 Animal::step(Uint64 cur, bool press) { Uint64 Animal::step(Uint64 cur, bool press) {
if (!press && cur >= move_time) { short move_interval = 25;
if (!press && cur >= move_time + move_interval) {
move(); move();
move_time += 25; move_time += move_interval;
// don't allow move_time be too far away from cur
if (cur > move_time + move_interval * 5) {
move_time = cur;
}
} }
if (cur >= animate_time) { if (cur >= animate_time + cur_state->interval) {
animate(false); animate(false);
animate_time += cur_state->interval; animate_time += cur_state->interval;
// don't allow animate_time be too far away from cur
if (cur > animate_time + cur_state->interval * 5) {
animate_time = cur;
}
} }
if (!press && cur >= random_time) { short random_interval = 1000 * 5;
if (!press && cur >= random_time + random_interval) {
shuffle(); shuffle();
random_time += 1000 * 5; random_time += random_interval;
// don't allow random_time be too far away from cur
if (cur >= random_time + random_interval * 5) {
random_time = cur;
}
} }
Uint64 lowest_time = move_time; Uint64 nearest_time = move_time + move_interval;
if (lowest_time > animate_time) lowest_time = animate_time; if (nearest_time > animate_time + cur_state->interval) {
if (lowest_time > random_time) lowest_time = random_time; nearest_time = animate_time + cur_state->interval;
}
if (lowest_time < cur) { if (nearest_time > random_time + random_interval) {
lowest_time = cur; nearest_time = random_time + random_interval;
} }
return lowest_time - cur; if (nearest_time < cur) {
nearest_time = cur;
}
return nearest_time - cur;
} }
SDL_Surface *Animal::getImage() { SDL_Surface *Animal::getImage() {