Lab 7 GUI & Building
Step 1. Tower Builder
- Create a new game object for a tower builder.
- Add a Sprite renderer so players can see it.
- Add a script and call it TowerBuilder. This class will be responsible for picking the positions for new towers and building them.
- Turn it into a prefab.
- Modify the new TowerBuilder script.
- In the Update method:
//Reposition the gameobject to mouse coordinates. //Round the coordinates to make it snap to a grid. //Verify that building area is free of other towers. //By using a static overlap method from Physics2D class we can make this work without collider and a 2d rigidbody. //Tint the sprite to green or red accordingly. //Call the build method when the player presses left mouse button
- Implement a Build method
//Verify that building area is free of other towers. (Turn this into a method) //Make a note to remove gold from player later when gold is implemented //Instantiate a tower prefab at the current position //Disable the Tower Builder gameobject
Step 2. Fix building on roads.
- Create another tilemap for roads
- Add roads with a transparent background to the palette. You may need to fix the sprite import settings if the roads are not placed at a correct position within a tile.
- Redraw roads on the new tilemap.
- Add a tilemap collider.
- Test that building does not work on tiles that are part of this new tilemap layer.
Step 3. Keyboard and GUI buttons for building
- Create a canvas
- Add a panel
- Add a grid layout group component.
- Add a button
- Add another image as a child for the building icon
- Position the text so that it could show keyboard hotkey of the button
- Add another text on the button to show the gold cost.
- Make a click on UI button enter building mode. (Enable TowerBuilder gameobject)
- Add a new BuildKey script to the button.
- Add gold value and keycode for the hotkey.
- Write the gold value and keycode to the button in Start.
- Use Update to check for the keycode and enter build mode.
- Turn the button into a prefab
Step 4. Controlling the game loop
- Create a new ScenarioController script.
- Add it to the canvas gameobject
- Create a panel for lives and gold. It should have value and label texts for gold and lives.
- Create methods in scenario controller for:
- Winning the level
- Losing the level
- Starting the level
- Adding gold
- Spending gold - Call when trying to build.
- Adding lives
- Losing lives - Call when an enemy reaches the base.
- Implement the previous methods. Practice using event-based architecture (taught in lab 5).
This is the end of lab 7. We will tie the scenario methods together with level loading and add configurable data objects in the next lab.