Gridarta Editor
AbstractGameObject.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.gameobject;
21 
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
38 public abstract class AbstractGameObject<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractBaseObject<G, A, R, G> implements GameObject<G, A, R> {
39 
43  private static final long serialVersionUID = 1L;
44 
50  @NotNull
51  private R archetype;
52 
64  @Nullable
66 
74  protected AbstractGameObject(@NotNull final R archetype, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final AnimationObjects animationObjects) {
75  super(faceObjectProviders, animationObjects);
76  this.archetype = archetype;
78  }
79 
80  @Override
81  @NotNull
82  public String getAttributeString(@NotNull final String attributeName, final boolean queryArchetype) {
83  final String result = getAttributeValue(attributeName);
84  if (result != null) {
85  return result;
86  }
87  return queryArchetype ? archetype.getAttributeString(attributeName) : "";
88  }
89 
90  @Nullable
91  @Override
92  protected String getEffectiveFaceName(@NotNull final String faceName) {
93  final String normalizedFaceName = faceName.isEmpty() ? null : faceName.intern();
94  //Strings are interned
95  //noinspection StringEquality
96  return normalizedFaceName != null && !normalizedFaceName.isEmpty() && normalizedFaceName != archetype.getFaceName() ? normalizedFaceName : null;
97  }
98 
99  @Override
100  public void notifyBeginChange() {
101  final MapSquare<G, A, R> mapSquare = getMapSquare();
102  if (mapSquare != null) {
104  }
105  }
106 
107  @Override
108  public void notifyEndChange() {
109  final MapSquare<G, A, R> mapSquare = getMapSquare();
110  if (mapSquare != null) {
111  mapSquare.getMapModel().endGameObjectChange(getThis());
112  }
113  }
114 
115  @Override
116  public void notifyTransientChange() {
117  final MapSquare<G, A, R> mapSquare = getMapSquare();
118  if (mapSquare != null) {
120  }
121  }
122 
123  @NotNull
124  @Override
125  @SuppressWarnings("unchecked")
126  public G clone() {
127  //noinspection OverriddenMethodCallDuringObjectConstruction
129  clone.container = null;
130  return clone.getThis();
131  }
132 
133  @NotNull
134  @Override
135  public G asGameObject() {
136  return getThis();
137  }
138 
139  @NotNull
140  @Override
141  public G newInstance(@NotNull final GameObjectFactory<G, A, R> gameObjectFactory) {
142  return gameObjectFactory.cloneGameObject(getThis());
143  }
144 
145  @Override
146  public void visit(@NotNull final BaseObjectVisitor<G, A, R> baseObjectVisitor) {
147  baseObjectVisitor.visit(this);
148  }
149 
150  @Override
151  public void remove() {
152  final G head = getHead();
153  for (G tail = head.getMultiNext(); tail != null; tail = tail.getMultiNext()) {
154  final GameObjectContainer<G, A, R> tailContainer = tail.getContainer();
155  if (tailContainer == null) {
157  }
158  tailContainer.remove(tail);
159  }
160  final GameObjectContainer<G, A, R> headContainer = head.getContainer();
161  if (headContainer == null) {
163  }
164  headContainer.remove(head);
165  }
166 
167  @Nullable
168  @Override
170  return container;
171  }
172 
173  @Override
174  public boolean isTop() {
175  //noinspection ConstantConditions
176  return container == null || container.isTop(getThis());
177  }
178 
179  @Override
180  public boolean isBottom() {
181  //noinspection ConstantConditions
182  return container == null || container.isBottom(getThis());
183  }
184 
185  @Override
186  public void moveTop() {
187  if (container != null) {
188  //noinspection ConstantConditions
190  }
191  }
192 
193  @Override
194  public void moveUp() {
195  if (container != null) {
196  //noinspection ConstantConditions
198  }
199  }
200 
201  @Override
202  public void moveDown() {
203  if (container != null) {
204  //noinspection ConstantConditions
206  }
207  }
208 
209  @Override
210  public void moveBottom() {
211  if (container != null) {
212  //noinspection ConstantConditions
214  }
215  }
216 
217  @Override
218  public void insertBefore(@NotNull final G node) {
219  if (container == null) {
221  }
222  //noinspection ConstantConditions
223  container.insertBefore(node, getThis());
224  }
225 
226  @Override
227  public void insertAfter(@NotNull final G node) {
228  if (container == null) {
230  }
231  //noinspection ConstantConditions
232  container.insertAfter(getThis(), node);
233  }
234 
235  @Nullable
236  @Override
238  return container == null ? null : container.asGameObject();
239  }
240 
241  @NotNull
242  @Override
243  public G getTopContainer() {
244  if (container != null) {
245  final G gameObject = container.asGameObject();
246  if (gameObject != null) {
247  return gameObject.getTopContainer();
248  }
249  }
250 
251  return getThis();
252  }
253 
254  @Override
255  public void setContainer(@Nullable final GameObjectContainer<G, A, R> container, final int mapX, final int mapY) {
256  this.container = container;
257  setMapX(mapX);
258  setMapY(mapY);
259  }
260 
261  @Override
262  public boolean isInContainer() {
263  return container != null && container instanceof GameObject;
264  }
265 
266  @Nullable
267  @Override
269  return container != null ? container.getMapSquare() : null;
270  }
271 
272  @Nullable
273  @Override
274  public G getPrev() {
275  //noinspection ConstantConditions
276  return container == null ? null : container.getPrev(getThis());
277  }
278 
279  @Nullable
280  @Override
281  public G getNext() {
282  //noinspection ConstantConditions
283  return container == null ? null : container.getNext(getThis());
284  }
285 
286  @NotNull
287  @Override
288  public R getArchetype() {
289  return archetype;
290  }
291 
292  @Override
293  public void setArchetype(@NotNull final R archetype) {
294  if (this.archetype == archetype) {
295  return;
296  }
297 
299  try {
300  this.archetype = archetype;
301  updateArchetype();
302  } finally {
304  }
305  }
306 
307  @Override
308  public boolean hasUndefinedArchetype() {
309  return archetype.isUndefinedArchetype();
310  }
311 
312  @Override
313  public void markModified() {
316  }
317 
318 }
R archetype
The Archetype of this game object.
Abstract base class of GameObject implementations.
void endGameObjectChange()
Records that this game object has changed.
GameObjectContainer< G, A, R > container
Container of this GameObject.
G getNext(@NotNull final G gameObject)
Return the GameObject succeeding a given game object.
void setContainer(@Nullable final GameObjectContainer< G, A, R > container, final int mapX, final int mapY)
abstract MapSquare< G, A, R > getMapSquare()
Returns the MapSquare of this container.
MapModel< G, A, R > getMapModel()
Returns the MapModel this map square is part of.
Definition: MapSquare.java:99
void visit(@NotNull final BaseObjectVisitor< G, A, R > baseObjectVisitor)
Gridarta can handle frame information of animations and allow the selection of an animation using a t...
void insertBefore(@NotNull final G gameObject, @Nullable final G nextGameObject)
Add a GameObject before another.
void transientGameObjectChange(@NotNull G gameObject)
Method to notify the model that a game object was changed but need not be restored by undo/redo...
Base class for classes that contain GameObjects as children in the sense of containment.
void moveTop(@NotNull final G gameObject)
Move an item to top.
String getEffectiveFaceName(@NotNull final String faceName)
boolean isTop(@NotNull final G gameObject)
Returns whether this game object is the top-most one.
G newInstance(@NotNull final GameObjectFactory< G, A, R > gameObjectFactory)
Base package of all Gridarta classes.
boolean isBottom(@NotNull final G gameObject)
Returns whether this game object is the bottom-most one.
This exception is thrown in case a method of a GameObject without a container or the wrong container ...
Reflects a game object (object on a map).
Definition: GameObject.java:36
void beginGameObjectChange()
Records that this game object is about to change.
final AnimationObjects animationObjects
The AnimationObjects for looking up animations.
AnimationObjects is a container for AnimationObjects.
abstract G asGameObject()
Returns this instance as a GameObject or.
void insertAfter(@Nullable final G previousGameObject, @NotNull final G gameObject)
Add a GameObject after another.
static final long serialVersionUID
The serial version UID.
AbstractGameObject(@NotNull final R archetype, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final AnimationObjects animationObjects)
Creates a new instance.
void moveUp(@NotNull final G gameObject)
Move an item up.
String getAttributeString(@NotNull final String attributeName, final boolean queryArchetype)
void moveDown(@NotNull final G gameObject)
Move an item down.
Provider for faces of GameObjects and Archetypes.
G getPrev(@NotNull final G gameObject)
Return the GameObject preceding a given game object.
The face is the appearance of an object.
void beginGameObjectChange(@NotNull G gameObject)
Method to notify the model that a game object is about to change.
Default implementation for GameObject implementing classes.
void endGameObjectChange(@NotNull G gameObject)
Method to notify the model that a game object was changed.
final transient FaceObjectProviders faceObjectProviders
The FaceObjectProviders for looking up faces.
void remove(@NotNull final G gameObject)
Remove a GameObject from this container.
String getAttributeValue(@NotNull final String attributeName)
Returns an attribute value by attribute name.
void updateArchetype()
Called whenever getArchetype() has changed.
void moveBottom(@NotNull final G gameObject)
Move an item to bottom.