diff options
author | Sven Gothel <[email protected]> | 2022-03-27 07:32:53 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-03-27 07:32:53 +0200 |
commit | a15b96c1a4c6ad44b3b47d69260d0e301c1b1a48 (patch) | |
tree | e20202fc525d009b0e97149f76900280414dfd81 /src | |
parent | 439d682c9321f2136d8bc7627d0a394ebb56af85 (diff) |
Introduce mode_t::LEVEL_START and global level counter
LEVEL_START initiated by main -> pacman, which in turn sets same mode of ghosts.
LEVEL_START is purely intermediate and manifests as HOME w/ additional settings performed.
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 7 | ||||
-rw-r--r-- | src/ghost.cpp | 42 | ||||
-rw-r--r-- | src/pacman.cpp | 24 |
3 files changed, 48 insertions, 25 deletions
diff --git a/src/game.cpp b/src/game.cpp index d97c529..71f293f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -43,6 +43,9 @@ int win_pixel_scale = 1; static int frames_per_sec=0; int get_frames_per_sec() { return frames_per_sec; } +static int current_level=-1; +int get_current_level() { return current_level; } + std::unique_ptr<maze_t> global_maze; std::shared_ptr<global_tex_t> global_tex; std::vector<ghost_ref> ghosts; @@ -226,7 +229,8 @@ static void set_game_mode(const game_mode_t m) noexcept { const game_mode_t old_mode = game_mode; switch( m ) { case game_mode_t::LEVEL_START: - pacman->set_mode( pacman_t::mode_t::HOME ); + ++current_level; + pacman->set_mode( pacman_t::mode_t::LEVEL_START ); global_maze->reset(); game_mode = m; if( audio_samples[ number( audio_clip_t::INTRO ) ]->is_valid() ) { @@ -518,6 +522,7 @@ int main(int argc, char *argv[]) } break; case SDL_SCANCODE_R: + current_level = -1; set_game_mode(game_mode_t::LEVEL_START); break; case SDL_SCANCODE_W: diff --git a/src/ghost.cpp b/src/ghost.cpp index 1bc0f91..85888ba 100644 --- a/src/ghost.cpp +++ b/src/ghost.cpp @@ -70,8 +70,8 @@ ghost_t::ghost_t(const personality_t id_, SDL_Renderer* rend, const float fields keyframei_(get_frames_per_sec(), fields_per_sec_total*current_speed_pct, true /* nearest */), sync_next_frame_cntr( keyframei_.sync_frame_count(), true /* auto_reload */), id( id_ ), - live_counter( 0 ), - mode_( mode_t::HOME ), + live_counter_during_pacman_live( 0 ), + mode_( mode_t::AWAY ), mode_ms_left ( 0 ), dir_( direction_t::LEFT ), atex_normal( "N", rend, ms_per_atex, global_tex->all_images(), 0, id_to_yoff(id), 14, 14, { { 0*14, 0 }, { 1*14, 0 }, { 2*14, 0 }, { 3*14, 0 } }), @@ -81,7 +81,6 @@ ghost_t::ghost_t(const personality_t id_, SDL_Renderer* rend, const float fields pos_( global_maze->ghost_home_pos() ), target_( global_maze->ghost_home_pos() ) { - set_mode( mode_t::HOME ); } @@ -192,11 +191,19 @@ void ghost_t::set_next_target() noexcept { void ghost_t::set_mode(const mode_t m, const int mode_ms) noexcept { const mode_t old_mode = mode_; + mode_t m1 = m; switch( m ) { - case mode_t::HOME: - mode_ = m; + case mode_t::LEVEL_START: + m1 = mode_t::HOME; + [[fallthrough]]; + case mode_t::HOME: { + mode_ = m1; mode_ms_left = 0 <= mode_ms ? mode_ms : 0; - if( ghost_t::personality_t::BLINKY == id && 0 <= mode_ms /** pacman's call */ ) { + const bool pacman_initiated = 0 <= mode_ms; /* pacman_initiated: LEVEL_START or pacman died */ + if( pacman_initiated ) { + live_counter_during_pacman_live = 0; + } + if( ghost_t::personality_t::BLINKY == id && pacman_initiated ) { // positioned outside of the box at start pos_ = global_maze->ghost_start_pos(); } else { @@ -205,12 +212,14 @@ void ghost_t::set_mode(const mode_t m, const int mode_ms) noexcept { pos_.set_centered(keyframei_); set_speed(0.75f); break; + } case mode_t::LEAVE_HOME: - mode_ = m; + mode_ = m1; mode_ms_left = -1; + pellet_counter_active_ = false; break; case mode_t::CHASE: - mode_ = m; + mode_ = m1; mode_ms_left = 0 <= mode_ms ? mode_ms : number( mode_duration_t::CHASING ); if( mode_t::LEAVE_HOME == old_mode ) { dir_ = direction_t::LEFT; @@ -220,7 +229,7 @@ void ghost_t::set_mode(const mode_t m, const int mode_ms) noexcept { set_speed(0.75f); break; case mode_t::SCATTER: - mode_ = m; + mode_ = m1; mode_ms_left = 0 <= mode_ms ? mode_ms : number( mode_duration_t::SCATTERING ); if( mode_t::LEAVE_HOME == old_mode ) { dir_ = direction_t::LEFT; @@ -233,26 +242,29 @@ void ghost_t::set_mode(const mode_t m, const int mode_ms) noexcept { if( mode_t::HOME == old_mode || mode_t::LEAVE_HOME == old_mode ) { // NOP } else { - mode_ = m; + mode_ = m1; mode_ms_left = 0 <= mode_ms ? mode_ms : number( mode_duration_t::SCARED ); dir_ = inverse(dir_); set_speed(0.50f); } break; case mode_t::PHANTOM: - mode_ = m; + mode_ = m1; mode_ms_left = -1; - ++live_counter; set_speed(0.75f); + ++live_counter_during_pacman_live; break; default: - mode_ = m; + mode_ = m1; mode_ms_left = -1; break; } set_next_target(); - log_printf("%s set_mode: %s -> %s [%d ms], pos %s -> %s\n", to_string(id).c_str(), to_string(old_mode).c_str(), to_string(mode_).c_str(), mode_ms_left, - pos_.toShortString().c_str(), target_.toShortString().c_str()); + if( log_moves ) { + log_printf("%s set_mode: %s -> %s -> %s [%d ms], pos %s -> %s\n", to_string(id).c_str(), + to_string(old_mode).c_str(), to_string(m).c_str(), to_string(mode_).c_str(), mode_ms_left, + pos_.toShortString().c_str(), target_.toShortString().c_str()); + } } void ghost_t::set_speed(const float pct) noexcept { diff --git a/src/pacman.cpp b/src/pacman.cpp index e3c2871..1ceace0 100644 --- a/src/pacman.cpp +++ b/src/pacman.cpp @@ -72,7 +72,7 @@ pacman_t::pacman_t(SDL_Renderer* rend, const float fields_per_sec_total_) noexce keyframei_(get_frames_per_sec(), fields_per_sec_total*current_speed_pct, true /* nearest */), sync_next_frame_cntr( keyframei_.sync_frame_count(), true /* auto_reload */), next_empty_field_frame_cntr(0, false /* auto_reload */), - mode( mode_t::HOME ), + mode( mode_t::LEVEL_START ), mode_ms_left ( -1 ), lives( 3 ), current_dir( direction_t::LEFT ), @@ -88,7 +88,7 @@ pacman_t::pacman_t(SDL_Renderer* rend, const float fields_per_sec_total_) noexce atex( &get_tex() ), pos_( global_maze->pacman_start_pos() ) { - set_mode( mode_t::HOME ); + set_mode( mode_t::LEVEL_START ); } void pacman_t::destroy() noexcept { @@ -102,10 +102,16 @@ void pacman_t::destroy() noexcept { void pacman_t::set_mode(const mode_t m) noexcept { const mode_t old_mode = mode; + mode_t m1 = m; + ghost_t::mode_t gm = ghost_t::mode_t::HOME; switch( m ) { + case mode_t::LEVEL_START: + m1 = mode_t::HOME; + gm = ghost_t::mode_t::LEVEL_START; + [[fallthrough]]; case mode_t::HOME: audio_samples[ ::number( audio_clip_t::MUNCH ) ]->stop(); - mode = m; + mode = m1; mode_ms_left = number( mode_duration_t::HOMESTAY ); pos_ = global_maze->pacman_start_pos(); pos_.set_aligned_dir( direction_t::LEFT, keyframei_ ); @@ -113,17 +119,17 @@ void pacman_t::set_mode(const mode_t m) noexcept { reset_stats(); // always, even if speed is unchanged atex = &get_tex(); for(ghost_ref g : ghosts) { - g->set_mode(ghost_t::mode_t::HOME, mode_ms_left); + g->set_mode(gm, mode_ms_left); } set_speed(0.80f); break; case mode_t::NORMAL: - mode = m; + mode = m1; mode_ms_left = -1; set_speed(0.80f); break; case mode_t::POWERED: - mode = m; + mode = m1; mode_ms_left = number( mode_duration_t::INPOWER ); for(ghost_ref g : ghosts) { g->set_mode(ghost_t::mode_t::SCARED); @@ -132,7 +138,7 @@ void pacman_t::set_mode(const mode_t m) noexcept { break; case mode_t::DEAD: audio_samples[ ::number( audio_clip_t::MUNCH ) ]->stop(); - mode = m; + mode = m1; mode_ms_left = number( mode_duration_t::DEADANIM ); atex_dead.reset(); for(ghost_ref g : ghosts) { @@ -141,11 +147,11 @@ void pacman_t::set_mode(const mode_t m) noexcept { audio_samples[ ::number( audio_clip_t::DEATH ) ]->play(); break; default: - mode = m; + mode = m1; mode_ms_left = -1; break; } - log_printf("pacman set_mode: %s -> %s [%d ms], %s\n", to_string(old_mode).c_str(), to_string(mode).c_str(), mode_ms_left, pos_.toShortString().c_str()); + log_printf("pacman set_mode: %s -> %s -> %s [%d ms], %s\n", to_string(old_mode).c_str(), to_string(m).c_str(), to_string(mode).c_str(), mode_ms_left, pos_.toShortString().c_str()); } void pacman_t::set_speed(const float pct) noexcept { |