fr.quatrevieux.araknemu.game.fight.castable.effect.handler.armor.ReduceDamageHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 41
ccs 16
cts 16
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsElement(Buff,Element) 0 2 1
A onDirectDamage(Buff,Fighter,Damage) 0 16 3
A buff(FightCastScope,EffectScope) 0 4 2
A handle(FightCastScope,EffectScope) 0 3 1
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.armor;
21
22
import fr.quatrevieux.araknemu.data.constant.Characteristic;
23
import fr.quatrevieux.araknemu.game.fight.castable.FightCastScope;
24
import fr.quatrevieux.araknemu.game.fight.castable.effect.Element;
25
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffHook;
27
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
28
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
29
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
30
import fr.quatrevieux.araknemu.game.world.creature.characteristics.Characteristics;
31
32
/**
33
 * Handle reducing damages
34
 */
35 1
public final class ReduceDamageHandler implements EffectHandler, BuffHook {
36
    @Override
37
    public void handle(FightCastScope cast, FightCastScope.EffectScope effect) {
38 1
        throw new UnsupportedOperationException("ReduceDamageHandler can only be used as buff");
39
    }
40
41
    @Override
42
    public void buff(FightCastScope cast, FightCastScope.EffectScope effect) {
43 1
        for (Fighter target : effect.targets()) {
44 1
            target.buffs().add(new Buff(effect.effect(), cast.action(), cast.caster(), target, this));
45 1
        }
46 1
    }
47
48
    @Override
49
    public void onDirectDamage(Buff buff, Fighter caster, Damage value) {
50 1
        if (!supportsElement(buff, value.element())) {
51 1
            return;
52
        }
53
54 1
        final Characteristics characteristics = buff.target().characteristics();
55
56 1
        final int boost = 200 + characteristics.get(Characteristic.INTELLIGENCE) + characteristics.get(value.element().boost());
57 1
        final int reduce = buff.effect().min() * boost / 200;
58
59 1
        if (reduce < 0) {
60 1
            return;
61
        }
62
63 1
        value.reduce(reduce);
64 1
    }
65
66
    /**
67
     * Check if the armor supports the damage element
68
     *
69
     * @param buff The buff
70
     * @param element The damage element
71
     *
72
     * @return true if the armor supports the element
73
     */
74
    private boolean supportsElement(Buff buff, Element element) {
75 1
        return buff.effect().special() == 0 || Element.fromBitSet(buff.effect().special()).contains(element);
76
    }
77
}
78