The teacher should start the previously developed game and observe how different actors behave. Suggest developing another type of tower that can be manually controlled to remove enemies more easily. The user should be able to control one tower at a time. When the tower is clicked, it should become manually controlled. To indicate which tower is manually controlled, the currently controlled tower should have a different appearance.
Since students already know how to make a descendant class, let them form teams and create a ManualTower class as a descendant of the Tower class. Students should implement both the constructors and the act method, ensuring that the super constructors are called from these methods. In this part of the class, students will review the material, apply it, and improve their practical knowledge of inheritance.
(Team Collaboration and Coding)
The teacher should prepare icons for the manually controlled tower. To change the icon of the object programmatically, the teacher should explain to the students how to use the Actor.setImage(String) method. Allow students some time to test this function.
The teacher should discuss with students how to determine whether the tower is manually controlled. Emphasize that it is not only important to change the object's state but also to update its image. Highlight that if a user wants to change the state of a Tower object and only changes the attribute directly, the image will remain the same. This discussion should help students understand the need to change the value of an attribute through a method and to keep attributes private rather than public. Explain to the students that this practice is called encapsulation, where the internal state is hidden, and public methods are used to change that state in a controlled manner.
Allow the students to implement the logic of the function. Let them manually invoke their method and observe changes in the internal state.
The teacher should point out that the tower's state can be changed only by manually invoking the method. Point out that the mouse could be outside the world, in which case the mouse information will be null. Remind students that the act() method is constantly running during the game and that it should check whether the object has been clicked and only then invoke the changeControl() method. Highlight that the logic for processing the control should be encapsulated inside a separate methodprocessUserControl().
After defining the processUserControl() private method, let students implement its logic. When the mouse is clicked, the controlled tower should change. If the tower is manually controlled, it should follow and be directed towards the mouse. Remind them that it is possible for mouse to be outside the world. After grouping students into teams, let them implement the logic of the processUserControl() method.
One or two teams will present their work, and the group will discuss the results along with the teacher.By the end of the session, all students should understand how this method is implemented.
(Team Formation and Project Assignment)
Add evidence of manually controlled tower.
To track which tower is currently selected in the game, add private static attribute-controlledInstance to the ManualTower class and initialize it to null. Static attribute is related to the whole class, not to an object of a class.Hence, defining a static variable will allow us to determinewhether tower has been selected and, if so, which one, byreferencing theclass name, without needing to access aspecific object. The teacher should emphasize that there is one controlledInstancefor the whole game. At the beginning, controlledInstance should be initialized to null, as there is no selected tower. Inspect the internal state of class. Here the teacher explains differences between static and non-static attributes. The teacher with studentsdiscussbenefits of using static attributes in games. Teacher should also mention here static methods and discuss with the students where usingstatic methods is beneficial.
Present the concept of class methods that can operate on class-level data.Discuss the need for methods like changeControlledInstance to manage switching the currently controlled tower. Emphasize that these methods can be called without needing an instance of the class. For example, school bell rings for everyone at the same time, it doesn’t matter who you are, on the other hand, checking a student’s homework requires information about that specific student.
Change of manually controlled tower from centralized place.
Teacher should add method changeControlledInstance to change manually controlled tower. Parameter of the method will be the tower user wants to select. First, it should be checked whether the controlled instance is currently selected. If it is, nothing should change, but if the passed instance is different than we should change currently controlled instance (reference to the currently controlled instance should be changed). Test out the function manually and observe that the icons of the towers don’t change. Point out that only changing the reference of the controlled instance,wouldn’t change the control and that it should be done manually. Add the code which releases the currently controlled instance and, after updating the reference, add code which sets manual control of newly controlled instance. Highlight the need for checking null references which could appear if there is no currently controlled instance and if there is no newly controlled instance (when the parameter is null).
Invoke change of manually controlled tower.
Manually test out the function whether it works correctly. Afterwards, discuss with the students where should this function be invoked. Method should be invoked inside of Arena’s act() function and inside of processUserControll() function. Lastly, make method ManualTower.changeControl(Boolean) private and observe changes of instance of ManualTower.