//Damage this actor from a particular faction void PhysicsActor::Damage(FactionId damagingFaction, float damage) { //Can't take damage if you're invulnerable if (!vulnerable) return; //Check that you're alive if (life > 0) { lastDamageRecieved = Game()->Now(); //Fire the damaged event Game()->Actors.ActorDamaged.Fire([this,damagingFaction,damage](function<void(Actor*,Actor*,FactionId,float)> observer) { observer(this,NULL,damagingFaction,damage); }); //Apply damage life -= damage; if (life < 0) life = 0; if (life <= 0) { //Can't die anymore vulnerable = false; //Fire on death event Game()->Actors.ActorKilled.Fire([this,damagingFaction](function<void(Actor*,Actor*,FactionId)> observer) { observer(this,NULL,damagingFaction); }); //This actor just died onDeath(); } } }
void DestructableLayer::_destroy(const int x, const int y) { //_hp_data[i] = -1; //destructed cell //LOG_DEBUG(("_destroy(%d, %d)", x, y)); const int i = _w * y + x; const int size = _w * _h; std::queue<int> queue; std::set<int> visited; queue.push(i); while(!queue.empty()) { int v = queue.front(); queue.pop(); assert( v >= 0 && v < size ); if (visited.find(v) != visited.end()) continue; visited.insert(v); int x = v % _w, y = v / _w; if (Layer::_get(x + _w * y) == 0) continue; onDeath(v); if (x > 0) queue.push(v - 1); else if (Map->torus()) //wrap to _w - 1 queue.push(y * _w + _w - 1); if (x < _w - 1) queue.push(v + 1); else if (Map->torus()) //wrap to x == 0 queue.push(y * _w); if (y > 0) queue.push(v - _w); else if (Map->torus()) queue.push((_h - 1) * _w + x); if (y < _h - 1) queue.push(v + _w); else if (Map->torus()) queue.push(x); } //LOG_DEBUG(("cleanup done")); }
//Update life bool PhysicsActor::Update() { //Regenerate life and energy while alive if (!Dead()) { life += SIMULATION_DELTA*lifeRegenRate; if (life > maxLife) life = maxLife; energyPool += SIMULATION_DELTA*energyRegenRate; if (energyPool > energyPoolMax) energyPool = energyPoolMax; } if ((life <= 0) && vulnerable) { //This unit died vulnerable = false; //call on death onDeath(); } return Actor::Update(); }
void World::update(const float dt) { bool hasChanged = false; bool entityDied = false; for (int32 i = m_entities.size() - 1; i >= 0; i--) { Entity* entity = m_entities[i]; if (entity->isAlive()) { entity->update(dt); if (entity->updateHash()) { m_spatialHash.update(entity); hasChanged = true; } } else { m_entities.erase(m_entities.begin() + i); onDeath(entity); // Make sure we remove the entity from the spatialhash and all of the nodes that it exists in. // This can cause problems because a node may contain a null reference to an entity which causesa c crash. m_spatialHash.remove(entity); delete entity; entityDied = true; } } if (hasChanged && m_debug) { m_vertexQuadArray.clear(); m_vertexLineArray.clear(); m_spatialHash.buildArray(m_vertexQuadArray, sf::Quads); m_spatialHash.buildArray(m_vertexLineArray, sf::Lines); } if (entityDied) { updateEntityText(); } }
void Creature::setHealthPercent(uint8 healthPercent) { if(healthPercent > 92) m_informationColor = Color(0x00, 0xBC, 0x00); else if(healthPercent > 60) m_informationColor = Color(0x50, 0xA1, 0x50); else if(healthPercent > 30) m_informationColor = Color(0xA1, 0xA1, 0x00); else if(healthPercent > 8) m_informationColor = Color(0xBF, 0x0A, 0x0A); else if(healthPercent > 3) m_informationColor = Color(0x91, 0x0F, 0x0F); else m_informationColor = Color(0x85, 0x0C, 0x0C); m_healthPercent = healthPercent; callLuaField("onHealthPercentChange", healthPercent); if(healthPercent <= 0) onDeath(); }
void Grunt::hit(BattleState &state, int damage) { if (isDead(state)) return; m_hp -= damage; if (isDead(state)) onDeath(state); }