made the player a Entity and gave it a controller
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 257 B |
+10
-4
@@ -5,22 +5,28 @@
|
||||
#include <sys/types.h>
|
||||
#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;
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -10,19 +10,21 @@
|
||||
struct Entity;
|
||||
|
||||
|
||||
using ControllerFunc = std::function<void(Entity&)>;
|
||||
using ControllerFunc = std::function<void(Entity&, float)>;
|
||||
|
||||
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);
|
||||
|
||||
};
|
||||
|
||||
|
||||
+41
-26
@@ -1,13 +1,28 @@
|
||||
#include "../include/raylib-cpp.hpp"
|
||||
#include "entities/entitybase.hpp"
|
||||
#include "render/textures.hpp"
|
||||
#include <iostream>
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "../../include/raylib-cpp.hpp"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user