RuneHive-Game
Loading...
Searching...
No Matches
NpcRangedStrategy.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.attack.FormulaFactory;
11import com.runehive.game.world.entity.combat.effect.impl.CombatPoisonEffect;
12import com.runehive.game.world.entity.combat.effect.impl.CombatVenomEffect;
13import com.runehive.game.world.entity.combat.hit.CombatHit;
14import com.runehive.game.world.entity.combat.hit.Hit;
15import com.runehive.game.world.entity.combat.projectile.CombatProjectile;
16import com.runehive.game.world.entity.combat.strategy.basic.RangedStrategy;
17import com.runehive.game.world.entity.mob.Mob;
18import com.runehive.game.world.entity.mob.npc.Npc;
19import com.runehive.util.RandomUtils;
20import org.jire.runehiveps.WorldTask;
21
22import javax.annotation.Nullable;
23import java.util.Optional;
24import java.util.function.Consumer;
25import java.util.function.Predicate;
26
27public class NpcRangedStrategy extends RangedStrategy<Npc> {
28
30
32 this.combatProjectile = combatProjectile;
33 }
34
35 @Override
39
40 @Override
41 public void start(Npc attacker, Mob defender, Hit[] hits) {
42 sendAnimation(attacker, defender);
43 sendProjectile(attacker, defender, null);
44 }
45
46 public void sendAnimation(Npc attacker, Mob defender) {
47 Animation animation = getAttackAnimation(attacker, defender);
48 if (animation.isReset()) {
49 Optional<Animation> projAnim = combatProjectile.getAnimation();
50 if (projAnim.isPresent()) animation = projAnim.get();
51 }
52
53 attacker.animate(animation, true);
54 combatProjectile.getStart().ifPresent(attacker::graphic);
55 }
56
57 public int sendProjectile(Mob from, Mob to, @Nullable Runnable onProjectileLand) {
58 final int duration = combatProjectile.sendProjectile(from, to);
59
60 final Graphic endGraphic = getEndGraphic(combatProjectile, false, null, duration);
61 if (endGraphic != null) to.graphic(endGraphic);
62
63 if (onProjectileLand != null) {
64 final int delay = GameEngine.clientTicksToServerTicks(duration);
65 WorldTask.schedule(delay, onProjectileLand);
66 }
67
68 return duration;
69 }
70
71 @Override
72 public void attack(Npc attacker, Mob defender, Hit hit) {
73 Predicate<CombatImpact> filter = effect -> effect.canAffect(attacker, defender, hit);
74 Consumer<CombatImpact> execute = effect -> effect.impact(attacker, defender, hit, null);
75 combatProjectile.getEffect().filter(filter).ifPresent(execute);
76
77 if (!attacker.definition.isPoisonous()) {
78 return;
79 }
80
81 if (CombatVenomEffect.isVenomous(attacker) && RandomUtils.success(0.25)) {
82 defender.venom();
83 } else {
84 CombatPoisonEffect.getPoisonType(attacker.id).ifPresent(defender::poison);
85 }
86 }
87
88 @Override
89 public CombatHit[] getHits(Npc attacker, Mob defender) {
90 int max = combatProjectile.getMaxHit();
91 if (max == -1)
92 max = FormulaFactory.getMaxHit(attacker, defender, getCombatType());
93 return new CombatHit[]{nextRangedHit(attacker, defender, max, combatProjectile)};
94 }
95
96 @Override
97 public int getAttackDelay(Npc attacker, Mob defender, FightType fightType) {
98 return attacker.definition.getAttackDelay();
99 }
100
101 @Override
102 public int getAttackDistance(Npc attacker, FightType fightType) {
103 return 10;
104 }
105
106 @Override
107 public Animation getAttackAnimation(Npc attacker, Mob defender) {
109 }
110
111 @Override
112 public boolean canAttack(Npc attacker, Mob defender) {
113 return true;
114 }
115
116 @Override
118 return CombatType.RANGED;
119 }
120
121}
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)
Supplies factory methods useful for combat.
static int getMaxHit(Mob attacker, Mob defender, CombatType type)
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 nextRangedHit(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)
int sendProjectile(Mob from, Mob to, @Nullable Runnable onProjectileLand)
int getAttackDelay(Npc attacker, Mob defender, FightType fightType)
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
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.