Skip to content

Commit

Permalink
init lol
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasByr committed May 7, 2023
1 parent c044da1 commit 5a1116f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions inc/core/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ class Map {
* (i.e. if the ghosts have been eaten once during the current power)
*
* @param ghost index/type of the ghost
* @return int option
* @return bool option
*/
int get_ghosts_powered(int ghost) const;
bool get_ghosts_powered(int ghost) const;
};

#endif // __inc_core_map_H__
8 changes: 4 additions & 4 deletions src/core/ghost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ void Ghost::update(std::shared_ptr<Map> map, std::tuple<int, int> pacman_pos,
if (is_at_home) {
switch (type) {
case GhostType::CLYDE: m_timer.start_timer(20); break;

case GhostType::PINKY: m_timer.start_timer(0); break;

case GhostType::INKY: m_timer.start_timer(10); break;

default: m_timer.start_timer(dis(gen));
Expand All @@ -243,11 +241,12 @@ void Ghost::update(std::shared_ptr<Map> map, std::tuple<int, int> pacman_pos,
if (!is_at_home && state != GhstState::FRIGHTENED &&
map->get_ghosts_powered(static_cast<int>(type))) {
state = GhstState::FRIGHTENED;
fmt::debug("lol");
m_timer.reset_timer(); // reset to use global timer
}

// change state
if ((m_timer.is_running() && m_timer.is_expired()) || p_timer->is_expired()) {
if (m_timer.is_expired() /* || !m_timer.is_running() */) {
if (is_at_home) {
is_at_home = false;
m_timer.reset_timer();
Expand Down Expand Up @@ -276,6 +275,7 @@ void Ghost::update(std::shared_ptr<Map> map, std::tuple<int, int> pacman_pos,
}
}
// go out of frightened state
// we cannot check if the timer is expired because it might be reset
if (state == GhstState::FRIGHTENED && !p_timer->is_running()) {
state = GhstState::SCATTER;
m_timer.reset_timer();
Expand Down Expand Up @@ -349,7 +349,7 @@ void Ghost::reset() {
is_at_home = type != GhostType::BLINKY;
m_direction = is_at_home ? Direction::UP : Direction::NONE;
m_reg_direction = Direction::NONE;
state = GhstState::CHASE;
state = GhstState::SCATTER;
m_timer.reset_timer(); // timer will start itself in update
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ void Map::reset(const std::string &path) {
using namespace std;

power_timer = std::make_shared<sys_pause::Timer>();
ghosts_powered = std::array<bool, 4>{false, false, false, false};

ifstream file{path};
string line = "";
Expand Down Expand Up @@ -336,6 +337,6 @@ void Map::set_ghosts_powered(const bool powered, std::optional<int> ghost) {
}
}

int Map::get_ghosts_powered(int ghost) const {
bool Map::get_ghosts_powered(int ghost) const {
return ghosts_powered.at(ghost);
}
12 changes: 9 additions & 3 deletions src/helper/renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

#include <thread>

#include "ini.hpp"

#include "helper/assets.h"
Expand All @@ -8,8 +10,8 @@

Renderer::Renderer(const std::string &title, const std::string &config_path)
: m_window{nullptr}, m_surface{nullptr}, m_sprites{nullptr}, m_scale{1},
size{0}, m_config_stack{},
m_rect_mode{RectMode::CORNER}, m_trans_x{0}, m_trans_y{0} {
size{0}, m_config_stack{}, m_rect_mode{RectMode::CORNER}, m_trans_x{0},
m_trans_y{0} {
ini::IniFile data;
try {
data.load(config_path);
Expand Down Expand Up @@ -78,7 +80,11 @@ void Renderer::flip() {

double target_delay = ms_in_s / m_fps;
auto wait_time = target_delay - static_cast<double>(elapsed_time);
if (wait_time > 0) SDL_Delay(static_cast<Uint32>(wait_time));
// if (wait_time > 0) SDL_Delay(static_cast<Uint32>(wait_time));
if (wait_time > 0) {
std::this_thread::sleep_for(
std::chrono::milliseconds(static_cast<Uint32>(wait_time)));
}

// update window as soon as we delay
switch (SDL_UpdateWindowSurface(m_window)) {
Expand Down

0 comments on commit 5a1116f

Please sign in to comment.