RuneHive-Game
Loading...
Searching...
No Matches
SkillAction.java
Go to the documentation of this file.
1package com.runehive.content.skill;
2
3import com.runehive.game.Animation;
4import com.runehive.game.action.Action;
5import com.runehive.game.world.entity.mob.Mob;
6import com.runehive.game.world.position.Position;
7
8import java.util.Optional;
9
10/**
11 * @author <a href="http://www.rune-server.org/members/stand+up/">Stand Up</a>
12 * @since 19-12-2016.
13 */
14public abstract class SkillAction extends Action<Mob> {
15
16 /**
17 * The position we should face.
18 */
19 private final Optional<Position> position;
20
21 /**
22 * Creates a new {@link Action} randomevent.
23 *
24 * @param mob {@link #mob}.
25 * @param position {@link #position}.
26 * @param delay the delay to repeat this action on.
27 * @param instant {@link #instant}.
28 */
29 public SkillAction(Mob mob, Optional<Position> position, int delay, boolean instant) {
30 super(mob, delay, instant);
31 this.position = position;
32 }
33
34 /**
35 * Creates a new {@link Action} randomevent.
36 *
37 * @param mob {@link #mob}.
38 * @param position {@link #position}.
39 * @param instant {@link #instant}.
40 */
41 public SkillAction(Mob mob, Optional<Position> position, boolean instant) {
42 this(mob, position, 1, instant);
43 }
44
45 /**
46 * Attempts to start the skill.
47 */
48 public final void start() {
49 //determine if this task can be initialized.
50 if (!canInit()) {
51 return;
52 }
53
54 //reset any other action.
55 getMob().action.clearNonWalkableActions();
56
57 //submit this action.
58 getMob().action.execute(this, false);
59 position.ifPresent(getMob()::face);
60 }
61
62 /**
63 * Determines if this action can be initialized.
64 *
65 * @return {@code true} if it can, {@code false} otherwise.
66 */
67 public abstract boolean canInit();
68
69 /**
70 * Any functionality that should be handled when this action is submitted.
71 */
72 public abstract void init();
73
74 /**
75 * The method which is called on intervals of the specified {@code #delay};
76 */
77 public abstract void onExecute();
78
79 /**
80 * The skill animation to execute.
81 *
82 * @return the skill animation to execute.
83 */
84 public abstract Optional<SkillAnimation> animation();
85
86 /**
87 * The experience given from this action.
88 *
89 * @return the numerical value representing the amount of experience given.
90 */
91 public abstract double experience();
92
93 /**
94 * The skill we should hand to experience to.
95 */
96 public abstract int skill();
97
98 /**
99 * Determines if future skill actions from the same type should be ignored.
100 *
101 * @return
102 */
103 public boolean ignore() {
104 return false;
105 }
106
107 /**
108 * The animation counter of this task.
109 */
110 private int animationCounter;
111
112 @Override
113 protected final void onSchedule() {
114 if (!canRun() || !canInit()) {
115 this.cancel();
116 return;
117 }
118 if (animation().isPresent() && animation().get().instant) {
119 getMob().animate(animation().get().animation);
120 }
121 init();
122 }
123
124 @Override
125 protected final void execute() {
126 if (!canRun()) {
127 cancel();
128 return;
129 }
130
131 if (animation().isPresent() && animationCounter++ > animation().get().delay) {
132 getMob().animate(animation().get().animation);
134 }
135
136 onExecute();
137 }
138
139 /**
140 * A simple wrapper class which wraps an animation to a delay.
141 *
142 * @author <a href="http://www.rune-server.org/members/stand+up/">Stand
143 * Up</a>
144 * @since 19-12-2016.
145 */
146 public final class SkillAnimation {
147
148 /**
149 * The animation for this set.
150 */
151 public final Animation animation;
152
153 /**
154 * The delay for this set.
155 */
156 public final int delay;
157
158 /**
159 * Determines if this animation should run when the task is submitted.
160 */
161 public final boolean instant;
162
163 /**
164 * Constructs a new {@link SkillAnimation};
165 *
166 * @param animation {@link #animation}.
167 * @param delay {@link #delay}.
168 * @param instant {@link #instant}.
169 */
171 this.animation = animation;
172 this.delay = delay;
173 this.instant = instant;
174 }
175
176 /**
177 * Constructs a new {@link SkillAnimation};
178 *
179 * @param animation {@link #animation}.
180 * @param delay {@link #delay}.
181 */
183 this(animation, delay, true);
184 }
185 }
186}
SkillAnimation(Animation animation, int delay, boolean instant)
Constructs a new SkillAnimation;.
final boolean instant
Determines if this animation should run when the task is submitted.
SkillAnimation(Animation animation, int delay)
Constructs a new SkillAnimation;.
final Animation animation
The animation for this set.
final void onSchedule()
A function executed on registration.
SkillAction(Mob mob, Optional< Position > position, boolean instant)
Creates a new Action randomevent.
abstract Optional< SkillAnimation > animation()
The skill animation to execute.
final void execute()
A function representing the unit of work that will be carried out.
abstract void onExecute()
The method which is called on intervals of the specified #delay;.
SkillAction(Mob mob, Optional< Position > position, int delay, boolean instant)
Creates a new Action randomevent.
final void start()
Attempts to start the skill.
int animationCounter
The animation counter of this task.
final Optional< Position > position
The position we should face.
abstract double experience()
The experience given from this action.
abstract boolean canInit()
Determines if this action can be initialized.
boolean ignore()
Determines if future skill actions from the same type should be ignored.
abstract void init()
Any functionality that should be handled when this action is submitted.
abstract int skill()
The skill we should hand to experience to.
Class that models a single animation used by an entity.
T getMob()
Gets the player.
Definition Action.java:44
final T mob
The Mob associated with this ActionEvent.
Definition Action.java:15
Action(T mob, int delay, boolean instant)
Creates a new Action randomevent.
Definition Action.java:24
synchronized final void cancel()
Cancels all subsequent executions.
Definition Task.java:113
boolean canRun()
Determines if the task can be ran.
Definition Task.java:108
final boolean instant
If execution happens instantly upon being scheduled.
Definition Task.java:14
int delay
The cyclic delay.
Definition Task.java:17
Handles the mob class.
Definition Mob.java:66