Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions scripts/api/entity/shiptemplatebasedobject.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ function Entity:onTakingDamage(callback)
if self.components.hull then self.components.hull.on_taking_damage = callback end
return self
end
--- Defines a function to call when this entity takes damage to its shields.
--- Passes the object taking damage to its shields and the instigator entity (or nil) to the function.
--- Example: stbo:onTakingShieldDamage(function(this_stbo,instigator) print(this_stbo:getCallSign() .. " had shields damaged by " .. instigator:getCallSign()) end)
function Entity:onTakingShieldDamage(callback)
if self.components.shields then self.components.shields.on_taking_damage = callback end
return self
end
--- Defines a function to call when this entity is destroyed by taking damage.
--- Passes the object taking damage and the instigator entity that delivered the destroying damage (or nil) to the function.
--- Example: stbo:onDestruction(function(this_stbo,instigator) print(this_stbo:getCallSign() .. " was destroyed by " .. instigator:getCallSign()) end)
Expand Down
4 changes: 3 additions & 1 deletion src/components/shields.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "ecs/entity.h"
#include "shipsystem.h"

#include "script/callback.h"

class Shields {
public:
Expand All @@ -28,4 +28,6 @@ class Shields {

ShipSystem& getSystemForIndex(int index);
float getDamageFactor(int index);

sp::script::Callback on_taking_damage;
};
1 change: 1 addition & 0 deletions src/script/components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ void initComponentScriptBindings()
BIND_MEMBER(Shields, calibration_delay);
BIND_MEMBER(Shields, frequency);
BIND_MEMBER(Shields, energy_use_per_second);
BIND_MEMBER(Shields, on_taking_damage);
BIND_ARRAY(Shields, entries);
BIND_ARRAY_MEMBER(Shields, entries, level);
BIND_ARRAY_MEMBER(Shields, entries, max);
Expand Down
10 changes: 10 additions & 0 deletions src/systems/damage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ void DamageSystem::applyDamage(sp::ecs::Entity entity, float amount, const Damag
{
amount = 0.0;
}

if (shields->on_taking_damage)
{
if (info.instigator)
{
LuaConsole::checkResult(shields->on_taking_damage.call<void>(entity, info.instigator));
} else {
LuaConsole::checkResult(shields->on_taking_damage.call<void>(entity));
}
}
}

if (amount > 0.0f)
Expand Down
Loading