score, points, collisions and unstable map

This commit is contained in:
rendo 2026-03-14 09:10:54 +05:00
commit 45a160fcaa
8 changed files with 90 additions and 48 deletions

View file

@ -1,20 +1,31 @@
######### ######### #############--##############
# #
# ####### ####### #
# #
# # P # #
# # # # # #
# #### #### #
# # # # # #
# # # #
# # # #
# # #### # #
# # # #
# # # #
# #### # # ##### #
# # # # # #
# # ### ### # #
# # # #
# # #### ##### # #
# # # #
######### ######### # #
# #
# #
# #
# #
# #
# #
# #
# #
# #
- -
- -
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# P #
############--###############

View file

@ -10,7 +10,7 @@ build/linux/raylib-test-linux-debug: $(FILES)
build/linux/raylib-test-linux-release: $(FILES) build/linux/raylib-test-linux-release: $(FILES)
mkdir -p build/linux mkdir -p build/linux
$(CP) $(FILES) $(CFLAGS) -o build/linux/raylib-test-linux-release $(CP) $(FILES) $(CFLAGS) -O3 -o build/linux/raylib-test-linux-release
release: build/linux/raylib-test-linux-release release: build/linux/raylib-test-linux-release

View file

@ -17,6 +17,7 @@ class Entity{
virtual void tick() {} virtual void tick() {}
virtual void collision(Entity* with) {} virtual void collision(Entity* with) {}
void queue_free(); void queue_free();
bool get_free_flag();
private: private:
bool free_queued; bool free_queued;
}; };

View file

@ -1,17 +1,12 @@
#include "components.h" #include "components.h"
#include "world.h"
void Entity::queue_free() { void Entity::queue_free() {
if (this->free_queued) if (this->free_queued)
return; return;
this->free_queued = true; this->free_queued = true;
World& world = get_world(); }
for(int i = 0; i < world.entities.size(); i++) bool Entity::get_free_flag() {
if(world.entities[i] == this) { return this->free_queued;
delete world.entities[i];
world.entities.erase(world.entities.begin()+i);
break;
}
} }

View file

@ -38,6 +38,9 @@ void load_world(World& world,const char* path) {
x = 0; x = 0;
y++; y++;
break; break;
case '-':
x++;
break;
default: default:
{ {
Scorepoint* point = new Scorepoint; Scorepoint* point = new Scorepoint;
@ -45,6 +48,7 @@ void load_world(World& world,const char* path) {
world.entities.push_back(point); world.entities.push_back(point);
} }
x++; x++;
break; break;
} }

View file

@ -37,7 +37,7 @@ void Pacman::tick() {
World& world = get_world(); World& world = get_world();
if (dynamic_cast<Wall*>(world.grid[indexify_position(new_position)]) == nullptr){ if (dynamic_cast<Wall*>(world.grid[world.indexify_position(new_position)]) == nullptr){
this->position=new_position; this->position=new_position;
} }
@ -77,6 +77,7 @@ void Pacman::collision(Entity* with){
Scorepoint* score = dynamic_cast<Scorepoint*>(with); Scorepoint* score = dynamic_cast<Scorepoint*>(with);
if (score != nullptr) { if (score != nullptr) {
score->queue_free(); score->queue_free();
get_world().points+=10;
} }
} }

View file

