MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / update

Method update

source/game/StarDamageManager.cpp:61–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59DamageManager::DamageManager(World* world, ConnectionId connectionId) : m_world(world), m_connectionId(connectionId) {}
60
61void 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

Callers

nothing calls this directly

Calls 15

makeSMutableMapIteratorFunction · 0.85
makeSMutableIteratorFunction · 0.85
DamageRequestClass · 0.85
toRgbaMethod · 0.80
entityIdMethod · 0.80
knockbackMomentumMethod · 0.80
hasNextMethod · 0.45
nextMethod · 0.45
entityMethod · 0.45
removeMethod · 0.45
emptyMethod · 0.45
forAllEntitiesMethod · 0.45

Tested by

no test coverage detected