| 59 | DamageManager::DamageManager(World* world, ConnectionId connectionId) : m_world(world), m_connectionId(connectionId) {} |
| 60 | |
| 61 | void DamageManager::update(float dt) { |
| 62 | float const DefaultDamageTimeout = 1.0f; |
| 63 | |
| 64 | auto damageIt = makeSMutableMapIterator(m_recentEntityDamages); |
| 65 | while (damageIt.hasNext()) { |
| 66 | auto& events = damageIt.next().second; |
| 67 | auto eventIt = makeSMutableIterator(events); |
| 68 | while (eventIt.hasNext()) { |
| 69 | auto& event = eventIt.next(); |
| 70 | event.timeout -= dt; |
| 71 | auto entityIdTimeoutGroup = event.timeoutGroup.maybe<EntityId>(); |
| 72 | if (event.timeout <= 0.0f || (entityIdTimeoutGroup && !m_world->entity(*entityIdTimeoutGroup))) |
| 73 | eventIt.remove(); |
| 74 | } |
| 75 | if (events.empty()) |
| 76 | damageIt.remove(); |
| 77 | } |
| 78 | |
| 79 | m_world->forAllEntities([&](EntityPtr const& causingEntity) { |
| 80 | for (auto& damageSource : causingEntity->damageSources()) { |
| 81 | if (damageSource.trackSourceEntity) |
| 82 | damageSource.translate(causingEntity->position()); |
| 83 | |
| 84 | if (auto poly = damageSource.damageArea.ptr<PolyF>()) |
| 85 | SpatialLogger::logPoly("world", *poly, Color::Orange.toRgba()); |
| 86 | else if (auto line = damageSource.damageArea.ptr<Line2F>()) |
| 87 | SpatialLogger::logLine("world", *line, Color::Orange.toRgba()); |
| 88 | |
| 89 | for (auto const& hitResultPair : queryHit(damageSource, causingEntity->entityId())) { |
| 90 | auto targetEntity = m_world->entity(hitResultPair.first); |
| 91 | if (!isAuthoritative(causingEntity, targetEntity)) |
| 92 | continue; |
| 93 | |
| 94 | auto& eventList = m_recentEntityDamages[hitResultPair.first]; |
| 95 | // Guard against rapidly repeating damages by either the causing |
| 96 | // entity id, or optionally the repeat group if specified. |
| 97 | bool allowDamage = true; |
| 98 | for (auto const& event : eventList) { |
| 99 | if (damageSource.damageRepeatGroup) { |
| 100 | if (event.timeoutGroup == *damageSource.damageRepeatGroup) |
| 101 | allowDamage = false; |
| 102 | } else { |
| 103 | if (event.timeoutGroup == causingEntity->entityId()) |
| 104 | allowDamage = false; |
| 105 | } |
| 106 | } |
| 107 | if (allowDamage) { |
| 108 | float timeout = damageSource.damageRepeatTimeout.value(DefaultDamageTimeout); |
| 109 | if (damageSource.damageRepeatGroup) |
| 110 | eventList.append({*damageSource.damageRepeatGroup, timeout}); |
| 111 | else |
| 112 | eventList.append({causingEntity->entityId(), timeout}); |
| 113 | |
| 114 | auto damageRequest = DamageRequest(hitResultPair.second, damageSource.damageType, damageSource.damage, |
| 115 | damageSource.knockbackMomentum(m_world->geometry(), targetEntity->position()), |
| 116 | damageSource.sourceEntityId, damageSource.damageSourceKind, damageSource.statusEffects); |
| 117 | addHitRequest({causingEntity->entityId(), targetEntity->entityId(), damageRequest}); |
| 118 |
nothing calls this directly
no test coverage detected