From c67ddcc6d220c1dd5e62261b9ee405a40a12118c Mon Sep 17 00:00:00 2001 From: Rendo Date: Sun, 15 Mar 2026 00:28:33 +0500 Subject: [PATCH] Cherry --- src/components.h | 12 ++++++++++-- src/ghost.cpp | 22 ++++++++++++++++++++-- src/main.cpp | 6 +++--- src/maploader.cpp | 8 ++++++++ src/pacman.cpp | 9 +++++---- src/scorepoint.cpp | 8 ++++++++ src/world.cpp | 16 +++++++++++++--- src/world.h | 4 ++++ 8 files changed, 71 insertions(+), 14 deletions(-) diff --git a/src/components.h b/src/components.h index 331f90a..6012349 100644 --- a/src/components.h +++ b/src/components.h @@ -26,7 +26,6 @@ class Entity{ class Pacman : public Entity{ - const int speed = 16; const int frame_count = 7; const int fps = 24; private: @@ -70,13 +69,14 @@ class Scorepoint : public Entity { }; class Ghost : public Entity { + const int respawn_time = 45; private: static unsigned int color_decision; TextureAtlas texture; - const int speed = 16; Color color; int direction = 1; Vector2 start_position; + int respawn_timer = 0; void recalculate_direction(); void try_to_chase(); public: @@ -87,4 +87,12 @@ class Ghost : public Entity { void collision(Entity* with) override; }; +class Cherry : public Entity { + private: + TextureAtlas texture; + public: + Cherry(); + void draw() const override; +}; + #endif diff --git a/src/ghost.cpp b/src/ghost.cpp index 6dc02c6..f23e4b5 100644 --- a/src/ghost.cpp +++ b/src/ghost.cpp @@ -35,6 +35,11 @@ void Ghost::tick() { World& world = get_world(); + if(respawn_timer>0){ + respawn_timer--; + return; + } + if(dynamic_cast(world.grid[world.indexify_position(check_position)]) != nullptr){ recalculate_direction(); check_position = project_position(direction, 1); @@ -80,9 +85,22 @@ void Ghost::recalculate_direction(){ } void Ghost::draw() const { - DrawTextureRec(this->texture.get_texture(), this->texture.rect_view(0,0,16, 16), this->position, this->color); + if (get_world().get_killmode() == true || respawn_timer > 0) + DrawTextureRec(texture.get_texture(), texture.rect_view(16, 0, 16, 16), position, WHITE); + else + DrawTextureRec(this->texture.get_texture(), this->texture.rect_view(0,0,16, 16), this->position, this->color); } void Ghost::collision(Entity* with) { - + Pacman* pacman = dynamic_cast(with); + if(pacman != nullptr) + { + if(get_world().get_killmode()){ + respawn_timer=respawn_time; + position=start_position; + } else{ + pacman->queue_free(); + get_world().lose(); + } + } } diff --git a/src/main.cpp b/src/main.cpp index 438b56b..c5cc6bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,13 +3,13 @@ #include "world.h" int main() { - create_world_with(1./5.); - - World& world = get_world(); Vector2i window_size = get_map_size("assets/map"); InitWindow(window_size.x, window_size.y, "Zoomba"); + create_world_with(1./5.); + + World& world = get_world(); world.load_atlas(); world.set_size(window_size); load_world(world,"assets/map"); diff --git a/src/maploader.cpp b/src/maploader.cpp index afcf25a..0232104 100644 --- a/src/maploader.cpp +++ b/src/maploader.cpp @@ -71,6 +71,14 @@ void load_world(World& world,const char* path) { } x++; + break; + case 'C': + { + Cherry* cherry = new Cherry; + cherry->position = {(float)x*16,(float)y*16}; + world.entities.push_back(cherry); + } + x++; break; default: x++; diff --git a/src/pacman.cpp b/src/pacman.cpp index 22a3b9b..38a0ef9 100644 --- a/src/pacman.cpp +++ b/src/pacman.cpp @@ -94,10 +94,11 @@ void Pacman::collision(Entity* with){ score->queue_free(); get_world().points+=10; } - Ghost* ghost = dynamic_cast(with); - if (ghost != nullptr){ - queue_free(); - get_world().lose(); + Cherry* cherry = dynamic_cast(with); + if (cherry != nullptr) { + cherry->queue_free(); + get_world().points+=30; + get_world().start_killmode(); } } diff --git a/src/scorepoint.cpp b/src/scorepoint.cpp index 56dad22..42cf2b8 100644 --- a/src/scorepoint.cpp +++ b/src/scorepoint.cpp @@ -9,3 +9,11 @@ Scorepoint::Scorepoint(){ void Scorepoint::draw() const { DrawTextureRec(this->texture.get_texture(), this->texture.full_view(), this->position, WHITE); } + +Cherry::Cherry(){ + texture = {get_world().get_atlas(),112,16,16,16}; +} + +void Cherry::draw() const { + DrawTextureRec(texture.get_texture(),texture.full_view(),position,WHITE); +} diff --git a/src/world.cpp b/src/world.cpp index 861dff1..dc9b4f5 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -10,7 +10,6 @@ void init_world() { World& world = get_world(); Vector2i window_size = get_map_size("assets/map"); - world.load_atlas(); world.set_size(window_size); load_world(world,"assets/map"); @@ -21,6 +20,7 @@ World::World(){ this->entities = {}; this->grid = new Entity*[get_capacity()]; this->seconds_per_tick = 0; + this->clock = 0; this->debug = false; this->bound_offset = 1; @@ -30,7 +30,7 @@ World::~World(){ for(int i = 0; i < this->entities.size();i++) { delete this->entities[i]; } - UnloadTexture(this->texture_atlas); + UnloadTexture(texture_atlas); } void World::load_atlas() { @@ -42,6 +42,14 @@ void World::setup(){ this->entities[i]->ready(); } +bool World::get_killmode(){ + return pacman_killmod_timer > 0; +} + +void World::start_killmode(){ + pacman_killmod_timer = PACMAN_KILLTIME; +} + void World::process(){ this->clock += GetFrameTime(); @@ -63,7 +71,7 @@ void World::process(){ for(int i = 0; i < this->entities.size(); i++) { this->entities[i]->process(); if (this->clock>=this->seconds_per_tick) { - this->entities[i]->tick(); + this->entities[i]->tick(); } // Collision check @@ -76,6 +84,8 @@ void World::process(){ if (this->clock>=this->seconds_per_tick) { this->clock = 0; + if(pacman_killmod_timer>0) + pacman_killmod_timer-=1; } // Check for freed. Very very bad loop diff --git a/src/world.h b/src/world.h index 7237d8a..4e91e22 100644 --- a/src/world.h +++ b/src/world.h @@ -20,6 +20,7 @@ enum LevelState{ Lost = 2, }; +const int PACMAN_KILLTIME = 45; /// Class that holds information about game world class World { public: @@ -45,6 +46,8 @@ class World { int get_capacity() const; int get_columns() const; int get_rows() const; + void start_killmode(); + bool get_killmode(); void load_atlas(); void lose(); void win(); @@ -53,6 +56,7 @@ class World { int points = 0; private: float clock = 0.; + int pacman_killmod_timer = 0; int state = 0; void update_grid(); Texture2D texture_atlas;