@ -1,5 +1,6 @@
#include "world.h" #include "world.h"
#include "components.h" #include "components.h"
#include <cstdlib>
#include <raylib.h> #include <raylib.h>
#include <string> #include <string>
@ -42,11 +43,31 @@ void World::process(){
if (this->clock>=this->seconds_per_tick) { if (this->clock>=this->seconds_per_tick) {
this->entities[i]->tick(); this->entities[i]->tick();
} }
// Collision check
for(int j = i+1; j < this->entities.size(); j++)
if (abs(this->entities[j]->position.x-this->entities[i]->position.x) < CELL_SIZE/2 && abs(this->entities[j]->position.y-this->entities[i]->position.y) < CELL_SIZE/2){
this->entities[j]->collision(this->entities[i]);
this->entities[i]->collision(this->entities[j]);
}
} }
if (this->clock>=this->seconds_per_tick) { if (this->clock>=this->seconds_per_tick) {
this->clock = 0; this->clock = 0;
} }
// Check for freed. Very very bad loop
bool freed_continue = true;
while (freed_continue) {
freed_continue = false;
for(int i = 0; i < this->entities.size(); i++)
if (this->entities[i]->get_free_flag()) {
this->entities.erase(this->entities.begin()+i);
freed_continue = true;
break;
}
}
} }
void World::draw() const { void World::draw() const {
@ -61,9 +82,9 @@ void World::draw() const {
if (IsKeyDown(KEY_F4)) if (IsKeyDown(KEY_F4))
{ {
std::string str = ""; std::string str = "";
for(int i = 0; i < GRID_CAPACITY; i++) for(int i = 0; i < this->get_capacity(); i++)
{ {
if (i!=0 && i%GRID_COLUMNS==0) if (i!=0 && i%this->get_columns()==0)
{ {
str += '\n'; str += '\n';
} }
@ -79,26 +100,19 @@ void World::draw() const {
} }
int indexify_position(Vector2 vector){ int World::indexify_position(Vector2 vector){
return (int)(vector.x / 16.0) + GRID_COLUMNS * (int)(vector.y / 16.0); return (int)(vector.x / 16.0) + this->get_columns() * (int)(vector.y / 16.0);
} }
void World::update_grid() { void World::update_grid() {
for(int i = 0; i < GRID_CAPACITY; i++) for(int i = 0; i < this->get_capacity(); i++)
this->grid[i] = nullptr; this->grid[i] = nullptr;
for(int i = 0; i < this->entities.size(); i++) for(int i = 0; i < this->entities.size(); i++)
{ {
int indexified_position = indexify_position(this->entities[i]->position); int indexified_position = indexify_position(this->entities[i]->position);
if (indexified_position < 0 || indexified_position >= GRID_CAPACITY) if (indexified_position < 0 || indexified_position >= this->get_capacity() || grid[indexified_position] != nullptr)
continue; continue;
if(grid[indexified_position] != nullptr){
Entity* entity = this->entities[i];
grid[indexified_position]->collision(entity);
if(entity != nullptr && grid[indexified_position] != nullptr && grid[indexified_position] != entity){
entity->collision(grid[indexified_position]);
}
}
grid[indexified_position] = this->entities[i]; grid[indexified_position] = this->entities[i];
} }
} }
@ -106,6 +120,21 @@ void World::update_grid() {
void World::set_size(Vector2i size){ void World::set_size(Vector2i size){
this->width = size.x; this->width = size.x;
this->height = size.y; this->height = size.y;
if(this->grid != nullptr)
delete [] this->grid;
this->grid = new Entity*[this->get_capacity()];
}
int World::get_capacity() const{
return this->get_columns()*this->get_rows();
}
int World::get_rows() const{
return this->height/CELL_SIZE;
}
int World::get_columns() const{
return this->width/CELL_SIZE;
} }
Texture2D* World::get_atlas() { Texture2D* World::get_atlas() {

View file

@ -7,9 +7,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
const int GRID_ROWS = 20;
const int GRID_COLUMNS = 20;
const int GRID_CAPACITY = GRID_COLUMNS*GRID_ROWS;
const int CELL_SIZE = 16; const int CELL_SIZE = 16;
struct Vector2i{ struct Vector2i{
@ -24,7 +21,7 @@ class World {
~World(); ~World();
bool debug; bool debug;
std::vector<Entity*> entities; /// Main subjects of game world. std::vector<Entity*> entities; /// Main subjects of game world.
Entity* grid[GRID_CAPACITY]; /// Grid representation Entity** grid; /// Grid representation
float seconds_per_tick; /// Internal clock speed float seconds_per_tick; /// Internal clock speed
int width; int width;
int height; int height;
@ -39,8 +36,13 @@ class World {
void draw() const; /// Should be called at the end of frame. void draw() const; /// Should be called at the end of frame.
/// Calls draw() on every entity /// Calls draw() on every entity
void set_size(Vector2i); void set_size(Vector2i);
int get_capacity() const;
int get_columns() const;
int get_rows() const;
void load_atlas(); void load_atlas();
Texture2D* get_atlas(); Texture2D* get_atlas();
int indexify_position(Vector2 vector);
int points;
private: private:
float clock; float clock;
void update_grid(); void update_grid();
@ -49,7 +51,6 @@ class World {
void create_world_with(float seconds_per_tick); void create_world_with(float seconds_per_tick);
World& get_world(); // Thanks, 2ndbeam, helps a lot World& get_world(); // Thanks, 2ndbeam, helps a lot
int indexify_position(Vector2 vector);
Vector2i get_map_size(const char* path); Vector2i get_map_size(const char* path);
void load_world(World& world, const char* path); void load_world(World& world, const char* path);