Crossfire JXClient, Trunk  R20561
AnimationMap.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 
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
35 public class AnimationMap {
36 
41  @NotNull
42  private final Map<Location, AnimationState> animations = new HashMap<>();
43 
47  public void clear() {
48  animations.clear();
49  }
50 
58  public void add(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, @NotNull final AnimationState animationState) {
59  freeAnimationState(animations.put(location, animationState), location);
60  animationState.allocate(mapUpdaterState, location);
61  }
62 
67  public void remove(@NotNull final Location location) {
68  freeAnimationState(animations.remove(location), location);
69  }
70 
77  public void updateSpeed(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, final int speed) {
78  final AnimationState animationState = animations.get(location);
79  if (animationState == null) {
80  System.err.println("No animation at "+location+" to update animation speed.");
81  return;
82  }
83 
84  animationState.setSpeed(mapUpdaterState, speed);
85  }
86 
94  public void scroll(final int dx, final int dy, final int width, final int height) {
95  final Iterable<AnimationState> tmp = new HashSet<>(animations.values());
96  animations.clear();
97  for (final AnimationState animationState : tmp) {
98  animationState.scroll(dx, dy, width, height);
99  for (final Location location : animationState) {
100  animations.put(location, animationState);
101  }
102  }
103  }
104 
111  private static void freeAnimationState(@Nullable final AnimationState animationState, @NotNull final Location location) {
112  if (animationState != null) {
113  animationState.free(location);
114  }
115  }
116 
117 }
Maintains AnimationState instances for map locations.
void scroll(final int dx, final int dy, final int width, final int height)
Scrolls all locations.
final Map< Location, AnimationState > animations
The active AnimationState instances.
static void freeAnimationState(@Nullable final AnimationState animationState, @NotNull final Location location)
Calls AnimationState#free(Location).
The state of an Animation on a map.
Update a CfMap model from protocol commands.
void add(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, @NotNull final AnimationState animationState)
Adds a new AnimationState to a Location.
void updateSpeed(@NotNull final MapUpdaterState mapUpdaterState, @NotNull final Location location, final int speed)
Updates the animation speed value of a Location.
void setSpeed(@NotNull final MapUpdaterState mapUpdaterState, final int speed)
Sets the animation speed.