Test Failed
Branch master (cd3a65)
by Vincent
11:52
created

applyDamage(ActiveFighter,Damage,PassiveFighter)   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage;
21
22
import fr.quatrevieux.araknemu.data.constant.Characteristic;
23
import fr.quatrevieux.araknemu.game.fight.Fight;
24
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectValue;
25
import fr.quatrevieux.araknemu.game.fight.castable.effect.Element;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
27
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
28
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
29
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
30
import fr.quatrevieux.araknemu.network.game.out.fight.action.ActionEffect;
31
32
/**
33
 * Apply simple damage to fighter
34
 *
35
 * Returns the effect damage value.
36
 * When applies damage, a negative value will be returned (-50 => The target lose 50 LP)
37
 * When no effect, zero will be returned
38
 * When damage is transformed to heal, will return a positive value (50 => The target win 50 LP)
39
 */
40
public final class DamageApplier {
41
    private final Element element;
42
    private final Fight fight;
43
44 1
    public DamageApplier(Element element, Fight fight) {
45 1
        this.element = element;
46 1
        this.fight = fight;
47 1
    }
48
49
    /**
50
     * Apply a direct damage effect to a fighter
51
     *
52
     * Note: do not use this method for a buff, it will call the invalid buff hook
53
     *
54
     * @param caster The spell caster
55
     * @param effect The effect to apply
56
     * @param target The target
57
     *
58
     * @return The real damage value
59
     *
60
     * @see DamageApplier#apply(Buff) For apply a buff damage (i.e. poison)
61
     * @see fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buffs#onDirectDamage(ActiveFighter, Damage) The called buff hook
62
     */
63
    public int apply(ActiveFighter caster, SpellEffect effect, PassiveFighter target) {
64 1
        final Damage damage = computeDamage(caster, effect, target);
65 1
        target.buffs().onDirectDamage(caster, damage);
66
67 1
        return applyDamage(caster, damage, target);
68
    }
69
70
    /**
71
     * Apply a damage buff effect
72
     *
73
     * @param buff Buff to apply
74
     *
75
     * @return The real damage value
76
     *
77
     * @see fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buffs#onBuffDamage(Buff, Damage) The called buff hook
78
     */
79
    public int apply(Buff buff) {
80 1
        final PassiveFighter target = buff.target();
81 1
        final ActiveFighter caster = buff.caster();
82
83 1
        final Damage damage = computeDamage(caster, buff.effect(), target);
84 1
        target.buffs().onBuffDamage(buff, damage);
85
86 1
        return applyDamage(caster, damage, target);
87
    }
88
89
    /**
90
     * Create the damage object
91
     */
92
    private Damage computeDamage(ActiveFighter caster, SpellEffect effect, PassiveFighter target) {
93 1
        final EffectValue value = new EffectValue(effect)
94 1
            .percent(caster.characteristics().get(element.boost()))
95 1
            .percent(caster.characteristics().get(Characteristic.PERCENT_DAMAGE))
96 1
            .fixed(caster.characteristics().get(Characteristic.FIXED_DAMAGE))
97
        ;
98
99 1
        return new Damage(value.value(), element)
100 1
            .percent(target.characteristics().get(element.percentResistance()))
101 1
            .fixed(target.characteristics().get(element.fixedResistance()))
102
        ;
103
    }
104
105
    /**
106
     * Apply the damage object to the target
107
     *
108
     * @return The life change value. Negative for damage, positive for heal.
109
     */
110
    private int applyDamage(ActiveFighter caster, Damage damage, PassiveFighter target) {
111 1
        if (damage.reducedDamage() > 0) {
112 1
            fight.send(ActionEffect.reducedDamage(target, damage.reducedDamage()));
113
        }
114
115
        // @todo returned damage
0 ignored issues
show
introduced by
Comment matches to-do format '(TODO:)|(@todo )'.
Loading history...
116
117 1
        return target.life().alter(caster, -damage.value());
118
    }
119
}
120