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-2023 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("no more elements");
83  }
84  findNext();
85  return result;
86  }
87 
88  @Override
89  public void remove() {
90  throw new UnsupportedOperationException("unsupported operation");
91  }
92 
96  private void findNext() {
97  while (true) {
98  if (gameObjectIterator != null) {
99  if (gameObjectIterator.hasNext()) {
100  assert gameObjectIterator != null;
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 }
net.sf.gridarta.model.mapmodel.MapModel< G, A, R >
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.TopLevelGameObjectIterator.mapSquareIterator
final Iterator< MapSquare< G, A, R > > mapSquareIterator
The Iterator returning all MapSquares to consider.
Definition: TopLevelGameObjectIterator.java:42
net.sf.gridarta.model.mapmodel.MapSquare< G, A, R >
net.sf
net.sf.gridarta.model.mapmodel.TopLevelGameObjectIterator
Iterator for iterating over top-level game object of a map model.
Definition: TopLevelGameObjectIterator.java:35
net.sf.gridarta.model.mapmodel.TopLevelGameObjectIterator.next
G next()
Definition: TopLevelGameObjectIterator.java:79
net.sf.gridarta.model.mapmodel.TopLevelGameObjectIterator.TopLevelGameObjectIterator
TopLevelGameObjectIterator(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Point start, final int direction, final boolean skipFirst)
Creates a new instance.
Definition: TopLevelGameObjectIterator.java:67
net.sf.gridarta.model.mapmodel.TopLevelGameObjectIterator.findNext
void findNext()
Updates gameObject to contain the next top-level game object.
Definition: TopLevelGameObjectIterator.java:96
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapmodel.TopLevelGameObjectIterator.gameObjectIterator
Iterator< G > gameObjectIterator
The Iterator returning all top-level game objects in the current map square or.
Definition: TopLevelGameObjectIterator.java:49
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.mapmodel.TopLevelGameObjectIterator.gameObject
G gameObject
The GameObject to return from the next call to next() or.
Definition: TopLevelGameObjectIterator.java:56
net.sf.gridarta.model.mapmodel.TopLevelGameObjectIterator.hasNext
boolean hasNext()
Definition: TopLevelGameObjectIterator.java:73
net.sf.gridarta.model.mapmodel.MapSquareIterator
Iterator for iterating over all squares of a model.
Definition: MapSquareIterator.java:37