Skip to content

Commit

Permalink
Entity class.
Browse files Browse the repository at this point in the history
  • Loading branch information
FloofyPlasma committed Nov 27, 2024
1 parent 7c4ca80 commit 262871c
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 3 deletions.
15 changes: 15 additions & 0 deletions DEVLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,19 @@
I also just broke up some large functions into smaller ones.
No new behaviors today, just cleanups.

</details>

- <details>
<summary>November 27, 2024: Entity class</summary>

Finally got over a block of motivation, and impemented a basic entity class.
A root entity will be the "scene" and other objects will be added to it as children.
It will have several child entities in that entity to classify different layers, foreground, background etc.

<details>
<summary>Screenshots</summary>

![Scene drawn using the new entity class.](./screenshots/enityClassRender.png)
</details>

</details>
88 changes: 88 additions & 0 deletions include/Core/Entity.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,101 @@
#ifndef ENTITY_H
#define ENTITY_H

#include <string>
#include <unordered_map>

#include "Render/Sprite.h"

class Entity
{
public:
Entity();
virtual ~Entity();

/**
* @brief Add a child to this entity
*
* @param child
* @param name
*/
void AddChild(Entity* child, std::string name);

/**
* @brief Get the Child entity by name
*
* @param name
* @return Entity*
*/
Entity* GetChild(std::string name);

/**
* @brief Remove a child entity by name
*
* @param name
*/
void RemoveChild(std::string name);

/**
* @brief Remove all child entities
*
*/
void RemoveAllChildren();

/**
* @brief Update the entity
* @note This calls update on **ALL** children
*
* @param delta
*/
virtual void Update(float delta);

/**
* @brief Render the sprite
* @note This calls render on **ALL** children
*
*/
virtual void Render();

/**
* @brief Set the Position
*
* @param position
*/
void SetPosition(Vec2 position);

/**
* @brief Set the Scale
*
* @param scale
*/
void SetScale(Vec2 scale);

/**
* @brief Set the Sprite
*
* @param sprite
*/
void SetSprite(Sprite sprite);

protected:
/**
* @brief Update child entities
*
* @param delta
*/
void UpdateChildren(float delta);

/**
* @brief Render child entities
*
*/
void RenderChildren();
std::unordered_map<std::string, Entity*> children;

private:
Sprite sprite;
Vec2 position;
Vec2 scale;
};

#endif
Binary file added screenshots/enityClassRender.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 55 additions & 1 deletion src/Core/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,58 @@

Entity::Entity() { }

Entity::~Entity() { }
Entity::~Entity() { }

void Entity::AddChild(Entity* child, std::string name)
{
if (this->children.count(name) == 0)
{
this->children[name] = child;
}
else
{
// TODO: Error(?)
// We already have a child with this name
}
}

Entity* Entity::GetChild(std::string name) { return this->children[name]; }

void Entity::RemoveChild(std::string name) { this->children.erase(name); }

void Entity::RemoveAllChildren() { this->children.clear(); }

void Entity::UpdateChildren(float delta)
{
for (auto& child : this->children)
{
child.second->Update(delta);
}
}

void Entity::RenderChildren()
{
for (auto& child : this->children)
{
child.second->Render();
}
}


void Entity::Update(float delta)
{
this->UpdateChildren(delta);
}


void Entity::Render()
{
this->sprite.Draw(this->position, this->scale);
this->RenderChildren();
}

void Entity::SetPosition(Vec2 position) { this->position = position; }

void Entity::SetScale(Vec2 scale) { this->scale = scale; }

void Entity::SetSprite(Sprite sprite) { this->sprite = sprite; }
18 changes: 16 additions & 2 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Render/Surface.h"
#include "Render/Sprite.h"
#include "Core/Resources.h"
#include "Core/Entity.h"

static SDL_Rect testRect = { 205, 205, 190, 190 };
static SDL_Rect testRect2 = { 200, 200, 200, 200 };
Expand All @@ -12,6 +13,8 @@ static SDL_Rect testRect3 = { 300, 250, 400, 400 };
static SDL_Texture* cursor = nullptr;

static Sprite testSprite;
static Entity testEntity;
static Entity testChild;

void Engine::OnInit()
{
Expand All @@ -24,6 +27,10 @@ void Engine::OnInit()
testSprite.SetFrameLimits(1, 10);
testSprite.SetFrameSize(Vec2(32.0f, 32.0f));
testSprite.SetCurrentFrame(1);

testEntity.SetSprite(testSprite);
testChild.SetSprite(testSprite);
testEntity.AddChild(&testChild, "Child test");
}

void Engine::OnEvent(SDL_Event *event, const Uint8 *keyboardState)
Expand All @@ -43,11 +50,18 @@ void Engine::OnRender()
// Surface::DrawRect(&testRect3, COLOR_WHITE, true);
Surface::DrawRect(&testRect2, false);

testSprite.Draw(Vec2(400.0f, 400.0f), Vec2(64.0f, 64.0f));
/*testSprite.Draw(Vec2(400.0f, 400.0f), Vec2(64.0f, 64.0f));
testSprite.Draw(Vec2(450.0f, 450.0f), Vec2(20.0f, 60.0f));
testSprite.Draw(Vec2(480.0f, 470.0f), Vec2(40.0f, 100.0f));
testSprite.Draw(Vec2(490.0f, 420.0f), Vec2(32.0f, 20.0f));
testSprite.Draw(Vec2(420.0f, 480.0f), Vec2(32.0f, 32.0f));
testSprite.Draw(Vec2(420.0f, 480.0f), Vec2(32.0f, 32.0f));*/

testEntity.SetPosition(Vec2(500.0f, 500.0f));
testEntity.SetScale(Vec2(64.0f, 64.0f));
testChild.SetPosition(Vec2(400.0f, 400.0f));
testChild.SetScale(Vec2(64.0f, 64.0f));

testEntity.Render();
}

void Engine::OnCleanup()
Expand Down

0 comments on commit 262871c

Please sign in to comment.