Gridarta Editor
MapSquare.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.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
45 public class MapSquare<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends GameObjectContainer<G, A, R> {
46 
50  private static final long serialVersionUID = 1L;
51 
55  @NotNull
56  private final MapModel<G, A, R> mapModel;
57 
61  private final int mapX;
62 
66  private final int mapY;
67 
72  private int lightRadius;
73 
79  @NotNull
80  private List<MapSquare<G, A, R>> lightSources = Collections.emptyList();
81 
88  public MapSquare(@NotNull final MapModel<G, A, R> mapModel, final int mapX, final int mapY) {
89  this.mapModel = mapModel;
90  this.mapX = mapX;
91  this.mapY = mapY;
92  }
93 
98  @NotNull
100  return mapModel;
101  }
102 
107  public int getMapX() {
108  return mapX;
109  }
110 
115  public int getMapY() {
116  return mapY;
117  }
118 
123  @NotNull
124  public Point getMapLocation() {
125  return new Point(mapX, mapY);
126  }
127 
132  public void getMapLocation(@NotNull final Point pos) {
133  pos.x = mapX;
134  pos.y = mapY;
135  }
136 
143  public void getMapLocation(@NotNull final Point pos, final int dx, final int dy) {
144  pos.x = mapX + dx;
145  pos.y = mapY + dy;
146  }
147 
148  @Override
149  @NotNull
151  return this;
152  }
153 
154  @Override
155  protected void notifyBeginChange() {
156  mapModel.beginSquareChange(this);
157  }
158 
159  @Override
160  protected void notifyEndChange() {
161  mapModel.endSquareChange(this);
162  }
163 
164  @NotNull
165  @Override
166  @SuppressWarnings("unchecked")
167  protected MapSquare<G, A, R> clone() {
168  //noinspection UnnecessaryLocalVariable
169  final MapSquare<G, A, R> clone = (MapSquare<G, A, R>) super.clone();
170  return clone;
171  }
172 
173  @Override
174  protected void setThisContainer(@NotNull final G gameObject) {
175  gameObject.setContainer(this, mapX, mapY);
176  }
177 
178  @Nullable
179  @Override
180  public G asGameObject() {
181  return null;
182  }
183 
189  @Nullable
190  public G getLast(@NotNull final GameObjectMatcher gameObjectMatcher) {
191  for (final G gameObject : reverse()) {
192  if (gameObjectMatcher.isMatching(gameObject)) {
193  return gameObject;
194  }
195  }
196  return null;
197  }
198 
207  @Nullable
208  public G getAfterLast(@NotNull final GameObjectMatcher gameObjectMatcher) {
209  G prev = null;
210  for (final G gameObject : reverse()) {
211  if (gameObjectMatcher.isMatching(gameObject)) {
212  return prev;
213  }
214  prev = gameObject;
215  }
216  return null;
217  }
218 
225  @Nullable
226  public G getFirst(@NotNull final GameObjectMatcher gameObjectMatcher) {
227  for (final G gameObject : this) {
228  if (gameObjectMatcher.isMatching(gameObject)) {
229  return gameObject;
230  }
231  }
232  return null;
233  }
234 
243  @Nullable
244  public G getBeforeFirst(@NotNull final GameObjectMatcher gameObjectMatcher) {
245  G prev = null;
246  for (final G gameObject : this) {
247  if (gameObjectMatcher.isMatching(gameObject)) {
248  return prev;
249  }
250  prev = gameObject;
251  }
252  return null;
253  }
254 
263  @Nullable
264  public G getLastOfLeadingSpan(@NotNull final GameObjectMatcher gameObjectMatcher) {
265  @Nullable G result = null;
266  for (final G tmp : this) {
267  if (!gameObjectMatcher.isMatching(tmp)) {
268  break;
269  }
270  result = tmp;
271  }
272  return result;
273  }
274 
278  public void beginSquareChange() {
279  mapModel.beginSquareChange(this);
280  }
281 
285  public void endSquareChange() {
286  mapModel.endSquareChange(this);
287  }
288 
292  @Override
293  public boolean equals(@Nullable final Object obj) {
294  if (obj == null || obj.getClass() != getClass()) {
295  return false;
296  }
297 
298  final MapSquare<?, ?, ?> mapSquare = (MapSquare<?, ?, ?>) obj;
299  return mapModel == mapSquare.mapModel && mapX == mapSquare.mapX && mapY == mapSquare.mapY;
300  }
301 
305  @Override
306  public int hashCode() {
307  return System.identityHashCode(mapModel) + 13 * mapX + 65537 * mapY;
308  }
309 
313  @NotNull
314  @Override
315  public String toString() {
316  return mapModel.getMapArchObject().getMapName() + ":" + mapX + "/" + mapY;
317  }
318 
325  public int getLightRadius() {
326  return lightRadius;
327  }
328 
335  public void setLightRadius(final int lightRadius) {
336  if (this.lightRadius == lightRadius) {
337  return;
338  }
339 
341  try {
342  this.lightRadius = lightRadius;
343  } finally {
344  notifyEndChange();
345  }
346  }
347 
352  public void addLightSource(@NotNull final MapSquare<G, A, R> mapSquare) {
353  if (lightSources.isEmpty()) {
354  lightSources = new ArrayList<>();
355  }
356 
358  try {
359  lightSources.add(mapSquare);
360  } finally {
361  notifyEndChange();
362  }
363  }
364 
369  public void removeLightSource(@NotNull final MapSquare<G, A, R> mapSquare) {
371  try {
372  if (!lightSources.remove(mapSquare)) {
373  assert false;
374  }
375  if (lightSources.isEmpty()) {
376  lightSources = Collections.emptyList();
377  }
378  } finally {
379  notifyEndChange();
380  }
381  }
382 
389  public boolean isLight() {
390  return !lightSources.isEmpty();
391  }
392 
393 }
boolean equals(@Nullable final Object obj)
}
Definition: MapSquare.java:293
int getMapX()
Returns the x coordinate on the map.
Definition: MapSquare.java:107
int lightRadius
The maximum light radius of all objects within this map square.
Definition: MapSquare.java:72
Iterable< G > reverse()
Return an object that is the reverse representation.
final int mapY
The Y Coordinate of this map square within the model&#39;s grid.
Definition: MapSquare.java:66
boolean isLight()
Returns whether this map square is affected by any light emitting game objects.
Definition: MapSquare.java:389
Interface for classes that match GameObjects.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
void getMapLocation(@NotNull final Point pos)
Returns the coordinate on the map.
Definition: MapSquare.java:132
MapModel< G, A, R > getMapModel()
Returns the MapModel this map square is part of.
Definition: MapSquare.java:99
void addLightSource(@NotNull final MapSquare< G, A, R > mapSquare)
Adds a light emitting game object that affects this map square.
Definition: MapSquare.java:352
void endSquareChange(@NotNull MapSquare< G, A, R > mapSquare)
Method to notify the model that a map square was changed.
final MapModel< G, A, R > mapModel
The MaoModel this square is associated with.
Definition: MapSquare.java:56
static final long serialVersionUID
The serial version UID.
Definition: MapSquare.java:50
Base class for classes that contain GameObjects as children in the sense of containment.
MapSquare(@NotNull final MapModel< G, A, R > mapModel, final int mapX, final int mapY)
Creates a new instance.
Definition: MapSquare.java:88
G getAfterLast(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the game object after the last occurrence of a matching game object.
Definition: MapSquare.java:208
G getLastOfLeadingSpan(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the last game object of the initial segment of matching game objects.
Definition: MapSquare.java:264
void removeLightSource(@NotNull final MapSquare< G, A, R > mapSquare)
Removes a light emitting game object that affects this map square.
Definition: MapSquare.java:369
final int mapX
The X Coordinate of this map square within the model&#39;s grid.
Definition: MapSquare.java:61
Base package of all Gridarta classes.
int getLightRadius()
Returns the maximum light radius of all light emitting objects within this map square.
Definition: MapSquare.java:325
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
void endSquareChange()
Method to notify the model that a map square was changed.
Definition: MapSquare.java:285
Point getMapLocation()
Returns the coordinate on the map.
Definition: MapSquare.java:124
void setLightRadius(final int lightRadius)
Sets the maximum light radius of all light emitting objects within this map square.
Definition: MapSquare.java:335
G getFirst(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the first occurrence of a matching game object.
Definition: MapSquare.java:226
void beginSquareChange()
Method to notify the model that a map square is about to change.
Definition: MapSquare.java:278
G getBeforeFirst(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the game object before the first occurrence of a matching game object.
Definition: MapSquare.java:244
void beginSquareChange(@NotNull MapSquare< G, A, R > mapSquare)
Method to notify the model that a map square is about to change.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
void setThisContainer(@NotNull final G gameObject)
Definition: MapSquare.java:174
G getLast(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the last occurrence of a matching game object.
Definition: MapSquare.java:190
void getMapLocation(@NotNull final Point pos, final int dx, final int dy)
Returns the coordinate with an offset on the map.
Definition: MapSquare.java:143
int getMapY()
Returns the y coordinate on the map.
Definition: MapSquare.java:115
List< MapSquare< G, A, R > > lightSources
The MapSquares on the map that contain light emitting game objects that affect this map square...
Definition: MapSquare.java:80