diff --git a/scripts/microbe_stage/microbe.as b/scripts/microbe_stage/microbe.as index 41644fac989..84f20835330 100644 --- a/scripts/microbe_stage/microbe.as +++ b/scripts/microbe_stage/microbe.as @@ -129,6 +129,7 @@ class MicrobeComponent : ScriptComponent{ // TODO: initialize float hitpoints = DEFAULT_HEALTH; + float previousHitpoints = DEFAULT_HEALTH; float maxHitpoints = DEFAULT_HEALTH; bool dead = false; uint deathTimer = 0; @@ -381,7 +382,7 @@ class MicrobeSystem : ScriptSystem{ atpDamage(microbeEntity); } - // Handle hitpoints + // Handle hitpoints if((microbeComponent.hitpoints < microbeComponent.maxHitpoints)) { if(MicrobeOperations::getCompoundAmount(world, microbeEntity, @@ -504,6 +505,11 @@ class MicrobeSystem : ScriptSystem{ applyCellMovement(components, logicTime); compoundAbsorberComponent.setAbsorbtionCapacity(microbeComponent.capacity); + + if(microbeComponent.hitpoints != microbeComponent.previousHitpoints) + membraneComponent.setHealthFraction(microbeComponent.hitpoints / microbeComponent.maxHitpoints); + + microbeComponent.previousHitpoints = microbeComponent.hitpoints; } private void updateDeadCell(MicrobeSystemCached@ &in components, uint logicTime) diff --git a/src/microbe_stage/membrane_system.cpp b/src/microbe_stage/membrane_system.cpp index 024a6b51710..72c4cddf0b6 100644 --- a/src/microbe_stage/membrane_system.cpp +++ b/src/microbe_stage/membrane_system.cpp @@ -10,6 +10,7 @@ #include #include +#include #include using namespace thrive; @@ -198,10 +199,21 @@ void ->getPass(0) ->getFragmentProgramParameters() ->setNamedConstant("membraneColour", colour); + coloredMaterial->compile(); + } +} + +void + MembraneComponent::setHealthFraction(float value) +{ + healthFraction = std::clamp(value, 0.0f, 1.0f); + + // If we already have created a material we need to re-apply it + if(coloredMaterial) { coloredMaterial->getTechnique(0) ->getPass(0) - ->getTextureUnitState(0) - ->setHardwareGammaEnabled(true); + ->getFragmentProgramParameters() + ->setNamedConstant("healthPercentage", healthFraction); coloredMaterial->compile(); } } @@ -326,6 +338,18 @@ void ->getPass(0) ->getFragmentProgramParameters() ->setNamedConstant("membraneColour", colour); + + coloredMaterial->getTechnique(0) + ->getPass(0) + ->getFragmentProgramParameters() + ->setNamedConstant("healthPercentage", healthFraction); + coloredMaterial->compile(); + + coloredMaterial->getTechnique(0) + ->getPass(0) + ->getTextureUnitState(0) + ->setHardwareGammaEnabled(true); + coloredMaterial->compile(); } diff --git a/src/microbe_stage/membrane_system.h b/src/microbe_stage/membrane_system.h index dcf0b4f6c12..54f2f66836f 100644 --- a/src/microbe_stage/membrane_system.h +++ b/src/microbe_stage/membrane_system.h @@ -53,6 +53,10 @@ class MembraneComponent : public Leviathan::Component { void setColour(const Float4& value); + //! Should set the health percentage. + void + setHealthFraction(float value); + //! Returns the last set colour Float4 getColour() const; @@ -213,6 +217,10 @@ class MembraneComponent : public Leviathan::Component { //! The amount of compounds stored in the membrane. int compoundAmount = 0; + // The health percentage of a cell, in the range [0.0, 1.0], used to get + // damage effects in the membrane. + float healthFraction = 1.0; + private: }; diff --git a/src/scripting/script_initializer.cpp b/src/scripting/script_initializer.cpp index b250a7a5197..7263681dcdf 100644 --- a/src/scripting/script_initializer.cpp +++ b/src/scripting/script_initializer.cpp @@ -575,6 +575,13 @@ bool ANGELSCRIPT_REGISTERFAIL; } + if(engine->RegisterObjectMethod("MembraneComponent", + "void setHealthFraction(float value)", + asMETHOD(MembraneComponent, setHealthFraction), + asCALL_THISCALL) < 0) { + ANGELSCRIPT_REGISTERFAIL; + } + if(engine->RegisterEnum("MEMBRANE_TYPE") < 0) { ANGELSCRIPT_REGISTERFAIL;