Your code is a little bit strange. Basically this is what it does:
- if the sensor (collision sensor) detects any object, you look for all objects with property “5.56mm” and decrease their health.
- when the sensor triggers (regardless if positive or not) you decrease the heath of all objects with property “9mm” and “12mm”.
basically when anything hits that object (and when stopping to hit) you decrease the health of all bullets (I assume these should be bullets).
First you need to decide what registers the impact. That can be the projectile or the hit target. As you let the target detect the impact we will continue from there.
Assumptions:
- more than one projectile can collide at the same time
- projectiles have a property that tells the damage e.g. “physical damage”:10
- projectiles are recognized by a property e.g. “projectile”
This makes projectile detection much easier, as you just need to filter for “projectile”.
This means when the sensor detects anything you check all detected projectiles:
projectileSensor = controller.sensors["detect projectiles"]
for projectile in projectileSensor.hitObjectList:
# Get the damage of the bullet. (it can depend on the speed).
receivedPhysicalDamage = projectile["physical damage"]
# Get other properties of the bullet/sender that modify the damage
armoryPenetration = projectile["armory penetration"]
# Let the projectile know it transferred the damage
# The projectile should deal with the consequences within the next frame.
# E.g. it can end itself or it continue and damges more objects
# We do not care here are this code deals with target damage only
projectile["hit"] = True
# Now deal with the damage. Damage is NOT health decrease.
# damage will increased or reduced by further attributes e.g. armory, armor penetration, resistances ...
finalPhysicalDamage =receivedPhysicalDamage / 1 - 0.0, owner.get("armory", 0.0) # armoryPenetration somewhere
# now we know the damage so we can adjust the health:
health = owner.get("health", 0.0)
health = max(0.0, health - finalPhysicalDamage)
owner["health"] = health
if health<= 0.0:
.... die()
You calculate the damage of each single projectile.
The same belongs to non-projectiles like swords, impacts, explosions, gas.
You can have different types of damages like physical, magical, chemical, fire, ice, radiation.
You can have different duration with different damage rates: immediately, one frame, damage over time, area damage, health percentage damage, impact dependent damage, multi-hit damage, amplified damage, reduced damage, heal …
You see there are an awful lot of options.