Skip to content

Commit

Permalink
Basic
Browse files Browse the repository at this point in the history
  • Loading branch information
James51332 committed Apr 17, 2023
1 parent c74adf7 commit c4cdb03
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 22 deletions.
68 changes: 68 additions & 0 deletions src/entry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "entry.h"
#include "ui.h"
#include "renderer.h"
#include "audio.h"
#include "leaderboard.h"
#include "style.h"

namespace ltrm
{

int EntryScene::s_Score = 0;

EntryScene::EntryScene()
{

}

EntryScene::~EntryScene()
{

}

void EntryScene::Load()
{

}

void EntryScene::Unload()
{

}

void EntryScene::Update()
{
Renderer::Clear(Color::Accent);

UI::Text("Enter your name!", Renderer::GetWidth() / 2, Renderer::GetHeight() / 2 - 200, 1.2f);
float width = UI::TiledTextWidth(m_Word.length() >= 3 ? m_Word.length() : 3);
UI::TiledText(m_Word, (Renderer::GetWidth() - width) / 2, Renderer::GetHeight() / 2, 1, m_Word.length() >= 3 ? m_Word.length() : 3);
}

void EntryScene::KeyDown(SDL_Keycode key)
{
if (isalpha(key))
{
char c = m_Word.length() == 0 ? SDL_toupper(key) : SDL_tolower(key);
m_Word.push_back(c);
UI::PulseLastTile();
Mixer::Pop();
return;
}

if (key == SDLK_BACKSPACE)
{
if (m_Word.length() == 0) return;
m_Word.pop_back();
Mixer::Pop();
}

if (key == SDLK_RETURN)
{
LeaderboardScene::AddEntry(m_Word, s_Score);
Mixer::Pop();
SceneManager::ChangeScene("leaderboard");
}
}

}
29 changes: 29 additions & 0 deletions src/entry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "scene.h"

#include <SDL3/SDL.h>

namespace ltrm
{

class EntryScene : public Scene
{
public:
EntryScene();
~EntryScene();

void Load();
void Unload();
void Update();
void KeyDown(SDL_Keycode key);

static void SetScore(int score) { s_Score = score; }

private:
std::string m_Word;

static int s_Score;
};

}
4 changes: 4 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "settings.h"
#include "selection.h"
#include "help.h"
#include "leaderboard.h"
#include "entry.h"
#include "dictionary.h"
#include "ui.h"
#include "animation.h"
Expand Down Expand Up @@ -83,6 +85,8 @@ void Game::Init()
SceneManager::AddScene(new LevelScene(), "level");
SceneManager::AddScene(new SettingsScene(), "settings");
SceneManager::AddScene(new HelpScene(), "help");
SceneManager::AddScene(new LeaderboardScene, "leaderboard");
SceneManager::AddScene(new EntryScene(), "entry");
}

void Game::Shutdown()
Expand Down
115 changes: 115 additions & 0 deletions src/leaderboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "leaderboard.h"
#include "ui.h"
#include "renderer.h"
#include "style.h"
#include "level.h"

namespace ltrm
{

std::list<LeaderboardScene::Entry> LeaderboardScene::s_Entries;

LeaderboardScene::LeaderboardScene()
{
AddEntry("James", 70);
AddEntry("Wes", 84);
}

LeaderboardScene::~LeaderboardScene()
{

}

void LeaderboardScene::Load()
{
m_LastTime = SDL_GetTicks();
}

void LeaderboardScene::Unload()
{

}

void LeaderboardScene::Update()
{
float time = SDL_GetTicks();
m_PulseTime += time - m_LastTime;
m_LastTime = time;

if (m_PulseTime >= 3000.0f)
{
m_PulseTime = 0;
UI::PulseTiles();
}

Renderer::Clear(Color::Accent);
UI::TiledText(std::string("LEADERBOARD"), Renderer::GetWidth() / 2, Style::SmallMargin + Style::TileSize / 2, 2);

float y = Renderer::GetHeight() / 2 + 2 * (150 + Style::SmallMargin);
if (UI::Button("Back", Renderer::GetWidth() / 2, y + 100, false))
{
SceneManager::ChangeScene("main");
}

float panelWidth = 800;
float panelHeight = 600;
float panelX = (Renderer::GetWidth() - panelWidth) / 2;
float panelY = (Renderer::GetHeight() - panelHeight) / 2;
Renderer::Fill(Color::Dark);
Renderer::Stroke(Color::Middle);
Renderer::StrokeWeight(5);
Renderer::Rect(panelX, panelY, panelWidth, panelHeight);

float textHeight;
y = panelY + 100 + textHeight / 2;
UI::TextSize("Level Complete!", nullptr, &textHeight, 1.2f);
UI::Text("Scores", Renderer::GetWidth() / 2, y, 1.2f);

float x = Renderer::GetWidth() / 2;
float height;
UI::TextSize("1.", nullptr, &height, 0.75f);

size_t place = 1;
y += Style::SmallMargin;
for (auto& entry : s_Entries)
{
y += height + Style::SmallMargin;
std::string s = std::to_string(place).append(". ").append(entry.Name).append(": ").append(std::to_string(entry.Score));
UI::Text(s.c_str(), x, y, 0.75f);
place++;
}
}

void LeaderboardScene::KeyDown(SDL_Keycode key)
{

}

void LeaderboardScene::AddEntry(const std::string &name, int score)
{
size_t i = 0;
for (auto& entry : s_Entries)
{
if (entry.Name == name)
{
entry.Score = (entry.Score < score) ? entry.Score : score;
return;
}

if (entry.Score >= score)
{
std::list<Entry>::iterator it = s_Entries.begin();
std::advance(it, i);
s_Entries.emplace(it, name, score);
LevelScene::Reset();
return;
}
i++;
}

std::list<Entry>::iterator it = s_Entries.begin();
std::advance(it, i);
s_Entries.emplace(it, name, score);
}

}
40 changes: 40 additions & 0 deletions src/leaderboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "scene.h"

