Added a timer module.

This commit is contained in:
Kapendev 2024-09-01 06:43:28 +03:00
parent 43691b5bd6
commit 39e1926791
9 changed files with 87 additions and 8 deletions

View file

@ -10,7 +10,7 @@ enum header = "// ---
// SPDX-License-Identifier: MIT
// Email: alexandroskapretsos@gmail.com
// Project: https://github.com/Kapendev/popka
// Version: v0.0.17
// Version: v0.0.18
// ---";
int main(string[] args) {

View file

@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT
// Email: alexandroskapretsos@gmail.com
// Project: https://github.com/Kapendev/popka
// Version: v0.0.17
// Version: v0.0.18
// ---
/// The `dialogue` module provides a simple and versatile dialogue system.

View file

@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT
// Email: alexandroskapretsos@gmail.com
// Project: https://github.com/Kapendev/popka
// Version: v0.0.17
// Version: v0.0.18
// ---
// TODO: Test the resources code and the tag thing.

View file

@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT
// Email: alexandroskapretsos@gmail.com
// Project: https://github.com/Kapendev/popka
// Version: v0.0.17
// Version: v0.0.18
// ---
module popka;
@ -11,3 +11,4 @@ module popka;
public import popka.dialogue;
public import popka.engine;
public import popka.tilemap;
public import popka.timer;

View file

@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT
// Email: alexandroskapretsos@gmail.com
// Project: https://github.com/Kapendev/popka
// Version: v0.0.17
// Version: v0.0.18
// ---
/// The `rl` module provides access to the raylib library.

View file

@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT
// Email: alexandroskapretsos@gmail.com
// Project: https://github.com/Kapendev/popka
// Version: v0.0.17
// Version: v0.0.18
// ---
/**********************************************************************************************

View file

@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT
// Email: alexandroskapretsos@gmail.com
// Project: https://github.com/Kapendev/popka
// Version: v0.0.17
// Version: v0.0.18
// ---
/**********************************************************************************************

View file

@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT
// Email: alexandroskapretsos@gmail.com
// Project: https://github.com/Kapendev/popka
// Version: v0.0.17
// Version: v0.0.18
// ---
/// The `tilemap` module provides a simple and fast tile map.

78
source/popka/timer.d Normal file
View file

@ -0,0 +1,78 @@
// ---
// Copyright 2024 Alexandros F. G. Kapretsos
// SPDX-License-Identifier: MIT
// Email: alexandroskapretsos@gmail.com
// Project: https://github.com/Kapendev/popka
// Version: v0.0.18
// ---
// TODO: Think about it. Just testing things for now.
/// The `timer` module provides a simple timer.
module popka.timer;
import popka.engine;
public import joka;
@safe @nogc nothrow:
struct Timer {
float time = 0.0f;
float duration = 0.0f;
float prevTime = 0.0f;
bool isPaused;
bool canRepeat;
@safe @nogc nothrow:
this(float duration, bool canRepeat = false) {
this.time = duration;
this.duration = duration;
this.prevTime = duration;
this.canRepeat = canRepeat;
}
bool isRunning() {
return !isPaused && time != duration;
}
bool hasStarted() {
return !isPaused && time != duration && prevTime != time && prevTime == 0.0f;
}
bool hasEnded() {
return !isPaused && time == duration && prevTime != duration;
}
void start(float duration = -1.0f) {
if (duration >= 0.0f) this.duration = duration;
time = 0.0f;
prevTime = 0.0f;
isPaused = false;
}
void pause() {
isPaused = true;
}
void resume() {
isPaused = false;
}
void toggleIsPaused() {
isPaused = !isPaused;
}
void end() {
time = duration;
prevTime = duration;
isPaused = false;
}
void update() {
if (isPaused) return;
if (canRepeat && hasEnded) start();
prevTime = time;
time = min(time + deltaTime, duration);
}
}