Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
- General clean up
- Updated the README
  • Loading branch information
deanblackborough committed Jun 1, 2022
1 parent f21b78c commit 32b3f16
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 60 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,23 @@ To date, the features are limited, I'll try to keep this list updated, excuse th

- Create a Window via SDL
- Enitity component system via entt
- Display sprites and animated sprites
- Display sprites and optionally animate them with sprite sheets
- Movement, well, a velocity component and movement system
- AABB collision system.
- AABB collision system via BoxColliderComponent and CollisionSystem
- Events, currently used by the CollisionSystem and HealthSystem, CollisionSystem emits a CollisionEvent picked up by the HealthSystem
- Debug toggle to display box colliders
- Logging via spdlog
- Sprite library
- Clear project structure, engine code in src/Engine, base code in src/, game code should reside in src/Game

## Missing features

The ability to create a game, including but not limited to....

- An example game
- Render backgrounds
- Render UI
- Music and Sound effects
- Game settings
- Game editor
- Do anything with user input other than exit
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace Prune
* @param size A glm::vec2 to set the size for the box collider
* @param offset A glm::vec2 to set the offset for the box collider
*/
struct BoxCollider2DComponent
struct BoxColliderComponent
{
public:
glm::vec2 size;
glm::vec2 offset;

BoxCollider2DComponent(
BoxColliderComponent(
glm::vec2 size = glm::vec2(0),
glm::vec2 offset = glm::vec2(0)
) {
Expand Down
9 changes: 8 additions & 1 deletion Sandbox/src/Engine/Event/CollisionEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@

namespace Prune
{
/**
* The collision event fired by the collision system, includes a reference to the registry and
* the two entities what collided. The Health system picks up the events and deals with them accordingly
*/
class CollisionEvent : public Event
{
public:
entt::entity a;
entt::entity b;
entt::registry& registry;

CollisionEvent(entt::registry& registry, entt::entity a, entt::entity b) : registry(registry), a(a), b(b) {}
CollisionEvent(entt::registry& registry, entt::entity a, entt::entity b)
: registry(registry), a(a), b(b)
{
}

private:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#include "BoxCollider2DRenderSystem.h"
#include "BoxColliderRenderSystem.h"
#include "../../Component/TransformComponent.h"
#include "../../Component/BoxCollider2DComponent.h"
#include "../../Component/BoxColliderComponent.h"
#include "../../../Log/Log.h"

void Prune::BoxCollider2DRenderSystem::Render(
void Prune::BoxColliderRenderSystem::Render(
entt::registry& registry,
SDL_Renderer* renderer,
bool showBoxColliders2D
)
{
if (showBoxColliders2D)
{
auto view = registry.view<TransformComponent, BoxCollider2DComponent>();
auto view = registry.view<TransformComponent, BoxColliderComponent>();

for (auto entity : view) {
TransformComponent& transformComponent = view.get<TransformComponent>(entity);
BoxCollider2DComponent& boxCollider2DComponent = view.get<BoxCollider2DComponent>(entity);
BoxColliderComponent& boxCollider2DComponent = view.get<BoxColliderComponent>(entity);

SDL_Rect boxCollider2DRect = {
static_cast<int>(transformComponent.translation.x + boxCollider2DComponent.offset.x),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Prune
* Iterate all the entities entities with transform and boxcollider2d components and render the
* box collider
*/
class BoxCollider2DRenderSystem
class BoxColliderRenderSystem
{
public:
BoxCollider2DRenderSystem() = default;
~BoxCollider2DRenderSystem() = default;
BoxColliderRenderSystem() = default;
~BoxColliderRenderSystem() = default;

void Render(
entt::registry& registry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#include "BoxCollider2DCollisionSystem.h"
#include "CollisionSystem.h"
#include "../../Component/TransformComponent.h"
#include "../../Component/BoxCollider2DComponent.h"
#include "../../Component/BoxColliderComponent.h"
#include "../../Event/CollisionEvent.h"
#include "../../../Log/Log.h"

void Prune::BoxCollider2DCollisionSystem::Update(entt::registry& registry, std::unique_ptr<EventBus>& eventBus)
void Prune::CollisionSystem::Update(entt::registry& registry, std::unique_ptr<EventBus>& eventBus)
{
auto outer_view = registry.view<TransformComponent, BoxCollider2DComponent>();
auto outer_view = registry.view<TransformComponent, BoxColliderComponent>();

for (auto outer_entity : outer_view) {
TransformComponent& transformComponentOuter = outer_view.get<TransformComponent>(outer_entity);
BoxCollider2DComponent& boxCollider2DComponentOuter = outer_view.get<BoxCollider2DComponent>(outer_entity);
BoxColliderComponent& boxCollider2DComponentOuter = outer_view.get<BoxColliderComponent>(outer_entity);

auto inner_view = registry.view<TransformComponent, BoxCollider2DComponent>();
auto inner_view = registry.view<TransformComponent, BoxColliderComponent>();

for (auto inner_entity : inner_view) {
TransformComponent& transformComponentInner = inner_view.get<TransformComponent>(inner_entity);
BoxCollider2DComponent& boxCollider2DComponentInner = inner_view.get<BoxCollider2DComponent>(inner_entity);
BoxColliderComponent& boxCollider2DComponentInner = inner_view.get<BoxColliderComponent>(inner_entity);

if (inner_entity == outer_entity)
{
Expand Down Expand Up @@ -45,7 +45,7 @@ void Prune::BoxCollider2DCollisionSystem::Update(entt::registry& registry, std::
}
};

bool Prune::BoxCollider2DCollisionSystem::checkForCollision(
bool Prune::CollisionSystem::checkForCollision(
int outerPositionX,
int outerPositionY,
int outerWidth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace Prune
* Detect collisions between box colliders, keeping things simple initially, using AABB collision,
* will add support for rotated box colliders down the line
*/
class BoxCollider2DCollisionSystem
class CollisionSystem
{
public:
BoxCollider2DCollisionSystem() = default;
~BoxCollider2DCollisionSystem() = default;
CollisionSystem() = default;
~CollisionSystem() = default;

void Update(entt::registry& registry, std::unique_ptr<EventBus>& eventBus);

Expand Down
21 changes: 0 additions & 21 deletions Sandbox/src/Engine/System/Update/DamageSystem.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "DamageSystem.h"
#include "HealthSystem.h"
#include "../../../Log/Log.h"

void Prune::DamageSystem::SubscribeToEvents(std::unique_ptr<EventBus>& eventBus)
void Prune::HealthSystem::SubscribeToEvents(std::unique_ptr<EventBus>& eventBus)
{
eventBus->SubscribeToEvent<CollisionEvent>(this, &DamageSystem::OnCollision);
eventBus->SubscribeToEvent<CollisionEvent>(this, &HealthSystem::OnCollision);
}

void Prune::DamageSystem::OnCollision(CollisionEvent& event)
void Prune::HealthSystem::OnCollision(CollisionEvent& event)
{
event.registry.destroy(event.a);
event.registry.destroy(event.b);
Expand Down
25 changes: 25 additions & 0 deletions Sandbox/src/Engine/System/Update/HealthSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <entt.hpp>
#include "../../../Event/EventBus.h"
#include "../../Event/CollisionEvent.h"

namespace Prune
{
/**
* Captures CollisionEvents and deals with them, for now, we simply delete the two entities that have collided,
* as the system expands we will deal with the actual 'health' of the entities once we have a health component
*/
class HealthSystem
{
public:
HealthSystem() = default;
~HealthSystem() = default;

void SubscribeToEvents(std::unique_ptr<EventBus>& eventBus);

void OnCollision(CollisionEvent& event);

private:
};
}
4 changes: 2 additions & 2 deletions Sandbox/src/Event/EventBus.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <map>
#include <typeindex>
#include <functional>
#include <list>
#include <map>
#include <typeindex>
#include "Event.h"

namespace Prune
Expand Down
18 changes: 9 additions & 9 deletions Sandbox/src/Game/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "Game.h"
#include "../Engine/Component/AnimatedSpriteComponent.h"
#include "../Engine/Component/BoxCollider2DComponent.h"
#include "../Engine/Component/BoxColliderComponent.h"
#include "../Engine/Component/SpriteComponent.h"
#include "../Engine/Component/TransformComponent.h"
#include "../Engine/Component/VelocityComponent.h"
#include "../Engine/System/Update/AnimatedSpriteSystem.h"
#include "../Engine/System/Update/BoxCollider2DCollisionSystem.h"
#include "../Engine/System/Update/DamageSystem.h"
#include "../Engine/System/Update/CollisionSystem.h"
#include "../Engine/System/Update/HealthSystem.h"
#include "../Engine/System/Update/MovementSystem.h"
#include "../Engine/System/Render/BoxCollider2DRenderSystem.h"
#include "../Engine/System/Render/BoxColliderRenderSystem.h"
#include "../Engine/System/Render/SpriteRenderSystem.h"

Prune::Game::Game()
Expand Down Expand Up @@ -101,13 +101,13 @@ void Prune::Game::CreateEntities()
m_Registry.emplace<TransformComponent>(plane_grey, glm::vec2(10, (300-16)), glm::vec2(1, 1));
m_Registry.emplace<VelocityComponent>(plane_grey, glm::vec2(100, 0));
m_Registry.emplace<SpriteComponent>(plane_grey, "plane-grey-right", 32, 32, 0, 0, 128, 128);
m_Registry.emplace<BoxCollider2DComponent>(plane_grey, glm::vec2(32, 32));
m_Registry.emplace<BoxColliderComponent>(plane_grey, glm::vec2(32, 32));

entt::entity plane_green = m_Registry.create();
m_Registry.emplace<TransformComponent>(plane_green, glm::vec2((800-10-32), (300 - 16)), glm::vec2(1, 1));
m_Registry.emplace<VelocityComponent>(plane_green, glm::vec2(-100, 0));
m_Registry.emplace<SpriteComponent>(plane_green, "plane-green-left", 32, 32, 0, 0, 128, 128);
m_Registry.emplace<BoxCollider2DComponent>(plane_green, glm::vec2(32, 32));
m_Registry.emplace<BoxColliderComponent>(plane_green, glm::vec2(32, 32));
}

void Prune::Game::AddSpritesToLibrary()
Expand All @@ -119,7 +119,7 @@ void Prune::Game::AddSpritesToLibrary()

void Prune::Game::RunSystems(double delta)
{
DamageSystem damageSystem = DamageSystem();
HealthSystem damageSystem = HealthSystem();
damageSystem.SubscribeToEvents(m_EventBus);

MovementSystem movementSystem = MovementSystem();
Expand All @@ -128,7 +128,7 @@ void Prune::Game::RunSystems(double delta)
AnimatedSpriteSystem animatedSpriteSystem = AnimatedSpriteSystem();
animatedSpriteSystem.Update(m_Registry);

BoxCollider2DCollisionSystem boxCollider2DCollisionSystem = BoxCollider2DCollisionSystem();
CollisionSystem boxCollider2DCollisionSystem = CollisionSystem();
boxCollider2DCollisionSystem.Update(m_Registry, m_EventBus);
}

Expand All @@ -137,7 +137,7 @@ void Prune::Game::RenderEntities()
SpriteRenderSystem spriteRenderSystem = SpriteRenderSystem();
spriteRenderSystem.Render(m_Registry, m_Renderer, m_SpriteLibrary);

BoxCollider2DRenderSystem boxCollider2DRenderSystem = BoxCollider2DRenderSystem();
BoxColliderRenderSystem boxCollider2DRenderSystem = BoxColliderRenderSystem();
boxCollider2DRenderSystem.Render(m_Registry, m_Renderer, m_ShowBoxColliders2D);
}

Expand Down

0 comments on commit 32b3f16

Please sign in to comment.