#include <SDL3/SDL.h>
#include <string>
#include <list>

namespace ltrm
{

class LeaderboardScene : public Scene
{
struct Entry
{
std::string Name;
int Score;

Entry(const std::string& name, int score)
: Name(name), Score(score) {}
};

public:
LeaderboardScene();
~LeaderboardScene();

void Load();
void Unload();
void Update();
void KeyDown(SDL_Keycode key);

static void AddEntry(const std::string& name, int score);

private:
float m_LastTime = 0, m_PulseTime = 0;

static std::list<Entry> s_Entries;
};

}
56 changes: 47 additions & 9 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "style.h"
#include "animation.h"
#include "audio.h"
#include "entry.h"

#include <SDL3/SDL.h>
#include <cstring>
Expand Down Expand Up @@ -43,6 +44,7 @@ static constexpr const char* levels[] = {
};

int LevelScene::s_Level = 1;
std::vector<int> LevelScene::s_CompleteScores;

LevelScene::LevelScene()
{
Expand All @@ -61,6 +63,9 @@ LevelScene::LevelScene()
scroll.Duration = 0.15;
scroll.Loop = false;
m_ScrollAnimation = Animator::RegisterAnimation(scroll);

for (size_t i = 0; i < numLevels; i++)
s_CompleteScores.push_back(0);
}

LevelScene::~LevelScene()
Expand Down Expand Up @@ -159,14 +164,7 @@ void LevelScene::Update()

if (UI::Button("Next", Renderer::GetWidth() / 2 + Style::SmallMargin / 2 + btnWidth / 2, panelY + panelHeight - buttonPadding - btnHeight / 2, btnWidth, btnHeight, Style::SmallScale))
{
if (s_Level < numLevels)
{
s_Level++;
SceneManager::ChangeScene("level");
} else
{
SceneManager::ChangeScene("selection");
}
NextLevel();
}
}
}
Expand All @@ -181,9 +179,32 @@ void LevelScene::Unload()
m_Won = false;
}

void LevelScene::NextLevel()
{
if (s_Level < numLevels)
{
s_Level++;
SceneManager::ChangeScene("level");
} else
{
if (m_AllComplete)
SceneManager::ChangeScene("entry");
else
SceneManager::ChangeScene("selection");
}
}

void LevelScene::KeyDown(SDL_Keycode key)
{
if (m_Won) return;
if (m_Won)
{
if (key == SDLK_RETURN)
{
NextLevel();
Mixer::Pop();
}
return;
}

std::string& lastWord = m_Words[m_Words.size() -1];
if (isalpha(key))
Expand Down Expand Up @@ -235,6 +256,17 @@ void LevelScene::KeyDown(SDL_Keycode key)
UI::PulseTiles(0.25f);
Mixer::Win();
m_Won = true;
s_CompleteScores[s_Level - 1] = m_Words.size();

m_AllComplete = true;
size_t score = 0;
for (auto s : s_CompleteScores)
{
if (s == 0) m_AllComplete = false;
score += s;
}
if (m_AllComplete) EntryScene::SetScore(score);

return;
}

Expand All @@ -249,4 +281,10 @@ void LevelScene::KeyDown(SDL_Keycode key)
}
}

void LevelScene::Reset()
{
for (auto& s : s_CompleteScores)
s = 0;
}

}
Loading

0 comments on commit c4cdb03

Please sign in to comment.