Made setup better and added sound stuff.

This commit is contained in:
Kapendev 2024-10-02 19:20:20 +03:00
parent 9984a3cd04
commit cf5bf2ac27
2 changed files with 58 additions and 13 deletions

View file

@ -9,7 +9,9 @@ enum webDir = buildPath(".", "web");
enum appFile = buildPath(".", "source", "app.d");
enum dubFile = buildPath(".", "dub.json");
enum dubCopyFile = buildPath(".", ".__dub_copy__.json");
enum dubLockFile = buildPath(".", "dub.selections.json");
enum dubLockCopyFile = buildPath(".", ".__dub_selections_copy__.json");
enum gitignoreFile = buildPath(".", ".gitignore");
enum appFileContent = `import parin;
@ -108,15 +110,31 @@ int main(string[] args) {
// Also raylib-d:install does not like it when you don't have one.
if (isDubProject) {
if (!exists(appFile)) std.file.write(appFile, appFileContent);
if (!isFirstRun) {
std.file.write(dubCopyFile, std.file.readText(dubFile));
if (exists(dubLockFile)) std.file.write(dubLockCopyFile, std.file.readText(dubLockFile));
}
}
// Use the raylib-d script to download the raylib library files.
if (isDubProject) {
writeln("Simply say \"yes\" to all prompts.\n");
auto dub1 = spawnProcess(["dub", "add", "raylib-d"]).wait();
if (dub1 != 0) return dub1;
if (dub1 != 0) {
if (!isFirstRun) {
std.file.remove(dubCopyFile);
if (exists(dubLockCopyFile)) std.file.remove(dubLockCopyFile);
}
return dub1;
}
auto dub2 = spawnProcess(["dub", "run", "raylib-d:install"]).wait();
if (dub2 != 0) return dub2;
if (dub2 != 0) {
if (!isFirstRun) {
std.file.remove(dubCopyFile);
if (exists(dubLockCopyFile)) std.file.remove(dubLockCopyFile);
}
return dub2;
}
}
// Remove old files.
@ -130,7 +148,14 @@ int main(string[] args) {
// Create new files.
if (isDubProject) {
if (isFirstRun) std.file.write(appFile, appFileContent);
std.file.write(dubFile, dubFileContent);
if (exists(dubCopyFile)) {
std.file.write(dubFile, std.file.readText(dubCopyFile));
std.file.remove(dubCopyFile);
if (exists(dubLockCopyFile)) std.file.write(dubLockFile, std.file.readText(dubLockCopyFile));
if (exists(dubLockCopyFile)) std.file.remove(dubLockCopyFile);
} else {
std.file.write(dubFile, dubFileContent);
}
}
if (isFirstRun) std.file.write(gitignoreFile, gitignoreFileContent);

View file

@ -6,7 +6,9 @@
// Version: v0.0.23
// ---
// TODO: Test the resources code and the tag thing.
// TODO: Test the resource loading code.
// TODO: Make sounds loop based on a variable and not on the file type.
// NOTE: The main problem with sound looping is the raylib API.
/// The `engine` module functions as a lightweight 2D game engine.
module parin.engine;
@ -215,7 +217,7 @@ struct Camera {
/// Moves the camera to follow the target position at the specified speed.
void followPosition(Vec2 target, float speed) {
position = position.moveTo(target, Vec2(speed * deltaTime));
position = position.moveTo(target, Vec2(speed));
}
/// Moves the camera to follow the target position with gradual slowdown.
@ -225,7 +227,7 @@ struct Camera {
/// Adjusts the cameras zoom level to follow the target value at the specified speed.
void followScale(float target, float speed) {
scale = scale.moveTo(target, speed * deltaTime);
scale = scale.moveTo(target, speed);
}
/// Adjusts the cameras zoom level to follow the target value with gradual slowdown.
@ -489,10 +491,19 @@ struct Sound {
}
}
/// Returns true if the sound is playing.
@trusted
bool isPlaying() {
if (data.isKind!(rl.Sound)) {
return rl.IsSoundPlaying(data.get!(rl.Sound)());
} else {
return rl.IsMusicStreamPlaying(data.get!(rl.Music)());
}
}
/// Returns the current playback time of the sound.
@trusted
float time() {
if (isEmpty) return 0.0f;
if (data.isKind!(rl.Sound)) {
return 0.0f;
} else {
@ -503,7 +514,6 @@ struct Sound {
/// Returns the total duration of the sound.
@trusted
float duration() {
if (isEmpty) return 0.0f;
if (data.isKind!(rl.Sound)) {
return 0.0f;
} else {
@ -511,6 +521,12 @@ struct Sound {
}
}
/// Returns the progress of the sound.
float progress() {
if (duration == 0.0f) return 0.0f;
return time / duration;
}
/// Sets the volume level for the sound.
@trusted
void setVolume(float value) {
@ -544,9 +560,7 @@ struct Sound {
/// Frees the loaded sound.
@trusted
void free() {
if (isEmpty) {
return;
}
if (isEmpty) return;
if (data.isKind!(rl.Sound)) {
rl.UnloadSound(data.get!(rl.Sound)());
} else {
@ -573,6 +587,10 @@ struct SoundId {
return getOr().duration;
}
float progress() {
return getOr().progress;
}
/// Checks if the resource identifier is valid. It becomes automatically invalid when the resource is freed.
bool isValid() {
return data.value != 0 && engineState.resources.sounds.has(data);
@ -772,7 +790,7 @@ struct EngineState {
Color borderColor;
Color backgroundColor;
Filter defaultFilter;
ulong tickCount;
Sz tickCount;
@safe:
@ -1166,7 +1184,7 @@ void updateWindow(bool function(float dt) updateFunc) {
// The main loop.
auto dt = deltaTime;
auto result = _updateFunc(dt);
engineState.tickCount = (engineState.tickCount + 1) % typeof(engineState.tickCount).max;
engineState.tickCount = (engineState.tickCount + 1) % engineState.tickCount.max;
// End drawing.
if (isResolutionLocked) {
@ -1666,6 +1684,7 @@ Vec2 wasd() {
}
/// Plays the specified sound.
/// The sound will loop automatically for certain file types (OGG, MP3).
@trusted
void playSound(Sound sound) {
if (sound.isEmpty) {
@ -1680,6 +1699,7 @@ void playSound(Sound sound) {
}
/// Plays the specified sound.
/// The sound will loop automatically for certain file types (OGG, MP3).
void playSound(SoundId sound) {
playSound(sound.getOr());
}