Gridarta Editor
LightMapModelTracker.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;
26 import net.sf.gridarta.utils.Size2D;
27 import org.jetbrains.annotations.NotNull;
28 
35 public class LightMapModelTracker<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
36 
41  private static final int MAX_LIGHT_RADIUS = 4;
42 
46  @NotNull
47  private final MapModel<G, A, R> mapModel;
48 
52  @NotNull
53  private final Point point = new Point();
54 
58  @NotNull
59  private final Point point2 = new Point();
60 
65  public LightMapModelTracker(@NotNull final MapModel<G, A, R> mapModel) {
66  this.mapModel = mapModel;
67  }
68 
74  public void mapSizeChanging(@NotNull final Size2D newSize, @NotNull final Size2D oldSize) {
75  final int newWidth = newSize.getWidth();
76  final int oldWidth = oldSize.getWidth();
77  final int newHeight = newSize.getHeight();
78  final int oldHeight = oldSize.getHeight();
79  for (point.x = newWidth; point.x < oldWidth; point.x++) {
80  for (point.y = 0; point.y < oldHeight; point.y++) {
81  final MapSquare<G, A, R> mapSquare = mapModel.getMapSquare(point);
82  setLightRadius(mapSquare, 0);
83  }
84  }
85  for (point.x = 0; point.x < newWidth; point.x++) {
86  for (point.y = newHeight; point.y < newHeight; point.y++) {
87  final MapSquare<G, A, R> mapSquare = mapModel.getMapSquare(point);
88  setLightRadius(mapSquare, 0);
89  }
90  }
91  }
92 
97  public void mapSquaresChanged(@NotNull final Iterable<MapSquare<G, A, R>> mapSquares) {
98  for (final MapSquare<G, A, R> mapSquare : mapSquares) {
99  updateLightRadius(mapSquare);
100  }
101  }
102 
108  private void updateLightRadius(@NotNull final MapSquare<G, A, R> mapSquare) {
109  int maxLightRadius = 0;
110  for (final G gameObject : mapSquare) {
111  final int lightRadius = gameObject.getLightRadius();
112  if (maxLightRadius < lightRadius) {
113  maxLightRadius = lightRadius;
114  }
115  }
116  setLightRadius(mapSquare, Math.min(maxLightRadius, MAX_LIGHT_RADIUS));
117  }
118 
125  private void setLightRadius(@NotNull final MapSquare<G, A, R> mapSquare, final int lightRadius) {
126  final int prevLightRadius = mapSquare.getLightRadius();
127  if (lightRadius == prevLightRadius) {
128  return;
129  }
130  mapSquare.setLightRadius(lightRadius);
131  if (lightRadius < prevLightRadius) {
132  // light radius shrinked => remove light sources
133  for (int dx = -prevLightRadius; dx <= prevLightRadius; dx++) {
134  for (int dy = -prevLightRadius; dy <= prevLightRadius; dy++) {
135  if (dx < -lightRadius || dx > lightRadius || dy < -lightRadius || dy > lightRadius || lightRadius == 0) {
136  point2.x = mapSquare.getMapX() + dx;
137  point2.y = mapSquare.getMapY() + dy;
138  try {
139  final MapSquare<G, A, R> mapSquare2 = mapModel.getMapSquare(point2);
140  mapSquare2.removeLightSource(mapSquare);
141  } catch (final IndexOutOfBoundsException ignored) {
142  // skip points outside map bounds
143  }
144  }
145  }
146  }
147  } else {
148  // light increased shrinked => add light sources
149  for (int dx = -lightRadius; dx <= lightRadius; dx++) {
150  for (int dy = -lightRadius; dy <= lightRadius; dy++) {
151  if (dx < -prevLightRadius || dx > prevLightRadius || dy < -prevLightRadius || dy > prevLightRadius || prevLightRadius == 0) {
152  point2.x = mapSquare.getMapX() + dx;
153  point2.y = mapSquare.getMapY() + dy;
154  try {
155  mapModel.getMapSquare(point2).addLightSource(mapSquare);
156  } catch (final IndexOutOfBoundsException ignored) {
157  // skip points outside map bounds
158  }
159  }
160  }
161  }
162  }
163  }
164 
165 }
static final int MAX_LIGHT_RADIUS
The maximal supported light radius.
void addLightSource(@NotNull final MapSquare< G, A, R > mapSquare)
Adds a light emitting game object that affects this map square.
Definition: MapSquare.java:352
LightMapModelTracker(@NotNull final MapModel< G, A, R > mapModel)
Creates a new instance.
void removeLightSource(@NotNull final MapSquare< G, A, R > mapSquare)
Removes a light emitting game object that affects this map square.
Definition: MapSquare.java:369
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void mapSizeChanging(@NotNull final Size2D newSize, @NotNull final Size2D oldSize)
Called whenever the tracked map is about to change size.
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.
void mapSquaresChanged(@NotNull final Iterable< MapSquare< G, A, R >> mapSquares)
Called whenever some game objects have changed.
final MapModel< G, A, R > mapModel
The tracked MapModel.
void updateLightRadius(@NotNull final MapSquare< G, A, R > mapSquare)
Recalculates information about light emitting game objects in a map square.
void setLightRadius(@NotNull final MapSquare< G, A, R > mapSquare, final int lightRadius)
Updates the light radius of a map square.
Tracks a MapModel for light emitting game objects.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30