示例#1
0
void GameStateUpdater::spawnObstacle(unsigned int obstacleGid, Coordinates coords, unsigned int spawnerId)
{
    Obstacle *obstacle = Obstacle::getInstanceByGid(obstacleGid);
    obstacle->setId(coords.y*_state->getWidth() + coords.x);
    obstacle->setPosition(coords.x*TILE_WIDTH, coords.y*TILE_HEIGHT);
    obstacle->setSize(TILE_WIDTH, TILE_HEIGHT);

    _state->getObstacleLayer()->addObject(obstacle);
    _state->getObstacleLayer()->updateGrid();

    this->logObstacleSpawn(obstacleGid, obstacle, spawnerId);
}
示例#2
0
void MainScene::createObstacle( float dt )
{
    Obstacle* obstacle = dynamic_cast<Obstacle*>(CSLoader::createNode("Obstacle.csb"));
    this->obstacles.pushBack( obstacle );
    this->back->addChild( obstacle );
    
    float obstacleY = ( OBSTACLE_MAX_Y - OBSTACLE_MIN_Y ) * CCRANDOM_0_1() + OBSTACLE_MIN_Y;
    obstacle->setPosition( Vec2( OBSTACLE_INIT_X, obstacleY ) );
    
    if( this->obstacles.size() > OBSTACLE_LIMIT )
    {
        this->obstacles.front()->removeFromParent();
        this->obstacles.erase(this->obstacles.begin());
    }
}
示例#3
0
文件: main.cpp 项目: mludtke/cs559p3
Obstacle Object_movement(Obstacle obst, vec3 obj_pos, float force, vec3 direction)
{
	float moved_X = obst.getPostion().x + direction.x * force * 0.01f;
	float moved_Z = obst.getPostion().z + direction.z * force * 0.01f;

	obst.setPosition(vec3(moved_X, ypos, moved_Z));

	float force2 = force;
	force2 -= 0.01f;
	force2 = clamp(force2, 0.0f, 10.0f);
	obst.setForce(force2);

	if (force > 0.0f)
	{
		cout << force << endl;
		cout << "direction " << direction.x << ", " << direction.z << endl;
		cout << "position " << obj_pos.x << ", " << obj_pos.z << endl;
	}

	return obst;
}
void World::createMap(ObstacleContainer* obstacleContainer, TargetContainer* targetContainer)
{




	obstacleContainer->add();

	Obstacle* wall1 = obstacleContainer->getLast();
	wall1->setLHW(1060.0f, 11.0f, 30.0f);
	wall1->setPosition(500.0f, 5.0f, -1015.0f);
	wall1->setColor(1.0f, 1.0f, 1.0f);
	
	obstacleContainer->add();

	Obstacle* wall2 = obstacleContainer->getLast();
	wall2->setLHW(1060.0f, 11.0f, 30.0f);
	wall2->setPosition(500.0f, 5.0f, 15.0f);
	wall2->setColor(1.0f, 1.0f, 1.0f);

	obstacleContainer->add();

	Obstacle* wall3 = obstacleContainer->getLast();
	wall3->setLHW(30.0f, 11.0f, 1000.0f);
	wall3->setPosition(-15.0f, 5.0f, -500.0f);
	wall3->setColor(1.0f, 1.0f, 1.0f);

	obstacleContainer->add();

	Obstacle* wall4 = obstacleContainer->getLast();
	wall4->setLHW(30.0f, 11.0f, 1000.0f);
	wall4->setPosition(1015.0f, 5.0f, -500.0f);
	wall4->setColor(1.0f, 1.0f, 1.0f);
	
	
	if(level == LEVEL_CHOICE_MAZE)
	{
		obstacleContainer->add();

		Obstacle* o = obstacleContainer->getLast();
		o->setLHW(203.0f, 101.0f, 304.0f);
		o->setPosition(200.0f, 50.0f, -750.0f);
		o->setColor(1.0f, 0.0f, 0.0f);

		obstacleContainer->add();

		o = obstacleContainer->getLast();
		o->setLHW(350.0f, 101.0f, 150.0f);
		o->setPosition(700.0f, 50.0f, -750.0f);
		o->setColor(1.0f, 0.0f, 0.0f);

		obstacleContainer->add();

		o = obstacleContainer->getLast();
		o->setLHW(425.0f, 101.0f, 200.0f);
		o->setPosition(302.0f, 50.0f, -401.0f);
		o->setColor(1.0f, 0.0f, 0.0f);

		obstacleContainer->add();

		o = obstacleContainer->getLast();
		o->setLHW(310.0f, 101.0f, 250.0f);
		o->setPosition(774.0f, 50.0f, -450.0f);
		o->setColor(1.0f, 0.0f, 0.0f);

		obstacleContainer->add();

		o = obstacleContainer->getLast();
		o->setLHW(400.0f, 101.0f, 100.0f);
		o->setPosition(501.0f, 50.0f, -174.0f);
		o->setColor(1.0f, 0.0f, 0.0f);
		
		
	}


	else if(level == LEVEL_CHOICE_BLOCK_FORT)
	{
	
		float diameter = 200.0f;

		obstacleContainer->add();
		Obstacle* o = obstacleContainer->getLast();
		o->setLHW(diameter, diameter-1.0f, diameter);
		o->setPosition(250.0f, 95.0f, -250.0f);
		o->setColor(1.0f, 0.0f, 0.0f);

		obstacleContainer->add();
		o = obstacleContainer->getLast();
		o->setLHW(diameter, diameter-1.0f, diameter);
		o->setPosition(751.0f, 95.0f, -250.0f);
		o->setColor(1.0f, 0.0f, 0.0f);

		obstacleContainer->add();
		o = obstacleContainer->getLast();
		o->setLHW(diameter, diameter-1.0f, diameter);
		o->setPosition(250.0f, 95.0f, -750.0f);
		o->setColor(1.0f, 0.0f, 0.0f);

		obstacleContainer->add();
		o = obstacleContainer->getLast();
		o->setLHW(diameter, diameter-1.0f, diameter);
		o->setPosition(751.0f, 95.0f, -750.0f);
		o->setColor(1.0f, 0.0f, 0.0f);
	
		//box in the middle
		obstacleContainer->add();
		o = obstacleContainer->getLast();
		o->setLHW(diameter/2.0f, 25.0f-1.0f, diameter/2.0f);
		o->setPosition(500.0f, 12.0f, -500.0f);
		o->setColor(1.0f, 0.0f, 0.0f);

	}

	else if(level == LEVEL_CHOICE_DEMO_2)
	{
	
		float diameter = 150.0f;

		obstacleContainer->add();
		Obstacle* o = obstacleContainer->getLast();
		o->setLHW(900.0f, 10.0f, 25.0f);
		o->setPosition(450.0f, 5.0f, -100.0f);
		o->setColor(1.0f, 1.0f, 1.0f);

		obstacleContainer->add();
		o = obstacleContainer->getLast();
		o->setLHW(diameter, diameter, diameter);
		o->setPosition(250.0f, 70.0f, -300.0f);
		o->setColor(1.0f, 1.0f, 1.0f);

		obstacleContainer->add();
		o = obstacleContainer->getLast();
		o->setLHW(diameter, diameter, diameter);
		o->setPosition(750.0f, 70.0f, -300.0f);
		o->setColor(1.0f, 1.0f, 1.0f);

		obstacleContainer->add();
		o = obstacleContainer->getLast();
		o->setLHW(diameter, diameter, diameter);
		o->setPosition(250.0f, 70.0f, -700.0f);
		o->setColor(1.0f, 1.0f, 1.0f);

		obstacleContainer->add();
		o = obstacleContainer->getLast();
		o->setLHW(diameter, diameter, diameter);
		o->setPosition(750.0f, 70.0f, -700.0f);
		o->setColor(1.0f, 1.0f, 1.0f);
	
		//box in the middle
		obstacleContainer->add();
		o = obstacleContainer->getLast();
		o->setLHW(diameter/2.0f, 25.0f, diameter/2.0f);
		o->setPosition(500.0f, 12.0f, -500.0f);
		o->setColor(1.0f, 1.0f, 1.0f);
	
	}

	else if(level == LEVEL_CHOICE_COLUMNS)
	{
		Obstacle* o;
		for(int x=205; x<1000; x=x+280)
		{
			for(int y=205; y<1000; y=y+280)
			{
				obstacleContainer->add();
				o = obstacleContainer->getLast();
				o->setLHW(118.0f, 101.0f, 120.0f);
				o->setPosition((float)x, 50.0f, -(float)y);
				o->setColor(1.0f, 1.0f, 1.0f);
			}
		}

	}

}
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);
    
}