示例#1
0
int main()
{
    sf::RenderWindow window(sf::VideoMode(1600, 900), "Roboprototype");
	sf::Event e;
	window.setFramerateLimit(60);
	bool in_focus = true; //if false, the window won't update
	int game_state = GAME_STATE::LIVE;

	//world setup
	Editor editor; //used to store all objects
	b2Vec2 gravity(0.0, -20.0f); //current gravity for the world
	b2World *world = new b2World(gravity);
	world->SetAllowSleeping(true); //allows particles to come to rest
	float32 time_step = 1.0 / 60.0; //the length of time passed to simulate (seconds)
	int32 velocity_iterations = 7; //how strongly to correct the velocity
	int32 position_iterations = 3; //how strongly to correct the position

	//game mode setup(live/editor)
	Timer game_mode_clock;
	sf::Font font;
	sf::Text game_mode_text;
	string game_mode_live = "Mode: Live";
	string game_mode_editor = "Mode: Editor";


	if(!font.loadFromFile("fonts//amatic.ttf") )
	{
		printf("Failed to load font on line %d \n", __LINE__);
		window.close();
	}

	game_mode_text.setFont(font);
	game_mode_text.setString( (game_state == GAME_STATE::LIVE) ? game_mode_live : game_mode_editor );
	game_mode_text.setCharacterSize(50);
	game_mode_text.setColor( sf::Color(0, 0, 0) );
	game_mode_text.scale( window.getSize().x / 1600.0, window.getSize().y / 900); //scales based on the window size it was created on
	game_mode_text.setPosition(10.0, 10.0);


	//mouse setup
	Timer mouse_clock;
	sf::Vector2f mouse_pos;

	//particles///////////
	bool particle_toggle = true; //if true, particles are constantly created
	sf::CircleShape particle_shape;
	particle_shape.setFillColor( sf::Color(255, 255, 255, 200) );
	particle_shape.setOutlineThickness(2);
	particle_shape.setOutlineColor( sf::Color(111, 211, 255, 200) );
	particle_shape.setRadius(6);
	particle_shape.setOrigin( particle_shape.getRadius(), particle_shape.getRadius() );

	b2ParticleSystem *particle_system; //the world the particles inhabit
	b2ParticleSystemDef particle_system_def;
	b2ParticleDef *particle_def = new b2ParticleDef; //definition of an individual particle

	particle_system_def.density = 1;
	particle_system_def.radius = (particle_shape.getRadius() + (particle_shape.getOutlineThickness() / 2.0) ) * PIXELS_TO_METERS;
	particle_system_def.maxCount = 500; //maximum number of particles on the screen

	particle_system = world->CreateParticleSystem(&particle_system_def); //creates the particle system to hold all the particles
	particle_system->SetRadius( particle_shape.getRadius() );
	particle_system->SetDestructionByAge(true); //particles are automatically destroyed based on their age
	particle_system->SetGravityScale(50.0);
	
	
	particle_def->lifetime = 0.25; //number of seconds particle will stay alive
	particle_def->color.Set(0, 255, 255, 255);
	particle_def->flags = b2_elasticParticle;
	particle_def->position.Set( window.getSize().x / 2.0, window.getSize().y / 2.0 );
	//end particles////////////////////////////////////
	
	

	//base platform
	Object *temp_object = NULL;
	b2FixtureDef fixture;
	sf::Texture platform_texture;

	if(!platform_texture.loadFromFile("images//platform.png"))
	{
		printf("Failed to load platform texture on line %d \n", __LINE__);
		window.close();
	}

	fixture.density = 1;
	fixture.restitution = 0.05;
	fixture.friction = 0.75;

	temp_object = new Object(window, world, fixture, platform_texture, STATIC_BODY, POLY_SHAPE);
	temp_object->getSprite()->setColor( sf::Color(0, 0, 0, 200) );
	temp_object->getBody()->SetTransform( b2Vec2( (window.getSize().x / 2.0) * PIXELS_TO_METERS, -(window.getSize().y / 1.1) * PIXELS_TO_METERS ), 0.0 );
	temp_object->updateSpritePos();
	editor.addStaticObject(temp_object);

	//smaller platform
	sf::Texture small_platform_texture;

	if(!small_platform_texture.loadFromFile("images//small_platform.png") )
	{
		printf("Failed to load small platform texture on line %d \n", __LINE__);
		window.close();
	}

	temp_object = new Object(window, world, fixture, small_platform_texture, STATIC_BODY, POLY_SHAPE);
	temp_object->getSprite()->setColor( sf::Color(0, 0, 0, 200) );
	temp_object->getBody()->SetTransform( b2Vec2( (small_platform_texture.getSize().x / 2.0) * PIXELS_TO_METERS, -(window.getSize().y / 1.25) * PIXELS_TO_METERS), 0.0 );
	temp_object->updateSpritePos();
	editor.addStaticObject(temp_object);

	fixture.friction = 1.0;
	temp_object = new Object(window, world, fixture, small_platform_texture, KINEMATIC_BODY, POLY_SHAPE);
	temp_object->getSprite()->setColor( sf::Color(0, 0, 0, 200) );
	temp_object->getBody()->SetTransform( b2Vec2( (small_platform_texture.getSize().x * 1.5) * PIXELS_TO_METERS, -(window.getSize().y / 1.45) * PIXELS_TO_METERS), 0.0 );
	temp_object->getBody()->SetLinearVelocity( b2Vec2(200.0 * PIXELS_TO_METERS, 0.0) );
	temp_object->updateSpritePos();
	sf::Vector2f left_boundary(temp_object->getSprite()->getPosition().x, temp_object->getSprite()->getPosition().y);
	sf::Vector2f right_boundary( (window.getSize().x - small_platform_texture.getSize().x / 2.0), temp_object->getSprite()->getPosition().y );
	editor.addKinematicObject(temp_object);
	

	//player setup
	sf::Vector2f player_speed = sf::Vector2f(400.0, 600.0);
	Timer player_clock;
	Timer player_jump_clock;
	sf::Texture player_texture;

	if(!player_texture.loadFromFile("images//player_color.png") )
	{
		printf("Error %d : Failed to load player texture.\n", __LINE__);
		return -1;
	}

	fixture.restitution = 0.1;
	fixture.friction = 1.0;
	fixture.density = 2.0;
	Object player(window, world, fixture, player_texture, DYNAMIC_BODY, POLY_SHAPE);
	player.getBody()->SetFixedRotation(true);
	player.getBody()->SetTransform( b2Vec2( 50 * PIXELS_TO_METERS, -(window.getSize().y / 2.0) * PIXELS_TO_METERS), 0.0 );
	player.updateSpritePos();


    while (window.isOpen())
    {
        while (window.pollEvent(e))
        {
            if (e.type == sf::Event::Closed)
                window.close();

			else if(e.type == sf::Event::KeyPressed && e.key.code == sf::Keyboard::Escape)
				window.close();

			else if(e.type == sf::Event::LostFocus)
				in_focus = false;

			else if(e.type == sf::Event::GainedFocus)
				in_focus = true;			
        }

		if(in_focus == true)
		{
			//checks for game mode change
			game_mode_clock.update();
			gameModeToggle(game_mode_text, game_mode_clock, game_mode_live, game_mode_editor, game_state);

			if(game_state == GAME_STATE::LIVE)
			{
				//update clocks
				mouse_clock.update();
				player_clock.update();
				player_jump_clock.update();
				
			
				//update mouse position
				updateMousePos(mouse_pos, window);

				//checks for player commands
				playerCommandUpdate(player_clock, player_jump_clock, player_speed, player);

				//update world
				world->Step(time_step, velocity_iterations, position_iterations);
			
				particleToggle(player, mouse_clock, particle_toggle, particle_def, particle_system);
				updatePosition(player); //updates the player sprite
				updatePosition(editor.getKinematicObjects());
				kinematicBoundaries(editor.getKinematicObjects()[0], left_boundary, right_boundary);
			}
			

			window.clear(sf::Color(200, 200, 200) ); 

			drawParticles(window, particle_shape, particle_system);
			draw( window, editor.getStaticObjects() ); //draws all the static objects to the screen
			draw( window, editor.getKinematicObjects() ); //draws all the kinematic objects to the screen
			draw( window, player ); //draws the player to the screen
			drawGameMode(window, game_mode_text);
			window.display();
		}        
    }
	
    return 0;
}