Game Development Reference
In-Depth Information
How to do it...
Perform the following steps to select units in RTS:
1. Let's start by creating the Control class and name it SelectableControl . It
should extend AbstractControl .
2. The class only has two fields: selected, which keeps track of whether the spa-
tial field is selected or not (duh), and marker, which is another spatial field
to show when selected is true.
3. The only logic in the class is in the setSelected method; we let it handle at-
taching or detaching the marker:
public void setSelected(boolean selected) {
this.selected = selected;
if (marker != null) {
if (this.selected) {
((Node) spatial).attachChild(marker);
} else {
((Node) spatial).detachChild(marker);
}
}
}
Note
The method assumes that the spatial is actually a Node . If it is not a Node , the
class can do other things, such as changing the color parameter of Material to
indicate that it is selected.
4. We might want to display different markers for different types of selections, so let's
make it flexible by adding a setter method for the marker.
5. Now, we create a new AppState class called SelectAppState . It should ex-
tend AbstractAppState and implement ActionListener to receive
mouse click events.
6. We'll add two fields, one static string to represent the mouse click, and a
List<Spatial> called selectables where it will store anything that is se-
lectable, as follows:
Search WWH ::




Custom Search