World working

This commit is contained in:
Rendo 2026-03-11 14:25:42 +05:00
commit 76284d80b9
4 changed files with 35 additions and 16 deletions

View file

@ -1,10 +1,10 @@
#include <iostream>
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include "components.h" #include "components.h"
// Pacman definitions // Pacman definitions
Rectangle Pacman::getTextureRect(){ Rectangle Pacman::getTextureRect(){
Rectangle result; Rectangle result;
result.x = 0; result.x = 0;
@ -21,7 +21,7 @@ Pacman::Pacman() {
Pacman::Pacman(Vector2 position) { Pacman::Pacman(Vector2 position) {
this->texture = LoadTexture("assets/sprites/pacman.png"); this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0; this->facing = 0;
this->position = position; this->position = {0.,0.};
} }
Pacman::~Pacman() { Pacman::~Pacman() {
UnloadTexture(this->texture); UnloadTexture(this->texture);
@ -38,12 +38,24 @@ void Pacman::tick() {
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)); this->position = Vector2Add(this->position,Vector2Scale(direction, (float)speed));
// Bound check
if (this->position.x < 0){
this->position.x += 320;
}
if (this->position.y < 0){
this->position.y += 320;
}
if (this->position.x >= 320){
this->position.x -= 320;
}
if (this->position.y >= 320){
this->position.y -= 320;
}
} }
void Pacman::draw() { void Pacman::draw() {
// Drawing // Drawing
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
DrawText(TextFormat("Position: %"), int posX, int posY, int fontSize, Color color)
} }
// Wall definitions // Wall definitions

View file

@ -3,17 +3,18 @@
#include "components.h" #include "components.h"
#include "world.h" #include "world.h"
int main() { const int SCREEN_HEIGHT = 320;
const int screen_height = 320; const int SCREEN_WIDTH = 320;
const int screen_width = 320;
InitWindow(screen_width, screen_height, "Test game"); int main() {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test game");
SetTargetFPS(60); SetTargetFPS(60);
world = create_world_with(1.0); world = create_world_with(.25);
world.entities.push_back(Pacman({screen_width/2,screen_height/2})); world.entities.push_back(new Pacman({SCREEN_WIDTH/2,SCREEN_HEIGHT/2}));
world.setup(); world.setup();
@ -25,7 +26,6 @@ int main() {
ClearBackground(BLACK); ClearBackground(BLACK);
DrawText(TextFormat("Entities: %i",world.entities.size()),0,0, 14,WHITE);
world.draw(); world.draw();
EndDrawing(); EndDrawing();

View file

@ -7,18 +7,24 @@ World::World(){
this->clock = 0; this->clock = 0;
} }
World::~World(){
for(int i = 0; i < this->entities.size();i++) {
delete this->entities[i];
}
}
void World::setup(){ void World::setup(){
for(int i = 0; i < this->entities.size(); i++) for(int i = 0; i < this->entities.size(); i++)
this->entities[i].ready(); this->entities[i]->ready();
} }
void World::process(){ void World::process(){
this->clock += GetFrameTime(); this->clock += GetFrameTime();
for(int i = 0; i < this->entities.size(); i++) { for(int i = 0; i < this->entities.size(); i++) {
this->entities[i].process(); this->entities[i]->process();
if (this->clock>=this->seconds_per_tick) { if (this->clock>=this->seconds_per_tick) {
this->entities[i].tick(); this->entities[i]->tick();
} }
} }
@ -29,7 +35,7 @@ void World::process(){
void World::draw() { void World::draw() {
for(int i = 0; i < this->entities.size(); i++) { for(int i = 0; i < this->entities.size(); i++) {
this->entities[i].draw(); this->entities[i]->draw();
} }
} }

View file

@ -8,8 +8,9 @@
class World { class World {
public: public:
World(); World();
~World();
std::vector<Entity> entities; /// Main subjects of game world. std::vector<Entity*> entities; /// Main subjects of game world.
float seconds_per_tick; /// Internal clock speed float seconds_per_tick; /// Internal clock speed
void setup(); /// Sets up game world. void setup(); /// Sets up game world.