Пример #1
0
/* Updating */
void HUD::update(int life, int energy, int ticsFusRoDah)
{
    // Player Life
    for (unsigned int i = 0; i < playerLife.size(); i++) {
        // Action
        if (life > (int)i) playerLife[i].setTextureIndex(GameData::LIFEFULL_HUD_TEXTURE_INDEX);
        else playerLife[i].setTextureIndex(GameData::LIFEEMPTY_HUD_TEXTURE_INDEX);
    }

    // Player energy
    while ((int)playerEnergy.size() > energy) playerEnergy.pop_back();
    while ((int)playerEnergy.size() < energy) {
        HUDElement element = HUDElement(screenWidth - (playerEnergy.size() + 1)*ENERGY_ELEMENT_SIZE, FUSRODAH_ELEMENT_HEIGHT + MARGIN_BETWEEN_ELEMENTS, ENERGY_ELEMENT_SIZE, ENERGY_ELEMENT_SIZE, GameData::ENERGY_HUD_TEXTURE_INDEX);
        playerEnergy.push_back(element);
    }

    // Instructions
    if (showInstr) {
        ticsInstr++;
        if (ticsInstr == TICS_CHANGE_INSTRUCTIONS) {
            int x = screenWidth/2 - INSTRUCTIONS_ELEMENT_WIDTH/2;
            instructions = HUDElement(x, 0, INSTRUCTIONS_ELEMENT_WIDTH, INSTRUCTIONS_ELEMENT_HEIGHT, GameData::INSTRUCTIONS_HUD_ACTIONS_INDEX);
        }
        else if (ticsInstr == TICS_CHANGE_INSTRUCTIONS*2) {
            int x = screenWidth/2 - INSTRUCTIONS_ELEMENT_WIDTH/2;
            instructions = HUDElement(x, 0, INSTRUCTIONS_ELEMENT_WIDTH, INSTRUCTIONS_ELEMENT_HEIGHT, GameData::INSTRUCTIONS_HUD_WIN_INDEX);
        }
        else if (ticsInstr == TICS_CHANGE_INSTRUCTIONS*3) showInstr = false;
    }

    // Fus-Ro-Dah
    fusRoDah.update(ticsFusRoDah);
}
Пример #2
0
Victory::Victory(float xPosition, float yPosition) : 
	WorldObject(AB_VICTORY, xPosition, yPosition),
	restartButton(Button(xPosition + AB_VICTORY_TEXT_OFFSET_X,
	yPosition + AB_VICTORY_TEXT_OFFSET_Y)),
	victoryText(HUDElement(xPosition + AB_VICTORY_MSG1_X, yPosition + AB_VICTORY_MSG1_Y)),
	teamText(HUDElement(xPosition + AB_VICTORY_MSG2_X, yPosition + AB_VICTORY_MSG2_Y)) {
		auto armyengine = ArmyEngine::getInstance();
		auto componentFactory = armyengine->getComponentFactory();
		//setup the shape component to represent the border of the 
		// victory screen
		auto shapeComponent = componentFactory.createShapeComponent(AB_VICTORY_SHAPE);
		this->getEntity()->addComponent(shapeComponent);

		//shape attributes
		shapeComponent->setAttribute_int(ATTRIBUTE_ENABLE, 0);
		shapeComponent->setAttribute_string(ATTRIBUTE_SHAPE_TYPE, SHAPETYPE_RECTANGLE);
		shapeComponent->setAttribute_float(ATTRIBUTE_WIDTH, 400.0);
		shapeComponent->setAttribute_float(ATTRIBUTE_HEIGHT, 400.0);
		COLOR_FILL_BLACK(shapeComponent);
		COLOR_OUTLINE_GREEN(shapeComponent);
		shapeComponent->update();

		//button parameters
		restartButton.hide();
		restartButton.setButtonText("Restart");
		restartButton.setSize(200, 100);
		restartButton.setButtonCallback((functionEventTemplate) [] (int ID, int eventIndex) {
			auto entityManager = EntityManager::getInstance();
			auto entity = entityManager->getEntityById(ID);
			sf::Event theEvent = *EventManager::getInstance()->getEvents()[eventIndex];
			auto mouseButton = theEvent.mouseButton.button;
			if (mouseButton == sf::Mouse::Button::Left) {
				auto world = World::getInstance();
				world->menu.hide();
				world->victory.hide();
				world->generateWorld();
				EntityManager::getInstance()->sortEntityList();

#ifdef LOGGING
				std::cout << "Restart Button Pressed" << std::endl;
#endif //LOGGING
			}
			return 0;
		});

		//set the Z-buffer of the text to show
		auto textComponent = restartButton.buttonHUDElement.getEntity()->getComponentByName(AB_HUD_TEXT);
		textComponent->setAttribute_float(ATTRIBUTE_ZBUFFER, 16.0);

		//change the text size for the restart button
		restartButton.buttonHUDElement.setTextSize(50);

		//text parameters
		victoryText.setSuccessString(AB_VICTORY_MSG1);
		victoryText.hide();
		teamText.setSuccessString("");
		teamText.hide();

}
Пример #3
0
/* Loading */
bool HUD::load(int screenWidth, int screenHeight, int maxLife)
{
    this->screenWidth = screenWidth;
    this->screenHeight = screenHeight;

    // HUD Life
    playerLife = std::vector<HUDElement>(maxLife);
    for (int i = 0; i < maxLife; i++) {
        playerLife[i] = HUDElement(i*LIFE_ELEMENT_SIZE, 0, LIFE_ELEMENT_SIZE, LIFE_ELEMENT_SIZE, GameData::LIFEFULL_HUD_TEXTURE_INDEX);
    }

    // First instructions
    int x = screenWidth/2 - INSTRUCTIONS_ELEMENT_WIDTH/2;
    instructions = HUDElement(x, 0, INSTRUCTIONS_ELEMENT_WIDTH, INSTRUCTIONS_ELEMENT_HEIGHT, GameData::INSTRUCTIONS_HUD_MOVE_INDEX);

    // Fus-Ro-Dah
    fusRoDah = FusRoDahHUDElement(screenWidth - FUSRODAH_ELEMENT_WIDTH, 0, FUSRODAH_ELEMENT_WIDTH, FUSRODAH_ELEMENT_HEIGHT, GameData::FUSRODAH_HUD_TEXTURE_INDEX);

    return true;
}
Пример #4
0
Button::Button(float xPosition, float yPosition) :
	WorldObject(AB_BUTTON, xPosition, yPosition),
	buttonHUDElement(HUDElement(xPosition, yPosition, BUTTON_FONT_SIZE)),
	buttonText("Test message") {
		this->setButtonText(buttonText);

		//change the Z-Buffer of our button to display over the
		//  the button
		auto textComponent_HUD = this->buttonHUDElement.getEntity()->getComponentByName(AB_HUD_TEXT);
		textComponent_HUD->setAttribute_float(ATTRIBUTE_ZBUFFER, 4.0);
		textComponent_HUD->update();

		auto armyEngine = ArmyEngine::getInstance();

		auto entity = this->getEntity();

		auto componentFactory = armyEngine->getComponentFactory();
		auto shapeComponent = componentFactory.createShapeComponent(AB_BUTTON_SHAPE);
		entity->addComponent(shapeComponent);

		//shape attributes
		shapeComponent->setAttribute_int(ATTRIBUTE_ENABLE, 1);
		shapeComponent->setAttribute_string(ATTRIBUTE_SHAPE_TYPE, SHAPETYPE_RECTANGLE);
		shapeComponent->setAttribute_float(ATTRIBUTE_WIDTH, BUTTON_DEFAULT_WIDTH);
		shapeComponent->setAttribute_float(ATTRIBUTE_HEIGHT, BUTTON_DEFAULT_HEIGHT);
		shapeComponent->setAttribute_int(ATTRIBUTE_OUTLINE_COLOR_RED, 0);
		shapeComponent->setAttribute_int(ATTRIBUTE_OUTLINE_COLOR_BLUE, 255);
		shapeComponent->setAttribute_int(ATTRIBUTE_OUTLINE_COLOR_GREEN, 0);
		shapeComponent->setAttribute_int(ATTRIBUTE_OUTLINE_COLOR_ALPHA, 255);
		shapeComponent->setAttribute_float(ATTRIBUTE_OUTLINE_THICKNESS, BUTTON_DEFAULT_OUTLINE);
		shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_RED, 0);
		shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_BLUE, 128);
		shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_GREEN, 128);
		shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_ALPHA, 255);
		shapeComponent->setAttribute_float(ATTRIBUTE_OFFSET_X, 0.0);
		shapeComponent->setAttribute_float(ATTRIBUTE_OFFSET_Y, 0.0);
		shapeComponent->setAttribute_float(ATTRIBUTE_ZBUFFER, 12.0);
		shapeComponent->update();

		//collision component for mouse over focusing
		auto collisionComponent = componentFactory.createCollisionComponent(AB_BUTTON_COLLISION);
		entity->addComponent(collisionComponent);

		collisionComponent->setAttribute_int(ATTRIBUTE_ENABLE, 1);
		collisionComponent->setAttribute_string(ATTRIBUTE_COLLISION_BOUND_TYPE, COLLISION_BOUND_RECTANGLE);
		collisionComponent->setAttribute_float(ATTRIBUTE_WIDTH, BUTTON_DEFAULT_WIDTH);
		collisionComponent->setAttribute_float(ATTRIBUTE_HEIGHT, BUTTON_DEFAULT_HEIGHT);
		collisionComponent->setAttribute_string(ATTRIBUTE_COLLISION_TAG, AB_BUTTON);

		armyEngine->addCollisionCallback(collisionTagTuple(AB_BUTTON, AB_MOUSE),
			(functionCollisionTemplate) [] (collisionParamTuple paramTuple) {
				int entityID1, entityID2;
				std::string compName1, compName2;
				bool bRegistered;
				std::tie(entityID1, compName1, entityID2, compName2, bRegistered) = paramTuple;

				//grab the button entity
				auto buttonEntity = EntityManager::getInstance()->getEntityById(entityID1);

				auto shapeComponent = buttonEntity->getComponentByName(AB_BUTTON_SHAPE);
				auto inputComponent = buttonEntity->getComponentByName(AB_BUTTON_PRESSED);


				if (bRegistered) {
					//change the shape component to have new outline
					shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_RED, 128);
					shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_GREEN, 0);
					shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_BLUE, 0);
					//enable input component
					inputComponent->setAttribute_int(ATTRIBUTE_ENABLE, 1);

				}
				else {
					shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_RED, 0);
					shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_GREEN, 128);
					shapeComponent->setAttribute_int(ATTRIBUTE_FILL_COLOR_BLUE, 128);
					//disable input component
					inputComponent->setAttribute_int(ATTRIBUTE_ENABLE, 0);

				}
				shapeComponent->update();

				return 0;
		});

		auto inputComponent_mousePressed = componentFactory.createInputComponent(AB_BUTTON_PRESSED);
		entity->addComponent(inputComponent_mousePressed);
		inputComponent_mousePressed->setAttribute_int(ATTRIBUTE_ENABLE, 0);
		inputComponent_mousePressed->setAttribute_string(ATTRIBUTE_INPUT_TYPE, INPUT_MOUSE_PRESSED);
		inputComponent_mousePressed->setAttribute_string(ATTRIBUTE_CALLBACK, AB_BUTTON_PRESSED);

		armyEngine->addInputCallback(AB_BUTTON_PRESSED, (functionEventTemplate) [] (int ID, int eventIndex) {
			auto entityManager = EntityManager::getInstance();
			auto entity = entityManager->getEntityById(ID);
			sf::Event theEvent = *EventManager::getInstance()->getEvents()[eventIndex];
			auto mouseButton = theEvent.mouseButton.button;
			if (mouseButton == sf::Mouse::Button::Left) {
				std::cout << "ERROR: callback hasn't been set" << std::endl;
				exit(0);
			}
			return 0;
		});

}