void Monster::update() { Being::update(); if (mKillStealProtectedTimeout.justFinished()) mOwner = NULL; // If dead, remove it if (mAction == DEAD) { if (mDecayTimeout.expired()) GameState::enqueueRemove(this); return; } if (mSpecy->getUpdateCallback().isValid()) { Script *script = ScriptManager::currentState(); script->setMap(getMap()); script->prepare(mSpecy->getUpdateCallback()); script->push(this); script->execute(); } // Cancel the rest when we are currently performing an attack if (!mAttackTimeout.expired()) return; refreshTarget(); if (!mTarget) { // We have no target - let's wander around if (mStrollTimeout.expired() && getPosition() == getDestination()) { if (mKillStealProtectedTimeout.expired()) { unsigned range = mSpecy->getStrollRange(); if (range) { Point randomPos(rand() % (range * 2 + 1) - range + getPosition().x, rand() % (range * 2 + 1) - range + getPosition().y); // Don't allow negative destinations, to avoid rounding // problems when divided by tile size if (randomPos.x >= 0 && randomPos.y >= 0) setDestination(randomPos); } mStrollTimeout.set(10 + rand() % 10); } } } if (mAction == ATTACK) processAttack(); }
void MonsterComponent::update(Entity &entity) { if (mKillStealProtectedTimeout.justFinished()) mOwner = nullptr; auto *beingComponent = entity.getComponent<BeingComponent>(); // If dead, remove it if (beingComponent->getAction() == DEAD) { if (mDecayTimeout.expired()) GameState::enqueueRemove(&entity); return; } if (mSpecy->getUpdateCallback().isValid()) { Script *script = ScriptManager::currentState(); script->prepare(mSpecy->getUpdateCallback()); script->push(&entity); script->execute(entity.getMap()); } refreshTarget(entity); // Cancel the rest when we have a target if (entity.getComponent<CombatComponent>()->getTarget()) return; const Point &position = entity.getComponent<ActorComponent>()->getPosition(); // We have no target - let's wander around if (mStrollTimeout.expired() && position == beingComponent->getDestination()) { if (mKillStealProtectedTimeout.expired()) { unsigned range = mSpecy->getStrollRange(); if (range) { Point randomPos(rand() % (range * 2 + 1) - range + position.x, rand() % (range * 2 + 1) - range + position.y); // Don't allow negative destinations, to avoid rounding // problems when divided by tile size if (randomPos.x >= 0 && randomPos.y >= 0) beingComponent->setDestination(entity, randomPos); } mStrollTimeout.set(10 + rand() % 10); } } }