RuneHive-Game
Loading...
Searching...
No Matches
NpcMagicStrategy.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.combat.strategy.npc;
2
3import com.runehive.game.Animation;
4import com.runehive.game.Graphic;
5import com.runehive.game.UpdatePriority;
6import com.runehive.game.engine.GameEngine;
7import com.runehive.game.world.entity.combat.CombatImpact;
8import com.runehive.game.world.entity.combat.CombatType;
9import com.runehive.game.world.entity.combat.attack.FightType;
10import com.runehive.game.world.entity.combat.effect.impl.CombatPoisonEffect;
11import com.runehive.game.world.entity.combat.effect.impl.CombatVenomEffect;
12import com.runehive.game.world.entity.combat.hit.CombatHit;
13import com.runehive.game.world.entity.combat.hit.Hit;
14import com.runehive.game.world.entity.combat.projectile.CombatProjectile;
15import com.runehive.game.world.entity.combat.strategy.basic.MagicStrategy;
16import com.runehive.game.world.entity.mob.Mob;
17import com.runehive.game.world.entity.mob.npc.Npc;
18import com.runehive.util.RandomUtils;
19import org.jire.runehiveps.WorldTask;
20
21import javax.annotation.Nullable;
22import java.util.Optional;
23import java.util.function.Consumer;
24import java.util.function.Predicate;
25
26public class NpcMagicStrategy extends MagicStrategy<Npc> {
27
29
31 this.combatProjectile = combatProjectile;
32 }
33
34 @Override
38
39 @Override
40 public void start(Npc attacker, Mob defender, Hit[] hits) {
41 sendAnimation(attacker, defender);
42 sendProjectile(attacker, hits, attacker, defender, null);
43 }
44
45 public void sendAnimation(Npc attacker, Mob defender) {
46 Animation animation = getAttackAnimation(attacker, defender);
47 if (animation.isReset()) {
48 Optional<Animation> projAnim = combatProjectile.getAnimation();
49 if (projAnim.isPresent()) animation = projAnim.get();
50 }
51
52 attacker.animate(animation, true);
53 combatProjectile.getStart().ifPresent(attacker::graphic);
54 }
55
56 public int sendProjectile(Npc attacker, Hit[] hits,
57 Mob from, Mob to,
58 @Nullable Runnable onProjectileLand) {
59 final int duration = combatProjectile.sendProjectile(from, to);
60
61 final Graphic endGraphic = getEndGraphic(combatProjectile, missed(hits), SPLASH, duration);
62 if (endGraphic != null) to.graphic(endGraphic);
63
64 for (Hit hit : hits) {
65 Predicate<CombatImpact> filter = effect -> effect.canAffect(attacker, to, hit);
66 Consumer<CombatImpact> execute = effect -> effect.impact(attacker, to, hit, null);
67 combatProjectile.getEffect().filter(filter).ifPresent(execute);
68
69 if (attacker.definition.isPoisonous()) {
70 if (CombatVenomEffect.isVenomous(attacker) && RandomUtils.success(0.25)) {
71 to.venom();
72 } else {
73 CombatPoisonEffect.getPoisonType(attacker.id).ifPresent(to::poison);
74 }
75 }
76 }
77
78 if (onProjectileLand != null) {
79 final int delay = GameEngine.clientTicksToServerTicks(duration);
80 WorldTask.schedule(delay, onProjectileLand);
81 }
82
83 return duration;
84 }
85
86 @Override
87 public CombatHit[] getHits(Npc attacker, Mob defender) {
88 return new CombatHit[]{nextMagicHit(attacker, defender, combatProjectile)};
89 }
90
91 @Override
92 public int getAttackDelay(Npc attacker, Mob defender, FightType fightType) {
93 int delay = attacker.definition.getAttackDelay();
94
95 if (attacker.getPosition().getDistance(defender.getPosition()) > 4) {
96 return 1 + delay;
97 }
98
99 return delay;
100 }
101
102 @Override
103 public int getAttackDistance(Npc attacker, FightType fightType) {
104 return 10;
105 }
106
107 @Override
108 public Animation getAttackAnimation(Npc attacker, Mob defender) {
110 }
111
112 @Override
113 public boolean canAttack(Npc attacker, Mob defender) {
114 return true;
115 }
116
117 @Override
119 return CombatType.MAGIC;
120 }
121
122}
Class that models a single animation used by an entity.
Represents a single graphic that can be used by entities.
Definition Graphic.java:10
static int clientTicksToServerTicks(final int clientTicks)
The combat effect applied when a character needs to be poisoned.
static Optional< PoisonType > getPoisonType(Item item)
Gets the PoisonType for item wrapped in an optional.
The combat effect applied when a character needs to be venomed.
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
final CombatHit nextMagicHit(T attacker, Mob defender)
void hit(T attacker, Mob defender, Hit hit)
Called when the attacking mob performs an attack on the defender.
static Graphic getEndGraphic(final CombatProjectile combatProjectile, final boolean splash, final Graphic splashGraphic)
static final Graphic SPLASH
The spell splash graphic.
int getAttackDelay(Npc attacker, Mob defender, FightType fightType)
int sendProjectile(Npc attacker, Hit[] hits, Mob from, Mob to, @Nullable Runnable onProjectileLand)
Handles the mob class.
Definition Mob.java:66
Optional< Graphic > graphic
Definition Mob.java:91
void venom()
Applies venom to the entity.
Definition Mob.java:508
Represents a non-player character in the in-game world.
Definition Npc.java:29
double getDistance(Position other)
Gets the distance between this location and another location.
A static-util class that provides additional functionality for generating pseudo-random numbers.
static boolean success(double value)
Determines if a pseudorandomly generated double rounded to two decimal places is below or equal to va...
Represents different priorities for updating.
The enumerated type whose elements represent the fighting types.