Start work on the HitboxComponent.
This commit is contained in:
parent
9013efcf31
commit
63b2d506ba
|
@ -10,6 +10,7 @@
|
|||
#include <simpleengine/components/movement/movement_component.h>
|
||||
#include <simpleengine/components/ssma_component.h>
|
||||
#include <simpleengine/events/entity_event.h>
|
||||
#include <simpleengine/components/hitbox_component.h>
|
||||
#include <simpleengine/animation.h>
|
||||
|
||||
#include <chrono>
|
||||
|
@ -22,7 +23,6 @@ private:
|
|||
float movement_speed = 130;
|
||||
sf::Vector2u window_size;
|
||||
|
||||
std::unique_ptr<simpleengine::SideScrollerMovementAnimationComponent> 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<simpleengine::SideScrollerMovementAnimationComponent>(*this, sprite,
|
||||
auto move_anim_component = std::make_unique<simpleengine::SideScrollerMovementAnimationComponent>(*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<simpleengine::HitboxComponent>(*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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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 <SFML/Graphics.hpp>
|
||||
|
||||
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
|
|
@ -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 <iostream>
|
||||
|
||||
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<simpleengine::SideScrollerMovementAnimationComponent>()) {
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue