RuneHive-Game
Loading...
Searching...
No Matches
CombatStrategy.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.combat.strategy;
2
3import com.runehive.game.Animation;
4import com.runehive.game.Graphic;
5import com.runehive.game.Projectile;
6import com.runehive.game.world.entity.combat.CombatType;
7import com.runehive.game.world.entity.combat.attack.FightType;
8import com.runehive.game.world.entity.combat.attack.FormulaFactory;
9import com.runehive.game.world.entity.combat.attack.listener.CombatListener;
10import com.runehive.game.world.entity.combat.hit.CombatHit;
11import com.runehive.game.world.entity.combat.hit.Hit;
12import com.runehive.game.world.entity.combat.projectile.CombatProjectile;
13import com.runehive.game.world.entity.mob.Mob;
14
15import java.util.Optional;
16
17import static com.runehive.game.world.entity.combat.CombatUtil.getHitDelay;
18import static com.runehive.game.world.entity.combat.CombatUtil.getHitsplatDelay;
19import static com.runehive.game.world.entity.combat.attack.FormulaFactory.getMaxHit;
20
21public abstract class CombatStrategy<T extends Mob> implements CombatListener<T> {
22
23 public abstract int getAttackDelay(T attacker, Mob defender, FightType fightType);
24
25 public abstract int getAttackDistance(T attacker, FightType fightType);
26
27 public abstract CombatHit[] getHits(T attacker, Mob defender);
28
29 public abstract Animation getAttackAnimation(T attacker, Mob defender);
30
31 @Override
32 public abstract boolean canAttack(T attacker, Mob defender);
33
34 @Override
35 public boolean canOtherAttack(Mob attacker, T defender) {
36 return true;
37 }
38
39 @Override
40 public void start(T attacker, Mob defender, Hit[] hits) {
41 }
42
43 @Override
44 public void attack(T attacker, Mob defender, Hit hit) {
45 }
46
47 @Override
48 public void hit(T attacker, Mob defender, Hit hit) {
49 }
50
51 @Override
52 public void hitsplat(T attacker, Mob defender, Hit hit) {
53 }
54
55 @Override
56 public void block(Mob attacker, T defender, Hit hit, CombatType combatType) {
57 }
58
59 @Override
60 public void preDeath(Mob attacker, T defender, Hit hit) {
61 }
62
63 @Override
64 public void onDeath(Mob attacker, T defender, Hit hit) {
65 }
66
67 @Override
68 public void preKill(Mob attacker, Mob defender, Hit hit) {
69 }
70
71 @Override
72 public void onKill(T attacker, Mob defender, Hit hit) {
73 }
74
75 @Override
76 public void finishOutgoing(T attacker, Mob defender) {
77 }
78
79 @Override
80 public void finishIncoming(Mob attacker, T defender) {
81 }
82
83 public abstract CombatType getCombatType();
84
85 /* ********** SIMPLIFIED ********** */
86
87 protected CombatHit nextMeleeHit(T attacker, Mob defender) {
88 int max = getMaxHit(attacker, defender, CombatType.MELEE);
89 return nextMeleeHit(attacker, defender, max, false);
90 }
91
92 protected final CombatHit nextRangedHit(T attacker, Mob defender) {
93 int max = getMaxHit(attacker, defender, CombatType.RANGED);
94 return nextRangedHit(attacker, defender, max);
95 }
96
97 protected final CombatHit nextMagicHit(T attacker, Mob defender) {
98 int max = getMaxHit(attacker, defender, CombatType.MAGIC);
99 return nextMagicHit(attacker, defender, max);
100 }
101
102 /* ********** MAX HITS ********** */
103
104 protected CombatHit nextMeleeHit(T attacker, Mob defender, int max) {
105 int hitDelay = getHitDelay(attacker, defender, CombatType.MELEE);
106 int hitsplatDelay = getHitsplatDelay(CombatType.MELEE);
107 return nextMeleeHit(attacker, defender, max, hitDelay, hitsplatDelay, false);
108 }
109
110 protected CombatHit nextMeleeHit(T attacker, Mob defender, int max, boolean multipleHitsAllowed) {
111 int hitDelay = getHitDelay(attacker, defender, CombatType.MELEE);
112 int hitsplatDelay = getHitsplatDelay(CombatType.MELEE);
113 return nextMeleeHit(attacker, defender, max, hitDelay, hitsplatDelay, multipleHitsAllowed);
114 }
115
116 protected final CombatHit nextRangedHit(T attacker, Mob defender, int max) {
117 int hitDelay = getHitDelay(attacker, defender, CombatType.RANGED);
118 int hitsplatDelay = getHitsplatDelay(CombatType.RANGED);
119 return nextRangedHit(attacker, defender, max, hitDelay, hitsplatDelay);
120 }
121
122 protected final CombatHit nextMagicHit(T attacker, Mob defender, int max) {
123 int hitDelay = getHitDelay(attacker, defender, CombatType.MAGIC);
124 int hitsplatDelay = getHitsplatDelay(CombatType.MAGIC);
125 return nextMagicHit(attacker, defender, max, hitDelay, hitsplatDelay);
126 }
127
128 /* ********** MAX HITS & COMBAT PROJECTILES ********** */
129
130 protected final CombatHit nextRangedHit(T attacker, Mob defender, int max, CombatProjectile projectile) {
131 int hitDelay = getHitDelay(attacker, defender, CombatType.RANGED);
132 int hitsplatDelay = getHitsplatDelay(CombatType.RANGED);
133 hitDelay = projectile.getHitDelay().orElse(hitDelay);
134 hitsplatDelay = projectile.getHitsplatDelay().orElse(hitsplatDelay);
135 return nextRangedHit(attacker, defender, max, hitDelay, hitsplatDelay);
136 }
137
138 protected CombatHit nextMagicHit(T attacker, Mob defender, int max, CombatProjectile projectile) {
139 int hitDelay = getHitDelay(attacker, defender, CombatType.MAGIC);
140 int hitsplatDelay = getHitsplatDelay(CombatType.MAGIC);
141 hitDelay = projectile.getHitDelay().orElse(hitDelay);
142 hitsplatDelay = projectile.getHitsplatDelay().orElse(hitsplatDelay);
143 return nextMagicHit(attacker, defender, max, hitDelay, hitsplatDelay);
144 }
145
146 /* ********** COMBAT PROJECTILES ********** */
147
148 protected final CombatHit nextRangedHit(T attacker, Mob defender, CombatProjectile projectile) {
149 int max = getMaxHit(attacker, defender, CombatType.RANGED);
150 return nextRangedHit(attacker, defender, max, projectile);
151 }
152
153 protected CombatHit nextMagicHit(T attacker, Mob defender, CombatProjectile projectile) {
154 int max = projectile.getMaxHit();
155 return nextMagicHit(attacker, defender, max, projectile);
156 }
157
158 /* ********** BASE METHODS ********** */
159
160 protected CombatHit nextMeleeHit(T attacker, Mob defender, int max, int hitDelay, int hitsplatDelay) {
161 return nextMeleeHit(attacker, defender, max, hitDelay, hitsplatDelay, false);
162 }
163
164 protected CombatHit nextMeleeHit(T attacker, Mob defender, int max, int hitDelay, int hitsplatDelay, boolean multipleHitsAllowed) {
165 Hit hit = FormulaFactory.nextMeleeHit(attacker, defender, max, multipleHitsAllowed);
166
167 if(hit.getMultipleHits() != null) // && maxHits > 1
168 return new CombatHit(hit.getMultipleHits(), hitDelay, hitsplatDelay);
169
170 return new CombatHit(hit, hitDelay, hitsplatDelay);
171 }
172
173 protected CombatHit nextRangedHit(T attacker, Mob defender, int max, int hitDelay, int hitsplatDelay) {
174 return new CombatHit(FormulaFactory.nextRangedHit(attacker, defender, max), hitDelay, hitsplatDelay);
175 }
176
177 protected final CombatHit nextMagicHit(T attacker, Mob defender, int max, int hitDelay, int hitsplatDelay) {
178 return new CombatHit(FormulaFactory.nextMagicHit(attacker, defender, max), hitDelay, hitsplatDelay);
179 }
180
181 @Override
182 public void onDamage(T defender, Hit hit) {
183
184 }
185
186 public static int getProjectileDuration(final CombatProjectile combatProjectile) {
187 final Optional<Projectile> optProjectile = combatProjectile.getProjectile();
188 if (optProjectile.isPresent()) {
189 final Projectile projectile = optProjectile.get();
190 return projectile.getClientTicks();
191 }
192 return 0;
193 }
194
195 public static Graphic getEndGraphic(final CombatProjectile combatProjectile,
196 final boolean splash, final Graphic splashGraphic) {
197 final int duration = getProjectileDuration(combatProjectile);
198 return getEndGraphic(combatProjectile.getEnd(), splash, splashGraphic, duration);
199 }
200
201 public static Graphic getEndGraphic(final CombatProjectile combatProjectile,
202 final boolean splash, final Graphic splashGraphic,
203 final int duration) {
204 return getEndGraphic(combatProjectile.getEnd(), splash, splashGraphic, duration);
205 }
206
207 public static Graphic getEndGraphic(final Optional<Graphic> end,
208 final boolean splash, final Graphic splashGraphic,
209 final int duration) {
210 final Graphic base = splash ? splashGraphic : end.orElse(null);
211 return base == null ? null : new Graphic(base.getId(), duration, base.getHeight());
212 }
213
214 public static boolean missed(final Hit... hits) {
215 for (final Hit hit : hits) {
216 if (hit.isAccurate()) {
217 return false;
218 }
219 }
220 return true;
221 }
222
224 return null;
225 }
226
227 public boolean isAlwaysAccurate() {
228 return false;
229 }
230
231}
Class that models a single animation used by an entity.
Represents a single graphic that can be used by entities.
Definition Graphic.java:10
int getId()
Gets the id of this graphic.
Definition Graphic.java:185
int getHeight()
Gets the height of this graphic.
Definition Graphic.java:176
Supplies factory methods useful for combat.
static Hit nextRangedHit(Mob attacker, Mob defender, int max)
static Hit nextMagicHit(Mob attacker, Mob defender, int max)
A wrapper for a Hit object, adding additional variables for hit and hitsplat delays.
A Hit object holds the damage amount and hitsplat data.
Definition Hit.java:10
void preKill(Mob attacker, Mob defender, Hit hit)
Called before attacker killed defender.
void finishOutgoing(T attacker, Mob defender)
Called when the defending mob finishes their strategy's attack.
abstract int getAttackDelay(T attacker, Mob defender, FightType fightType)
abstract Animation getAttackAnimation(T attacker, Mob defender)
boolean canOtherAttack(Mob attacker, T defender)
Checks if the attacker can attack the defender.
CombatHit nextMeleeHit(T attacker, Mob defender, int max, int hitDelay, int hitsplatDelay)
void onDeath(Mob attacker, T defender, Hit hit)
Called when the defending mob dies.
CombatHit nextMeleeHit(T attacker, Mob defender, int max, int hitDelay, int hitsplatDelay, boolean multipleHitsAllowed)
final CombatHit nextMagicHit(T attacker, Mob defender, int max, int hitDelay, int hitsplatDelay)
CombatHit nextMeleeHit(T attacker, Mob defender, int max)
CombatHit nextMeleeHit(T attacker, Mob defender, int max, boolean multipleHitsAllowed)
CombatHit nextMagicHit(T attacker, Mob defender, int max, CombatProjectile projectile)
void preDeath(Mob attacker, T defender, Hit hit)
Called right before the defending mob dies.
final CombatHit nextRangedHit(T attacker, Mob defender, int max)
abstract int getAttackDistance(T attacker, FightType fightType)
static Graphic getEndGraphic(final Optional< Graphic > end, final boolean splash, final Graphic splashGraphic, final int duration)
final CombatHit nextRangedHit(T attacker, Mob defender, CombatProjectile projectile)
void attack(T attacker, Mob defender, Hit hit)
Called when the attacking hit executes on the defender.
static Graphic getEndGraphic(final CombatProjectile combatProjectile, final boolean splash, final Graphic splashGraphic, final int duration)
final CombatHit nextRangedHit(T attacker, Mob defender, int max, CombatProjectile projectile)
void start(T attacker, Mob defender, Hit[] hits)
Called when the strategy starts.
final CombatHit nextMagicHit(T attacker, Mob defender, int max)
final CombatHit nextRangedHit(T attacker, Mob defender)
abstract boolean canAttack(T attacker, Mob defender)
Checks if the attacker can attack the defender.
void block(Mob attacker, T defender, Hit hit, CombatType combatType)
Called when the defending mob blocks a hit from the attacker.
void finishIncoming(Mob attacker, T defender)
Called when the attacking mob finishes their strategy's attack.
final CombatHit nextMagicHit(T attacker, Mob defender)
void hitsplat(T attacker, Mob defender, Hit hit)
Called when attacker does the hitsplat.
void hit(T attacker, Mob defender, Hit hit)
Called when the attacking mob performs an attack on the defender.
CombatHit nextRangedHit(T attacker, Mob defender, int max, int hitDelay, int hitsplatDelay)
void onKill(T attacker, Mob defender, Hit hit)
Called when attacker killed defender.
CombatHit nextMagicHit(T attacker, Mob defender, CombatProjectile projectile)
static int getProjectileDuration(final CombatProjectile combatProjectile)
abstract CombatHit[] getHits(T attacker, Mob defender)
static Graphic getEndGraphic(final CombatProjectile combatProjectile, final boolean splash, final Graphic splashGraphic)
Handles the mob class.
Definition Mob.java:66
The enumerated type whose elements represent the fighting types.
A combat attack is used to describe what the attacking and defending mobs should do in each stage of ...