void Actor::takeTurn() { bool shouldAttack = hasValidTarget() && targetPtr != this && targetPtr->getIsLiving(); // Aggressive, attacks whatever isn't itself. if (!shouldAttack) { cycleTarget(); shouldAttack = hasValidTarget() && targetPtr != this && targetPtr->getIsLiving(); } if (shouldAttack) { onAttack(); } }
void CActor::performAttack( std::shared_ptr<CActor> other ) { if ( abs( magicEnergy ) < abs( other->magicEnergy ) ) { return; } onAttack(); int diff = ( attack - other->defence ); if ( diff > 0 ) { other->hp -= diff; } int deltaEnergy = (other->magicEnergy) / 2; magicEnergy += deltaEnergy; }
void SGSHero::handleMessage(SGMessage* message) { unsigned int what = message->what(); switch (what) { case kWhatAttack: onAttack(message); break; case kWhatAttacked: onAttacked(message); break; case kWhatMove: onMoveTo(message); break; case kWhatDelay: onDelay(message); break; default: break; } }
/* * think - * */ bool Ogre::think(Game *game){ if(alertCooldown>0)alertCooldown--; if(attackCooldown>0)attackCooldown--; if(isDead() && this->getCurrentState()==DEATH_STATE && justStartedState()) return true; else if(isDead() && this->getCurrentState()!=DEATH_STATE){ this->setCurrentState((wstring)DEATH_STATE); body->SetActive(false); return true; } else if (isDead()) return false; if(getBody()->GetLinearVelocity().x>0){ if(getCurrentState()!=L"IDLE_STATE_SOUTH")this->setCurrentState((wstring)L"IDLE_STATE_SOUTH");} else if(getCurrentState()!=L"IDLE_STATE_NORTH")this->setCurrentState((wstring)L"IDLE_STATE_NORTH"); if(isDisturbed<=0){ b2Vec2 vel=getBody()->GetLinearVelocity(); //vel.Normalize(); vel*=-this->getVelocityDampener()*movementImpulse; getBody()->ApplyForceToCenter(vel); }else{ if(order.type!=ATTACKUNITORDER)untargetUnit(); if(isDisturbed>0)isDisturbed--; return false; } if(targetUnit!=NULL&&(targetUnit->isDead()||targetUnit->birthID!=birthIDTarget)){ untargetUnit(); if(order.type==ATTACKUNITORDER) order.type=STOPORDER; } if(targetUnit!=NULL&&state==attacking&& attackCooldown<=0){ onAttack(game,targetUnit,strengthBase*strengthFactor+strengthIncrease); state=standby; attackCooldown=attackSpeedBase*attackSpeedFactor; } if(lastThink--<=0){ Physics* p =game->getGSM()->getPhysics(); b2World* w=p->getWorld(); if(order.type!=MOVEORDER) if(targetUnit==NULL){ targetArea.upperBound-=targetArea.lowerBound; targetArea.lowerBound.x=getBody()->GetPosition().x-targetArea.upperBound.x/2.0f; targetArea.lowerBound.y=getBody()->GetPosition().y-targetArea.upperBound.y/2.0f; targetArea.upperBound+=targetArea.lowerBound; w->QueryAABB((&callback),targetArea); } if(targetUnit!=NULL){ closeArea.upperBound-=closeArea.lowerBound; closeArea.lowerBound.x=getBody()->GetPosition().x-closeArea.upperBound.x/2.0f; closeArea.lowerBound.y=getBody()->GetPosition().y-closeArea.upperBound.y/2.0f; closeArea.upperBound+=closeArea.lowerBound; if(b2TestOverlap(targetUnit->getAABB(),closeArea)){ attackTargetedUnit(); b2Vec2 vel=getBody()->GetLinearVelocity(); vel*=.3f; getBody()->SetLinearVelocity(vel); } else{ state=standby; if(order.type!=ATTACKUNITORDER){ AABBCallback<CloseRange> range; CloseRange c; range.f=&c; range.self=this; w->QueryAABB((&range),closeArea); } } } lastThink=10; } if(!waypoints.empty()){ pair<float,float> coord=waypoints.back(); float dx =(coord.first-getWorldX()); float dy = (coord.second-getWorldY()); b2Vec2 displacement(dx,dy); if(displacement.Length()<2){ waypoints.pop_back(); } displacement.Normalize(); displacement*=this->getMovementImpulse(); body->ApplyForceToCenter(displacement); } if(lastWaypointGeneration--<=0) handleOrders(game); return false; }