Gridarta Editor
TopLevelGameObjectIterator.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.model.mapmodel;
21 
22 import java.awt.Point;
23 import java.util.Iterator;
24 import java.util.NoSuchElementException;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
35 public class TopLevelGameObjectIterator<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Iterator<G> {
36 
41  @NotNull
42  private final Iterator<MapSquare<G, A, R>> mapSquareIterator;
43 
48  @Nullable
49  private Iterator<G> gameObjectIterator;
50 
55  @Nullable
56  private G gameObject;
57 
67  public TopLevelGameObjectIterator(@NotNull final MapModel<G, A, R> mapModel, @NotNull final Point start, final int direction, final boolean skipFirst) {
68  mapSquareIterator = new MapSquareIterator<>(mapModel, start, direction, skipFirst);
69  findNext();
70  }
71 
72  @Override
73  public boolean hasNext() {
74  return gameObject != null;
75  }
76 
77  @NotNull
78  @Override
79  public G next() {
80  final G result = gameObject;
81  if (result == null) {
82  throw new NoSuchElementException();
83  }
84  findNext();
85  return result;
86  }
87 
88  @Override
89  public void remove() {
90  throw new UnsupportedOperationException();
91  }
92 
96  private void findNext() {
97  while (true) {
98  if (gameObjectIterator != null) {
99  if (gameObjectIterator.hasNext()) {
100  assert gameObjectIterator != null;
101  gameObject = gameObjectIterator.next();
102  return;
103  }
104  gameObjectIterator = null;
105  }
106 
107  if (!mapSquareIterator.hasNext()) {
108  gameObject = null;
109  return;
110  }
111 
112  final MapSquare<G, A, R> mapSquare = mapSquareIterator.next();
113  if (!mapSquare.isEmpty()) {
114  gameObjectIterator = mapSquare.iterator();
115  }
116  }
117  }
118 
119 }
TopLevelGameObjectIterator(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Point start, final int direction, final boolean skipFirst)
Creates a new instance.
Iterator for iterating over top-level game object of a map model.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
G gameObject
The GameObject to return from the next call to next() or.
GameObjects are the objects based on Archetypes found on maps.
void findNext()
Updates gameObject to contain the next top-level game object.
final Iterator< MapSquare< G, A, R > > mapSquareIterator
The Iterator returning all MapSquares to consider.
Iterator< G > gameObjectIterator
The Iterator returning all top-level game objects in the current map square or.
Iterator for iterating over all squares of a model.