void redraw() { drawNode->clear(); vector<Unit>::iterator itor; for (itor = unitList.begin(); itor != unitList.end(); ++itor) { drawNode->drawCircle(itor->pos, 10, 360, 10, false, Color4F::BLUE); drawNode->drawLine(itor->pos, itor->pos + itor->velocity * 30, Color4F::GREEN); } }
Waypoint::Waypoint(HelloWorld *theGame, const Vec2& location, Waypoint *nextWaypoint) : theGame(theGame), nextWaypoint(nextWaypoint) { this->myPosition = location; this->setPosition(cocos2d::Vec2::ZERO); #ifdef DRAW_DEBUG_INFO DrawNode *circle = DrawNode::create(); circle->drawCircle(location, 6, 360, 30, false, Color4F::GREEN); this->addChild(circle); DrawNode *circle2 = DrawNode::create(); circle2->drawCircle(location, 2, 360, 30, false, Color4F::GREEN); this->addChild(circle2); if (nextWaypoint) { DrawNode *line = DrawNode::create(); line->drawLine(this->myPosition, nextWaypoint->myPosition, Color4F::GREEN); this->addChild(line); } #endif theGame->addChild(this); }
void HelloWorldScene::initEntites() { // Create Vehicles int numOfVehicles = Prm.getValueAsInt("NumOfVehicles"); for(int i = 0 ; i < numOfVehicles ; ++ i) { Vehicle* vehicle = Vehicle::create("Vehicle.png"); vehicle->setGameWorld(this); vehicle->setBRadius(15.0f); vehicle->setPosition(Vec2(random(0.0f, 600.0f), random(0.0f, 600.0f))); vehicle->setTag(getNextValidID()); this->setBehaviorAsNormalVehicle(vehicle); _gameView->addChild(vehicle); _vehicles.pushBack(vehicle); // for DetailView Vehicle Vehicle* detailViewVehicle = Vehicle::create("Vehicle.png"); _detailView->addChild(detailViewVehicle); _detailViewVehicles.pushBack(detailViewVehicle); } _pursuer = Vehicle::create("pursuer.png"); _pursuer->setGameWorld(this); _pursuer->setBRadius(15.0f); _pursuer->setMaxSpeed(100.0f); _pursuer->setPosition(Vec2(random(0.0f, 600.0f), random(0.0f, 600.0f))); _pursuer->setTag(getNextValidID()); _pursuer->getSteering()->enableBehavior(BehaviorType::kWander); _pursuer->getSteering()->enableBehavior(BehaviorType::kPursuit); _pursuer->getSteering()->enableBehavior(BehaviorType::kObstacleAvoidance); _gameView->addChild(_pursuer); _vehicles.pushBack(_pursuer); Vehicle* detailViewPursuer = Vehicle::create("pursuer.png"); _detailView->addChild(detailViewPursuer); _detailViewVehicles.pushBack(detailViewPursuer); // initialize selected detailView vehicle and worldView vehicle. _selectedVehicleDetail = Vehicle::create("Vehicle.png"); _selectedVehicleDetail->setVisible(false); _detailView->addChild(_selectedVehicleDetail); this->setBehaviorAsSelectedVehicle(_vehicles.at(0)); // Create Obstacles; int numOfObstacles = Prm.getValueAsInt("NumOfObstacles"); for(int i = 0 ; i < numOfObstacles ; ++ i) { int numOfAllowableTrys = 2000; while(numOfAllowableTrys--) { double radius = random(Prm.getValueAsFloat("MinObstacleRadius"), Prm.getValueAsFloat("MaxObstacleRadius")); const int border = 10; float x = random(radius + border, 600.0f - radius - border); float y = random(radius + border, 600.0f - radius - border); Obstacle* ob = Obstacle::create(); ob->setPosition(Vec2(x, y)); ob->setBRadius(radius); const int minGapBetweenObstacles = 20; if(!overlapped(ob, _obstacles, minGapBetweenObstacles)) { DrawNode* node = DrawNode::create(); node->drawCircle(Vec2::ZERO, ob->getBRadius(), 360.0f, 100, false, 1.0f, 1.0f, Color4F::BLUE); ob->addChild(node); _obstacles.pushBack(ob); ob->setTag(getNextValidID()); _gameView->addChild(ob); // for DetailView Obstacles Obstacle* detailViewOb = Obstacle::create(); detailViewOb->setPosition(Vec2(x, y)); detailViewOb->setBRadius(radius); DrawNode* avoidanceNode = DrawNode::create(); avoidanceNode->drawCircle(Vec2::ZERO, detailViewOb->getBRadius(), 360.0f, 100, false, 1.0f, 1.0f, Color4F::BLUE); detailViewOb->addChild(avoidanceNode); _detailViewObstacles.pushBack(detailViewOb); _detailView->addChild(detailViewOb); break; } } } // Create Walls const int numOfVertex = Prm.getValueAsInt("NumOfWallVertice"); std::vector<Vec2> walls { cocos2d::Vec2(20,20), cocos2d::Vec2(580,20), cocos2d::Vec2(580,580), cocos2d::Vec2(20,580) }; for(int i = 0 ; i < numOfVertex - 1; ++ i) { Wall2D* wall = Wall2D::create(Segment(Vec2(walls.at(i)), Vec2(walls.at(i+1)))); _gameView->addChild(wall); _walls.pushBack(wall); } Wall2D* wall = Wall2D::create(Segment(Vec2(walls.at(numOfVertex - 1)), Vec2(walls.at(0)))); _gameView->addChild(wall); _walls.pushBack(wall); }