RuneHive-Game
Loading...
Searching...
No Matches
ActionManager.java
Go to the documentation of this file.
1package com.runehive.game.action;
2
3import com.runehive.game.action.policy.WalkablePolicy;
4import com.runehive.game.world.World;
5import com.runehive.game.world.entity.mob.Mob;
6
7import java.util.ArrayDeque;
8import java.util.Objects;
9import java.util.Queue;
10
11/**
12 * The class which manages {@link Action}s executed by mobs.
13 *
14 * @author <a href="http://www.rune-server.org/members/stand+up/">Stand Up</a>
15 */
16public final class ActionManager {
17
18 /** A queue of {@link Action} object. */
19 private final Queue<Action<?>> queuedActions = new ArrayDeque<>();
20
21 /** The current action. */
22 private Action<?> currentAction = null;
23
24 /** Sequences the pending actions, and as soon as the current action has stopped, it executes the head of the queue. */
25 public void sequence() {
26 if (queuedActions.isEmpty() || (currentAction != null && currentAction.isRunning())) {
27 return;
28 }
29
30 execute(this.currentAction = queuedActions.poll(), true);
31 }
32
33 /** Queues the specified {@code action}. */
34 public <A extends Action<?>> void queue(A action) {
36 }
37
38 public <A extends Action<?>> void execute(A action) {
39 execute(action, true);
40 }
41
42 /** Adds an <code>Action</code> to the queue. */
43 public <A extends Action<?>> void execute(A action, boolean override) {
44 //if current action is priorized stop.
45 if (currentAction != null && currentAction.isRunning() && currentAction.prioritized()) {
46 return;
47 }
48
49 //if there is already an action active and we don't want to override
50 //we ignore.
51 //if u don't want to override but you also dont want to ignore
52 //queue the action.
53 if (currentAction != null && currentAction.isRunning() && !override) {
54 return;
55 }
56
57 //if there is an action active, make sure we stop it before running
58 //another action.
59 if (currentAction != null && currentAction.isRunning()) {
60 currentAction.cancel();
61 }
62
63 //finally submit the action.
65 }
66
67 /** Cancels all the queued actions by stopping them and removing all elements from the queue. */
68 private void cancelQueuedActions() {
70 queuedActions.clear();
71 }
72
73 /** Cancels the current {@link Action} for the underlying {@link Mob}. */
74 public void cancel() {
75 if (currentAction != null && currentAction.isRunning() && currentAction.prioritized()) {
76 currentAction.cancel();
77 currentAction = null;
78 }
79 }
80
81 /** Resets all the actions for the underlying {@link Mob}. */
82 public void reset() {
83 cancel();
85 }
86
87 /** Purges actions in the queue with a <code>WalkablePolicy</code> of <code>NON_WALKABLE</code>. */
89 final Action<?> currentAction = this.currentAction;
90 if (currentAction != null) {
91 if (!currentAction.cancellableInProgress()) {
92 return;
93 }
94 if (currentAction.getWalkablePolicy() == WalkablePolicy.NON_WALKABLE) {
95 currentAction.cancel();
96 this.currentAction = null;
97 }
98 }
99
100 for (Action<?> actionEvent : queuedActions) {
101 if (actionEvent.getWalkablePolicy() != WalkablePolicy.WALKABLE) {
102 actionEvent.cancel();
103 queuedActions.remove(actionEvent);
104 }
105 }
106 }
107
108 /** Purges actions in the queue with a <code>WalkablePolicy</code> of <code>NON_WALKABLE</code>. */
109 public void cancel(String name) {
110 if (currentAction != null && Objects.equals(currentAction.getName(), name)) {
111 currentAction.cancel();
112 currentAction = null;
113 }
114
115 for (Action<?> actionEvent : queuedActions) {
116 if (Objects.equals(actionEvent.getName(), name)) {
117 actionEvent.cancel();
118 queuedActions.remove(actionEvent);
119 }
120 }
121 }
122
123 /** Gets the current action. */
125 return currentAction;
126 }
127
128 @Override
129 public String toString() {
130 return String.format("ActionManager[size=%s, action=%s]", queuedActions.size(), currentAction);
131 }
132
133
134}
The class which manages Actions executed by mobs.
void cancel()
Cancels the current Action for the underlying Mob.
void clearNonWalkableActions()
Purges actions in the queue with a WalkablePolicy of NON_WALKABLE.
public< A extends Action<?> > void queue(A action)
Queues the specified action.
public< A extends Action<?> > void execute(A action, boolean override)
Adds an Action to the queue.
void cancelQueuedActions()
Cancels all the queued actions by stopping them and removing all elements from the queue.
void sequence()
Sequences the pending actions, and as soon as the current action has stopped, it executes the head of...
Action<?> getCurrentAction()
Gets the current action.
void reset()
Resets all the actions for the underlying Mob.
public< A extends Action<?> > void execute(A action)
Action<?> currentAction
The current action.
void cancel(String name)
Purges actions in the queue with a WalkablePolicy of NON_WALKABLE.
final Queue< Action<?> > queuedActions
A queue of Action object.
synchronized final void cancel()
Cancels all subsequent executions.
Definition Task.java:113
Represents the game world.
Definition World.java:46
static void schedule(Task task)
Submits a new event.
Definition World.java:247
A queue policy determines whether the action can occur while walking.
WALKABLE
This indicates actions may occur while walking.
NON_WALKABLE
This indicates actions cannot occur while walking.