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-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 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();
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  }
98  remainingMapSquares = mapWidth * mapHeight;
99  if (skipFirst) {
100  nextMapSquare();
101  }
102  }
103 
104  @Override
105  public void remove() {
106  throw new UnsupportedOperationException();
107  }
108 
109  @Override
110  public boolean hasNext() {
111  return remainingMapSquares > 0;
112  }
113 
114  @Override
116  if (remainingMapSquares <= 0) {
117  throw new NoSuchElementException();
118  }
119  remainingMapSquares--;
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 }
int remainingMapSquares
The number of remaining map square to return from next().
final MapModel< G, A, R > mapModel
The MapModel to iterate over.
final int mapWidth
The mapModel&#39;s map width.
final int direction
The direction to iterate:
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
GameObjects are the objects based on Archetypes found on maps.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
MapSquareIterator(@NotNull final MapModel< G, A, R > mapModel, @Nullable final Point start, final int direction, final boolean skipFirst)
Creates a new instance.
final int mapHeight
The mapModel&#39;s map height.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
void nextMapSquare()
Updates point to the next map square.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
Iterator for iterating over all squares of a model.