Commit ee5c6c67 authored by louiz’'s avatar louiz’

ProjectileWork has a callback on impact instead of a damage value

parent 0bd65a57
......@@ -41,7 +41,12 @@ bool AttackTask::tick(World* world)
// Only if ranged entity
Entity* projectile = world->do_new_entity(1, this->location->position(), 1);
projectile->set_work(
std::make_unique<ProjectileWork>(projectile, this->target, 7u));
std::make_unique<ProjectileWork>(projectile, this->target, [](Entity* target) -> void
{
Health* health = target->get<Health>();
assert(health);
health->add(-10);
}));
}
if (this->backswing)
{
......
......@@ -8,10 +8,10 @@
#include <world/world_callbacks.hpp>
ProjectileWork::ProjectileWork(Entity* entity, std::weak_ptr<Entity> target,
const int dmg):
std::function<void(Entity*)> callback):
Work(entity),
target(target),
dmg(dmg)
callback(callback)
{
}
......@@ -26,11 +26,10 @@ bool ProjectileWork::tick(World* world)
if (!this->target.expired())
{
// impact
world->callbacks->impact(this->entity, this->target.lock().get());
Health* health = target.lock()->get<Health>();
assert(health);
health->add(-this->dmg);
// callback
Entity* targetp = this->target.lock().get();
if (this->callback)
this->callback(targetp);
world->callbacks->impact(this->entity, targetp);
}
return true;
}
......
#ifndef PROJECTILE_WORK_HPP_INCLUDED
#define PROJECTILE_WORK_HPP_INCLUDED
#include <memory>
#include <world/work.hpp>
#include <functional>
#include <memory>
/**
* Simple Work where the entity just follows (without path finding or
* collision or anything) an other entity, until the entity is reach (in
......@@ -18,7 +19,7 @@ class ProjectileWork: public Work
{
public:
ProjectileWork(Entity* entity, std::weak_ptr<Entity> target,
const int dmg);
std::function<void(Entity*)> callback);
~ProjectileWork() = default;
bool tick(World* world) override final;
......@@ -28,9 +29,9 @@ private:
*/
std::weak_ptr<Entity> target;
/**
* Info about the event when we reach our goal.
* Called when the entity hits the target, if there is one
*/
const int dmg;
std::function<void(Entity*)> callback;
ProjectileWork(const ProjectileWork&) = delete;
ProjectileWork(ProjectileWork&&) = delete;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment