Wall collisions

This commit is contained in:
rendo 2026-03-12 16:58:51 +05:00
commit 4b4e83ac7e
4 changed files with 40 additions and 10 deletions

View file

@ -11,8 +11,10 @@ int main() {
SetTargetFPS(60); SetTargetFPS(60);
world = create_world_with(1./10.); create_world_with(1./10.);
World& world = get_world();
loadmap(world); loadmap(world);
world.setup(); world.setup();

View file

@ -1,6 +1,7 @@
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include "components.h" #include "components.h"
#include "world.h"
// Pacman definitions // Pacman definitions
@ -37,7 +38,11 @@ void Pacman::tick() {
// Movement in direction // Movement in direction
double angle = PI/2.0*facing; double angle = PI/2.0*facing;
Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))}; Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
this->position = Vector2Add(this->position,Vector2Scale(direction, (float)speed)); Vector2 new_position = Vector2Add(this->position,Vector2Scale(direction, (float)speed));
if (get_world().grid[indexify_position(new_position)] == nullptr){
this->position=new_position;
}
// Bound check // Bound check
if (this->position.x < 0){ if (this->position.x < 0){

View file

@ -1,5 +1,7 @@
#include "world.h" #include "world.h"
#include "components.h"
#include <raylib.h> #include <raylib.h>
#include <string>
World::World(){ World::World(){
this->entities = {}; this->entities = {};
@ -50,8 +52,27 @@ void World::draw() const {
DrawText(TextFormat("entities: %i",this->entities.size()), 0, 0, 14, WHITE); DrawText(TextFormat("entities: %i",this->entities.size()), 0, 0, 14, WHITE);
DrawText(TextFormat("seconds_per_tick: %f",this->seconds_per_tick), 0, 16, 14, WHITE); DrawText(TextFormat("seconds_per_tick: %f",this->seconds_per_tick), 0, 16, 14, WHITE);
if (IsKeyDown(KEY_F4))
{
std::string str = "";
for(int i = 0; i < GRID_CAPACITY; i++)
{
if (i!=0 && i%GRID_COLUMNS==0)
{
str += '\n';
}
if (dynamic_cast<Wall*>(grid[i]))
str += '#';
else if (dynamic_cast<Pacman*>(grid[i]))
str += 'P';
else
str += ' ';
}
DrawText(str.c_str(), 32, 0, 14, WHITE);
}
} }
int indexify_position(Vector2 vector){ int indexify_position(Vector2 vector){
return (int)(vector.x / 16.0) + GRID_COLUMNS * (int)(vector.y / 16.0); return (int)(vector.x / 16.0) + GRID_COLUMNS * (int)(vector.y / 16.0);
} }
@ -71,10 +92,11 @@ void World::update_grid() {
World create_world_with(float seconds_per_tick){ void create_world_with(float seconds_per_tick){
World result = World(); get_world().seconds_per_tick = seconds_per_tick;
}
result.seconds_per_tick = seconds_per_tick;
World& get_world() {
return result; static World world;
return world;
} }

View file

@ -1,7 +1,9 @@
#ifndef WORLD_H #ifndef WORLD_H
#define WORLD_H #define WORLD_H
#include "components.h" #include "components.h"
#include <string>
#include <vector> #include <vector>
const int GRID_ROWS = 20; const int GRID_ROWS = 20;
@ -31,9 +33,8 @@ class World {
void update_grid(); void update_grid();
}; };
static World world; /// World singleton void create_world_with(float seconds_per_tick);
World& get_world(); // Thanks, 2ndbeam, helps a lot
World create_world_with(float seconds_per_tick);
int indexify_position(Vector2 vector); int indexify_position(Vector2 vector);
#endif #endif