Passed
Push — master ( 400146...55821f )
by Vincent
04:09
created

fr.arakne.utils.maps.constant.Direction.restrictedDirections()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
/*
2
 * This file is part of ArakneUtils.
3
 *
4
 * ArakneUtils 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
 * ArakneUtils 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 ArakneUtils.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2020 Vincent Quatrevieux
18
 */
19
20
package fr.arakne.utils.maps.constant;
21
22
import java.util.Arrays;
23
import java.util.function.Function;
24
25
/**
26
 * List of available directions
27
 */
28 1
public enum Direction {
29 1
    EAST(width -> 1),
30 1
    SOUTH_EAST(width -> width),
31 1
    SOUTH(width -> 2 * width - 1),
32 1
    SOUTH_WEST(width -> width - 1),
33 1
    WEST(width -> -1),
34 1
    NORTH_WEST(width -> -width),
35 1
    NORTH(width -> -(2 * width - 1)),
36 1
    NORTH_EAST(width -> -(width - 1));
37
38
    /**
39
     * Array of restricted directions (can be used on fight)
40
     */
41 1
    final static private Direction[] restricted = Arrays.stream(values()).filter(Direction::restricted).toArray(Direction[]::new);
42
43
    /**
44
     * Cache values
45
     */
46 1
    final static private Direction[] values = values();
47
48
    final private Function<Integer, Integer> computeNextCell;
49
50
    /**
51
     * @param computeNextCell The function for compute the next cell id following the direction
52
     */
53 1
    Direction(Function<Integer, Integer> computeNextCell) {
54 1
        this.computeNextCell = computeNextCell;
55 1
    }
56
57
    /**
58
     * Get the char value of the direction
59
     *
60
     * @return The direction char value
61
     */
62
    public char toChar(){
63 1
        return (char) (ordinal() + 'a');
64
    }
65
66
    /**
67
     * Get the opposite direction
68
     *
69
     * @return The direction
70
     */
71
    public Direction opposite(){
72 1
        return values[(ordinal() + 4) % 8];
73
    }
74
75
    /**
76
     * Get the orthogonal direction
77
     *
78
     * @return The direction
79
     */
80
    public Direction orthogonal() {
81 1
        return values[(ordinal() + 2) % 8];
82
    }
83
84
    /**
85
     * Does the direction is restricted
86
     *
87
     * A restricted direction can be used in fight, or by monsters's sprites
88
     * Restricted direction do not allow diagonal
89
     *
90
     * @return true if the direction can be used when restrictions are enabled
91
     */
92
    public boolean restricted() {
93 1
        return ordinal() % 2 == 1;
94
    }
95
96
    /**
97
     * Get the increment to apply to cell id for get the next cell on the direction
98
     *
99
     * Ex:
100
     * MapCell current = map.get(123);
101
     * MapCell east = map.get(Direction.EAST.nextCellIncrement(map.dimensions().width()) + current.id());
102
     *
103
     * @param mapWidth The map width
104
     *
105
     * @return The next cell id increment
106
     */
107
    public int nextCellIncrement(int mapWidth) {
108 1
        return computeNextCell.apply(mapWidth);
109
    }
110
111
    /**
112
     * Get the direction by its char value
113
     *
114
     * @param c The direction character value
115
     * @return The direction
116
     */
117
    static public Direction byChar(char c) {
118 1
        return values[c - 'a'];
119
    }
120
121
    /**
122
     * Get the restricted directions (i.e. can be used on fight)
123
     * Note: a new array is always returned
124
     *
125
     * @return The array of Direction
126
     */
127
    static public Direction[] restrictedDirections() {
128 1
        return restricted.clone();
129
    }
130
}
131