From 9f1374c0b1977a6e86b5616fabd85d361cd36ea5 Mon Sep 17 00:00:00 2001 From: nosch Date: Sun, 31 Aug 2025 16:36:03 +0200 Subject: [PATCH] made the player a Entity and gave it a controller --- src/assets/BasicTile.png | Bin 0 -> 257 bytes src/entities/base.cpp | 14 +++++--- src/entities/entitybase.hpp | 8 +++-- src/main.cpp | 67 ++++++++++++++++++++++-------------- src/render/textures.cpp | 7 +++- src/render/textures.hpp | 4 ++- 6 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 src/assets/BasicTile.png diff --git a/src/assets/BasicTile.png b/src/assets/BasicTile.png new file mode 100644 index 0000000000000000000000000000000000000000..17ca266172f2334b99de0c74ceea55860b2c8b4d GIT binary patch literal 257 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=jKx9jP7LeL$-D$|c6+)whE&XX zdwZkcAqRn@7Y;i+ZgJ);<34ibQQ4h|oo|FH4tic}lRE4FePiBjOSQC%OtYSs*4)>B z`LW#Yx8bo2Mplgmu?r#&+! #include -Entity::Entity(ID Id, raylib::Rectangle Rect, std::string &n, int h, int maxh) { +void emptycontroller (Entity &e, float deltatime) { + std::cout << "doing no shit cuz funny lmao"; +} + +Entity::Entity(ID Id, raylib::Rectangle Rect,std::string n, int h, int maxh) { id = Id; + spriteid = 0; rect = Rect; health = h; maxhealth = maxh; nametag = n; + controller = emptycontroller; } -void Entity::Update() { +void Entity::Update(float deltatime) { if (controller) { - controller(*this); + controller(*this, deltatime); }else { std::cout << "Entity is missing an controller, doing nothing.\n"; } } void Entity::Draw() { - DrawTextureEx(entityTextures.GetTexture(id), raylib::Vector2(rect.x, rect.y), 0, 0, WHITE); + DrawTextureEx(entityTextures.GetTexture(spriteid), raylib::Vector2(rect.x, rect.y), 0, 0, WHITE); } diff --git a/src/entities/entitybase.hpp b/src/entities/entitybase.hpp index bae3a8f..4a8580b 100644 --- a/src/entities/entitybase.hpp +++ b/src/entities/entitybase.hpp @@ -10,19 +10,21 @@ struct Entity; -using ControllerFunc = std::function; +using ControllerFunc = std::function; struct Entity { ID id; + ID spriteid; raylib::Rectangle rect; int health; int maxhealth; std::string nametag; ControllerFunc controller; - void Update(); + Vector2 velocity; + void Update(float deltatime); void Draw(); - Entity(ID Id, raylib::Rectangle rect, std::string &n, int h, int maxh); + Entity(ID Id, raylib::Rectangle rect, std::string n, int h, int maxh); }; diff --git a/src/main.cpp b/src/main.cpp index d131020..343795a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,28 @@ #include "../include/raylib-cpp.hpp" #include "entities/entitybase.hpp" +#include "render/textures.hpp" #include -struct player { - float x; - float y; - float width; - float height; -}; +void PlayerController(Entity &e, float deltatime) { + raylib::Vector2 velocity = {0,0}; + raylib::Vector2 acceleration = {0,0}; + if (IsKeyDown(KEY_W)) acceleration.y--; + if (IsKeyDown(KEY_A)) acceleration.x--; + if (IsKeyDown(KEY_S)) acceleration.y++; + if (IsKeyDown(KEY_D)) acceleration.x++; + + acceleration.Normalize(); + + e.velocity.x += acceleration.x * 200 * deltatime; + e.velocity.y += acceleration.y * 200 * deltatime; + + e.velocity.x *= 0.98; + e.velocity.y *= 0.98; + + e.rect.x += e.velocity.x * deltatime; + + e.rect.y += e.velocity.y * deltatime; +} int main() { int screenWidth = 1920; @@ -17,42 +32,42 @@ int main() { SetTargetFPS(60); - player Player; - Player.x = 100.0f; - Player.y = 100.0f; - Player.width = 50.0f; - Player.height = 50.0f; + GenDefaultTexture(); + + raylib::Rectangle playerrect{screenWidth / 2, screenHeight / 2,64,64}; + Entity player{1, playerrect, "MainPlayer", 100, 100}; + player.controller = PlayerController; + player.spriteid = 1; + + entityTextures.LoadImage("./src/assets/playersprite.png"); + //tileTextures.LoadImage("./src/assets/BasicTile.png"); Camera2D camera = { 0 }; - camera.target = { Player.x, Player.y }; camera.offset = { screenWidth/2.0f, screenHeight/2.0f }; camera.rotation = 0.0f; camera.zoom = 1.0f; - raylib::Texture logo("./src/assets/playersprite.png"); - Texture2D bg = LoadTexture("./src/assets/Dirt_01.png"); - // background.height = 1080; - // background.width = 1920; while (!window.ShouldClose()) { + float deltatime = GetFrameTime(); + + player.Update(deltatime); + BeginDrawing(); window.ClearBackground(BLACK); BeginMode2D(camera); - if (IsKeyDown(KEY_S)) Player.y += 1; - if (IsKeyDown(KEY_W)) Player.y -= 1; - if (IsKeyDown(KEY_D)) Player.x += 1; - if (IsKeyDown(KEY_A)) Player.x -= 1; - - camera.target = { Player.x, Player.y }; + camera.target = { player.rect.x, player.rect.y }; if (IsKeyDown(KEY_Q)) camera.zoom += 0.1f; - DrawText("Middle", 190, 200, 20, WHITE); + Texture bg = tileTextures.GetTexture(1); + std::cout << bg.height; + // Infinite Background int bh = bg.height; int bw = bg.width; @@ -67,11 +82,11 @@ int main() { -offsetY + j * bh, WHITE ); + } } - } - logo.Draw(Player.x, Player.y); - + player.Draw(); + EndMode2D(); EndDrawing(); } diff --git a/src/render/textures.cpp b/src/render/textures.cpp index 05de634..dc365e0 100644 --- a/src/render/textures.cpp +++ b/src/render/textures.cpp @@ -1,4 +1,5 @@ #include "../../include/raylib-cpp.hpp" +#include #include #include "textures.hpp" using namespace std; @@ -7,10 +8,11 @@ typedef int ID; TextureAtlas::TextureAtlas (){ atlas.clear(); + atlas.push_back(defaultTexture); } Texture TextureAtlas::GetTexture(ID id) { - if (atlas.size() >= id) { + if (atlas.size() <= id) { return atlas[0]; } return atlas[id]; @@ -26,6 +28,9 @@ void TextureAtlas::AddTexture(Texture texture){ Texture defaultTexture; TextureAtlas entityTextures; +TextureAtlas backgroundTextures; +TextureAtlas tileTextures; + void GenDefaultTexture(){ Image img = GenImageChecked(64, 64, 16, 16, PURPLE, BLACK); diff --git a/src/render/textures.hpp b/src/render/textures.hpp index 52a654d..3e49d52 100644 --- a/src/render/textures.hpp +++ b/src/render/textures.hpp @@ -19,7 +19,9 @@ struct TextureAtlas { extern Texture defaultTexture; extern TextureAtlas entityTextures; +extern TextureAtlas backgroundTextures; +extern TextureAtlas tileTextures; -void GenDefaulTexture(); +void GenDefaultTexture(); #endif // !TEXTURES_HPP