Gridarta Editor
MapSquareIterator.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 net.sf.gridarta.utils.Size2D;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31 
37 public class MapSquareIterator<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Iterator<MapSquare<G, A, R>> {
38 
42  @NotNull
43  private final MapModel<G, A, R> mapModel;
44 
49  private final int direction;
50 
54  private final int mapWidth;
55 
59  private final int mapHeight;
60 
66  private int point;
67 
71  private int remainingMapSquares;
72 
82  public MapSquareIterator(@NotNull final MapModel<G, A, R> mapModel, @Nullable final Point start, final int direction, final boolean skipFirst) {
83  if (direction != -1 && direction != +1) {
84  throw new IllegalArgumentException("invalid direction: " + direction);
85  }
86  this.mapModel = mapModel;
87  this.direction = direction;
88  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
89  mapWidth = mapSize.getWidth();
90  mapHeight = mapSize.getHeight();
91  if (start != null) {
92  point = start.x + start.y * mapWidth;
93  } else if (direction > 0) {
94  point = 0;
95  } else {
96  point = mapWidth * mapHeight - 1;
97  }
99  if (skipFirst) {
100  nextMapSquare();
101  }
102  }
103 
104  @Override
105  public void remove() {
106  throw new UnsupportedOperationException("unsupported operation");
107  }
108 
109  @Override
110  public boolean hasNext() {
111  return remainingMapSquares > 0;
112  }
113 
114  @Override
116  if (remainingMapSquares <= 0) {
117  throw new NoSuchElementException("no more elements");
118  }
120 
121  final MapSquare<G, A, R> square = mapModel.getMapSquare(new Point(point % mapWidth, point / mapWidth));
122  nextMapSquare();
123  return square;
124  }
125 
129  private void nextMapSquare() {
130  point += direction;
131  if (point < 0) {
132  point += mapWidth * mapHeight;
133  } else if (point >= mapWidth * mapHeight) {
134  point -= mapWidth * mapHeight;
135  }
136  }
137 
138 }
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
net.sf.gridarta.model.mapmodel.MapModel< G, A, R >
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.MapSquareIterator.mapModel
final MapModel< G, A, R > mapModel
The MapModel to iterate over.
Definition: MapSquareIterator.java:43
net.sf.gridarta.model.mapmodel.MapSquare< G, A, R >
net.sf.gridarta.model.mapmodel.MapSquareIterator.MapSquareIterator
MapSquareIterator(@NotNull final MapModel< G, A, R > mapModel, @Nullable final Point start, final int direction, final boolean skipFirst)
Creates a new instance.
Definition: MapSquareIterator.java:82
net.sf
net.sf.gridarta.model.mapmodel.MapSquareIterator.remainingMapSquares
int remainingMapSquares
The number of remaining map square to return from next().
Definition: MapSquareIterator.java:71
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.MapSquareIterator.mapWidth
final int mapWidth
The mapModel's map width.
Definition: MapSquareIterator.java:54
net.sf.gridarta.model.mapmodel.MapSquareIterator.direction
final int direction
The direction to iterate:
Definition: MapSquareIterator.java:49
net.sf.gridarta.model.mapmodel.MapSquareIterator.hasNext
boolean hasNext()
Definition: MapSquareIterator.java:110
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.model.mapmodel.MapSquareIterator.next
MapSquare< G, A, R > next()
Definition: MapSquareIterator.java:115
net
net.sf.gridarta.utils.Size2D.getHeight
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
net.sf.gridarta.model.mapmodel.MapSquareIterator.point
int point
Index of current map square.
Definition: MapSquareIterator.java:66
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.mapmodel.MapSquareIterator.mapHeight
final int mapHeight
The mapModel's map height.
Definition: MapSquareIterator.java:59
net.sf.gridarta.model.mapmodel.MapModel.getMapSquare
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.mapmodel.MapSquareIterator.nextMapSquare
void nextMapSquare()
Updates point to the next map square.
Definition: MapSquareIterator.java:129
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.mapmodel.MapSquareIterator
Iterator for iterating over all squares of a model.
Definition: MapSquareIterator.java:37