Passed
Push — master ( 7071c1...27dd9b )
by Vincent
07:02
created

fr.arakne.utils.maps.CoordinateCell.directionTo(C)   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;
21
22
import fr.arakne.utils.maps.constant.Direction;
23
24
import java.util.Objects;
25
26
/**
27
 * Map cell with coordinates
28
 *
29
 * https://github.com/Emudofus/Dofus/blob/1.29/ank/battlefield/utils/Pathfinding.as#L191
30
 */
31
public final class CoordinateCell<C extends MapCell<C>> {
32
    private final C cell;
33
34
    private final int x;
35
    private final int y;
36
37
    /**
38
     * CoordinateCell constructor.
39
     * Prefer use {@link MapCell#coordinate()} method instead of directly call the constructor
40
     *
41
     * @param cell The cell to wrap
42
     *
43
     * @see MapCell#coordinate() For get the {@link CoordinateCell} instance from a cell
44
     */
45 1
    public CoordinateCell(C cell) {
46 1
        this.cell = cell;
47
48 1
        final int width = cell.map().dimensions().width();
49 1
        final int line = cell.id() / (width * 2 - 1);
50 1
        final int column = cell.id() - line * (width * 2 - 1);
51 1
        final int offset = column % width;
52
53 1
        this.y = line - offset;
54 1
        this.x = (cell.id() - (width - 1) * this.y) / width;
55 1
    }
56
57
    /**
58
     * Get the related map cell
59
     *
60
     * @return The base cell instance
61
     */
62
    public C cell() {
63 1
        return cell;
64
    }
65
66
    /**
67
     * Get the cell id
68
     *
69
     * @return The cell id
70
     */
71
    public int id() {
72 1
        return cell.id();
73
    }
74
75
    /**
76
     * @return The X coordinate of the cell
77
     */
78
    public int x() {
79 1
        return x;
80
    }
81
82
    /**
83
     * @return The Y coordinate of the cell
84
     */
85
    public int y() {
86 1
        return y;
87
    }
88
89
    /**
90
     * Check if the cell is at the given coordinates
91
     *
92
     * @param x The X coordinate
93
     * @param y The Y coordinate
94
     *
95
     * @return true if coordinates match, or false otherwise
96
     */
97
    public boolean is(int x, int y) {
98 1
        return this.x == x && this.y == y;
99
    }
100
101
    /**
102
     * Compute the direction to the target cell
103
     *
104
     * https://github.com/Emudofus/Dofus/blob/1.29/ank/battlefield/utils/Pathfinding.as#L204
105
     *
106
     * @param target The target cell
107
     * @return The direction
108
     */
109
    public Direction directionTo(CoordinateCell<C> target) {
110 1
        if (x == target.x) {
111 1
            if (target.y > y) {
112 1
                return Direction.SOUTH_WEST;
113
            } else {
114 1
                return Direction.NORTH_EAST;
115
            }
116 1
        } else if (target.x > x) {
117 1
            return Direction.SOUTH_EAST;
118
        } else {
119 1
            return Direction.NORTH_WEST;
120
        }
121
    }
122
123
    /**
124
     * Compute the direction to the target cell
125
     *
126
     * https://github.com/Emudofus/Dofus/blob/1.29/ank/battlefield/utils/Pathfinding.as#L204
127
     *
128
     * @param target The target cell
129
     * @return The direction
130
     */
131
    public Direction directionTo(C target) {
132 1
        return directionTo(target.coordinate());
133
    }
134
135
    /**
136
     * Get the cell distance
137
     * Note: Do not compute a pythagorean distance, but "square" distance.
138
     *       So, this distance represents the minimal number of cells for a path between current and target.
139
     *
140
     * @param target The target cell
141
     * @return The distance, in cells number
142
     */
143
    public int distance(CoordinateCell<C> target) {
144 1
        return Math.abs(x - target.x) + Math.abs(y - target.y);
145
    }
146
147
    /**
148
     * Get the cell distance
149
     * Note: Do not compute a pythagorean distance, but "square" distance.
150
     *       So, this distance represents the minimal number of cells for a path between current and target.
151
     *
152
     * @param target The target cell
153
     * @return The distance, in cells number
154
     */
155
    public int distance(C target) {
156 1
        return distance(target.coordinate());
157
    }
158
159
    @Override
160
    public boolean equals(Object o) {
161 1
        if (this == o) {
162 1
            return true;
163
        }
164
165 1
        if (o == null || getClass() != o.getClass()) {
166 1
            return false;
167
        }
168
169 1
        final CoordinateCell<?> that = (CoordinateCell<?>) o;
170
171 1
        return x == that.x && y == that.y;
172
    }
173
174
    @Override
175
    public int hashCode() {
176 1
        return Objects.hash(x, y);
177
    }
178
}
179