Пример #1
0
void FireFighterEvent::activate()
{
	EntityManager* manager = EntityManager::getInstance();
	sf::Vector2f position = sf::Vector2f(Util::getRandom(0.f, static_cast<float>(Globals::screenSize.x / 2)), 
		Util::getRandom(0.f,static_cast<float>(Globals::screenSize.y / 2)));
	sf::Vector2f major = sf::Vector2f(Util::getRandom(0.f, 1.f), Util::getRandom(0.f, 1.f));
	major = Util::getMajorVector(major);
	Util::normalize(major);

	position.x *= major.x;
	position.y *= major.y;

	if (rand() % 2 == 0)
	{
		position.x += major.y * Globals::screenSize.x / 2;
		position.y += major.x * Globals::screenSize.y / 2;
	}

	manager->addEntity(std::make_shared<FireFighter>(position.x, position.y, mEntity.lock()));
}
Пример #2
0
int main (int argc, const char * argv[])
{  
  string gameName = "Lazy Crab";
  float currentFPS = 0;
  
  // Create the main window
  sf::VideoMode DesktopMode = sf::VideoMode::getDesktopMode();
  sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(DesktopMode.width/2, DesktopMode.height/2,DesktopMode.bitsPerPixel), gameName);
  window->setVerticalSyncEnabled(true);
  
  
  // Load a sprite to display
  sf::Texture texture;
  if (!texture.loadFromFile(resourcePath() + "images/cute_image.jpg"))
  	return EXIT_FAILURE;
  sf::Sprite sprite(texture);

  // Create a graphical text to display
  sf::Font font;
  if (!font.loadFromFile(resourcePath() + "fonts/sansation.ttf"))
  	return EXIT_FAILURE;
  sf::Text text("Hello SFML", font, 50);
  text.setColor(sf::Color::Black);

  // Load a music to play
  sf::Music music;
  if (!music.openFromFile(resourcePath() + "music/fairy_road.ogg"))
  	return EXIT_FAILURE;

  
  sf::Clock frameTimer;
  sf::Time deltaTime;
    
  // Generates the World
  b2Vec2 gravity(0, -9.8);
  b2World* world = new b2World(gravity);
  
  GameContainer* gc = new GameContainer(window, world);
    
  
  EntityManager* em = new EntityManager();
  
  Entity* player = em->addEntity(new Entity(gc, "player"));
  player->setPosition(8, 0);
  player->addComponent(new BodyComponent(gc, 0.6f, 1.8f, true));
  player->addComponent(new WireboxRenderComponent("wirebox"));
  
  float windowRatio = (float)gc->getWindow()->getSize().y / (float)gc->getWindow()->getSize().x;
  Entity* camera = em->addEntity(new Entity(gc, "camera"));
  camera->setPosition(100, 10);
  camera->addComponent(new CameraComponent(gc, player, 30, 30*windowRatio));
  
  Entity* terrain = em->addEntity(new Entity(gc, "terrain"));
  terrain->setPosition(0, -10);
  terrain->addComponent(new WirechainRenderComponent(6));
  terrain->addComponent(new TerrainComponent(gc, "gameTerrain", 6));
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(0, 5, 0);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(1, 20, 5);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(2, 30, -2);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(3, 50, 10);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(4, 80, 5);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(5, 100, 15);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->generate();
  
  Entity* options = em->addEntity(new Entity(gc, "options"));
  options->addComponent(new OptionComponent("controls", "moveLeft", "A"));
  options->addComponent(new OptionComponent("controls", "moveLeft_alt", "arrow_left"));
  options->addComponent(new OptionComponent("controls", "moveRight", "D"));
  options->addComponent(new OptionComponent("controls", "moveRight_alt", "arrow_right"));
  
  
  
  
  //ControlManager::player(player);
  
  
  // Play the music
  music.play();
      

  // Start the game loop
  while (window->isOpen())
  {
  	// Process events
  	sf::Event event;
  	while (window->pollEvent(event))
  	{
  		// Close window : exit
  		if (event.type == sf::Event::Closed)
  			window->close();
          
  		// Escape pressed : exit
  		if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
  			window->close();
  	}
    
    currentFPS =  1000 / (gc->getDelta().asMicroseconds() / 1000.0f);

  	// Clear screen
  	gc->getWindow()->clear();
  	    
    em->updateRender();
    
    em->getNearbyEntities(em->getEntity("player"), 2);
    
    
    
    gc->getWorld()->Step(1/currentFPS,  8, 3);
    gc->getWindow()->setView(gc->view);
    
    // Update the window
  	gc->getWindow()->display();
    
    gc->setDelta(frameTimer.restart());
  }

	return EXIT_SUCCESS;
}
Пример #3
0
int main (int argc, const char * argv[])
{  
  string gameName = "Lazy Crab";
  float currentFPS = 0;
  
  // Create the main window
  sf::VideoMode DesktopMode = sf::VideoMode::getDesktopMode();
  sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(DesktopMode.width/2, DesktopMode.height/2,DesktopMode.bitsPerPixel), gameName);

  sf::Clock frameTimer;
    
  // Generates the World
  b2Vec2 gravity(0, -9.8);
  b2World* world = new b2World(gravity);
  
  GameContainer* gc = new GameContainer(window, world);
    
  
  EntityManager* em = new EntityManager();
  
  Entity* player = em->addEntity(new Entity(gc, "player"));
  player->setPosition(8, 30);
  player->addComponent(new BodyComponent(gc, 0.6f, 1.8f, true));
  player->addComponent(new WireboxRenderComponent("wirebox"));
  
  float windowRatio = (float)gc->getWindow()->getSize().y / (float)gc->getWindow()->getSize().x;
  Entity* camera = em->addEntity(new Entity(gc, "camera"));
  camera->setPosition(100, 10);
  camera->addComponent(new CameraComponent(gc, player, 30, 30*windowRatio));
  
  Entity* terrain = em->addEntity(new Entity(gc, "terrain"));
  
  terrain->setPosition(0, 0);
  terrain->addComponent(new WirechainRenderComponent(5));
  terrain->addComponent(new TerrainComponent(gc, "gameTerrain", 5));
  /*for(int i = 0; i != coords.size(); i++)
  {
    ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(i, coords[i].x, coords[i].y);
  }*/
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(0, 0, 5);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(1, 30, -2);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(2, 50, 10);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(3, 80, 5);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->addPoint(4, 100, 15);
  ((TerrainComponent*) terrain->getComponent("gameTerrain"))->generate();

  
  Entity* options = em->addEntity(new Entity(gc, "options"));
  options->addComponent(new OptionComponent("controls", "moveLeft", "A"));
  options->addComponent(new OptionComponent("controls", "moveLeft_alt", "arrow_left"));
  options->addComponent(new OptionComponent("controls", "moveRight", "D"));
  options->addComponent(new OptionComponent("controls", "moveRight_alt", "arrow_right"));
  
  
  
  
  
  
  //ControlManager::player(player);
  
  // Start the game loop
  while (window->isOpen())
  {
  	// Process events
  	sf::Event event;
  	while (window->pollEvent(event))
  	{
  		// Close window : exit
  		if (event.type == sf::Event::Closed)
  			window->close();
          
  		// Escape pressed : exit
  		if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
  			window->close();
  	}
    
    currentFPS =  1000 / (gc->getDelta().asMicroseconds() / 1000.0f);

  	// Clear screen
  	gc->getWindow()->clear();
  	    
    em->updateRender();
    
    em->getNearbyEntities(em->getEntity("player"), 2);
    
    
    
    gc->getWorld()->Step(1/currentFPS,  8, 3);
    gc->getWindow()->setView(gc->view);
    
    // Update the window
  	gc->getWindow()->display();
    
    gc->setDelta(frameTimer.restart());
  }

	return EXIT_SUCCESS;
}
void MapGenerator::generateMap(std::string imageNameBlock, std::string imageNameObject, std::string imageNameXML)
{
    GenerateDoor::loadDocument(imageNameXML);
    GenerateDoor::GenerateDoors();

    sf::Image image;
    image.loadFromFile("Maps/" + imageNameBlock);
    EntityManager* entityManager = EntityManager::getInstance();
    //
    //------------------------------------------------------skapar block och karaktärer
    //
    for(int i = 0; i < image.getSize().x; ++i)
    {
        for(int j = 0; j < image.getSize().y; ++j)
        {
            sf::Color color = image.getPixel(i ,j);
            sf::Vector2f position = sf::Vector2f(i * 64.f, j *64.f);

            // Skapar Block vid Red = 0
            if(color.r == 0 && color.a == 255)
            {
                if(color.g == 0)
                {
                    entityManager->addEntity(new NormalBlock(position, color.b));
                }
                else if(color.g == 50)
                {
                    entityManager->addEntity(new FireRune(position));
                }
                else if(color.g == 100)
                {
                    entityManager->addEntity(new Vine(position, color.b));
                }
                else if(color.g == 150)
                {
                    entityManager->addEntity(new Stone(position));
                }
                else if(color.g == 200)
                {
                    entityManager->addEntity(new Lava(position));
                }
                else if(color.g == 250)
                {
                    entityManager->addEntity(new WoodenWall(position));
                }
                else if(color.g == 255)
                {
                    entityManager->addEntity(new FinishDoor(position));
                }
            }

            // Skapar Karaktärerna när red = 100
            else if(color.r == 100 && color.a == 255)
            {
                if(color.g == 0)
                {
                    entityManager->addEntity(new Kiba(position));
                }
                else if(color.g == 50)
                {
                    entityManager->addEntity(new Charlotte(position));
                }
                else if(color.g == 100)
                {
                    entityManager->addEntity(new Fenrir(position));
                }
                else if(color.g == 150)
                {
                    entityManager->addEntity(new Sheeka(position));
                }
            }
        }
    }
    //
    //------------------------------------------------------------------------skapar objekt
    //

    image.loadFromFile("Maps/" + imageNameObject);

    for(int i = 0; i < image.getSize().x; ++i)
    {
        for(int j = 0; j < image.getSize().y; ++j)
        {
            sf::Color color = image.getPixel(i ,j);
            sf::Vector2f position = sf::Vector2f(i * 64.f, j *64.f);

            // Skapar Object vid Red = 200
            if(color.r == 200 && color.a == 255)
            {
                if(color.g == 0) // höger :-P Sista färgen avgör skut intervallet i tiondels sekunder
                {
                    entityManager->addEntity(new ShottingTrap(position, color.b, true, false));
                }
                else if(color.g == 50) // vänster :-b
                {
                    entityManager->addEntity(new ShottingTrap(position,color.b ,true, true));
                }
                else if(color.g == 100)
                {
                    entityManager->addEntity(new SpikeBlock(position));
                }
                else if(color.g == 150)// liten som föjrer bakgrund
                {
                    entityManager->addEntity(new Decoration(position, color.b, false, true));
                }
                else if(color.g == 155)// stor som följer bakgrund
                {
                    entityManager->addEntity(new Decoration(position, color.b, true, true));
                }
                else if(color.g == 160)// liten som INTE föjrer bakgrund
                {
                    entityManager->addEntity(new Decoration(position, color.b, false, false));
                }
                else if(color.g == 170)// stor som INTE följer bakgrund
                {
                    entityManager->addEntity(new Decoration(position, color.b, true, false));
                }
                else if(color.g == 180)// större som föjrer bakgrund
                {
                    entityManager->addEntity(new Decoration(position, color.b, false, true, true));
                }
                else if(color.g == 190)// större som INTE följer bakgrund
                {
                    entityManager->addEntity(new Decoration(position, color.b, false, false, true));
                }
                else if(color.g == 250)
                {
                    entityManager->addEntity(new TutorialSign(position, color.b));
                }
            }
        }
    }
}