mirror of
https://github.com/Kapendev/parin.git
synced 2025-04-27 13:39:54 +03:00
Added a timer module.
This commit is contained in:
parent
43691b5bd6
commit
39e1926791
9 changed files with 87 additions and 8 deletions
|
@ -10,7 +10,7 @@ enum header = "// ---
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// Email: alexandroskapretsos@gmail.com
|
// Email: alexandroskapretsos@gmail.com
|
||||||
// Project: https://github.com/Kapendev/popka
|
// Project: https://github.com/Kapendev/popka
|
||||||
// Version: v0.0.17
|
// Version: v0.0.18
|
||||||
// ---";
|
// ---";
|
||||||
|
|
||||||
int main(string[] args) {
|
int main(string[] args) {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// Email: alexandroskapretsos@gmail.com
|
// Email: alexandroskapretsos@gmail.com
|
||||||
// Project: https://github.com/Kapendev/popka
|
// Project: https://github.com/Kapendev/popka
|
||||||
// Version: v0.0.17
|
// Version: v0.0.18
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
/// The `dialogue` module provides a simple and versatile dialogue system.
|
/// The `dialogue` module provides a simple and versatile dialogue system.
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// Email: alexandroskapretsos@gmail.com
|
// Email: alexandroskapretsos@gmail.com
|
||||||
// Project: https://github.com/Kapendev/popka
|
// Project: https://github.com/Kapendev/popka
|
||||||
// Version: v0.0.17
|
// Version: v0.0.18
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
// TODO: Test the resources code and the tag thing.
|
// TODO: Test the resources code and the tag thing.
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// Email: alexandroskapretsos@gmail.com
|
// Email: alexandroskapretsos@gmail.com
|
||||||
// Project: https://github.com/Kapendev/popka
|
// Project: https://github.com/Kapendev/popka
|
||||||
// Version: v0.0.17
|
// Version: v0.0.18
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
module popka;
|
module popka;
|
||||||
|
@ -11,3 +11,4 @@ module popka;
|
||||||
public import popka.dialogue;
|
public import popka.dialogue;
|
||||||
public import popka.engine;
|
public import popka.engine;
|
||||||
public import popka.tilemap;
|
public import popka.tilemap;
|
||||||
|
public import popka.timer;
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// Email: alexandroskapretsos@gmail.com
|
// Email: alexandroskapretsos@gmail.com
|
||||||
// Project: https://github.com/Kapendev/popka
|
// Project: https://github.com/Kapendev/popka
|
||||||
// Version: v0.0.17
|
// Version: v0.0.18
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
/// The `rl` module provides access to the raylib library.
|
/// The `rl` module provides access to the raylib library.
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// Email: alexandroskapretsos@gmail.com
|
// Email: alexandroskapretsos@gmail.com
|
||||||
// Project: https://github.com/Kapendev/popka
|
// Project: https://github.com/Kapendev/popka
|
||||||
// Version: v0.0.17
|
// Version: v0.0.18
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
/**********************************************************************************************
|
/**********************************************************************************************
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// Email: alexandroskapretsos@gmail.com
|
// Email: alexandroskapretsos@gmail.com
|
||||||
// Project: https://github.com/Kapendev/popka
|
// Project: https://github.com/Kapendev/popka
|
||||||
// Version: v0.0.17
|
// Version: v0.0.18
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
/**********************************************************************************************
|
/**********************************************************************************************
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// Email: alexandroskapretsos@gmail.com
|
// Email: alexandroskapretsos@gmail.com
|
||||||
// Project: https://github.com/Kapendev/popka
|
// Project: https://github.com/Kapendev/popka
|
||||||
// Version: v0.0.17
|
// Version: v0.0.18
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
/// The `tilemap` module provides a simple and fast tile map.
|
/// The `tilemap` module provides a simple and fast tile map.
|
||||||
|
|
78
source/popka/timer.d
Normal file
78
source/popka/timer.d
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue