20 package net.sf.gridarta.model.mapmodel;
22 import java.awt.Point;
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
53 private final List<ArrayList<ArrayList<G>>>
savedSquares =
new ArrayList<>();
83 final int x = mapSquare.getMapX();
84 final int y = mapSquare.getMapY();
90 for (
final G content : mapSquare) {
91 if (content.isHead()) {
107 final ArrayList<ArrayList<G>> col;
108 if (x >= savedSquares.size()) {
109 while (savedSquares.size() < x) {
110 savedSquares.add(null);
112 col =
new ArrayList<>();
113 savedSquares.add(col);
115 final ArrayList<ArrayList<G>> tmp = savedSquares.get(x);
119 col =
new ArrayList<>();
120 savedSquares.set(x, col);
124 final ArrayList<G> result;
125 if (y >= col.size()) {
126 while (col.size() < y) {
129 result =
new ArrayList<>();
132 final Collection<G> tmp = col.get(y);
137 result =
new ArrayList<>();
149 return savedSquares.isEmpty();
156 savedSquares.clear();
168 savedSquares.clear();
177 final Collection<G> objectsToDelete =
new ArrayList<>();
179 final Point point =
new Point();
181 for (
final Iterable<ArrayList<G>> col : savedSquares) {
184 for (
final Iterable<G> square : col) {
185 if (square != null) {
187 for (
final G gameObject : mapSquare) {
188 if (gameObject.isHead()) {
189 objectsToDelete.add(gameObject);
192 for (
final G gameObject : objectsToDelete) {
193 mapModel.removeGameObject(gameObject,
false);
195 objectsToDelete.clear();
196 for (
final G gameObject : square) {
197 mapSquare.addLast(gameObject);
206 final Point point2 =
new Point();
208 for (
final Iterable<ArrayList<G>> col : savedSquares) {
211 for (
final Iterable<G> square : col) {
212 if (square != null) {
214 final Point map = gameObject.getMapLocation();
215 for (G tailGameObject = gameObject.getMultiNext(); tailGameObject != null; tailGameObject = tailGameObject.getMultiNext()) {
216 point2.x = map.x + tailGameObject.getArchetype().getMultiX();
217 point2.y = map.y + tailGameObject.getArchetype().getMultiY();
219 mapSquare.
addLast(tailGameObject);
235 final int width = size.getWidth();
236 final int height = size.getHeight();
239 throw new IllegalArgumentException();
242 for (
int x = 0; x < width && x < savedSquares.size(); x++) {
243 final List<ArrayList<G>> col = savedSquares.get(x);
245 for (
int y = col.size() - 1; y >= height; y--) {
250 for (
int x = savedSquares.size() - 1; x >= width; x--) {
251 savedSquares.remove(x);
262 for (
int x = 0; x < width && x < savedSquares.size(); x++) {
263 final List<ArrayList<G>> col = savedSquares.get(x);
265 for (
int y = height; y < col.size(); y++) {
266 final Collection<G> square = col.get(y);
267 if (!square.isEmpty()) {
274 for (
int x = width; x < savedSquares.size(); x++) {
275 final Iterable<ArrayList<G>> col = savedSquares.get(x);
277 for (
final Collection<G> square : col) {
278 if (square != null && !square.isEmpty()) {
void applyChanges(@NotNull final MapModel< G, A, R > mapModel)
Applies the saved squares to the given map model.
boolean isEmpty()
Returns whether no saved squares exist.
SavedSquares(@NotNull final GameObjectFactory< G, A, R > gameObjectFactory, @NotNull final GameObjectMatchers gameObjectMatchers)
Creates a new instance.
void addLast(@NotNull final G gameObject)
Add the given GameObject at the end of this Container.
void recordMapSquare(@NotNull final MapSquare< G, A, R > mapSquare)
Records a map square as changed.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
abstract MapSquare< G, A, R > getMapSquare()
Returns the MapSquare of this container.
boolean isOutsideEmpty(final int width, final int height)
Returns whether all squares outside a given area are empty or unchanged.
G cloneMultiGameObject(@NotNull G gameObject)
Creates a copy of a game object, including tail parts.
Base class for classes that contain GameObjects as children in the sense of containment.
SavedSquares< G, A, R > cloneAndClear()
Creates a new instance having the same contents as this instance, then forgets all saves squares in t...
Base package of all Gridarta classes.
final List< ArrayList< ArrayList< G > > > savedSquares
The saved squares.
Reflects a game object (object on a map).
Abstract factory for creating GameObject instances.
final GameObjectFactory< G, A, R > gameObjectFactory
The GameObjectFactory to use.
GameObjects are the objects based on Archetypes found on maps.
ArrayList< G > allocateMapSquare(final int x, final int y)
Allocates a saved map square.
Maintains GameObjectMatcher instances.
static final long serialVersionUID
The serial version UID.
final GameObjectMatchers gameObjectMatchers
The GameObjectMatchers to use.
void clear()
Forgets all saved squares.
void removeEmptySquares(@NotNull final Size2D size)
Removes empty squares outside a given area.
Records a set of changed map squares.
Interface for MapArchObjects.
The class Size2D represents a 2d rectangular area.