Made the hurt hazard work better.
This commit is contained in:
@ -7,5 +7,41 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
HurtHazard::HurtHazard(SceneItem* item) : SceneItemComponent(item) {}
|
||||
HurtHazard::HurtHazard(SceneItem* item) :
|
||||
trigger(nullptr),
|
||||
SceneItemComponent(item)
|
||||
{
|
||||
}
|
||||
|
||||
void HurtHazard::onStart() {
|
||||
this->evtTriggerEnter = [&]{};
|
||||
|
||||
useEffect([&]{
|
||||
this->evtTriggerEnter();
|
||||
if(this->trigger == nullptr) return;
|
||||
|
||||
this->evtTriggerEnter = useEvent([&](CharacterController2D *controller) {
|
||||
// Check faction(s)
|
||||
auto otherFaction = controller->item->getComponent<EntityFaction>();
|
||||
if(otherFaction != nullptr && faction == otherFaction->faction) return;
|
||||
|
||||
// Check health
|
||||
auto otherHealth = controller->item->getComponent<EntityHealth>();
|
||||
if(otherHealth == nullptr) return;
|
||||
|
||||
// Damage
|
||||
if(otherHealth->isInvincible()) return;
|
||||
|
||||
glm::vec2 back = (
|
||||
physics3Dto2D(this->transform->getWorldPosition()) -
|
||||
physics3Dto2D(controller->transform->getWorldPosition())
|
||||
);
|
||||
controller->velocity = back * -hitKnockback;
|
||||
otherHealth->damage({
|
||||
.amount = damage
|
||||
});
|
||||
|
||||
std::cout << "Trigger" << std::endl;
|
||||
}, this->trigger->eventTriggerEnter);
|
||||
}, this->trigger)();
|
||||
}
|
@ -6,13 +6,26 @@
|
||||
#pragma once
|
||||
#include "scene/SceneItemComponent.hpp"
|
||||
#include "scene/components/entity/EntityFaction.hpp"
|
||||
#include "scene/components/physics/2d/TriggerController2D.hpp"
|
||||
#include "scene/components/entity/EntityHealth.hpp"
|
||||
#include "scene/components/entity/EntityFaction.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class HurtHazard : public SceneItemComponent {
|
||||
private:
|
||||
std::function<void()> evtTriggerEnter;
|
||||
|
||||
public:
|
||||
// @optional
|
||||
float_t hitKnockback = 20.0f;
|
||||
// @optional
|
||||
int32_t damage = 1;
|
||||
enum Faction faction = FACTION_NONE;
|
||||
// @optional
|
||||
StateProperty<TriggerController2D*> trigger;
|
||||
|
||||
HurtHazard(SceneItem* item);
|
||||
|
||||
void onStart() override;
|
||||
};
|
||||
}
|
@ -11,13 +11,6 @@ EntityHealth::EntityHealth(SceneItem* item) : SceneItemComponent(item) {
|
||||
|
||||
}
|
||||
|
||||
std::vector<SceneItemComponent*> EntityHealth::getDependencies() {
|
||||
return {
|
||||
this->characterController = this->item->getComponent<CharacterController2D>(),
|
||||
this->entityFaction = this->item->getComponent<EntityFaction>()
|
||||
};
|
||||
}
|
||||
|
||||
void EntityHealth::onStart() {
|
||||
// Update
|
||||
useEvent([&](float_t delta){
|
||||
@ -32,29 +25,31 @@ void EntityHealth::onStart() {
|
||||
|
||||
|
||||
// Hurt Hazard Processing
|
||||
useEvent([&](struct CharacterController2DCollisionEventInfo info) {
|
||||
if(this->isInvincible()) return;
|
||||
// if(this->characterController != nullptr) {
|
||||
// useEvent([&](struct CharacterController2DCollisionEventInfo info) {
|
||||
// if(this->isInvincible()) return;
|
||||
|
||||
auto hurtHazard = info.collider->item->getComponent<HurtHazard>();
|
||||
if(hurtHazard == nullptr) return;
|
||||
if(
|
||||
entityFaction != nullptr &&
|
||||
hurtHazard->faction == this->entityFaction->faction
|
||||
) return;
|
||||
// auto hurtHazard = info.collider->item->getComponent<HurtHazard>();
|
||||
// if(hurtHazard == nullptr) return;
|
||||
// if(
|
||||
// entityFaction != nullptr &&
|
||||
// hurtHazard->faction == this->entityFaction->faction
|
||||
// ) return;
|
||||
|
||||
glm::vec2 back = glm::vec2(
|
||||
info.collider->transform->getLocalPosition().x,
|
||||
info.collider->transform->getLocalPosition().z
|
||||
) - glm::vec2(
|
||||
this->transform->getLocalPosition().x,
|
||||
this->transform->getLocalPosition().z
|
||||
);
|
||||
// glm::vec2 back = glm::vec2(
|
||||
// info.collider->transform->getLocalPosition().x,
|
||||
// info.collider->transform->getLocalPosition().z
|
||||
// ) - glm::vec2(
|
||||
// this->transform->getLocalPosition().x,
|
||||
// this->transform->getLocalPosition().z
|
||||
// );
|
||||
|
||||
this->characterController->velocity = back * -hitKnockback;
|
||||
this->damage({
|
||||
.amount = hurtHazard->damage
|
||||
});
|
||||
}, this->characterController->eventCollision);
|
||||
// this->characterController->velocity = back * -hitKnockback;
|
||||
// this->damage({
|
||||
// .amount = hurtHazard->damage
|
||||
// });
|
||||
// }, this->characterController->eventCollision);
|
||||
// }
|
||||
}
|
||||
|
||||
bool_t EntityHealth::isInvincible() {
|
||||
|
@ -5,9 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "scene/SceneItemComponent.hpp"
|
||||
#include "scene/components/entity/EntityFaction.hpp"
|
||||
#include "scene/components/physics/2d/CharacterController2D.hpp"
|
||||
#include "scene/components/HurtHazard.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct DamageInformation {
|
||||
@ -17,10 +15,6 @@ namespace Dawn {
|
||||
};
|
||||
|
||||
class EntityHealth : public SceneItemComponent {
|
||||
protected:
|
||||
EntityFaction *entityFaction = nullptr;
|
||||
CharacterController2D *characterController = nullptr;
|
||||
|
||||
public:
|
||||
// @optional
|
||||
int32_t health = 10;
|
||||
@ -30,11 +24,8 @@ namespace Dawn {
|
||||
float_t invincibleTime = 0.0f;
|
||||
// @optional
|
||||
float_t stunTime = 0.0f;
|
||||
// @optional
|
||||
float_t hitKnockback = 20.0f;
|
||||
|
||||
EntityHealth(SceneItem* item);
|
||||
std::vector<SceneItemComponent*> getDependencies() override;
|
||||
void onStart() override;
|
||||
|
||||
/**
|
||||
|
@ -24,10 +24,8 @@ namespace Dawn {
|
||||
|
||||
auto player = Player::create(this);
|
||||
|
||||
auto wall = Wall::create(this);
|
||||
|
||||
// auto urchin = Urchin::create(this);
|
||||
// urchin->transform.setLocalPosition(glm::vec3(0, 0, -5));
|
||||
auto urchin = Urchin::create(this);
|
||||
urchin->transform.setLocalPosition(glm::vec3(0, 0, -5));
|
||||
|
||||
// auto crab = Crab::create(this);
|
||||
// crab->transform.setLocalPosition(glm::vec3(3, 0, 0));
|
||||
|
Reference in New Issue
Block a user