2274 lines
92 KiB
C++
2274 lines
92 KiB
C++
/* _ _
|
|
* c/a`k-e'f`o^o~t-, | a single-button action game | by @ooofoam
|
|
* / _< | wow a living cake the sweet | play online: https://shampoo.ooo/cakefoot
|
|
* > `~_/ | taste of victory | open source: https://open.shampoo.ooo/shampoo/cakefoot
|
|
*/
|
|
|
|
#if defined(__ANDROID__) || defined(ANDROID)
|
|
#include <android/asset_manager_jni.h>
|
|
#endif
|
|
|
|
#include "Cakefoot.hpp"
|
|
|
|
Cakefoot::Cakefoot()
|
|
{
|
|
#ifdef __ANDROID__
|
|
SDL_SetHint(SDL_HINT_ORIENTATIONS, "Landscape");
|
|
#endif
|
|
|
|
/* Merge the level JSON */
|
|
configuration().merge(levels_file_path);
|
|
|
|
/* Load scores */
|
|
if (fs::exists(arcade_scores_file_path))
|
|
{
|
|
for (nlohmann::json score : Configuration::json_from_file(arcade_scores_file_path))
|
|
{
|
|
arcade_scores.add(ArcadeScores::Score(score.at("time"), score.at("distance"), score.at("name")));
|
|
}
|
|
}
|
|
|
|
/* Add default progress data */
|
|
configuration()["progress"] = {
|
|
{"current level", 1},
|
|
{"max level", 1},
|
|
{"current difficulty", 0},
|
|
{"max difficulty", 0},
|
|
{"current challenge", 1},
|
|
{"max challenge", 4},
|
|
{"current view", 0},
|
|
{"max view", 0},
|
|
{"deaths", 0},
|
|
{"total time", 0.0f},
|
|
{"quest level", 1},
|
|
{"quest checkpoint", 0.0f},
|
|
{"quest difficulty", 0},
|
|
{"quest time", 0.0f},
|
|
{"quest coin", false},
|
|
{"quest bank", 0},
|
|
{"quest best", 0.0},
|
|
{"arcade level", 1},
|
|
{"arcade checkpoint", 0.0f},
|
|
{"arcade difficulty", 0},
|
|
{"arcade max distance", 0},
|
|
{"arcade time", 0.0f},
|
|
{"arcade coin", false},
|
|
{"arcade bank", 0}
|
|
};
|
|
|
|
/* Overwrite progress data with saved data if it is available */
|
|
if (fs::exists(progress_file_path))
|
|
{
|
|
/* Check progress data for existence of quest progress entries. If they are missing, it indicates an older progress file, so use the general progress
|
|
* values for the quest progress. */
|
|
nlohmann::json progress = Configuration::json_from_file(progress_file_path);
|
|
if (!progress.at("progress").contains("quest level"))
|
|
{
|
|
configuration()["progress"]["quest level"] = progress.at("progress").at("current level");
|
|
configuration()["progress"]["current challenge"] = 0;
|
|
}
|
|
if (!progress.at("progress").contains("quest checkpoint"))
|
|
{
|
|
configuration()["progress"]["quest difficulty"] = progress.at("progress").at("current difficulty");
|
|
}
|
|
if (!progress.at("progress").contains("quest time"))
|
|
{
|
|
configuration()["progress"]["quest time"] = progress.at("progress").at("total time");
|
|
}
|
|
|
|
/* Merge save data to overwrite defaults */
|
|
configuration().merge(progress_file_path);
|
|
}
|
|
|
|
/* Enforce arcade-only mode */
|
|
if (configuration()("display", "arcade only"))
|
|
{
|
|
configuration()["progress"]["current challenge"] = 4;
|
|
configuration()["progress"]["current difficulty"] = 0;
|
|
|
|
/* In arcade-only, there is no resume, so reset the current level regardless of what the saved state was. */
|
|
configuration()["progress"]["current level"] = 1;
|
|
}
|
|
|
|
/* Enforce max challenge to 4 */
|
|
configuration()["progress"]["max challenge"] = 4;
|
|
if (configuration()("progress", "current challenge") > 4)
|
|
{
|
|
configuration()["progress"]["current challenge"] = 4;
|
|
}
|
|
|
|
/* Set the spinner values to what the player was last playing. */
|
|
level_select_index = configuration()("progress", "current level");
|
|
profile_index = configuration()("progress", "current difficulty");
|
|
challenge_index = configuration()("progress", "current challenge");
|
|
view_index = configuration()("progress", "current view");
|
|
|
|
/* Subscribe to command events */
|
|
delegate().subscribe(&Cakefoot::respond, this);
|
|
delegate().subscribe(&Cakefoot::respond, this, SDL_MOUSEMOTION);
|
|
delegate().subscribe(&Cakefoot::respond, this, SDL_MOUSEBUTTONDOWN);
|
|
delegate().subscribe(&Cakefoot::respond, this, SDL_MOUSEBUTTONUP);
|
|
delegate().subscribe(&Cakefoot::respond, this, SDL_MOUSEWHEEL);
|
|
|
|
/* Set up playing field, the plane that provides the background of the curve, character, and enemies */
|
|
sb::Plane playing_field_plane;
|
|
playing_field_plane.scale(glm::vec3{_configuration("display", "playing field aspect"), 1.0f, 1.0f});
|
|
playing_field = sb::Sprite(playing_field_plane);
|
|
|
|
/* Open the configuration and load the curve data per level */
|
|
load_curves();
|
|
|
|
/* loading GL context for 3D */
|
|
load_gl_context();
|
|
|
|
/* Load and fill VBO */
|
|
load_vbo();
|
|
|
|
/* Load cursors from the system library that will be freed automatically */
|
|
poke = std::shared_ptr<SDL_Cursor>(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND), SDL_FreeCursor);
|
|
grab = std::shared_ptr<SDL_Cursor>(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL), SDL_FreeCursor);
|
|
|
|
/* Set the character to use the profile stored to progress */
|
|
character.profile(configuration()("character", "profile", profile_index, "name"));
|
|
|
|
/* Try to load fonts. Default to the game object's font if the font doesn't load. */
|
|
for (auto [name, font_config] : configuration()("font").items())
|
|
{
|
|
fonts[name] = {TTF_OpenFont(font_config["path"].get<std::string>().c_str(), font_config["size"]), TTF_CloseFont};
|
|
std::ostringstream message;
|
|
if (fonts[name].get() == nullptr)
|
|
{
|
|
message << "Could not load " << font_config["path"] << ". Using framework's font instead.";
|
|
sb::Log::log(message, sb::Log::ERR);
|
|
|
|
/* Default to framework's font */
|
|
fonts[name] = font();
|
|
}
|
|
else
|
|
{
|
|
message << "Loaded " << font_config["path"];
|
|
sb::Log::log(message);
|
|
}
|
|
}
|
|
|
|
/* Set up checkpoint on and off sprites */
|
|
checkpoint_on = sb::Sprite {"resource/checkpoint/on.png", glm::vec2(12.0f / 486.0f)};
|
|
checkpoint_off = sb::Sprite {"resource/checkpoint/off.png", glm::vec2(12.0f / 486.0f)};
|
|
|
|
/* Set hitbox */
|
|
character.box_size(configuration()("character", "hitbox").get<float>());
|
|
|
|
/* Load coin graphics */
|
|
coin.load();
|
|
|
|
/* Load SFX and BGM */
|
|
load_audio();
|
|
|
|
/* Set value from configuration once at construction so it doesn't change at runtime and break the program */
|
|
use_play_button = configuration()("display", "use play button");
|
|
|
|
/* Set to default values in case these get displayed erroneously */
|
|
label.at("arcade rank").content("999th");
|
|
label.at("arcade distance").content("10000m");
|
|
|
|
/* Initialize scoreboard content */
|
|
refresh_scoreboard();
|
|
|
|
/* Start tracking hue rotation */
|
|
shift_hue_animation.frame_length(configuration()("display", "hue shift frequency"));
|
|
shift_hue_animation.play();
|
|
|
|
if (!use_play_button)
|
|
{
|
|
/* Load title screen */
|
|
load_level(0);
|
|
}
|
|
else
|
|
{
|
|
/* Just set up buttons because only play and volume buttons are needed */
|
|
set_up_buttons();
|
|
}
|
|
|
|
/* Switch volume on */
|
|
button.at("volume").press();
|
|
}
|
|
|
|
void Cakefoot::load_audio()
|
|
{
|
|
audio = {};
|
|
for (const auto& [name, path] : configuration()("audio", "files").items())
|
|
{
|
|
std::ostringstream message;
|
|
message << "Loading audio at " << path.get<std::string>();
|
|
sb::Log::log(message);
|
|
audio[name] = sb::audio::Chunk(path.get<std::string>());
|
|
}
|
|
|
|
/* Set number of loops for looping audio */
|
|
audio.at("restart").loop(5);
|
|
audio.at("main").loop();
|
|
audio.at("menu").loop();
|
|
audio.at("walk").loop();
|
|
audio.at("reverse").loop();
|
|
|
|
/* Set volumes */
|
|
for (const auto& [name, volume] : configuration()("audio", "volume").items())
|
|
{
|
|
audio.at(name).volume(volume);
|
|
}
|
|
|
|
/* Reserve two channels for walk sound effects so the channel volume manipulation won't affect other sounds. */
|
|
int result = Mix_ReserveChannels(2);
|
|
if (result != 2)
|
|
{
|
|
sb::Log::sdl_error("Unable to reserve audio channels in SDL mixer", sb::Log::WARN);
|
|
}
|
|
}
|
|
|
|
void Cakefoot::load_curves()
|
|
{
|
|
/* Reset curve list and count of vertex buffer bytes needed for the curves */
|
|
curve_byte_count = 0;
|
|
curves.clear();
|
|
|
|
/* Open the levels section of the configuration and iterate through the list of levels. */
|
|
nlohmann::json levels = configuration()["levels"];
|
|
for (std::size_t ii = 0; ii < levels.size(); ii++)
|
|
{
|
|
/* Get the current level curve points, which is a list of 2D vertices in the old format of 864x486. The vertices are control points for
|
|
* the bezier. */
|
|
nlohmann::json control = levels[ii]["curve"];
|
|
glm::vec2 orig = {864.0f, 486.0f}, point;
|
|
float rat = orig.x / orig.y;
|
|
|
|
/* Translate each control point into the -aspect - aspect format. */
|
|
for (std::size_t jj = 0; jj < control.size(); jj++)
|
|
{
|
|
point = control[jj];
|
|
control[jj] = {(point.x / orig.x) * (2.0f * rat) - rat, (1.0f - point.y / orig.y) * 2.0f - 1.0f};
|
|
}
|
|
|
|
/* For each group of four control points, create a bezier, and add each of its vertices to a vector containing all the non-wrapped vertices, which
|
|
* is the full curve for the current level before being wrapped. */
|
|
std::vector<glm::vec3> unwrapped;
|
|
for (std::size_t jj = 0; jj < control.size() - 2; jj += 3)
|
|
{
|
|
std::vector<glm::vec3> segment;
|
|
for (const glm::vec2& vertex : sb::bezier({control[jj], control[jj + 1], control[jj + 2], control[jj + 3]},
|
|
_configuration("curve", "bezier resolution").get<int>()))
|
|
{
|
|
segment.push_back({vertex, 0.0f});
|
|
}
|
|
unwrapped.insert(unwrapped.end(), segment.begin(), segment.end());
|
|
}
|
|
|
|
/* Pass the vertices to a curve object, which will store the originals, then wrap them. */
|
|
Curve curve {rat};
|
|
curve.add(unwrapped);
|
|
curves.push_back(curve);
|
|
curve_byte_count += curve.size();
|
|
}
|
|
}
|
|
|
|
void Cakefoot::load_gl_context()
|
|
{
|
|
super::load_gl_context();
|
|
|
|
/* Generate a vertex array object ID, bind it as current (requirement of OpenGL) */
|
|
vao.generate();
|
|
sb::Log::gl_errors("after generating VAO");
|
|
vao.bind();
|
|
sb::Log::gl_errors("after binding VAO");
|
|
|
|
/* Load shader program */
|
|
GLuint vertex_shader = load_shader("src/shaders/shader.vert", GL_VERTEX_SHADER);
|
|
GLuint fragment_shader = load_shader("src/shaders/shader.frag", GL_FRAGMENT_SHADER);
|
|
shader_program = glCreateProgram();
|
|
glAttachShader(shader_program, vertex_shader);
|
|
sb::Log::gl_errors("after loading vertex shader");
|
|
glAttachShader(shader_program, fragment_shader);
|
|
sb::Log::gl_errors("after loading fragment shader");
|
|
|
|
/* link shaders */
|
|
link_shader(shader_program);
|
|
glUseProgram(shader_program);
|
|
sb::Log::gl_errors("after linking");
|
|
|
|
/* store uniform locations after linking */
|
|
uniform["mvp"] = glGetUniformLocation(shader_program, "mvp");
|
|
uniform["time"] = glGetUniformLocation(shader_program, "time");
|
|
uniform["uv transformation"] = glGetUniformLocation(shader_program, "uv_transformation");
|
|
uniform["coordinate bound"] = glGetUniformLocation(shader_program, "coordinate_bound");
|
|
uniform["model texture"] = glGetUniformLocation(shader_program, "model_texture");
|
|
uniform["texture enabled"] = glGetUniformLocation(shader_program, "texture_enabled");
|
|
uniform["color addition"] = glGetUniformLocation(shader_program, "color_addition");
|
|
|
|
/* Set the active texture and uniform once at context load time because only one texture is used per draw */
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glUniform1i(uniform["model texture"], 0);
|
|
sb::Log::gl_errors("after uniform locations");
|
|
|
|
/* Initialize color addition to zero. */
|
|
glUniform4f(uniform["color addition"], 0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
/* Enable alpha rendering, disable depth test, set clear color */
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
glEnable(GL_BLEND);
|
|
glDisable(GL_DEPTH_TEST);
|
|
glm::vec4 clear_color = _configuration("display", "clear color").get<glm::vec4>();
|
|
glClearColor(clear_color[0], clear_color[1], clear_color[2], clear_color[3]);
|
|
sb::Log::gl_errors("after GL initialization");
|
|
}
|
|
|
|
void Cakefoot::set_up_buttons()
|
|
{
|
|
/* Set up text buttons */
|
|
for (const std::string& name : {
|
|
"start", "resume", "reset", "level increment", "level decrement", "profile increment", "profile decrement", "challenge increment",
|
|
"challenge decrement", "view increment", "view decrement"
|
|
})
|
|
{
|
|
sb::Text text {fonts.at("text")};
|
|
float scale;
|
|
glm::vec2 dimensions;
|
|
if (name == "start" || name == "resume" || name == "reset")
|
|
{
|
|
dimensions = glm::vec2{configuration()("button", "text dimensions")};
|
|
scale = configuration()("button", "text scale");
|
|
}
|
|
else
|
|
{
|
|
dimensions = glm::vec2{configuration()("button", "level decrement dimensions")};
|
|
if (configuration()("button", "level select scale").is_array())
|
|
{
|
|
scale = configuration()("button", "level select scale")[1];
|
|
}
|
|
else
|
|
{
|
|
scale = configuration()("button", "level select scale");
|
|
}
|
|
}
|
|
text.foreground(configuration()("button", "text foreground").get<glm::vec4>());
|
|
text.background(configuration()("button", "text background").get<glm::vec4>());
|
|
text.content(configuration()("button", name + " text"));
|
|
text.dimensions(dimensions);
|
|
button.at(name) = sb::Pad<>{text, configuration()("button", name + " translation"), scale, dimensions.y / dimensions.x};
|
|
}
|
|
|
|
/* Replace start text texture with arcade prompt image in arcade-only mode */
|
|
if (configuration()("display", "arcade only"))
|
|
{
|
|
sb::Texture arcade_prompt_texture {configuration()("button", "start alt texture").get<std::string>()};
|
|
arcade_prompt_texture.load();
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
sb::Plane arcade_prompt_plane;
|
|
arcade_prompt_plane.texture(arcade_prompt_texture);
|
|
button.at("start") = sb::Pad<>{arcade_prompt_plane, configuration()("button", "start alt translation"), configuration()("button", "start alt scale")[1],
|
|
1.0f / 5.0f};
|
|
}
|
|
|
|
/* Set up text button callbacks */
|
|
button.at("start").on_state_change([&](bool state){
|
|
if (configuration()("challenge", challenge_index, "name") == "NEW QUEST")
|
|
{
|
|
configuration()["progress"]["quest level"] = 1;
|
|
configuration()["progress"]["quest checkpoint"] = 0.0f;
|
|
configuration()["progress"]["quest difficulty"] = profile_index;
|
|
configuration()["progress"]["quest time"] = 0.0f;
|
|
configuration()["progress"]["quest bank"] = 0;
|
|
challenge_index = 0;
|
|
configuration()["progress"]["current challenge"] = challenge_index;
|
|
}
|
|
else if (configuration()("challenge", challenge_index, "name") == "ARCADE")
|
|
{
|
|
configuration()["progress"]["arcade level"] = 1;
|
|
configuration()["progress"]["arcade checkpoint"] = 0.0f;
|
|
configuration()["progress"]["arcade difficulty"] = profile_index;
|
|
configuration()["progress"]["arcade max distance"] = 0;
|
|
configuration()["progress"]["arcade time"] = 0.0f;
|
|
configuration()["progress"]["arcade bank"] = 0;
|
|
challenge_index = 3;
|
|
configuration()["progress"]["current challenge"] = challenge_index;
|
|
}
|
|
load_level(level_select_index);
|
|
});
|
|
button.at("resume").on_state_change([&](bool state){
|
|
unpaused_timer.on();
|
|
run_timer.on();
|
|
|
|
/* Transition between menu theme and main theme */
|
|
if (audio.at("menu").playing())
|
|
{
|
|
audio.at("menu").pause();
|
|
}
|
|
if (audio.at("main").paused())
|
|
{
|
|
audio.at("main").resume();
|
|
}
|
|
else if (audio.at("main").fading() || !audio.at("main").playing())
|
|
{
|
|
audio.at("main").play();
|
|
}
|
|
});
|
|
button.at("reset").on_state_change([&](bool state){
|
|
sb::Delegate::post(reset_command_name, false);
|
|
});
|
|
|
|
/* Set up pause button */
|
|
sb::Texture pause_texture {configuration()("button", "pause texture").get<std::string>()};
|
|
pause_texture.load();
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
sb::Plane pause_plane;
|
|
pause_plane.texture(pause_texture);
|
|
button.at("pause") = sb::Pad<>{pause_plane, configuration()("button", "pause translation"), configuration()("button", "pause scale"), 1.0f};
|
|
button.at("pause").on_state_change([&](bool state){
|
|
unpaused_timer.off();
|
|
run_timer.off();
|
|
|
|
/* Transition between main theme and menu theme */
|
|
if (audio.at("main").playing())
|
|
{
|
|
audio.at("main").pause();
|
|
}
|
|
if (audio.at("menu").paused())
|
|
{
|
|
audio.at("menu").resume();
|
|
}
|
|
else if (audio.at("menu").fading() || !audio.at("menu").playing())
|
|
{
|
|
audio.at("menu").play();
|
|
}
|
|
});
|
|
|
|
/* Set up volume button */
|
|
bool original_state = button.at("volume").pressed();
|
|
sb::Texture volume_off_texture {configuration()("button", "volume off texture").get<std::string>()};
|
|
volume_off_texture.load();
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
sb::Texture volume_on_texture {configuration()("button", "volume on texture").get<std::string>()};
|
|
volume_on_texture.load();
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
sb::Plane volume_plane;
|
|
volume_plane.texture(volume_off_texture);
|
|
volume_plane.texture(volume_on_texture);
|
|
button.at("volume") = sb::Pad<>{volume_plane, configuration()("button", "volume translation"), configuration()("button", "volume scale"), 1.0f};
|
|
button.at("volume").state(original_state);
|
|
button.at("volume").on_state_change([&](bool state){
|
|
/* Mute or unmute (to full volume) depending on the state of the button */
|
|
if (Mix_QuerySpec(nullptr, nullptr, nullptr) != 0)
|
|
{
|
|
if (state)
|
|
{
|
|
Mix_Volume(-1, 128);
|
|
}
|
|
else
|
|
{
|
|
Mix_Volume(-1, 0);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sb::Log::log("Cannot mute or unmute. Audio device is not open.", sb::Log::WARN);
|
|
}
|
|
});
|
|
|
|
/* Set up spinners */
|
|
for (const std::string& name : {"profile", "level select", "challenge", "view"})
|
|
{
|
|
glm::vec2 dimensions {configuration()("button", name + " dimensions")};
|
|
label.at(name).foreground(configuration()("button", "text foreground").get<glm::vec4>());
|
|
label.at(name).background(configuration()("button", "text background").get<glm::vec4>());
|
|
label.at(name).untransform();
|
|
std::ostringstream message;
|
|
message << configuration()("button", name + " text").get<std::string>();
|
|
if (name == "profile")
|
|
{
|
|
message << configuration()("character", "profile", profile_index, "name").get<std::string>();
|
|
}
|
|
else if (name == "level select")
|
|
{
|
|
message << level_select_index;
|
|
}
|
|
else if (name == "challenge")
|
|
{
|
|
message << configuration()("challenge", challenge_index, "name").get<std::string>();
|
|
}
|
|
else if (name == "view")
|
|
{
|
|
message << configuration()("view", view_index, "name").get<std::string>();
|
|
}
|
|
label.at(name).content(message.str());
|
|
label.at(name).translate(configuration()("button", name + " translation"));
|
|
label.at(name).scale(configuration()("button", name + " scale"));
|
|
label.at(name).dimensions(dimensions);
|
|
}
|
|
button.at("profile decrement").on_state_change([&](bool state){
|
|
/* Disable in arcade-only mode and resume game modes */
|
|
if (!configuration()("display", "arcade only") && configuration()("challenge", challenge_index, "name") != "RESUME QUEST" &&
|
|
configuration()("challenge", challenge_index, "name") != "RESUME ARCADE")
|
|
{
|
|
if (--profile_index < 0)
|
|
{
|
|
profile_index = configuration()("progress", "max difficulty");
|
|
}
|
|
configuration()["progress"]["current difficulty"] = profile_index;
|
|
write_progress();
|
|
set_up_buttons();
|
|
character.profile(configuration()("character", "profile", profile_index, "name"));
|
|
}
|
|
});
|
|
button.at("profile increment").on_state_change([&](bool state){
|
|
/* Disable in arcade-only mode and resume game modes */
|
|
if (!configuration()("display", "arcade only") && configuration()("challenge", challenge_index, "name") != "RESUME QUEST" &&
|
|
configuration()("challenge", challenge_index, "name") != "RESUME ARCADE")
|
|
{
|
|
if (++profile_index > configuration()("progress", "max difficulty"))
|
|
{
|
|
profile_index = 0;
|
|
}
|
|
if (profile_index == configuration()("progress", "max difficulty"))
|
|
{
|
|
/* If the max difficulty was selected, clamp the level to the max level. */
|
|
if (level_select_index > configuration()("progress", "max level"))
|
|
{
|
|
level_select_index = configuration()("progress", "max level");
|
|
}
|
|
}
|
|
configuration()["progress"]["current difficulty"] = profile_index;
|
|
write_progress();
|
|
set_up_buttons();
|
|
character.profile(configuration()("character", "profile", profile_index, "name"));
|
|
}
|
|
});
|
|
button.at("level decrement").on_state_change([&](bool state){
|
|
|
|
/* Only allow level select in level select mode */
|
|
if (configuration()("challenge", challenge_index, "name") == "LEVEL SELECT")
|
|
{
|
|
/* If the level is decreased below 1, wrap to the last level if the current difficulty is complete, otherwise wrap to the max level
|
|
* unlocked. */
|
|
if (--level_select_index < 1)
|
|
{
|
|
if (profile_index < configuration()("progress", "max difficulty"))
|
|
{
|
|
level_select_index = configuration()("levels").size() - 2;
|
|
}
|
|
else
|
|
{
|
|
level_select_index = configuration()("progress", "max level");
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Save state and redraw button */
|
|
configuration()["progress"]["current level"] = level_select_index;
|
|
write_progress();
|
|
set_up_buttons();
|
|
});
|
|
button.at("level increment").on_state_change([&](bool state){
|
|
|
|
/* Only allow level select in level select mode */
|
|
if (configuration()("challenge", challenge_index, "name") == "LEVEL SELECT")
|
|
{
|
|
/* If the level is increased past the total number of levels or past the max level unlocked on the current difficulty,
|
|
* wrap the spinner back to 1. */
|
|
if (++level_select_index >= static_cast<int>(configuration()("levels").size() - 1) || (
|
|
profile_index == configuration()("progress", "max difficulty") && level_select_index > configuration()("progress", "max level")))
|
|
{
|
|
level_select_index = 1;
|
|
}
|
|
}
|
|
|
|
/* Save state and redraw button */
|
|
configuration()["progress"]["current level"] = level_select_index;
|
|
write_progress();
|
|
set_up_buttons();
|
|
});
|
|
button.at("challenge decrement").on_state_change([&](bool state){
|
|
/* Only allow change if arcade-only mode is not active */
|
|
if (!configuration()("display", "arcade only"))
|
|
{
|
|
if (--challenge_index < 0) challenge_index = configuration()("progress", "max challenge");
|
|
if (skip_resume_quest() || skip_resume_arcade() || skip_level_select())
|
|
{
|
|
button.at("challenge decrement").press();
|
|
}
|
|
else
|
|
{
|
|
toggle_challenge();
|
|
}
|
|
}
|
|
});
|
|
button.at("challenge increment").on_state_change([&](bool state){
|
|
/* Only allow change if arcade-only mode is not active */
|
|
if (!configuration()("display", "arcade only"))
|
|
{
|
|
if (++challenge_index > configuration()("progress", "max challenge")) challenge_index = 0;
|
|
if (skip_resume_quest() || skip_resume_arcade() || skip_level_select())
|
|
{
|
|
button.at("challenge increment").press();
|
|
}
|
|
else
|
|
{
|
|
toggle_challenge();
|
|
}
|
|
}
|
|
});
|
|
button.at("view decrement").on_state_change([&](bool state){
|
|
if (--view_index < 0)
|
|
{
|
|
view_index = configuration()("progress", "max view");
|
|
}
|
|
configuration()["progress"]["current view"] = view_index;
|
|
write_progress();
|
|
set_up_buttons();
|
|
});
|
|
button.at("view increment").on_state_change([&](bool state){
|
|
if (++view_index > configuration()("progress", "max view"))
|
|
{
|
|
view_index = 0;
|
|
}
|
|
configuration()["progress"]["current view"] = view_index;
|
|
write_progress();
|
|
set_up_buttons();
|
|
});
|
|
|
|
/* Set up play button */
|
|
original_state = button.at("play").pressed();
|
|
sb::Texture play_texture {configuration()("button", "play texture").get<std::string>()};
|
|
play_texture.load();
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
sb::Plane play_plane;
|
|
play_plane.texture(play_texture);
|
|
button.at("play") = sb::Pad<>{play_plane, configuration()("button", "play translation"), configuration()("button", "play scale"), 1.0f};
|
|
button.at("play").state(original_state);
|
|
button.at("play").on_state_change([&](bool state){
|
|
load_level(0);
|
|
});
|
|
|
|
/* Set up name entry buttons */
|
|
for (const std::string& character_index : {"1", "2", "3"})
|
|
{
|
|
glm::vec2 character_dimensions {configuration()("button", "name", "character dimensions")};
|
|
sb::Text character {large_font, "", configuration()("display", "clock hud foreground").get<glm::vec4>(),
|
|
configuration()("display", "clock hud background").get<glm::vec4>(), character_dimensions};
|
|
character.content(name_entry[std::stoi(character_index) - 1]);
|
|
button.at("name " + character_index) = sb::Pad<>{
|
|
character, {configuration()("button", "name", "character " + character_index + " x"), configuration()("button", "name", "character y")},
|
|
configuration()("button", "name", "character scale")[1], character_dimensions.y / character_dimensions.x};
|
|
sb::Texture increment_texture {configuration()("button", "name", "arrow increment texture").get<std::string>()};
|
|
increment_texture.load();
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
sb::Plane increment_plane;
|
|
increment_plane.texture(increment_texture);
|
|
glm::vec2 arrow_dimensions {configuration()("button", "name", "arrow dimensions")};
|
|
button.at("name " + character_index + " increment") = sb::Pad<>{
|
|
increment_plane, {configuration()("button", "name", "character " + character_index + " x"), configuration()("button", "name", "arrow increment y")},
|
|
configuration()("button", "name", "arrow scale")[1], arrow_dimensions.y / arrow_dimensions.x};
|
|
sb::Texture decrement_texture {configuration()("button", "name", "arrow decrement texture").get<std::string>()};
|
|
decrement_texture.load();
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
sb::Plane decrement_plane;
|
|
decrement_plane.texture(decrement_texture);
|
|
button.at("name " + character_index + " decrement") = sb::Pad<>{
|
|
decrement_plane, {configuration()("button", "name", "character " + character_index + " x"), configuration()("button", "name", "arrow decrement y")},
|
|
configuration()("button", "name", "arrow scale")[1], arrow_dimensions.y / arrow_dimensions.x};
|
|
button.at("name " + character_index).on_state_change([&, character_index](bool state){
|
|
name_entry_index = std::stoi(character_index) - 1;
|
|
});
|
|
button.at("name " + character_index + " increment").on_state_change([&, character_index, character_dimensions](bool state){
|
|
char current = name_entry[std::stoi(character_index) - 1];
|
|
if (++current > 'Z')
|
|
{
|
|
current = 'A';
|
|
}
|
|
name_entry[std::stoi(character_index) - 1] = current;
|
|
set_up_buttons();
|
|
});
|
|
button.at("name " + character_index + " decrement").on_state_change([&, character_index, character_dimensions](bool state){
|
|
char current = name_entry[std::stoi(character_index) - 1];
|
|
if (--current < 'A')
|
|
{
|
|
current = 'Z';
|
|
}
|
|
name_entry[std::stoi(character_index) - 1] = current;
|
|
set_up_buttons();
|
|
});
|
|
}
|
|
}
|
|
|
|
void Cakefoot::toggle_challenge()
|
|
{
|
|
/* In resume modes, set the level select and difficulty to the saved values. */
|
|
if (configuration()("challenge", challenge_index, "name") == "RESUME QUEST")
|
|
{
|
|
level_select_index = configuration()("progress", "quest level").get<int>();
|
|
profile_index = configuration()("progress", "quest difficulty").get<int>();
|
|
configuration()["progress"]["current difficulty"] = profile_index;
|
|
character.profile(configuration()("character", "profile", profile_index, "name"));
|
|
}
|
|
else if (configuration()("challenge", challenge_index, "name") == "RESUME ARCADE")
|
|
{
|
|
level_select_index = configuration()("progress", "arcade level").get<int>();
|
|
profile_index = configuration()("progress", "arcade difficulty").get<int>();
|
|
configuration()["progress"]["current difficulty"] = profile_index;
|
|
character.profile(configuration()("character", "profile", profile_index, "name"));
|
|
}
|
|
|
|
/* In new game modes, set the level select to 1 and leave the difficulty unchanged. */
|
|
else if (configuration()("challenge", challenge_index, "name") == "ARCADE" || configuration()("challenge", challenge_index, "name") == "NEW QUEST")
|
|
{
|
|
level_select_index = 1;
|
|
}
|
|
|
|
configuration()["progress"]["current challenge"] = challenge_index;
|
|
configuration()["progress"]["current level"] = level_select_index;
|
|
write_progress();
|
|
set_up_buttons();
|
|
}
|
|
|
|
void Cakefoot::set_up_hud()
|
|
{
|
|
/* Shrink the FPS indicator, and give it an opaque black background. */
|
|
float modifier = configuration()("display", "fps indicator scale");
|
|
glm::vec3 scale = {modifier, modifier * window_box().aspect(), 1.0f};
|
|
label.at("fps").foreground({255.0f, 255.0f, 255.0f, 255.0f});
|
|
label.at("fps").background({0.0f, 0.0f, 0.0f, 255.0f});
|
|
label.at("fps").untransform();
|
|
label.at("fps").translate({1.0f - scale.x, 1.0f - scale.y, 0.0f});
|
|
label.at("fps").scale(scale);
|
|
|
|
glm::vec3 clock_scale, clock_translation;
|
|
if (static_cast<std::size_t>(level_index) == _configuration("levels").size() - 1)
|
|
{
|
|
label.at("clock").font(large_font);
|
|
if (arcade())
|
|
{
|
|
/* Arcade results size */
|
|
clock_scale = configuration()("display", "arcade time remaining scale");
|
|
clock_translation = configuration()("display", "arcade time remaining translation");
|
|
}
|
|
else
|
|
{
|
|
/* Quest results size */
|
|
clock_scale = configuration()("display", "clock hud large scale");
|
|
clock_translation = configuration()("display", "clock hud large translation");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
label.at("clock").font(font());
|
|
/* Standard HUD size */
|
|
clock_scale = configuration()("display", "clock hud scale");
|
|
clock_translation = configuration()("display", "clock hud translation");
|
|
}
|
|
|
|
/* Style the clock */
|
|
label.at("clock").foreground(configuration()("display", "clock hud foreground").get<glm::vec4>());
|
|
label.at("clock").background(configuration()("display", "clock hud background").get<glm::vec4>());
|
|
label.at("clock").untransform();
|
|
label.at("clock").translate(clock_translation);
|
|
label.at("clock").scale(clock_scale);
|
|
|
|
/* Style the level indicator */
|
|
label.at("level").foreground(configuration()("display", "level hud foreground").get<glm::vec4>());
|
|
label.at("level").background(configuration()("display", "level hud background").get<glm::vec4>());
|
|
label.at("level").untransform();
|
|
label.at("level").translate(configuration()("display", "level hud translation"));
|
|
label.at("level").scale(configuration()("display", "level hud scale"));
|
|
|
|
/* Style the game over text */
|
|
label.at("game over").content(configuration()("display", "game over text"));
|
|
label.at("game over").foreground(configuration()("display", "game over foreground").get<glm::vec4>());
|
|
label.at("game over").background(configuration()("display", "game over background").get<glm::vec4>());
|
|
label.at("game over").untransform();
|
|
label.at("game over").translate(configuration()("display", "game over translation"));
|
|
label.at("game over").scale(configuration()("display", "game over scale"));
|
|
|
|
/* Style arcade results */
|
|
for (const std::string& name : {"arcade rank", "arcade distance"})
|
|
{
|
|
label.at(name).foreground(configuration()("display", "clock hud foreground").get<glm::vec4>());
|
|
label.at(name).background(configuration()("display", "clock hud background").get<glm::vec4>());
|
|
label.at(name).untransform();
|
|
label.at(name).translate(configuration()("display", name + " translation"));
|
|
label.at(name).scale(configuration()("display", name + " scale"));
|
|
label.at(name).dimensions(configuration()("display", name + " dimensions"));
|
|
}
|
|
|
|
/* Style the scoreboard */
|
|
scoreboard.foreground(configuration()("display", "scoreboard foreground").get<glm::vec4>());
|
|
scoreboard.background(configuration()("display", "scoreboard background").get<glm::vec4>());
|
|
scoreboard.untransform();
|
|
scoreboard.translate(configuration()("display", "scoreboard translation"));
|
|
scoreboard.scale(configuration()("display", "scoreboard scale"));
|
|
|
|
/* Style the QR code */
|
|
qr_code.texture(configuration()("display", "qr texture").get<std::string>());
|
|
qr_code.translate(configuration()("display", "qr translation"));
|
|
qr_code.scale(configuration()("display", "qr scale"));
|
|
|
|
/* Style the quest best time indicator */
|
|
label.at("quest best").foreground(configuration()("display", "quest best foreground").get<glm::vec4>());
|
|
label.at("quest best").background(configuration()("display", "quest best background").get<glm::vec4>());
|
|
label.at("quest best").untransform();
|
|
label.at("quest best").translate(configuration()("display", "quest best translation"));
|
|
label.at("quest best").scale(configuration()("display", "quest best scale"));
|
|
label.at("quest best").dimensions(configuration()("display", "quest best dimensions"));
|
|
if (configuration()("progress", "quest best") > 0.0f)
|
|
{
|
|
label.at("quest best").content(configuration()("display", "quest best text").get<std::string>() +
|
|
format_clock(configuration()("progress", "quest best")));
|
|
}
|
|
|
|
}
|
|
|
|
void Cakefoot::load_vbo()
|
|
{
|
|
/* Generate ID for the vertex buffer object that will hold all vertex data. Using one buffer for all attributes, data
|
|
* will be copied in one after the other. */
|
|
vbo.generate();
|
|
vbo.bind();
|
|
sb::Log::gl_errors("after generating and binding VBO");
|
|
|
|
/*!
|
|
* Fill VBO with attribute data:
|
|
*
|
|
* Postion, UV, and color vertices for a single sb::Plane, and curve.
|
|
*/
|
|
vbo.allocate(sb::Plane().size() + playing_field.attributes("color")->size() + curve_byte_count, GL_STATIC_DRAW);
|
|
vbo.add(*sb::Plane::position);
|
|
vbo.add(*sb::Plane::uv);
|
|
vbo.add(*sb::Plane::color);
|
|
for (Curve& curve : curves)
|
|
{
|
|
for (sb::Attributes& attr : curve.position)
|
|
{
|
|
vbo.add(attr);
|
|
}
|
|
vbo.add(curve.color);
|
|
}
|
|
sb::Log::gl_errors("after filling VBO");
|
|
|
|
/* Bind UV attributes once at load time because they will not be changing */
|
|
sb::Plane::uv->bind("vertex_uv", shader_program);
|
|
}
|
|
|
|
const Curve& Cakefoot::curve() const
|
|
{
|
|
return curves[curve_index % curves.size()];
|
|
}
|
|
|
|
Curve& Cakefoot::curve()
|
|
{
|
|
return curves[curve_index % curves.size()];
|
|
}
|
|
|
|
void Cakefoot::load_level(int index)
|
|
{
|
|
/* Wrap the index if it is out of range. */
|
|
index = glm::mod(index, static_cast<int>(configuration()("levels").size()));
|
|
|
|
/* Play menu theme on title screen and end screen. Play main theme on any other level. Cross fade between the two. */
|
|
if (index == 0 || static_cast<std::size_t>(index) == configuration()("levels").size() - 1)
|
|
{
|
|
/* If menu theme is already playing, let it continue to play. */
|
|
if (!audio.at("menu").playing() || audio.at("menu").paused() || audio.at("menu").fading())
|
|
{
|
|
audio.at("menu").play();
|
|
}
|
|
|
|
audio.at("main").stop();
|
|
}
|
|
else
|
|
{
|
|
if (audio.at("main").paused() || !audio.at("main").playing())
|
|
{
|
|
audio.at("main").play();
|
|
}
|
|
|
|
audio.at("menu").stop();
|
|
}
|
|
|
|
/* Update indices and reset character. */
|
|
level_index = index;
|
|
curve_index = index;
|
|
character.beginning(curve());
|
|
coin_collected = false;
|
|
|
|
/* The wrap space of the field is necessary for flame enemy objects */
|
|
sb::Box field {-curve().aspect, -1.0f, 2.0f * curve().aspect, 2.0f};
|
|
|
|
/* Query the end screen to time out */
|
|
if (static_cast<std::size_t>(index) == configuration()("levels").size() - 1)
|
|
{
|
|
submit_score_animation.play_once(configuration()("display", "end screen timeout"));
|
|
}
|
|
else
|
|
{
|
|
submit_score_animation.reset();
|
|
}
|
|
|
|
/* Reset enemies list to empty. Open configuration for the current level. Repopulate list of enemies one by one using the list of enemies in the
|
|
* configuration. For each enemy, add a challenge coin if the config specifies the coin parameters.
|
|
*
|
|
* Values read from the config are in some cases converted from old 25fps hard-coded per-frame values to per-second values, and hard-coded
|
|
* 864px by 486px pixel space to relative NDC space.
|
|
*/
|
|
this->enemies.clear();
|
|
if (configuration()("levels", index).contains("enemies"))
|
|
{
|
|
nlohmann::json enemies = configuration()("levels", index, "enemies");
|
|
for (std::size_t ii = 0; ii < enemies.size(); ii++)
|
|
{
|
|
nlohmann::json enemy = enemies[ii];
|
|
std::string type = enemy[0];
|
|
if (type == "slicer")
|
|
{
|
|
std::shared_ptr<Slicer> slicer = std::make_shared<Slicer>(
|
|
curve(), enemy[1].get<float>(), 2.0f * 25.0f * enemy[2].get<float>() / 486.0f, 2.0f * enemy[3].get<float>() / 486.0f);
|
|
|
|
/* Add coin to slicer */
|
|
if (enemy.size() > 4)
|
|
{
|
|
slicer->coin(coin, enemy[4].get<float>(), enemy[5].get<float>());
|
|
}
|
|
|
|
this->enemies.push_back(slicer);
|
|
}
|
|
else if (type == "fish")
|
|
{
|
|
std::shared_ptr<Fish> fish = std::make_shared<Fish>(
|
|
curve(), enemy[1].get<float>(), 25.0f * enemy[2].get<float>(), 2.0f * enemy[3].get<float>() / 486.0f,
|
|
enemy[4].get<float>());
|
|
|
|
/* Add coin to fish */
|
|
if (enemy.size() > 6)
|
|
{
|
|
fish->coin(coin, enemy[5].get<float>(), enemy[6].get<float>());
|
|
}
|
|
else if (enemy.size() > 5)
|
|
{
|
|
fish->coin(coin, enemy[5].get<float>());
|
|
}
|
|
|
|
this->enemies.push_back(fish);
|
|
}
|
|
else if (type == "projector")
|
|
{
|
|
std::shared_ptr<Projector> projector = std::make_shared<Projector>(
|
|
character,
|
|
(glm::vec3{2.0f * 1.7777f, 2.0f, 1.0f} * enemy[1].get<glm::fvec3>() / glm::vec3{864.0f, 486.0f, 1.0f} -
|
|
glm::vec3(1.77777f, 1.0f, 0.0f)) * glm::vec3(1.0f, -1.0f, 0.0f),
|
|
2.0f * 25.0f * enemy[2].get<float>() / 486.0, enemy[3].get<float>(), enemy[4].get<float>());
|
|
|
|
/* Add coin to projector */
|
|
if (enemy.size() > 5)
|
|
{
|
|
projector->coin(coin, enemy[5].get<float>());
|
|
}
|
|
|
|
this->enemies.push_back(projector);
|
|
}
|
|
else if (type == "flame")
|
|
{
|
|
std::shared_ptr<Flame> flame = std::make_shared<Flame>(
|
|
field, enemy[1].get<glm::fvec3>(), enemy[2].get<float>(), enemy[3].get<float>(), enemy[4].get<float>());
|
|
|
|
/* Add coin to flame */
|
|
if (enemy.size() > 5)
|
|
{
|
|
flame->coin(coin, enemy[5], enemy[6]);
|
|
}
|
|
|
|
this->enemies.push_back(flame);
|
|
}
|
|
else if (type == "grid")
|
|
{
|
|
/* Add a grid of flame objects */
|
|
float y = field.top();
|
|
glm::vec2 margin {0.59259f, 0.5f};
|
|
bool shift = false;
|
|
int count = 0;
|
|
while (y > field.bottom())
|
|
{
|
|
float x = field.left() + shift * margin.x / 2.0f;
|
|
while (x < field.right())
|
|
{
|
|
std::shared_ptr<Flame> flame = std::make_shared<Flame>(field, glm::vec3{x, y, 0.0f}, 0.41152263f, glm::quarter_pi<float>());
|
|
|
|
/* Add a challenge coin */
|
|
if (++count == 15)
|
|
{
|
|
flame->coin(coin, margin.x / 2.0f, 1.57f);
|
|
}
|
|
|
|
this->enemies.push_back(flame);
|
|
x += margin.x;
|
|
}
|
|
shift = !shift;
|
|
y -= margin.y;
|
|
}
|
|
}
|
|
else if (type == "wave")
|
|
{
|
|
/* Add a wave of flame objects */
|
|
float y = 0.0f;
|
|
float speed = enemy[4].get<float>();
|
|
float amplitude = enemy[1].get<float>();
|
|
float period = enemy[2].get<float>();
|
|
float step = enemy[3].get<float>();
|
|
float shift = enemy[5].get<float>();
|
|
float mirror = -1.0f;
|
|
if (enemy.size() > 7)
|
|
{
|
|
mirror = enemy[7].get<float>();
|
|
}
|
|
glm::vec2 range {field.left(), field.right()};
|
|
if (enemy.size() > 6)
|
|
{
|
|
range = enemy[6].get<glm::vec2>();
|
|
}
|
|
float x = range.x;
|
|
for (std::size_t count = 0; x < range.y; ++count)
|
|
{
|
|
y = amplitude * glm::sin(period * x) + shift;
|
|
std::shared_ptr<Flame> flame = std::make_shared<Flame>(field, glm::vec3{x, y, 0.0f}, speed, 3.0f * glm::half_pi<float>(), mirror);
|
|
if (enemy.size() > 8 && enemy[8].get<std::size_t>() == count)
|
|
{
|
|
flame->coin(coin, enemy[9].get<float>(), enemy[10].get<float>());
|
|
}
|
|
this->enemies.push_back(flame);
|
|
x += step;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* If the level is the end screen, reset the player's current level to the beginning and load ending screen coin list. Otherwise, if the level is not
|
|
* the title screen, save it as the current level in the player's progress. Also save the newly assigned current level in the level select index. */
|
|
if (end_screen())
|
|
{
|
|
/* Load ending coins */
|
|
std::string coin_texture;
|
|
ending_coins.clear();
|
|
glm::vec2 coin_range = configuration()("ending", "coin range").get<glm::vec2>();
|
|
float coin_step = (coin_range.y - coin_range.x) / (bank() - 1);
|
|
for (std::size_t ii = 0; ii < bank(); ii++)
|
|
{
|
|
Flame coin {
|
|
field, glm::vec3{coin_range.x + coin_step * ii, configuration()("ending", "coin y").get<float>(), 0.0f}, 0.0f, 0.0f, -1.0f, bank() < max_bank()
|
|
};
|
|
ending_coins.push_back(coin);
|
|
}
|
|
|
|
configuration()["progress"]["current level"] = 1;
|
|
|
|
/* Update save progress */
|
|
if (arcade())
|
|
{
|
|
configuration()["progress"]["arcade level"] = 1;
|
|
configuration()["progress"]["arcade checkpoint"] = 0.0f;
|
|
configuration()["progress"]["arcade max distance"] = 0;
|
|
configuration()["progress"]["arcade time"] = 0.0f;
|
|
configuration()["progress"]["arcade bank"] = 0;
|
|
challenge_index = 4;
|
|
configuration()["progress"]["current challenge"] = challenge_index;
|
|
}
|
|
else if (quest())
|
|
{
|
|
configuration()["progress"]["quest level"] = 1;
|
|
configuration()["progress"]["quest checkpoint"] = 0.0f;
|
|
configuration()["progress"]["quest time"] = 0.0f;
|
|
configuration()["progress"]["quest bank"] = 0;
|
|
challenge_index = 1;
|
|
configuration()["progress"]["current challenge"] = challenge_index;
|
|
}
|
|
|
|
/* In quest mode, unlock higher difficulty at the end of the game: increase difficulty if there is a higher difficulty than the current one.
|
|
* Then reset the max level because it will now refer to max level of the next difficulty. Save the time if it is better than the existing
|
|
* record. */
|
|
if (quest())
|
|
{
|
|
if (profile_index < static_cast<int>(configuration()("character", "profile").size()) - 1)
|
|
{
|
|
configuration()["progress"]["max difficulty"] = ++profile_index;
|
|
configuration()["progress"]["max level"] = 1;
|
|
character.profile(configuration()("character", "profile", profile_index, "name"));
|
|
}
|
|
|
|
float best = configuration()("progress", "quest best");
|
|
if (best <= 0.0f || run_timer.elapsed() < best)
|
|
{
|
|
configuration()["progress"]["quest best"] = run_timer.elapsed();
|
|
label.at("quest best").content(configuration()("display", "quest best text").get<std::string>() + " " + format_clock(run_timer.elapsed()));
|
|
}
|
|
}
|
|
|
|
write_progress();
|
|
level_select_index = 1;
|
|
}
|
|
else if (index > 0)
|
|
{
|
|
/* Unlock the level if it is a newly reached level */
|
|
if (configuration()("progress", "max difficulty") == profile_index && configuration()("progress", "max level").get<int>() < index)
|
|
{
|
|
configuration()["progress"]["max level"] = index;
|
|
}
|
|
|
|
/* Read and write save progress */
|
|
if (arcade())
|
|
{
|
|
if (configuration()("progress", "arcade level") == index)
|
|
{
|
|
/* If resuming, set the clock and checkpoint */
|
|
run_timer.elapsed(configuration()("progress", "arcade time").get<float>());
|
|
character.checkpoint(configuration()("progress", "arcade checkpoint").get<float>());
|
|
character.spawn(curve());
|
|
if (configuration()("progress", "arcade coin"))
|
|
{
|
|
for (auto& enemy : enemies) enemy->take_coin();
|
|
collect_coin(false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
configuration()["progress"]["arcade level"] = index;
|
|
configuration()["progress"]["arcade checkpoint"] = 0;
|
|
configuration()["progress"]["arcade difficulty"] = profile_index;
|
|
configuration()["progress"]["arcade max distance"] = distance();
|
|
configuration()["progress"]["arcade coin"] = false;
|
|
}
|
|
}
|
|
else if (quest())
|
|
{
|
|
if (configuration()("progress", "quest level") == index)
|
|
{
|
|
/* If resuming, set the checkpoint and clock */
|
|
run_timer.elapsed(configuration()("progress", "quest time").get<float>());
|
|
character.checkpoint(configuration()("progress", "quest checkpoint").get<float>());
|
|
character.spawn(curve());
|
|
if (configuration()("progress", "quest coin"))
|
|
{
|
|
for (auto& enemy : enemies) enemy->take_coin();
|
|
collect_coin(false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
configuration()["progress"]["quest level"] = index;
|
|
configuration()["progress"]["quest checkpoint"] = 0;
|
|
configuration()["progress"]["quest difficulty"] = profile_index;
|
|
configuration()["progress"]["quest coin"] = false;
|
|
}
|
|
}
|
|
|
|
configuration()["progress"]["current level"] = index;
|
|
write_progress();
|
|
level_select_index = index;
|
|
}
|
|
|
|
/* If it's the title or end level, stop the run timer. */
|
|
if (index == 0 || end_screen())
|
|
{
|
|
run_timer.off();
|
|
|
|
/* In arcade mode, reset the clock on the title screen */
|
|
if (arcade() && index == 0)
|
|
{
|
|
run_timer.reset();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* If it's the first level, start the run timer from the beginning. */
|
|
if (index == 1)
|
|
{
|
|
run_timer.reset();
|
|
}
|
|
run_timer.on();
|
|
}
|
|
|
|
/* Refresh HUD elements */
|
|
set_up_hud();
|
|
|
|
/* Refresh the buttons so the level select will reflect the change in level */
|
|
set_up_buttons();
|
|
|
|
/* Set the color background according to current world. Use world 0 color for the end screen. */
|
|
nlohmann::json world = configuration()("world");
|
|
if (static_cast<std::size_t>(index) == configuration()("levels").size() - 1)
|
|
{
|
|
world_color = world[0].at("color").get<glm::fvec4>();
|
|
}
|
|
else
|
|
{
|
|
for (std::size_t ii = 0; ii < world.size(); ii++)
|
|
{
|
|
if (ii == world.size() - 1 || world[ii + 1].at("start").get<int>() > index)
|
|
{
|
|
world_color = world[ii].at("color").get<glm::fvec4>();
|
|
break;
|
|
} } }
|
|
}
|
|
|
|
void Cakefoot::write_progress() const
|
|
{
|
|
/* Create directory for save files */
|
|
if (!fs::exists("storage"))
|
|
{
|
|
try
|
|
{
|
|
fs::create_directory("storage");
|
|
}
|
|
catch (fs::filesystem_error error)
|
|
{
|
|
std::ostringstream message;
|
|
message << "Could not create storage directory. Progress will not be saved. " << error.what();
|
|
sb::Log::log(message, sb::Log::ERR);
|
|
}
|
|
}
|
|
|
|
/* Save player's progress file */
|
|
std::ofstream progress_file {progress_file_path};
|
|
nlohmann::json progress {
|
|
{"progress", configuration()("progress")}
|
|
};
|
|
if (progress_file << std::setw(4) << progress << std::endl)
|
|
{
|
|
std::ostringstream message;
|
|
message << "Successfully saved progress to " << progress_file_path;
|
|
sb::Log::log(message);
|
|
}
|
|
else
|
|
{
|
|
std::ostringstream message;
|
|
message << "Could not save progress to " << progress_file_path;
|
|
sb::Log::log(message, sb::Log::ERR);
|
|
}
|
|
progress_file.close();
|
|
|
|
#if defined(EMSCRIPTEN)
|
|
EM_ASM(
|
|
FS.syncfs(false, function(error) {
|
|
if (error !== null)
|
|
{
|
|
console.log("Error syncing storage using Filesystem API", error);
|
|
}
|
|
});
|
|
collectData();
|
|
);
|
|
#endif
|
|
}
|
|
|
|
void Cakefoot::write_scores() const
|
|
{
|
|
/* Create directory for save files */
|
|
if (!fs::exists("storage"))
|
|
{
|
|
try
|
|
{
|
|
fs::create_directory("storage");
|
|
}
|
|
catch (fs::filesystem_error error)
|
|
{
|
|
std::ostringstream message;
|
|
message << "Could not create storage directory. Scores will not be saved. " << error.what();
|
|
sb::Log::log(message, sb::Log::ERR);
|
|
}
|
|
}
|
|
|
|
/* Save scores */
|
|
std::ofstream arcade_scores_file {arcade_scores_file_path};
|
|
if (arcade_scores_file << std::setw(4) << arcade_scores.json(date_format) << std::endl)
|
|
{
|
|
std::ostringstream message;
|
|
message << "Successfully saved arcade scores to " << arcade_scores_file_path;
|
|
sb::Log::log(message);
|
|
}
|
|
else
|
|
{
|
|
std::ostringstream message;
|
|
message << "Could not save arcade scores to " << arcade_scores_file_path;
|
|
sb::Log::log(message, sb::Log::ERR);
|
|
}
|
|
arcade_scores_file.close();
|
|
|
|
#if defined(EMSCRIPTEN)
|
|
EM_ASM(
|
|
FS.syncfs(false, function(error) {
|
|
if (error !== null)
|
|
{
|
|
console.log("Error syncing storage using Filesystem API", error);
|
|
}
|
|
});
|
|
);
|
|
#endif
|
|
}
|
|
|
|
int Cakefoot::length() const
|
|
{
|
|
int length = 0;
|
|
|
|
/* Ignore the title and end levels */
|
|
for (auto curve = curves.begin() + 1; curve != curves.end() - 1; curve++) length += curve->length();
|
|
|
|
return length;
|
|
}
|
|
|
|
int Cakefoot::distance() const
|
|
{
|
|
int distance = 0;
|
|
|
|
if (level_index > 0) {
|
|
|
|
distance += int(character.relative(curve()) * curve().length());
|
|
if (level_index > 1)
|
|
for (auto curve = curves.begin() + 1; curve != curves.begin() + level_index; curve++) {
|
|
distance += curve->length(); } }
|
|
|
|
return distance;
|
|
}
|
|
|
|
float Cakefoot::limit() const
|
|
{
|
|
if (arcade())
|
|
{
|
|
float limit = configuration()("challenge", challenge_index, "time limit");
|
|
|
|
if (level_index > 0)
|
|
{
|
|
const nlohmann::json& levels {configuration()("levels")};
|
|
for (auto level = levels.begin() + 1; level != levels.begin() + level_index + 1; level++) {
|
|
std::string level_addition = "level addition";
|
|
std::string checkpoint_addition = "checkpoint addition";
|
|
if (level >= levels.begin() + configuration()("challenge", challenge_index, "advanced").get<int>())
|
|
{
|
|
level_addition += " advanced";
|
|
checkpoint_addition += " advanced";
|
|
}
|
|
if (level < levels.begin() + level_index)
|
|
{
|
|
limit += configuration()("challenge", challenge_index, level_addition).get<float>();
|
|
if (level->contains("checkpoints"))
|
|
limit += configuration()("challenge", challenge_index, checkpoint_addition).get<float>() * level->at("checkpoints").size();
|
|
}
|
|
else if (level->contains("checkpoints"))
|
|
{
|
|
for (const nlohmann::json& checkpoint : level->at("checkpoints"))
|
|
{
|
|
if (checkpoint.at("position").get<float>() <= character.checkpoint())
|
|
limit += configuration()("challenge", challenge_index, checkpoint_addition).get<float>();
|
|
} } }
|
|
|
|
/* Add bank bonus */
|
|
limit += configuration()("progress", "arcade bank").get<int>() * configuration()("challenge", challenge_index, "bank bonus").get<float>();
|
|
}
|
|
|
|
return limit;
|
|
}
|
|
else return 0.0f;
|
|
}
|
|
|
|
bool Cakefoot::arcade() const
|
|
{
|
|
return configuration()("challenge", challenge_index).contains("time limit");
|
|
}
|
|
|
|
bool Cakefoot::quest() const
|
|
{
|
|
return !arcade() && !level_select();
|
|
}
|
|
|
|
bool Cakefoot::level_select() const
|
|
{
|
|
return !arcade() && configuration()("challenge", challenge_index, "name") == "LEVEL SELECT";
|
|
}
|
|
|
|
bool Cakefoot::end_screen(std::optional<std::size_t> index) const
|
|
{
|
|
if (!index.has_value())
|
|
{
|
|
index = level_index;
|
|
}
|
|
return static_cast<std::size_t>(index.value()) == _configuration("levels").size() - 1;
|
|
}
|
|
|
|
bool Cakefoot::resuming() const
|
|
{
|
|
return configuration()("challenge", challenge_index, "name") == "RESUME QUEST" || configuration()("challenge", challenge_index, "name") == "RESUME ARCADE";
|
|
}
|
|
|
|
std::size_t Cakefoot::bank() const
|
|
{
|
|
std::string mode = quest() ? "quest bank" : "arcade bank";
|
|
return configuration()("progress", mode).get<std::size_t>();
|
|
}
|
|
|
|
std::size_t Cakefoot::max_bank() const
|
|
{
|
|
return configuration()("levels").size() - 2;
|
|
}
|
|
|
|
void Cakefoot::collect_coin(bool add_to_bank)
|
|
{
|
|
if (!coin_collected)
|
|
{
|
|
for (auto& enemy : enemies)
|
|
{
|
|
if (enemy->coin_taken())
|
|
{
|
|
enemy->collect_coin();
|
|
coin_collected = true;
|
|
if (add_to_bank)
|
|
{
|
|
if (arcade())
|
|
{
|
|
configuration()["progress"]["arcade bank"].get_ref<nlohmann::json::number_integer_t&>()++;
|
|
configuration()["progress"]["arcade coin"] = true;
|
|
write_progress();
|
|
std::cout << "Bank increase! " << configuration()("progress", "arcade bank") << std::endl;
|
|
}
|
|
else if (quest())
|
|
{
|
|
configuration()["progress"]["quest bank"].get_ref<nlohmann::json::number_integer_t&>()++;
|
|
configuration()["progress"]["quest coin"] = true;
|
|
write_progress();
|
|
std::cout << "Bank increase! " << configuration()("progress", "quest bank") << std::endl;
|
|
} } } } }
|
|
}
|
|
|
|
void Cakefoot::end_game_over_display()
|
|
{
|
|
load_level(configuration()("levels").size() - 1);
|
|
}
|
|
|
|
float Cakefoot::arcade_time_remaining(float limit) const
|
|
{
|
|
return std::max(0.0f, limit - run_timer.elapsed());
|
|
}
|
|
|
|
void Cakefoot::submit_score()
|
|
{
|
|
arcade_score.name = name_entry;
|
|
arcade_scores.add(arcade_score);
|
|
write_scores();
|
|
refresh_scoreboard();
|
|
load_level(0);
|
|
}
|
|
|
|
void Cakefoot::shift_hue()
|
|
{
|
|
rotating_hue.shift_hue(configuration()("display", "hue shift").get<float>());
|
|
}
|
|
|
|
std::string Cakefoot::format_clock(float amount)
|
|
{
|
|
int minutes = int(amount) / 60;
|
|
float seconds = amount - (minutes * 60);
|
|
std::stringstream clock;
|
|
clock << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(4) << std::setprecision(1) << std::fixed << seconds;
|
|
return clock.str();
|
|
}
|
|
|
|
void Cakefoot::refresh_scoreboard()
|
|
{
|
|
std::string text {arcade_scores.formatted(4, 4)};
|
|
scoreboard.content(text);
|
|
}
|
|
|
|
void Cakefoot::respond(SDL_Event& event)
|
|
{
|
|
/* Get mouse button states */
|
|
bool left_mouse_pressed = SDL_GetMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK;
|
|
bool shift_pressed = SDL_GetModState() & KMOD_SHIFT;
|
|
|
|
/* Get mouse coordinates in pixel resolution and NDC. These values are invalid if the event isn't a mouse event. */
|
|
glm::vec2 mouse_pixel = event.type == SDL_MOUSEBUTTONDOWN ? glm::vec2{event.button.x, event.button.y} :
|
|
glm::vec2{event.motion.x, event.motion.y};
|
|
glm::vec2 mouse_ndc {
|
|
float(mouse_pixel.x) / window_box().width() * 2.0f - 1.0f, (1.0f - float(mouse_pixel.y) / window_box().height()) * 2.0f - 1.0f
|
|
};
|
|
|
|
/* Track whether cursor should display */
|
|
bool hovering = false;
|
|
|
|
/* Ignore most events when play button is active */
|
|
if (!use_play_button || button.at("play").pressed())
|
|
{
|
|
/* Custom keys for the title screen */
|
|
if (level_index == 0)
|
|
{
|
|
if (!shift_pressed && sb::Delegate::compare(event, "left"))
|
|
{
|
|
button.at("level decrement").press();
|
|
}
|
|
else if (!shift_pressed && sb::Delegate::compare(event, "right"))
|
|
{
|
|
button.at("level increment").press();
|
|
}
|
|
else if (sb::Delegate::compare(event, "any"))
|
|
{
|
|
button.at("start").press();
|
|
}
|
|
}
|
|
|
|
/* Custom keys for name entry */
|
|
else if (static_cast<std::size_t>(level_index) == configuration()("levels").size() - 1)
|
|
{
|
|
if (sb::Delegate::compare(event, "up"))
|
|
{
|
|
button.at("name " + std::to_string(name_entry_index + 1) + " increment").press();
|
|
}
|
|
else if (sb::Delegate::compare(event, "right"))
|
|
{
|
|
if (++name_entry_index > 2) name_entry_index = 0;
|
|
}
|
|
else if (sb::Delegate::compare(event, "down"))
|
|
{
|
|
button.at("name " + std::to_string(name_entry_index + 1) + " decrement").press();
|
|
}
|
|
else if (sb::Delegate::compare(event, "left"))
|
|
{
|
|
if (--name_entry_index < 0) name_entry_index = 2;
|
|
}
|
|
}
|
|
|
|
/* Perspective and view modifications */
|
|
if (event.type == SDL_MOUSEWHEEL && shift_pressed)
|
|
{
|
|
/* Edit zoom level with mouse wheel, which will modify the FOV */
|
|
zoom -= event.wheel.preciseY * glm::radians(2.0f);
|
|
if (zoom < glm::radians(-30.0f))
|
|
{
|
|
zoom = glm::radians(-30.0f);
|
|
}
|
|
else if (zoom > glm::radians(30.0f))
|
|
{
|
|
zoom = glm::radians(30.0f);
|
|
}
|
|
}
|
|
|
|
else if (event.type == SDL_MOUSEBUTTONUP || sb::Delegate::compare_cancel(event, "any"))
|
|
{
|
|
/* End character acceleration */
|
|
if (sb::Delegate::compare_cancel(event, "any") || (event.type == SDL_MOUSEBUTTONUP && event.button.button == SDL_BUTTON_LEFT))
|
|
{
|
|
character.accelerating = false;
|
|
}
|
|
}
|
|
|
|
else if (event.type == SDL_MOUSEMOTION || event.type == SDL_MOUSEBUTTONDOWN || sb::Delegate::compare(event, "any"))
|
|
{
|
|
/* Track whether a button has been pressed with this event */
|
|
bool button_pressed = false;
|
|
|
|
/* Collide with start button and spinners only on title screen */
|
|
if (level_index == 0)
|
|
{
|
|
for (const std::string& name : {
|
|
"start", "level increment", "level decrement", "profile increment", "profile decrement", "challenge increment",
|
|
"challenge decrement", "view increment", "view decrement"})
|
|
{
|
|
if (!configuration()("display", "arcade only") || name == "start")
|
|
{
|
|
if (button.at(name).enabled() && button.at(name).collide(mouse_ndc, view, projection))
|
|
{
|
|
hovering = true;
|
|
if (event.type == SDL_MOUSEBUTTONDOWN)
|
|
{
|
|
button.at(name).press();
|
|
|
|
/* Cancel hover on the start button because the button will be removed from the screen after the press. */
|
|
if (name == "start") hovering = false;
|
|
} } } } }
|
|
|
|
/* Collide with pause button only during levels */
|
|
if (level_index > 0 && unpaused_timer && button.at("pause").collide(mouse_ndc, view, projection))
|
|
{
|
|
if (event.type == SDL_MOUSEBUTTONDOWN)
|
|
{
|
|
button.at("pause").press();
|
|
button_pressed = true;
|
|
}
|
|
else hovering = true;
|
|
}
|
|
|
|
/* Check pause menu buttons */
|
|
else if (level_index > 0 && !unpaused_timer)
|
|
{
|
|
for (const std::string& button_name : {"resume", "reset"})
|
|
{
|
|
if (button.at(button_name).collide(mouse_ndc, view, projection))
|
|
{
|
|
if (event.type == SDL_MOUSEBUTTONDOWN)
|
|
{
|
|
button.at(button_name).press();
|
|
button_pressed = true;
|
|
}
|
|
else hovering = true;
|
|
} } }
|
|
|
|
/* Collide with name entry in arcade mode on end screen */
|
|
else if (static_cast<std::size_t>(level_index) == configuration()("levels").size() - 1 && arcade())
|
|
{
|
|
for (const std::string& button_name : {std::string("name 1"), std::string("name 2"), std::string("name 3"),
|
|
"name " + std::to_string(name_entry_index + 1) + " increment",
|
|
"name " + std::to_string(name_entry_index + 1) + " decrement"})
|
|
{
|
|
if (button.at(button_name).collide(mouse_ndc, view, projection))
|
|
{
|
|
if (event.type == SDL_MOUSEBUTTONDOWN)
|
|
{
|
|
button.at(button_name).press();
|
|
button_pressed = true;
|
|
}
|
|
else hovering = true;
|
|
} } }
|
|
|
|
/* Rotate scene */
|
|
if (event.type == SDL_MOUSEMOTION && left_mouse_pressed && shift_pressed)
|
|
{
|
|
rotation += glm::vec2{event.motion.xrel, event.motion.yrel} * glm::half_pi<float>() * 0.005f;
|
|
}
|
|
|
|
/* Start character acceleration */
|
|
bool acceleration_pressed = ((event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) || sb::Delegate::compare(event, "any"));
|
|
if (!shift_pressed && !button_pressed && acceleration_pressed)
|
|
{
|
|
character.accelerating = true;
|
|
}
|
|
}
|
|
|
|
else if (sb::Delegate::compare(event, "fps"))
|
|
{
|
|
configuration()["display"]["fps"] = !configuration()["display"]["fps"];
|
|
}
|
|
|
|
else if (sb::Delegate::compare(event, "skip forward"))
|
|
{
|
|
load_level(level_index + 1);
|
|
}
|
|
|
|
else if (sb::Delegate::compare(event, "skip backward"))
|
|
{
|
|
load_level(level_index - 1);
|
|
}
|
|
|
|
else if (sb::Delegate::compare(event, "reset"))
|
|
{
|
|
zoom = 0.0f;
|
|
rotation = {0.0f, 0.0f};
|
|
load_level(0);
|
|
unpaused_timer.on();
|
|
run_timer.reset();
|
|
|
|
/* In arcade-only mode, reset the level select to the first level */
|
|
if (configuration()("display", "arcade only"))
|
|
{
|
|
level_select_index = 1;
|
|
set_up_buttons();
|
|
}
|
|
}
|
|
|
|
else if (sb::Delegate::compare(event, "reconfig"))
|
|
{
|
|
load_curves();
|
|
load_vbo();
|
|
load_level(level_index);
|
|
character.box_size(configuration()("character", "hitbox").get<float>());
|
|
set_up_buttons();
|
|
set_up_hud();
|
|
load_audio();
|
|
}
|
|
|
|
else if (sb::Delegate::compare(event, "window resize"))
|
|
{
|
|
set_up_buttons();
|
|
set_up_hud();
|
|
}
|
|
|
|
#if !defined(__MINGW32__)
|
|
/* Taken from mallinfo man page, log a profile of the memory when the command is sent. */
|
|
else if (sb::Delegate::compare(event, "memory"))
|
|
{
|
|
/* Struct with quantified memory allocation information. */
|
|
#if !defined(EMSCRIPTEN)
|
|
struct mallinfo2 malloc_info = mallinfo2();
|
|
#else
|
|
struct mallinfo malloc_info = mallinfo();
|
|
#endif
|
|
|
|
/* Create a map from the struct's member variables. */
|
|
std::map<std::string, int> malloc_map = {
|
|
{"Total non-mmapped bytes (arena):", malloc_info.arena},
|
|
{"# of free chunks (ordblks):", malloc_info.ordblks},
|
|
{"# of free fastbin blocks (smblks):", malloc_info.smblks},
|
|
{"# of mapped regions (hblks):", malloc_info.hblks},
|
|
{"Bytes in mapped regions (hblkhd):", malloc_info.hblkhd},
|
|
{"Max. total allocated space (usmblks):", malloc_info.usmblks},
|
|
{"Free bytes held in fastbins (fsmblks):", malloc_info.fsmblks},
|
|
{"Total allocated space (uordblks):", malloc_info.uordblks},
|
|
{"Total free space (fordblks):", malloc_info.fordblks},
|
|
{"Topmost releasable block (keepcost):", malloc_info.keepcost},
|
|
};
|
|
|
|
/* Loop through the map, and print each value. */
|
|
std::ostringstream message;
|
|
int first_column = 40, second_column = 12, count = 0;
|
|
message << std::endl;
|
|
for (std::pair<std::string, int> malloc_info_entry : malloc_map)
|
|
{
|
|
message << std::setw(first_column) << malloc_info_entry.first << std::setw(second_column) << std::setprecision(2)
|
|
<< std::fixed << malloc_info_entry.second / 1000.0 << " KB";
|
|
if ((++count % 3) == 0)
|
|
{
|
|
message << std::endl;
|
|
}
|
|
}
|
|
sb::Log::log(message);
|
|
}
|
|
#endif
|
|
|
|
/* Print the coordinates of the cake sprite in all coordinate spaces */
|
|
else if (sb::Delegate::compare(event, "coords"))
|
|
{
|
|
std::ostringstream message;
|
|
glm::vec2 translation = sb::wrap_point(character.position, {-curve().aspect, -1.0f, 0.0f}, {curve().aspect, 1.0f, 1.0f});
|
|
message << std::fixed << std::setprecision(2) << "Character coords: unwrapped " << character.position << ", wrapped " << translation
|
|
<< ", clip " << sb::world_to_clip(translation, projection * view) << ", ndc " << sb::world_to_ndc(translation, projection * view)
|
|
<< ", window " << sb::world_to_viewport(translation, window_box().size(), projection * view);
|
|
sb::Log::log(message);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* Collide with play button */
|
|
if ((event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEMOTION) && button.at("play").collide(mouse_ndc, view, projection))
|
|
{
|
|
if (event.type == SDL_MOUSEBUTTONDOWN)
|
|
{
|
|
button.at("play").press();
|
|
}
|
|
else
|
|
{
|
|
hovering = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Always collide with volume button */
|
|
if ((event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEMOTION) && button.at("volume").collide(mouse_ndc, view, projection))
|
|
{
|
|
if (event.type == SDL_MOUSEBUTTONDOWN)
|
|
{
|
|
button.at("volume").press();
|
|
}
|
|
else
|
|
{
|
|
hovering = true;
|
|
}
|
|
}
|
|
|
|
/* Set the cursor image appropriately */
|
|
if (hovering && SDL_GetCursor() == SDL_GetDefaultCursor())
|
|
{
|
|
SDL_SetCursor(poke.get());
|
|
}
|
|
else if (!hovering && SDL_GetCursor() == poke.get())
|
|
{
|
|
SDL_SetCursor(SDL_GetDefaultCursor());
|
|
}
|
|
}
|
|
|
|
void Cakefoot::run()
|
|
{
|
|
/* Start timers precisely when the game update loop starts */
|
|
on_timer.on();
|
|
unpaused_timer.on();
|
|
|
|
/* Enable auto refresh */
|
|
#if defined(__LINUX__)
|
|
configuration().enable_auto_refresh(levels_file_path);
|
|
#endif
|
|
|
|
/* Start the update loop */
|
|
Game::run();
|
|
}
|
|
|
|
void Cakefoot::update(float timestamp)
|
|
{
|
|
sb::Log::gl_errors("at beginning of update");
|
|
|
|
/* Update time in seconds the game has been running for, pass to the shader. */
|
|
on_timer.update(timestamp);
|
|
glUniform1f(uniform["time"], on_timer.elapsed());
|
|
|
|
/* Keep animation time updated */
|
|
game_over_animation.update(timestamp);
|
|
submit_score_animation.update(timestamp);
|
|
shift_hue_animation.update(timestamp);
|
|
|
|
/* Transformation for looking at the center of the field of play from the camera position. */
|
|
view = glm::lookAt(camera_position, {0.0f, 0.0f, 0.0f}, glm::vec3{0.0f, 1.0f, 0.0f});
|
|
|
|
/* Transformation from camera space to clip space. */
|
|
float fov;
|
|
if (window_box().aspect() >= curve().aspect)
|
|
{
|
|
fov = 2.0f * glm::atan(1.0f / camera_position.z) + zoom;
|
|
}
|
|
else
|
|
{
|
|
fov = 2.0f * glm::atan(((1.0f / (window_box().width() * (9.0f / 16.0f))) * window_box().height()) / camera_position.z) + zoom;
|
|
}
|
|
projection = glm::perspective(fov, window_box().aspect(), 0.1f, 100.0f);
|
|
|
|
/* Transformation that applies the rotation state of the entire scene */
|
|
glm::mat4 rotation_matrix = glm::rotate(glm::mat4(1), rotation.x, {0.0f, 1.0f, 0.0f}) * glm::rotate(glm::mat4(1), rotation.y, {1.0f, 0.0f, 0.0f});
|
|
|
|
/* Clear screen to configured color */
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
/* Ignore most of the update and draw loop if the play button is enabled and hasn't been pressed */
|
|
if (!use_play_button || button.at("play").pressed())
|
|
{
|
|
/* Update other timers */
|
|
run_timer.update(timestamp);
|
|
configuration()["progress"]["total time"].get_ref<nlohmann::json::number_float_t&>() += run_timer.frame();
|
|
if (arcade())
|
|
{
|
|
configuration()["progress"]["arcade time"].get_ref<nlohmann::json::number_float_t&>() += run_timer.frame();
|
|
}
|
|
else if (quest())
|
|
{
|
|
configuration()["progress"]["quest time"].get_ref<nlohmann::json::number_float_t&>() += run_timer.frame();
|
|
}
|
|
unpaused_timer.update(timestamp);
|
|
|
|
/* Arcade scoring */
|
|
auto& maximum_distance = configuration()["progress"]["arcade max distance"].get_ref<nlohmann::json::number_integer_t&>();
|
|
float extended_limit = limit();
|
|
if (arcade())
|
|
{
|
|
/* Check if maximum distance increased. Using auto as the type handles differences between integer types in different compilers. */
|
|
if (distance() > maximum_distance)
|
|
{
|
|
maximum_distance = distance();
|
|
}
|
|
|
|
/* End run if there is a time limit and the time limit is passed. Queue end level to load after a delay. */
|
|
bool game_over_active = arcade() && level_index > 0 &&
|
|
run_timer.elapsed() > extended_limit && static_cast<std::size_t>(level_index) < configuration()("levels").size() - 1;
|
|
if (game_over_active && !game_over_animation.playing())
|
|
{
|
|
/* Play once with a delay to let the game over screen display temporarily before the end level is loaded. */
|
|
game_over_animation.play_once(configuration()("display", "game over display time"));
|
|
|
|
/* Create arcade score */
|
|
arcade_score = ArcadeScores::Score(arcade_time_remaining(extended_limit), maximum_distance);
|
|
int rank = std::min(9999, arcade_scores.rank(arcade_score));
|
|
std::ostringstream rank_str, distance_str;
|
|
rank_str << rank;
|
|
if (rank == 1)
|
|
{
|
|
rank_str << "st";
|
|
}
|
|
else if (rank == 2)
|
|
{
|
|
rank_str << "nd";
|
|
}
|
|
else if (rank == 3)
|
|
{
|
|
rank_str << "rd";
|
|
}
|
|
else
|
|
{
|
|
rank_str << "th";
|
|
}
|
|
label.at("arcade rank").content(rank_str.str());
|
|
distance_str << arcade_score.distance << "m";
|
|
label.at("arcade distance").content(distance_str.str());
|
|
}
|
|
}
|
|
|
|
/* Freeze screen while game over display is active. */
|
|
if (!game_over_animation.playing())
|
|
{
|
|
/* Update character, along the curve, using the timer to determine movement since last frame, and update enemies. Check for collison
|
|
* as enemies are updated. */
|
|
character.update(curve(), unpaused_timer, !button.at("volume").pressed());
|
|
if (character.at_end(curve()))
|
|
{
|
|
/* On the end level, save the score and name entry. */
|
|
if (arcade() && static_cast<std::size_t>(level_index) == configuration()("levels").size() - 1)
|
|
{
|
|
submit_score();
|
|
}
|
|
else
|
|
{
|
|
/* Collect any previously taken coins */
|
|
collect_coin();
|
|
|
|
/* Load next level, or reload current level if in level select mode */
|
|
audio.at("teleport").play();
|
|
load_level(level_select() ? level_index : level_index + 1);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* Update checkpoint */
|
|
if (profile_index == 0 && configuration()("levels", level_index).contains("checkpoints"))
|
|
{
|
|
for (nlohmann::json checkpoint : configuration()("levels", level_index, "checkpoints"))
|
|
{
|
|
if (character.relative(curve()) >= checkpoint["position"].get<float>() && character.checkpoint() < checkpoint["position"].get<float>())
|
|
{
|
|
audio.at("checkpoint").play();
|
|
character.checkpoint(checkpoint["position"].get<float>());
|
|
|
|
/* Collect any previously taken coins */
|
|
collect_coin();
|
|
|
|
/* Save progress */
|
|
if (arcade())
|
|
{
|
|
configuration()["progress"]["arcade checkpoint"] = character.checkpoint();
|
|
}
|
|
else if (quest())
|
|
{
|
|
configuration()["progress"]["quest checkpoint"] = character.checkpoint();
|
|
}
|
|
write_progress();
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Collide with enemies and challenge coins */
|
|
bool enemy_collision = false;
|
|
glm::vec3 clip_upper {-curve().aspect, -1.0f, -1.0f}, clip_lower {curve().aspect, 1.0f, 1.0f};
|
|
for (auto& enemy : enemies)
|
|
{
|
|
enemy->update(unpaused_timer);
|
|
if (enemy->collide(character.box(), character.sprite(), clip_upper, clip_lower))
|
|
{
|
|
enemy_collision = true;
|
|
}
|
|
else if (enemy->collide_coin(character.box(), clip_upper, clip_lower))
|
|
{
|
|
audio.at("take").play();
|
|
enemy->take_coin();
|
|
}
|
|
}
|
|
|
|
/* Collide with ending screen coins */
|
|
if (end_screen())
|
|
{
|
|
ending_coins.erase(
|
|
std::remove_if(
|
|
ending_coins.begin(),
|
|
ending_coins.end(),
|
|
[&](Flame& coin)
|
|
{
|
|
if (coin.collide(character.box(), character.sprite(), {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}))
|
|
{
|
|
coin.camo() ? audio.at("take").play() : audio.at("bong").play();
|
|
return true;
|
|
}
|
|
return false;
|
|
}),
|
|
ending_coins.end());
|
|
}
|
|
|
|
/* Respawn */
|
|
if (!character.resting() && enemy_collision)
|
|
{
|
|
audio.at("restart").play();
|
|
character.spawn(curve());
|
|
for (auto& enemy : enemies)
|
|
{
|
|
enemy->reset();
|
|
}
|
|
|
|
/* Record a death */
|
|
configuration()["progress"]["deaths"].get_ref<nlohmann::json::number_integer_t&>()++;
|
|
write_progress();
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Plane position vertices will be used for everything before the curve */
|
|
sb::Plane::position->bind("vertex_position", shader_program);
|
|
|
|
/* Disable texture, set background color for the current world using the color addition uniform, and draw playing field (background) */
|
|
glUniform1i(uniform["texture enabled"], false);
|
|
glUniform4fv(uniform["color addition"], 1, &world_color[0]);
|
|
playing_field.attributes("color")->bind("vertex_color", shader_program);
|
|
playing_field.attributes("color")->enable();
|
|
playing_field.draw(uniform["mvp"], view * rotation_matrix, projection, uniform["texture enabled"]);
|
|
playing_field.attributes("color")->disable();
|
|
|
|
/* Reset color addition, and draw curve. */
|
|
glUniform4fv(uniform["color addition"], 1, &glm::vec4(0)[0]);
|
|
glm::mat4 vp = projection * view * rotation_matrix;
|
|
glUniformMatrix4fv(uniform["mvp"], 1, GL_FALSE, &vp[0][0]);
|
|
curve().color.bind("vertex_color", shader_program);
|
|
curve().color.enable();
|
|
for (sb::Attributes& position : curve().position)
|
|
{
|
|
position.bind("vertex_position", shader_program);
|
|
position.enable();
|
|
glDrawArrays(GL_LINE_STRIP, 0, position.count());
|
|
position.disable();
|
|
}
|
|
curve().color.disable();
|
|
|
|
/* Draw checkpoints */
|
|
sb::Plane::position->bind("vertex_position", shader_program);
|
|
sb::Plane::color->bind("vertex_color", shader_program);
|
|
if (profile_index == 0 && configuration()("levels", level_index).contains("checkpoints"))
|
|
{
|
|
for (nlohmann::json checkpoint : configuration()("levels", level_index, "checkpoints"))
|
|
{
|
|
sb::Sprite* sprite;
|
|
if (checkpoint["position"].get<float>() > character.checkpoint())
|
|
{
|
|
sprite = &checkpoint_off;
|
|
}
|
|
else
|
|
{
|
|
sprite = &checkpoint_on;
|
|
}
|
|
glm::vec3 position = curve().relative(checkpoint["position"].get<float>());
|
|
glm::vec2 delta = sb::angle_to_vector(checkpoint["angle"].get<float>(), configuration()("display", "checkpoint distance"));
|
|
position += glm::vec3{delta.x, delta.y, 0.0f};
|
|
sprite->translate(curve().wrap(position));
|
|
sprite->draw(uniform.at("mvp"), view * rotation_matrix, projection, uniform.at("texture enabled"));
|
|
}
|
|
}
|
|
|
|
/* Draw enemies */
|
|
for (auto& enemy : enemies)
|
|
{
|
|
enemy->draw(uniform.at("mvp"), view * rotation_matrix, projection, uniform.at("texture enabled"), rotating_hue, uniform.at("color addition"));
|
|
}
|
|
|
|
/* Draw cake */
|
|
character.draw(curve(), uniform.at("mvp"), view * rotation_matrix, projection, uniform.at("texture enabled"));
|
|
|
|
/* Draw end screen coins */
|
|
if (end_screen())
|
|
{
|
|
for (Flame& coin : ending_coins)
|
|
{
|
|
coin.draw(uniform.at("mvp"), view * rotation_matrix, projection, uniform.at("texture enabled"), rotating_hue, uniform.at("color addition"));
|
|
}
|
|
}
|
|
|
|
/* Check if any buttons should be disabled */
|
|
bool profile_spinner_enabled = configuration()("progress", "max difficulty") > 0 && !resuming();
|
|
bool profile_spinner_visible = configuration()("progress", "max difficulty") > 0;
|
|
bool view_spinner_enabled = configuration()("progress", "max view") > 0;
|
|
button.at("level decrement").visible(level_select());
|
|
button.at("level decrement").enabled(level_select());
|
|
button.at("level increment").visible(level_select());
|
|
button.at("level increment").enabled(level_select());
|
|
button.at("profile decrement").visible(profile_spinner_enabled);
|
|
button.at("profile decrement").enabled(profile_spinner_enabled);
|
|
button.at("profile increment").visible(profile_spinner_enabled);
|
|
button.at("profile increment").enabled(profile_spinner_enabled);
|
|
button.at("view decrement").enabled(view_spinner_enabled);
|
|
button.at("view decrement").visible(view_spinner_enabled);
|
|
button.at("view increment").enabled(view_spinner_enabled);
|
|
button.at("view increment").visible(view_spinner_enabled);
|
|
|
|
/* Draw buttons. Don't include rotation matrix in view, so buttons will remain flat in the z-dimension. */
|
|
glm::mat4 label_transformation {0.0f};
|
|
if (level_index == 0)
|
|
{
|
|
button.at("start").draw(uniform["mvp"], view, projection, uniform["texture enabled"]);
|
|
|
|
/* Disable spinners in arcade-only mode */
|
|
if (!configuration()("display", "arcade only"))
|
|
{
|
|
/* Draw spinner buttons */
|
|
for (const std::string& name : {
|
|
"level decrement", "level increment", "profile decrement", "profile increment", "challenge decrement", "challenge increment",
|
|
"view decrement", "view increment"
|
|
})
|
|
{
|
|
button.at(name).draw(uniform.at("mvp"), view, projection, uniform.at("texture enabled"));
|
|
}
|
|
|
|
/* Draw spinner labels */
|
|
for (const std::string& name : {"level select", "profile", "challenge", "view"})
|
|
{
|
|
if ((name != "profile" || profile_spinner_visible) && (name != "view" || view_spinner_enabled))
|
|
{
|
|
label.at(name).texture(0).bind();
|
|
label_transformation = projection * view * label.at(name).transformation();
|
|
glUniformMatrix4fv(uniform["mvp"], 1, GL_FALSE, &label_transformation[0][0]);
|
|
label.at(name).enable();
|
|
glDrawArrays(GL_TRIANGLES, 0, label.at(name).attributes("position")->count());
|
|
} } } }
|
|
else
|
|
{
|
|
if (unpaused_timer)
|
|
{
|
|
button.at("pause").draw(uniform["mvp"], view, projection, uniform["texture enabled"]);
|
|
}
|
|
else
|
|
{
|
|
for (std::string name : {"resume", "reset"})
|
|
{
|
|
button.at(name).draw(uniform["mvp"], view, projection, uniform["texture enabled"]);
|
|
} }
|
|
|
|
/* Draw name entry */
|
|
if (static_cast<std::size_t>(level_index) == configuration()("levels").size() - 1 && arcade())
|
|
{
|
|
for (const std::string& button_name : {std::string("name 1"), std::string("name 2"), std::string("name 3"),
|
|
"name " + std::to_string(name_entry_index + 1) + " increment",
|
|
"name " + std::to_string(name_entry_index + 1) + " decrement"})
|
|
{
|
|
button.at(button_name).draw(uniform.at("mvp"), view, projection, uniform.at("texture enabled"));
|
|
} } }
|
|
|
|
/* Draw the clock */
|
|
float amount;
|
|
if (arcade())
|
|
{
|
|
if (static_cast<std::size_t>(level_index) == configuration()("levels").size() - 1)
|
|
{
|
|
amount = arcade_time_remaining(arcade_score.time);
|
|
}
|
|
else
|
|
{
|
|
amount = arcade_time_remaining(extended_limit);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
amount = run_timer.elapsed();
|
|
}
|
|
label.at("clock").content(format_clock(amount));
|
|
sb::Plane::position->bind("vertex_position", shader_program);
|
|
glUniform1i(uniform["texture enabled"], true);
|
|
label.at("clock").texture(0).bind();
|
|
label_transformation = projection * view * label.at("clock").transformation();
|
|
glUniformMatrix4fv(uniform["mvp"], 1, GL_FALSE, &label_transformation[0][0]);
|
|
label.at("clock").enable();
|
|
glDrawArrays(GL_TRIANGLES, 0, label.at("clock").attributes("position")->count());
|
|
|
|
/* Draw the level indicator */
|
|
if (level_index > 0 && static_cast<std::size_t>(level_index) < _configuration("levels").size() - 1)
|
|
{
|
|
std::stringstream level_indicator;
|
|
level_indicator << std::setw(2) << std::setfill('0') << level_index << "/" << std::setw(2) << _configuration("levels").size() - 2;
|
|
label.at("level").content(level_indicator.str());
|
|
label.at("level").texture(0).bind();
|
|
label_transformation = projection * view * label.at("level").transformation();
|
|
glUniformMatrix4fv(uniform["mvp"], 1, GL_FALSE, &label_transformation[0][0]);
|
|
label.at("level").enable();
|
|
glDrawArrays(GL_TRIANGLES, 0, label.at("level").attributes("position")->count());
|
|
}
|
|
|
|
/* Draw game over text */
|
|
if (game_over_animation.playing())
|
|
{
|
|
label.at("game over").texture(0).bind();
|
|
label_transformation = projection * view * label.at("game over").transformation();
|
|
glUniformMatrix4fv(uniform["mvp"], 1, GL_FALSE, &label_transformation[0][0]);
|
|
label.at("game over").enable();
|
|
glDrawArrays(GL_TRIANGLES, 0, label.at("game over").attributes("position")->count());
|
|
}
|
|
|
|
/* Draw arcade results */
|
|
if (static_cast<std::size_t>(level_index) == configuration()("levels").size() - 1 && arcade())
|
|
{
|
|
for (const std::string& name : {"arcade rank", "arcade distance"})
|
|
{
|
|
label.at(name).texture(0).bind();
|
|
label_transformation = projection * view * label.at(name).transformation();
|
|
glUniformMatrix4fv(uniform["mvp"], 1, GL_FALSE, &label_transformation[0][0]);
|
|
label.at(name).enable();
|
|
glDrawArrays(GL_TRIANGLES, 0, label.at(name).attributes("position")->count());
|
|
} }
|
|
|
|
/* Draw scoreboard, QR, and quest best on title screen */
|
|
if (level_index == 0)
|
|
{
|
|
/* Only draw scoreboard if arcade mode is selected */
|
|
if (arcade())
|
|
{
|
|
scoreboard.texture(0).bind();
|
|
label_transformation = projection * view * scoreboard.transformation();
|
|
glUniformMatrix4fv(uniform["mvp"], 1, GL_FALSE, &label_transformation[0][0]);
|
|
scoreboard.enable();
|
|
glDrawArrays(GL_TRIANGLES, 0, scoreboard.attributes("position")->count());
|
|
}
|
|
else if (quest() && configuration()("progress", "quest best") > 0.0f)
|
|
{
|
|
label.at("quest best").texture(0).bind();
|
|
label_transformation = projection * view * label.at("quest best").transformation();
|
|
glUniformMatrix4fv(uniform["mvp"], 1, GL_FALSE, &label_transformation[0][0]);
|
|
label.at("quest best").enable();
|
|
glDrawArrays(GL_TRIANGLES, 0, label.at("quest best").attributes("position")->count());
|
|
}
|
|
|
|
/* Draw QR */
|
|
qr_code.draw(uniform.at("mvp"), view, projection, uniform.at("texture enabled"));
|
|
}
|
|
|
|
/* Update FPS indicator display to the current FPS count and draw. */
|
|
if (configuration()["display"]["fps"])
|
|
{
|
|
if (current_frames_per_second != previous_frames_per_second)
|
|
{
|
|
std::string padded = sb::pad(current_frames_per_second, 2);
|
|
label.at("fps").content(padded);
|
|
previous_frames_per_second = current_frames_per_second;
|
|
}
|
|
if (label.at("fps").texture(0).generated())
|
|
{
|
|
/* Draw FPS indicator */
|
|
sb::Plane::position->bind("vertex_position", shader_program);
|
|
glUniform1i(uniform["texture enabled"], true);
|
|
label.at("fps").texture(0).bind();
|
|
glUniformMatrix4fv(uniform["mvp"], 1, GL_FALSE, &label.at("fps").transformation()[0][0]);
|
|
label.at("fps").enable();
|
|
glDrawArrays(GL_TRIANGLES, 0, label.at("fps").attributes("position")->count());
|
|
} } }
|
|
else
|
|
{
|
|
/* Draw the play button if it is enabled and hasn't been pressed yet */
|
|
sb::Plane::position->bind("vertex_position", shader_program);
|
|
sb::Plane::color->bind("vertex_color", shader_program);
|
|
button.at("play").draw(uniform["mvp"], view, projection, uniform["texture enabled"]);
|
|
}
|
|
|
|
/* Always draw the volume button */
|
|
sb::Plane::position->bind("vertex_position", shader_program);
|
|
sb::Plane::color->bind("vertex_color", shader_program);
|
|
button.at("volume").draw(uniform["mvp"], view, projection, uniform["texture enabled"]);
|
|
|
|
/* Update display */
|
|
SDL_GL_SwapWindow(window());
|
|
sb::Log::gl_errors("at end of update");
|
|
}
|
|
|
|
int main()
|
|
{
|
|
Cakefoot game = Cakefoot();
|
|
game.run();
|
|
game.quit();
|
|
return 0;
|
|
}
|