Crossfire JXClient, Trunk  R20561
StatsWatcher.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.sound;
23 
31 import org.jetbrains.annotations.NotNull;
32 
37 public class StatsWatcher {
38 
42  private static final long DELAY = 5000;
43 
47  @NotNull
48  private final SoundManager soundManager;
49 
53  private boolean active;
54 
58  private boolean poisoned;
59 
63  private int level;
64 
70  private long ignoreLevelChange = System.currentTimeMillis()+DELAY;
71 
75  @NotNull
76  @SuppressWarnings("FieldCanBeLocal")
77  private final StatsListener statsListener = new StatsListener() {
78 
79  @Override
80  public void reset() {
81  ignoreLevelChange = System.currentTimeMillis()+DELAY;
82  }
83 
84  @Override
85  public void statChanged(final int statNo, final int value) {
86  checkStats(statNo, value);
87  }
88 
89  @Override
90  public void simpleWeaponSpeedChanged(final boolean simpleWeaponSpeed) {
91  // ignore
92  }
93 
94  @Override
95  public void titleChanged(@NotNull final String title) {
96  // ignore
97  }
98 
99  @Override
100  public void rangeChanged(@NotNull final String range) {
101  // ignore
102  }
103 
104  @Override
105  public void activeSkillChanged(@NotNull final String activeSkill) {
106  // ignore
107  }
108 
109  @Override
110  public void experienceChanged(final long exp) {
111  // ignore
112  }
113 
114  @Override
115  public void experienceNextLevelChanged(final long expNextLevel) {
116  // ignore
117  }
118 
119  };
120 
124  @NotNull
125  @SuppressWarnings("FieldCanBeLocal")
127 
128  @Override
129  public void guiStateChanged(@NotNull final RendererGuiState rendererGuiState) {
130  active = rendererGuiState == RendererGuiState.PLAYING;
131  ignoreLevelChange = System.currentTimeMillis()+DELAY;
132  }
133 
134  };
135 
139  @NotNull
140  @SuppressWarnings("FieldCanBeLocal")
142 
143  @Override
144  public void delinvReceived(final int tag) {
145  // ignore
146  }
147 
148  @Override
149  public void delitemReceived(@NotNull final int[] tags) {
150  // ignore
151  }
152 
153  @Override
154  public void addItemReceived(final int location, final int tag, final int flags, final int weight, final int faceNum, @NotNull final String name, @NotNull final String namePl, final int anim, final int animSpeed, final int nrof, final int type) {
155  // ignore
156  }
157 
158  @Override
159  public void playerReceived(final int tag, final int weight, final int faceNum, @NotNull final String name) {
160  ignoreLevelChange = System.currentTimeMillis()+DELAY;
161  }
162 
163  @Override
164  public void upditemReceived(final int flags, final int tag, final int valLocation, final int valFlags, final int valWeight, final int valFaceNum, @NotNull final String valName, @NotNull final String valNamePl, final int valAnim, final int valAnimSpeed, final int valNrof) {
165  // ignore
166  }
167 
168  };
169 
177  public StatsWatcher(@NotNull final Stats stats, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final CrossfireServerConnection server, @NotNull final SoundManager soundManager) {
178  this.soundManager = soundManager;
179  poisoned = stats.getStat(Stats.C_STAT_POISONED) != 0;
180  level = stats.getStat(Stats.CS_STAT_LEVEL);
181  stats.addCrossfireStatsListener(statsListener);
182  windowRenderer.addGuiStateListener(rendererGuiStateListener);
183  rendererGuiStateListener.guiStateChanged(windowRenderer.getGuiState());
184  server.addCrossfireUpdateItemListener(crossfireUpdateItemListener);
185  }
186 
192  private void checkStats(final int statNo, final int value) {
193  if (statNo == Stats.C_STAT_POISONED) {
194  final boolean newPoisoned = value != 0;
195  if (poisoned != newPoisoned) {
196  poisoned = newPoisoned;
197  if (active) {
198  playClip(newPoisoned ? Sounds.POISON_ON : Sounds.POISON_OFF);
199  }
200  }
201  } else if (statNo == Stats.CS_STAT_LEVEL) {
202  final int newLevel = value;
203  if (level != newLevel) {
204  if (ignoreLevelChange != 0 && ignoreLevelChange <= System.currentTimeMillis()) {
205  ignoreLevelChange = 0;
206  }
207  if (ignoreLevelChange == 0 && level < newLevel && active) {
209  }
210  level = newLevel;
211  }
212  }
213  }
214 
219  private void playClip(@NotNull final String clip) {
220  if (active) {
221  soundManager.playClip(Sounds.CHARACTER, null, clip);
222  }
223  }
224 
225 }
void playClip(@NotNull final String clip)
Plays a clip if sounds should be generated.
void guiStateChanged(@NotNull RendererGuiState rendererGuiState)
The gui state has changed.
final RendererGuiStateListener rendererGuiStateListener
The gui state listener.
final SoundManager soundManager
The SoundManager instance to watch.
final CrossfireUpdateItemListener crossfireUpdateItemListener
The CrossfireUpdateItemListener to receive item updates.
static final int CS_STAT_LEVEL
The Global Level stat.
Definition: Stats.java:108
Monitors stat changes and generates appropriate sound effects.
void playClip(@NotNull final Sounds type, @Nullable final String name, @NotNull final String action)
Plays a sound clip.
boolean poisoned
The last known poisoned state.
final StatsListener statsListener
The crossfire stats listener.
Interface for listeners interested in changes of Stats instances.
StatsWatcher(@NotNull final Stats stats, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final CrossfireServerConnection server, @NotNull final SoundManager soundManager)
Creates a new instance.
static final int C_STAT_POISONED
The "is poisoned" indicator.
Definition: Stats.java:440
static final String LEVEL_UP
The sound to play when the character gains a new level.
Definition: Sounds.java:53
static final String POISON_OFF
The sound to play when the character gets un-poisoned.
Definition: Sounds.java:47
void checkStats(final int statNo, final int value)
Checks for changed stats and generate sound effects.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
This is the representation of all the statistics of a player, like its speed or its experience...
Definition: Stats.java:43
boolean active
Whether sounds should be generated.
static final String POISON_ON
The sound to play when the character get poisoned.
Definition: Sounds.java:41
Interface for listeners interested in gui state changes.
long ignoreLevelChange
Ignore level changes until this time has reached.
static final long DELAY
Duration for which to ignore level changes after login.