Gridarta Editor
ExitIterator.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.map.mapactions;
21 
22 import java.awt.Point;
23 import java.util.Iterator;
24 import java.util.NoSuchElementException;
30 import net.sf.gridarta.utils.Size2D;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
39 public class ExitIterator<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Iterator<G> {
40 
44  @NotNull
46 
50  @NotNull
51  private final MapModel<G, A, R> mapModel;
52 
56  private final int direction;
57 
61  @NotNull
62  private final Point point;
63 
67  @Nullable
68  private final G start;
69 
73  private int remainingMapSquares;
74 
78  private boolean findNextPending = true;
79 
83  @Nullable
84  private G next;
85 
95  public ExitIterator(@NotNull final ExitMatcher<G, A, R> exitMatcher, @NotNull final MapModel<G, A, R> mapModel, final int xStart, final int yStart, final int direction) {
96  if (direction != -1 && direction != 1) {
97  throw new IllegalArgumentException();
98  }
99  this.exitMatcher = exitMatcher;
100  this.mapModel = mapModel;
101  this.direction = direction;
102  point = new Point(xStart, yStart);
103  start = exitMatcher.getValidExit(mapModel, point);
104  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
105  remainingMapSquares = mapSize.getWidth() * mapSize.getHeight();
106  }
107 
108  @Override
109  public boolean hasNext() {
110  findNext();
111  return next != null;
112  }
113 
114  @Override
115  public G next() {
116  findNext();
117  if (next == null) {
118  throw new NoSuchElementException();
119  }
120  findNextPending = true;
121  return next;
122  }
123 
124  @Override
125  public void remove() {
126  throw new UnsupportedOperationException();
127  }
128 
132  private void findNext() {
133  if (!findNextPending) {
134  return;
135  }
136  findNextPending = false;
137 
138  //noinspection WhileLoopSpinsOnField
139  while (remainingMapSquares-- > 0) {
140  mapModel.nextPoint(point, direction);
141  next = exitMatcher.getValidExit(mapModel, point);
142  if (next != null && next != start) {
143  return;
144  }
145  }
146  next = null;
147  }
148 
149 }
final int direction
The search direction:
final ExitMatcher< G, A, R > exitMatcher
The ExitMatcher for selecting exit game objects.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
An Iterator that returns all map squares containing an exit game object.
void nextPoint(Point point, int direction)
Moves the given point forward or backward one map square.
boolean findNextPending
Whether next is valid.
void findNext()
Updates next to the next match.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Selects valid exit game objects from maps.
GameObjects are the objects based on Archetypes found on maps.
G getValidExit(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Point point)
Returns an exit game object on a given map square having exit information.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
ExitIterator(@NotNull final ExitMatcher< G, A, R > exitMatcher, @NotNull final MapModel< G, A, R > mapModel, final int xStart, final int yStart, final int direction)
Creates a new instance.
final MapModel< G, A, R > mapModel
The MapModel begin searched.
int remainingMapSquares
The number of map squares remaining to be searched.
final Point point
The current location on the map.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
final G start
The starting exit game object or.
G next
The next exit game object to return from next().
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30