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

alter(PassiveFighter,int)   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 7
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.fighter.player;
21
22
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
23
import fr.quatrevieux.araknemu.game.fight.fighter.BaseFighterLife;
24
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
25
import fr.quatrevieux.araknemu.game.fight.fighter.FighterLife;
26
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
27
import fr.quatrevieux.araknemu.game.world.creature.Life;
28
29
/**
30
 * Handle life points for {@link PlayerFighter}
31
 *
32
 * The life points will be saved when fight started
33
 */
34
public final class PlayerFighterLife implements FighterLife {
35
    private final Life baseLife;
36
    private final Fighter fighter;
37
38
    private BaseFighterLife delegate;
39
40 1
    public PlayerFighterLife(Life baseLife, Fighter fighter) {
41 1
        this.baseLife = baseLife;
42 1
        this.fighter = fighter;
43 1
    }
44
45
    @Override
46
    public int current() {
47 1
        return delegate != null ? delegate.current() : baseLife.current();
48
    }
49
50
    @Override
51
    public int max() {
52 1
        return delegate != null ? delegate.max() : baseLife.max();
53
    }
54
55
    @Override
56
    public boolean dead() {
57 1
        return delegate != null && delegate.dead();
58
    }
59
60
    @Override
61
    public int alter(PassiveFighter caster, int value) {
62 1
        if (delegate == null) {
63 1
            throw new IllegalStateException("PlayerFighterLife must be initialized");
64
        }
65
66 1
        return delegate.alter(caster, value);
67
    }
68
69
    @Override
70
    public void kill(ActiveFighter caster) {
71 1
        if (delegate == null) {
72 1
            throw new IllegalStateException("PlayerFighterLife must be initialized");
73
        }
74
75 1
        delegate.kill(caster);
76 1
    }
77
78
    /**
79
     * Initialise the fighter life when fight is started
80
     */
81
    public void init() {
82 1
        if (delegate != null) {
83 1
            throw new IllegalStateException("Player fighter life is already initialised");
84
        }
85
86 1
        delegate = new BaseFighterLife(fighter, baseLife.current(), baseLife.max());
87 1
    }
88
}
89