flowi
JCHJava Community | Help. Code. Learn.
•Created by flowi on 4/26/2025 in #java-help
Am I violating Open Closed Principle ?
public class ShadowBeast extends Actor {
private Map<Integer, Behaviour> activityPriorities = new TreeMap<>();
public ShadowBeast() {
super("Shadow Beast", 'b', 75);
activityPriorities.put(0, new AmbushBehaviour()); // Add behaviour with priority 0
activityPriorities.put(1, new ProwlBehaviour()); // Add behaviour with priority 1
}
/**
* Selects and returns an action for the ShadowBeast's turn.
* Iterates through its behaviours based on priority.
*
* @param actions Collection of possible Actions for this Actor
* @param map The map containing the Actor
* @return The Action to be performed, or DontDoAnythingAction
*/
@Override
public Action playTurn(ActionList actions, Action lastAction, GameMap map, Display display) {
for (Behaviour currentActivity : activityPriorities.values()) {
Action action = currentActivity.getAction(this, map);
if (action != null) {
return action;
}
}
// No suitable behaviour found
return new DontDoAnythingAction();
public class ShadowBeast extends Actor {
private Map<Integer, Behaviour> activityPriorities = new TreeMap<>();
public ShadowBeast() {
super("Shadow Beast", 'b', 75);
activityPriorities.put(0, new AmbushBehaviour()); // Add behaviour with priority 0
activityPriorities.put(1, new ProwlBehaviour()); // Add behaviour with priority 1
}
/**
* Selects and returns an action for the ShadowBeast's turn.
* Iterates through its behaviours based on priority.
*
* @param actions Collection of possible Actions for this Actor
* @param map The map containing the Actor
* @return The Action to be performed, or DontDoAnythingAction
*/
@Override
public Action playTurn(ActionList actions, Action lastAction, GameMap map, Display display) {
for (Behaviour currentActivity : activityPriorities.values()) {
Action action = currentActivity.getAction(this, map);
if (action != null) {
return action;
}
}
// No suitable behaviour found
return new DontDoAnythingAction();
3 replies