Crossfire JXClient, Trunk  R20561
CfMapAnimations.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.map;
23 
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Random;
31 import java.util.WeakHashMap;
32 import org.jetbrains.annotations.NotNull;
33 
38 public class CfMapAnimations {
39 
44  @NotNull
45  private final Random random = new Random();
46 
50  private int width;
51 
55  private int height;
56 
60  @NotNull
61  private final AnimationMap animations = new AnimationMap();
62 
66  @NotNull
67  private final Map<AnimationState, Void> animationStates = new WeakHashMap<>();
68 
72  @NotNull
73  private final Map<Integer, AnimationState> syncAnimationStates = new HashMap<>();
74 
79  @NotNull
80  private final Collection<AnimationState> pendingTickUpdates = new ArrayList<>();
81 
85  public void clear() {
86  animations.clear();
87  animationStates.clear();
88  syncAnimationStates.clear();
89  pendingTickUpdates.clear();
90  }
91 
99  public void add(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, @NotNull final Animation animation, final int type) {
100  assert 0 <= location.getX();
101  assert 0 <= location.getY();
102  assert 0 <= type && type < 4;
103 
104  final AnimationState animationState;
105  final boolean addToPendingTickUpdates;
106  switch (type) {
107  default: // invalid; treated as "normal"
108  case Map2.ANIM_NORMAL: // animation starts at index 0
109  animationState = new AnimationState(animation, 0);
110  addToPendingTickUpdates = true;
111  break;
112 
113  case Map2.ANIM_RANDOM: // animation starts at random index
114  animationState = new AnimationState(animation, random.nextInt(animation.getFaces()));
115  addToPendingTickUpdates = true;
116  break;
117 
118  case Map2.ANIM_SYNC: // animation is synchronized with other animations
119  final int animationId = animation.getAnimationId();
120  final AnimationState tmp = syncAnimationStates.get(animationId);
121  if (tmp == null) {
122  animationState = new AnimationState(animation, 0);
123  syncAnimationStates.put(animationId, animationState);
124  addToPendingTickUpdates = true;
125  } else {
126  animationState = tmp;
127  addToPendingTickUpdates = false;
128  }
129  break;
130  }
131 
132  animationStates.put(animationState, null);
133  animations.add(mapUpdaterState, location, animationState);
134  if (addToPendingTickUpdates) {
135  pendingTickUpdates.add(animationState);
136  }
137  }
138 
144  public void remove(final int x, final int y) {
145  for (int layer = 0; layer < Map2.NUM_LAYERS; layer++) {
146  animations.remove(new Location(x, y, layer));
147  }
148  }
149 
154  public void remove(@NotNull final Location location) {
155  animations.remove(location);
156  }
157 
164  public void updateSpeed(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, final int speed) {
165  animations.updateSpeed(mapUpdaterState, location, speed);
166  }
167 
174  public void scroll(final int dx, final int dy) {
175  animations.scroll(dx, dy, width, height);
176  }
177 
183  public void tick(@NotNull final MapUpdaterState mapUpdaterState, final int tickNo) {
184  for (final AnimationState animationState : pendingTickUpdates) {
185  animationState.setTickNo(tickNo);
186  }
187  pendingTickUpdates.clear();
188  final Iterable<AnimationState> animationStatesToUpdate = new ArrayList<>(animationStates.keySet());
189  synchronized (mapUpdaterState.mapBegin()) {
190  for (final AnimationState animationState : animationStatesToUpdate) {
191  animationState.updateTickNo(mapUpdaterState, tickNo);
192  }
193  mapUpdaterState.mapEnd(false);
194  }
195  }
196 
202  public void setMapSize(final int width, final int height) {
203  this.width = width;
204  this.height = height;
205  clear();
206  }
207 
208 }
final Map< AnimationState, Void > animationStates
All AnimationState instances referenced by animations.
void updateSpeed(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, final int speed)
Updates the animation speed value.
Maintains AnimationState instances for map locations.
void scroll(final int dx, final int dy, final int width, final int height)
Scrolls all locations.
void remove(@NotNull final Location location)
Clears a Location.
void add(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, @NotNull final Animation animation, final int type)
Adds a visible animation.
int NUM_LAYERS
The total number of map layers to display.
Definition: Map2.java:33
void scroll(final int dx, final int dy)
Scrolls the animations.
final Map< Integer, AnimationState > syncAnimationStates
All AnimationState for Map2#ANIM_SYNC animations.
Interface defining constants for the "map2" Crossfire protocol message.
Definition: Map2.java:28
Manages a set of animated map squares.
final Random random
The random number generator for Map2#ANIM_RANDOM type animations.
int height
The height of the visible map area.
The state of an Animation on a map.
Update a CfMap model from protocol commands.
void tick(@NotNull final MapUpdaterState mapUpdaterState, final int tickNo)
Processes a tick command.
void add(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, @NotNull final AnimationState animationState)
Adds a new AnimationState to a Location.
int ANIM_SYNC
Animation type: synchronized animation.
Definition: Map2.java:129
int ANIM_RANDOM
Animation type: randomized animation.
Definition: Map2.java:123
void updateSpeed(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, final int speed)
Updates the animation speed value of a Location.
Manages animations received from the server.
Definition: Animation.java:31
void setMapSize(final int width, final int height)
Updates the map size.
int width
The width of the visible map area.
final Collection< AnimationState > pendingTickUpdates
The AnimationState instances that have been added but not yet received a "tick" value.
final AnimationMap animations
The animations in the visible map area.
int ANIM_NORMAL
Animation type: normal animation.
Definition: Map2.java:118