Skip to content

UIElementsDisplay

Shivam edited this page Oct 2, 2023 · 9 revisions

Introduction

UIElementsDisplay is a class that extends the UIComponent and adds important buttons and interactive menus for the user making the game area more appealing and interactive for the user. The main things implemented using this class include the remaining mobs button, wave timer, level progress slider and the interactive tower select screen.

The following page describes how everything is created and added to the main game screen.

Adding 'actors' using addActors()

This method of UIElementsDisplay handles positioning of tables, which are further used to place buttons/labels into the screen. For this implementation, two seperate tables namely buttonTable and towerTable are created housing (remainingMobsButton, timerButton) and the tower select buttons respectively.

In order to position the respective buttons, buttonTable is added to the top-right of the stage (which is essentially the display screen) using .top().right(), and the towerTable is placed in the middle-top using .top().

Further down, all the user selected towers are added to the tower select ui. A list containing default towers is created, which is used in the case no towers are selected by the user in the tower select screen. If the user selects towers of their choice in the tower select screen, the selected towers are stored in a list and used to create text buttons for each tower using the following code:

Array<TowerType> towers = new Array<>();
for (TowerType tower : ServiceLocator.getTowerTypes()) {
    towers.add(tower);
}

Moreover, each tower button is created using names of towers retrieved from the list formed earlier by the following code. The same format code is run for all different towers in the towers list.

TextButton tower1 = new TextButton(towers.get(0).getTowerName(), skin);

After each button is created, listeners are added for all buttons which trigger necessary events like currency deduction and spawning of the towers in the selected tile.

Towards the end of this method, all the created buttons (remaining mobs button and wave timer button) and tower buttons are added to their respective tables with appropriate padding. These buttons are further drawn by stage which houses the tables containing buttons.

Updating mob count: updateMobCount()

The updateMobCount() function uses WaveService and updates (rewrites) the text written on the remainingMobsButton. Text on any TextButton can be rewritten using the .setText() method. ServiceLocator.getWaveService().getEnemyCount() returns the mob count in that time instance.

Creating and updating the Timer Button: createTimerButton() and updateTimerButton()

As the name suggests the createTimerButton() creates the timer button. This is achieved using the ButtonFactory's createButton() method. In order to add this button to the stage, the same table (which was used for remainingMobsButton) is used however, the timerButton is added to a new row which is just below reaminingMobsButton. A new row is created using .row() method and the button is added using .add() with appropriate padding.

The updateTimerButton() method decrements the value of the timer based off how long the game has run for. An initial start value of 110 is used (which up to now is the minimum time required to complete this level) which gets decremented by 1 for every passing second in the game. This is done by calling the method every time game is rendered (that is, method is called in the render function of MainGameScreen.java). The final calculated value in minutes and seconds, based off of how much time has passed since the level started, is stored in a MM:SS format (where MM means minutes and SS means seconds) in finalTime variable which is added to the updated text using the setText() method.

Conclusion

UIElementsDisplay simplifies the process of incorporating various types of UI elements, such as text buttons, labels, or even intricate features like tower placement, with great ease. It offers the flexibility to customize the positioning and scaling of each element on the screen, streamlining the creation of UI components into a straightforward and efficient task.

Clone this wiki locally