SpellEffectService(Map)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
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.spell.effect;
21
22
import fr.quatrevieux.araknemu.data.value.EffectArea;
23
import fr.quatrevieux.araknemu.data.value.SpellTemplateEffect;
24
import fr.quatrevieux.araknemu.game.spell.effect.area.CellArea;
25
import fr.quatrevieux.araknemu.game.spell.effect.area.CheckboardArea;
26
import fr.quatrevieux.araknemu.game.spell.effect.area.CircleArea;
27
import fr.quatrevieux.araknemu.game.spell.effect.area.CrossArea;
28
import fr.quatrevieux.araknemu.game.spell.effect.area.LineArea;
29
import fr.quatrevieux.araknemu.game.spell.effect.area.PerpendicularLineArea;
30
import fr.quatrevieux.araknemu.game.spell.effect.area.RingArea;
31
import fr.quatrevieux.araknemu.game.spell.effect.area.SpellEffectArea;
32
import fr.quatrevieux.araknemu.game.spell.effect.target.SpellEffectTarget;
33
34
import java.util.ArrayList;
35
import java.util.EnumMap;
36
import java.util.List;
37
import java.util.Map;
38
import java.util.NoSuchElementException;
39
import java.util.function.Function;
40
41
/**
42
 * Handle spell effects
43
 */
44
public final class SpellEffectService {
45
    private final Map<EffectArea.Type, Function<EffectArea, SpellEffectArea>> areaFactories;
46
47
    public SpellEffectService() {
48 1
        this(new EnumMap<>(EffectArea.Type.class));
49
50 1
        areaFactories.put(EffectArea.Type.CELL, area -> CellArea.INSTANCE);
51 1
        areaFactories.put(EffectArea.Type.CIRCLE, CircleArea::new);
52 1
        areaFactories.put(EffectArea.Type.LINE, LineArea::new);
53 1
        areaFactories.put(EffectArea.Type.CROSS, CrossArea::new);
54 1
        areaFactories.put(EffectArea.Type.PERPENDICULAR_LINE, PerpendicularLineArea::new);
55 1
        areaFactories.put(EffectArea.Type.RING, RingArea::new);
56 1
        areaFactories.put(EffectArea.Type.CHECKERBOARD, CheckboardArea::new);
57 1
    }
58
59 1
    public SpellEffectService(Map<EffectArea.Type, Function<EffectArea, SpellEffectArea>> areaFactories) {
60 1
        this.areaFactories = areaFactories;
61 1
    }
62
63
    /**
64
     * Make the spell effect
65
     *
66
     * @param template The effect template
67
     * @param area The effect area
68
     * @param target The effect target filter
69
     */
70
    public SpellEffect make(SpellTemplateEffect template, EffectArea area, SpellEffectTarget target) {
71 1
        return new SpellTemplateEffectAdapter(template, area(area), target);
72
    }
73
74
    /**
75
     * Make an effect list
76
     *
77
     * @param templates List of effects
78
     * @param areas The effects areas
79
     * @param targets The effects targets
80
     */
81
    public List<SpellEffect> makeAll(List<SpellTemplateEffect> templates, List<EffectArea> areas, int[] targets) {
82 1
        final List<SpellEffect> effects = new ArrayList<>(templates.size());
83
84 1
        for (int i = 0; i < templates.size(); ++i) {
85 1
            effects.add(make(templates.get(i), areas.get(i), targets.length > i ? new SpellEffectTarget(targets[i]) : SpellEffectTarget.DEFAULT));
86
        }
87
88 1
        return effects;
89
    }
90
91
    /**
92
     * Create the spell effect area from area data
93
     *
94
     * @param area The effect area data
95
     */
96
    public SpellEffectArea area(EffectArea area) {
97 1
        if (!areaFactories.containsKey(area.type())) {
98
            throw new NoSuchElementException("Effect area " + area + " not available");
99
        }
100
101 1
        return areaFactories.get(area.type()).apply(area);
102
    }
103
}
104