diff --git a/examples/animation/src/main.cpp b/examples/animation/src/main.cpp index af6b203..2aaeea8 100644 --- a/examples/animation/src/main.cpp +++ b/examples/animation/src/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -22,7 +23,6 @@ private: float movement_speed = 130; sf::Vector2u window_size; - std::unique_ptr move_anim_component; public: explicit PlayerEntity(sf::Vector2u window_size) : window_size(window_size) { texture.loadFromFile("player_sheet.png"); @@ -30,13 +30,17 @@ public: sprite.setTexture(texture); sprite.setScale(.7, .7); - move_anim_component = std::make_unique(*this, sprite, + auto move_anim_component = std::make_unique(*this, sprite, texture, movement_speed, 5, 1.1); move_anim_component->SetAnimation(simpleengine::MovementAnimationType::WALK_LEFT, 8, 0, 8, 9, 8, 128, 128); move_anim_component->SetAnimation(simpleengine::MovementAnimationType::IDLE_LEFT, 20, 0, 0, 6, 0, 128, 128); AddComponent(std::move(move_anim_component)); + + AddComponent(std::make_unique(*this, sprite, + 0, 0, + sprite.getGlobalBounds().width, sprite.getGlobalBounds().height)); } ~PlayerEntity() override { @@ -62,6 +66,8 @@ public: } void Render(sf::RenderTarget* target) override { + Entity::Render(target); + target->draw(sprite); } }; diff --git a/include/simpleengine/components/hitbox_component.h b/include/simpleengine/components/hitbox_component.h new file mode 100644 index 0000000..4745c95 --- /dev/null +++ b/include/simpleengine/components/hitbox_component.h @@ -0,0 +1,33 @@ +// +// Created by SeanOMik on 7/6/2020. +// Github: https://github.com/SeanOMik +// Email: seanomik@gmail.com +// + +#ifndef SIMPLEENGINE_HITBOX_COMPONENT_H +#define SIMPLEENGINE_HITBOX_COMPONENT_H + +#include "../component.h" + +#include + +namespace simpleengine { + class HitboxComponent : public Component { + private: + sf::Sprite& sprite; + sf::RectangleShape hitbox; + + float offset_x; + float offset_y; + public: + HitboxComponent(Entity& owning_entity, sf::Sprite& sprite, float sprite_offset_x, float sprite_offset_y, + float width, float height); + + bool DoesIntersect(const sf::FloatRect& rect); + + void Update(const float &delta_time) override; + void Render(sf::RenderTarget *target) override; + }; +} + +#endif //SIMPLEENGINE_HITBOX_COMPONENT_H diff --git a/src/components/hitbox_component.cpp b/src/components/hitbox_component.cpp new file mode 100644 index 0000000..d541637 --- /dev/null +++ b/src/components/hitbox_component.cpp @@ -0,0 +1,39 @@ +// +// Created by SeanOMik on 7/6/2020. +// Github: https://github.com/SeanOMik +// Email: seanomik@gmail.com +// + +#include "components/hitbox_component.h" +#include "components/ssma_component.h" +#include "entity.h" + +#include + +simpleengine::HitboxComponent::HitboxComponent(Entity& owning_entity, sf::Sprite &sprite, float sprite_offset_x, float sprite_offset_y, + float width, float height) : Component(owning_entity), sprite(sprite), offset_x(sprite_offset_x), offset_y(sprite_offset_y) { + + hitbox.setPosition(sprite.getPosition().x + sprite_offset_x, sprite.getPosition().y + sprite_offset_y); + hitbox.setSize(sf::Vector2f(width, height)); + hitbox.setFillColor(sf::Color::Transparent); + hitbox.setOutlineThickness(1); + hitbox.setOutlineColor(sf::Color::Red); + + // SSMA Component set's the sprites texture offset so we need to set + // the offset to the origin if the entity has the SSMA component. + if (owning_entity.HasComponent()) { + offset_x += sprite.getGlobalBounds().width * -0.5f; + } +} + +bool simpleengine::HitboxComponent::DoesIntersect(const sf::FloatRect &rect) { + return hitbox.getGlobalBounds().intersects(rect); +} + +void simpleengine::HitboxComponent::Update(const float &delta_time) { + hitbox.setPosition(sprite.getPosition().x + offset_x, sprite.getPosition().y + offset_y); +} + +void simpleengine::HitboxComponent::Render(sf::RenderTarget *target) { + target->draw(hitbox); +}