Passed
Pull Request — master (#241)
by Vincent
14:02
created

apply(Operation)   B

Complexity

Conditions 7

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 12
dl 0
loc 23
ccs 11
cts 11
cp 1
crap 7
rs 8
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-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.exploration.map.cell;
21
22
import fr.arakne.utils.maps.MapCell;
23
import fr.quatrevieux.araknemu.game.exploration.creature.ExplorationCreature;
24
import fr.quatrevieux.araknemu.game.exploration.creature.Operation;
25
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap;
26
import org.checkerframework.checker.index.qual.NonNegative;
27
28
/**
29
 * Cell of exploration map
30
 */
31
public interface ExplorationMapCell extends MapCell<ExplorationMapCell> {
32
    @Override
33
    public @NonNegative int id();
34
35
    @Override
36
    public ExplorationMap map();
37
38
    /**
39
     * Does the cell is free ?
40
     * A cell is free if a new sprite can be added on, and no other objects is present
41
     */
42
    public boolean free();
43
44
    /**
45
     * Apply an operation to all creatures on current cell
46
     * If the operation return false, the iteration will be stopped
47
     *
48
     * @see ExplorationCreature#apply(Operation)
49
     */
50
    public default void apply(Operation<Boolean> operation)  {
51
        // Optimisation : the cell is not walkable, no creatures can be located here
52 1
        if (!walkable()) {
53 1
            return;
54
        }
55
56 1
        for (ExplorationCreature creature : map().creatures()) {
57
            try {
58
                // Cell do not match : skip
59 1
                if (!equals(creature.cell())) {
60 1
                    continue;
61
                }
62 1
            } catch (IllegalStateException e) {
63
                // Creature may leave map (and cell) during application of Operation
64
                // which will cause an IllegalStateException (ex: change map or start fight)
65
                // So skip the creature
66 1
                continue;
67 1
            }
68
69 1
            final Boolean result = creature.apply(operation);
70
71 1
            if (result != null && !result) {
72 1
                break;
73
            }
74 1
        }
75 1
    }
76
}
77