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-2021 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.arakne.utils.maps.sight; |
21
|
|
|
|
22
|
|
|
import fr.arakne.utils.maps.BattlefieldCell; |
23
|
|
|
import fr.arakne.utils.maps.CoordinateCell; |
24
|
|
|
|
25
|
|
|
import java.util.Iterator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Line of sight iterator for cells with same X coordinate |
29
|
|
|
*/ |
30
|
|
|
final class SameXLineOfSightIterator<C extends BattlefieldCell> implements Iterator<C> { |
31
|
|
|
private final BattlefieldSight<C> battlefield; |
32
|
|
|
private final CoordinateCell<C> source; |
33
|
|
|
private final CoordinateCell<C> target; |
34
|
|
|
private final int yDirection; |
35
|
|
|
|
36
|
|
|
private int currentY; |
37
|
|
|
|
38
|
1 |
|
public SameXLineOfSightIterator(BattlefieldSight<C> battlefield, CoordinateCell<C> source, CoordinateCell<C> target) { |
39
|
1 |
|
this.battlefield = battlefield; |
40
|
1 |
|
this.source = source; |
41
|
1 |
|
this.target = target; |
42
|
|
|
|
43
|
1 |
|
yDirection = source.y() > target.y() ? -1 : 1; |
44
|
1 |
|
currentY = source.y(); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
@Override |
48
|
|
|
public boolean hasNext() { |
49
|
1 |
|
return target.y() != currentY; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
@Override |
53
|
|
|
public C next() { |
|
|
|
|
54
|
1 |
|
currentY += yDirection; |
55
|
|
|
|
56
|
1 |
|
return getCurrentCell(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return The cell on the current coordinates |
61
|
|
|
*/ |
62
|
|
|
private C getCurrentCell() { |
63
|
1 |
|
return battlefield.getCellByCoordinates(source.x(), currentY); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|