Passed
Pull Request — master (#207)
by Vincent
10:53
created

fr.quatrevieux.araknemu.game.fight.fighter.BaseFighterLife   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 67
c 0
b 0
f 0
ccs 29
cts 29
cp 1
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A BaseFighterLife(Fighter,int,int) 0 4 1
A current() 0 3 1
A dead() 0 3 1
A max() 0 3 1
A BaseFighterLife(Fighter,int) 0 2 1
A kill(ActiveFighter) 0 9 2
A alter(PassiveFighter,int) 0 24 5
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.fighter;
21
22
import fr.quatrevieux.araknemu.game.fight.fighter.event.FighterDie;
23
import fr.quatrevieux.araknemu.game.fight.fighter.event.FighterLifeChanged;
24
25
/**
26
 * Handle life points for fighters
27
 */
28
public final class BaseFighterLife implements FighterLife {
29
    private final Fighter fighter;
30
31
    private int max;
32
    private int current;
33 1
    private boolean dead = false;
34
35 1
    public BaseFighterLife(Fighter fighter, int life, int max) {
36 1
        this.max = max;
37 1
        this.current = life;
38 1
        this.fighter = fighter;
39 1
    }
40
41
    public BaseFighterLife(Fighter fighter, int life) {
42 1
        this(fighter, life, life);
43 1
    }
44
45
    @Override
46
    public int current() {
47 1
        return current;
48
    }
49
50
    @Override
51
    public int max() {
52 1
        return max;
53
    }
54
55
    @Override
56
    public boolean dead() {
57 1
        return dead;
58
    }
59
60
    @Override
61
    public int alter(PassiveFighter caster, int value) {
62 1
        if (dead) {
63 1
            return 0;
64
        }
65
66 1
        if (value < -current) {
67 1
            value = -current;
68 1
        } else if (value > max - current) {
69 1
            value = max - current;
70
        }
71
72 1
        current += value;
73
74 1
        fighter.fight().dispatch(new FighterLifeChanged(fighter, caster, value));
75
76 1
        if (current == 0) {
77 1
            dead = true;
78 1
            fighter.fight().dispatch(new FighterDie(fighter, caster));
79
        } else {
80 1
            fighter.buffs().onLifeAltered(value);
81
        }
82
83 1
        return value;
84
    }
85
86
    @Override
87
    public void kill(ActiveFighter caster) {
88 1
        if (dead) {
89 1
            return;
90
        }
91
92 1
        current = 0;
93 1
        dead = true;
94 1
        fighter.fight().dispatch(new FighterDie(fighter, caster));
95 1
    }
96
}
97