made the player a Entity and gave it a controller

This commit is contained in:
nosch
2025-08-31 16:36:03 +02:00
parent 84cd82c8c0
commit 9f1374c0b1
6 changed files with 65 additions and 35 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 257 B

+10 -4
View File
@@ -5,22 +5,28 @@
#include <sys/types.h> #include <sys/types.h>
#include <iostream> #include <iostream>
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; id = Id;
spriteid = 0;
rect = Rect; rect = Rect;
health = h; health = h;
maxhealth = maxh; maxhealth = maxh;
nametag = n; nametag = n;
controller = emptycontroller;
} }
void Entity::Update() { void Entity::Update(float deltatime) {
if (controller) { if (controller) {
controller(*this); controller(*this, deltatime);
}else { }else {
std::cout << "Entity is missing an controller, doing nothing.\n"; std::cout << "Entity is missing an controller, doing nothing.\n";
} }
} }
void Entity::Draw() { 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);
} }
+5 -3
View File
@@ -10,19 +10,21 @@
struct Entity; struct Entity;
using ControllerFunc = std::function<void(Entity&)>; using ControllerFunc = std::function<void(Entity&, float)>;
struct Entity { struct Entity {
ID id; ID id;
ID spriteid;
raylib::Rectangle rect; raylib::Rectangle rect;
int health; int health;
int maxhealth; int maxhealth;
std::string nametag; std::string nametag;
ControllerFunc controller; ControllerFunc controller;
void Update(); Vector2 velocity;
void Update(float deltatime);
void Draw(); 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);
}; };
+40 -25
View File
@@ -1,13 +1,28 @@
#include "../include/raylib-cpp.hpp" #include "../include/raylib-cpp.hpp"
#include "entities/entitybase.hpp" #include "entities/entitybase.hpp"
#include "render/textures.hpp"
#include <iostream> #include <iostream>
struct player { void PlayerController(Entity &e, float deltatime) {
float x; raylib::Vector2 velocity = {0,0};
float y; raylib::Vector2 acceleration = {0,0};
float width; if (IsKeyDown(KEY_W)) acceleration.y--;
float height; 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 main() {
int screenWidth = 1920; int screenWidth = 1920;
@@ -17,42 +32,42 @@ int main() {
SetTargetFPS(60); SetTargetFPS(60);
player Player; GenDefaultTexture();
Player.x = 100.0f;
Player.y = 100.0f; raylib::Rectangle playerrect{screenWidth / 2, screenHeight / 2,64,64};
Player.width = 50.0f; Entity player{1, playerrect, "MainPlayer", 100, 100};
Player.height = 50.0f; player.controller = PlayerController;
player.spriteid = 1;
entityTextures.LoadImage("./src/assets/playersprite.png");
//tileTextures.LoadImage("./src/assets/BasicTile.png");
Camera2D camera = { 0 }; Camera2D camera = { 0 };
camera.target = { Player.x, Player.y };
camera.offset = { screenWidth/2.0f, screenHeight/2.0f }; camera.offset = { screenWidth/2.0f, screenHeight/2.0f };
camera.rotation = 0.0f; camera.rotation = 0.0f;
camera.zoom = 1.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()) while (!window.ShouldClose())
{ {
float deltatime = GetFrameTime();
player.Update(deltatime);
BeginDrawing(); BeginDrawing();
window.ClearBackground(BLACK); window.ClearBackground(BLACK);
BeginMode2D(camera); BeginMode2D(camera);
if (IsKeyDown(KEY_S)) Player.y += 1; camera.target = { player.rect.x, player.rect.y };
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 };
if (IsKeyDown(KEY_Q)) camera.zoom += 0.1f; if (IsKeyDown(KEY_Q)) camera.zoom += 0.1f;
DrawText("Middle", 190, 200, 20, WHITE); DrawText("Middle", 190, 200, 20, WHITE);
Texture bg = tileTextures.GetTexture(1);
std::cout << bg.height;
// Infinite Background // Infinite Background
int bh = bg.height; int bh = bg.height;
int bw = bg.width; int bw = bg.width;
@@ -67,10 +82,10 @@ int main() {
-offsetY + j * bh, -offsetY + j * bh,
WHITE WHITE
); );
}
} }
}
logo.Draw(Player.x, Player.y); player.Draw();
EndMode2D(); EndMode2D();
EndDrawing(); EndDrawing();
+6 -1
View File
@@ -1,4 +1,5 @@
#include "../../include/raylib-cpp.hpp" #include "../../include/raylib-cpp.hpp"
#include <iostream>
#include <vector> #include <vector>
#include "textures.hpp" #include "textures.hpp"
using namespace std; using namespace std;
@@ -7,10 +8,11 @@ typedef int ID;
TextureAtlas::TextureAtlas (){ TextureAtlas::TextureAtlas (){
atlas.clear(); atlas.clear();
atlas.push_back(defaultTexture);
} }
Texture TextureAtlas::GetTexture(ID id) { Texture TextureAtlas::GetTexture(ID id) {
if (atlas.size() >= id) { if (atlas.size() <= id) {
return atlas[0]; return atlas[0];
} }
return atlas[id]; return atlas[id];
@@ -26,6 +28,9 @@ void TextureAtlas::AddTexture(Texture texture){
Texture defaultTexture; Texture defaultTexture;
TextureAtlas entityTextures; TextureAtlas entityTextures;
TextureAtlas backgroundTextures;
TextureAtlas tileTextures;
void GenDefaultTexture(){ void GenDefaultTexture(){
Image img = GenImageChecked(64, 64, 16, 16, PURPLE, BLACK); Image img = GenImageChecked(64, 64, 16, 16, PURPLE, BLACK);
+3 -1
View File
@@ -19,7 +19,9 @@ struct TextureAtlas {
extern Texture defaultTexture; extern Texture defaultTexture;
extern TextureAtlas entityTextures; extern TextureAtlas entityTextures;
extern TextureAtlas backgroundTextures;
extern TextureAtlas tileTextures;
void GenDefaulTexture(); void GenDefaultTexture();
#endif // !TEXTURES_HPP #endif // !TEXTURES_HPP