This commit is contained in:
Rendo 2026-03-15 00:28:33 +05:00
commit c67ddcc6d2
8 changed files with 71 additions and 14 deletions

View file

@ -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

View file

@ -35,6 +35,11 @@ void Ghost::tick() {
World& world = get_world();
if(respawn_timer>0){
respawn_timer--;
return;
}
if(dynamic_cast<GhostWall*>(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<Pacman*>(with);
if(pacman != nullptr)
{
if(get_world().get_killmode()){
respawn_timer=respawn_time;
position=start_position;
} else{
pacman->queue_free();
get_world().lose();
}
}
}

View file

@ -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");

View file

@ -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++;

View file

@ -94,10 +94,11 @@ void Pacman::collision(Entity* with){
score->queue_free();
get_world().points+=10;
}
Ghost* ghost = dynamic_cast<Ghost*>(with);
if (ghost != nullptr){
queue_free();
get_world().lose();
Cherry* cherry = dynamic_cast<Cherry*>(with);
if (cherry != nullptr) {
cherry->queue_free();
get_world().points+=30;
get_world().start_killmode();
}
}

View file

@ -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);
}

View file

@ -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

View file

@ -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;