void Unit::attackUnit(Game* g,float damage,Unit* u){ damageUnit(damage); if(alertCooldown<=0){ b2World* world=g->getGSM()->getPhysics()->getWorld(); b2AABB alertRct; float x=getWorldX(),y=getWorldY(); alertRct.lowerBound.x=x-20; alertRct.lowerBound.y=y-20; alertRct.upperBound.x=x+20; alertRct.upperBound.y=y+20; AlertUnitsCallback cb; cb.attackingUnit=u; cb.me=this; world->QueryAABB(&cb,alertRct); alertCooldown=100; } FXParticleSprite *bloodpool=(FXParticleSprite*)g->getGSM()->getFXSys()->getParticle(BloodPoolParticleID); b2Vec2 vec= u->getBody()->GetPosition()-getBody()->GetPosition(); bloodpool->x=getX()+(rand()%50-25); bloodpool->y=getY()+(rand()%50-25); bloodpool->r=rand()/50; g->getGSM()->getWorld()->addLowerParticle(bloodpool); FXParticleSprite *blood=(FXParticleSprite*)g->getGSM()->getFXSys()->getParticle(BloodParticleID); vec= u->getBody()->GetPosition()-getBody()->GetPosition(); blood->x=getX(); blood->y=getY(); g->getGSM()->getWorld()->addParticle(blood); if(state==standby) targetUnit=u; }
void Ogre::onAttack(Game* game, Unit* u, float damage){ FXParticleSprite *arrow=(FXParticleSprite*)game->getGSM()->getFXSys()->getParticle(DirtExplosionParticleID); b2Vec2 vec= u->getBody()->GetPosition()-getBody()->GetPosition(); arrow->x=u->getX(); arrow->y=u->getY(); game->getGSM()->getWorld()->addParticle(arrow); b2World* world=game->getGSM()->getPhysics()->getWorld(); b2AABB splashRct; float x=getWorldX(),y=getWorldY(); splashRct.lowerBound.x=x-2; splashRct.lowerBound.y=y-2; splashRct.upperBound.x=x+2; splashRct.upperBound.y=y+2; SplashDamageCallback cb; cb.me=this; cb.invrange=.5f; cb.g=game; cb.damage=(strengthBase*strengthFactor+strengthIncrease)/2; cb.epicenter=u->getBody()->GetPosition(); world->QueryAABB(&cb,splashRct); targetUnit->attackUnit(game,strengthBase*strengthFactor+strengthIncrease,this); if(targetUnit->isDead()){ p->numKills++; } }
void Warrior::handleOrders(Game* game){ InGameState *manager = (InGameState*)game->getGSM()->getManager(); if(order.type==MOVEORDER){ determineWayPoint(manager,game,order.x,order.y); float dx = order.x-getWorldX(); float dy = order.y-getWorldY(); b2Vec2 displacement(dx,dy); if(displacement.Length()<warriorFindTargetRange/2*BOX2D_Scale_Factor) this->issueStopOrder(); } else if(targetUnit==NULL && order.type==PATROLTOORDER){ determineWayPoint(manager,game,order.x,order.y); float dx = (float)(order.x-getWorldX()); float dy = (float)(order.y-getWorldY()); b2Vec2 displacement(dx,dy); if(displacement.Length()<warriorFindTargetRange/2*BOX2D_Scale_Factor){ int tmp = order.x; order.x=order.initialX; order.initialX=tmp; tmp = order.y; order.y=order.initialY; order.initialY=tmp; } } else if(order.type==ATTACKTOORDER && targetUnit==NULL){ determineWayPoint(manager,game,order.x,order.y); float dx = order.x-getWorldX(); float dy = order.y-getWorldY(); b2Vec2 displacement(dx,dy); if(displacement.Length()<warriorFindTargetRange/2*BOX2D_Scale_Factor) this->issueStopOrder(); } else if(order.type==ATTACKUNITORDER && targetUnit!=NULL) determineWayPoint(manager,game,targetUnit->getWorldX(),targetUnit->getWorldY()); else if(targetUnit!=NULL && state==standby){ determineWayPoint(manager,game,targetUnit->getWorldX(),targetUnit->getWorldY()); } else lastWaypointGeneration=10; }
void Warrior::determineWayPoint(InGameState* state,Game* game,float x, float y){ b2Vec2 pos = getBody()->GetPosition(); pos.x-=x; pos.y-=y; if(pos.Length()>8){ state->gridMap.aStarAlgorithm(game,getWorldX(),getWorldY(),x,y,&waypoints); lastWaypointGeneration=60; }else{ waypoints.clear(); waypoints.push_back(make_pair(x,y)); lastThink=lastWaypointGeneration=5; } }
int main( int argc, char* argv[] ) { bool fullscreen = false; char* world = "world"; for (int i=0; i<argc; i++) { if (strcmp(argv[i], "-win") == 0) fullscreen = false; if (strcmp(argv[i], "-f") == 0) fullscreen = true; if (strcmp(argv[i], "-w") == 0) world = argv[i+1]; if (strcmp(argv[i], "-d") == 0) debug = true; } SetupWorld(world); you_x = getWorldX(); you_z = getWorldZ(); you_angle = getWorldAngle(); const SDL_VideoInfo* info = NULL; int width = 0; int height = 0; int bpp = 0; int flags = 0; if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { fprintf( stderr, "Video initialization failed: %s\n", SDL_GetError( ) ); quit_app( 1 ); } info = SDL_GetVideoInfo( ); if( !info ) { fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) ); quit_app( 1 ); } width = 640; height = 480; bpp = info->vfmt->BitsPerPixel; SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); flags = SDL_OPENGL; if (fullscreen) flags |= SDL_FULLSCREEN; if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) { fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) ); quit_app( 1 ); } setup_opengl( width, height ); bool init = true; while( 1 ) { process_events( ); draw_screen( ); if (init) { glEnable( GL_LIGHTING ); init=false; } } return 0; }
bool Warrior::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); } else if (isDead()) return false; 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){ if(justStartedState()){ this->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)lastWaypointGeneration=0; } 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; }