Squaby Demo
import QtQuick 2.0
import Felgo 4.0
ComponentBase {
property real health: 100
property real healthInPercent: currentHealth*__healthInverse
property real __healthInverse: 1/health
property real currentHealth: health
property variant damages: {
"nailgun": { value: 20, type: "applySimple"},
"flamethrower": { value: 40, type: "perSecond"},
"taser": { value: 40, type: "perSecond"},
"tesla": { value: 40, type: "perSecond"}
}
property variant damageMultiplicators: {
"nailgun": 1,
"flamethrower": 1,
"taser": 1,
"tesla": 1
}
property bool autoDestroyEntityWhenDied: false
signal died
property bool __alreadyDied: false
Component.onCompleted: {
}
onHealthChanged: {
resetHealth();
}
onCurrentHealthChanged: {
if(currentHealth === health) {
resetHealth();
} else if(currentHealth<0.001) {
if(!__alreadyDied) {
__alreadyDied = true;
died();
if(autoDestroyEntityWhenDied) {
owningEntity.removeEntity();
}
}
currentHealth = 0;
}
}
function resetHealth() {
console.debug("HealthComponent: resetHealth called, setting currentHealth to health value:", health)
currentHealth = health;
__alreadyDied = false;
}
function hit(damageValue) {
__applyDamage(damageValue);
}
function hitWithAttackerIdAndType(attackerId, attackerType) {
var damageForAttacker = damages[attackerType].value;
if(damageForAttacker) {
__applyDamageComplex(attackerId, attackerType, damageForAttacker);
}
}
function hitByValue(attackerId, attackerType, damageValue) {
__applyDamageComplex(attackerId, attackerType, damageValue);
}
function __applyDamage(damage) {
currentHealth -= damage;
}
function __applyDamageComplex(attackerId, attackerType, damageForAttacker) {
var damageMultiplicatorForAttacker = damageMultiplicators[attackerType];