Crossfire JXClient, Trunk  R20561
StatGaugeUpdater.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.gui.gauge;
23 
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
38 public class StatGaugeUpdater extends GaugeUpdater {
39 
44  private static final int LOWFOOD_LIMIT = 100;
45 
49  private final int stat;
50 
54  @NotNull
55  private final Stats stats;
56 
60  @NotNull
61  private final ItemSet itemSet;
62 
66  private boolean active;
67 
71  @NotNull
72  private final StatsListener statsListener = new StatsListener() {
73 
74  @Override
75  public void reset() {
76  // ignore
77  }
78 
79  @Override
80  public void statChanged(final int statNo, final int value) {
81  switch (stat) {
82  case Stats.CS_STAT_HP:
83  if (statNo == Stats.CS_STAT_HP) {
84  setValues(value, 0, stats.getStat(Stats.CS_STAT_MAXHP));
85  } else if (statNo == Stats.CS_STAT_MAXHP) {
86  setValues(stats.getStat(Stats.CS_STAT_HP), 0, value);
87  }
88  break;
89 
90  case Stats.CS_STAT_SP:
91  if (statNo == Stats.CS_STAT_SP) {
92  setValues(value, 0, stats.getStat(Stats.CS_STAT_MAXSP));
93  } else if (statNo == Stats.CS_STAT_MAXSP) {
94  setValues(stats.getStat(Stats.CS_STAT_SP), 0, value);
95  }
96  break;
97 
98  case Stats.CS_STAT_FOOD:
99  if (statNo == Stats.CS_STAT_FOOD) {
100  setValues(value, 0, 999);
101  }
102  break;
103 
104  case Stats.C_STAT_LOWFOOD:
105  if (statNo == Stats.C_STAT_LOWFOOD) {
106  setValues(active && stats.getStat(Stats.CS_STAT_FOOD) < LOWFOOD_LIMIT ? 1 : 0, 0, 1);
107  }
108  break;
109 
110  case Stats.CS_STAT_GRACE:
111  if (statNo == Stats.CS_STAT_GRACE) {
112  setValues(value, 0, stats.getStat(Stats.CS_STAT_MAXGRACE));
113  } else if (statNo == Stats.CS_STAT_MAXGRACE) {
114  setValues(stats.getStat(Stats.CS_STAT_GRACE), 0, value);
115  }
116  break;
117 
118  case Stats.C_STAT_POISONED:
119  if (statNo == Stats.C_STAT_POISONED) {
120  setValues(value, 0, 1);
121  }
122  break;
123 
124  case Stats.CS_STAT_GOLEM_HP:
125  if (statNo == Stats.CS_STAT_GOLEM_HP) {
126  setValues(value, 0, stats.getStat(Stats.CS_STAT_GOLEM_MAXHP));
127  } else if (statNo == Stats.CS_STAT_GOLEM_MAXHP) {
128  setValues(stats.getStat(Stats.CS_STAT_GOLEM_HP), 0, value);
129  }
130  break;
131 
132  default:
133  if (Stats.CS_STAT_RESIST_START <= stat && stat <= Stats.CS_STAT_RESIST_END && statNo == stat) {
134  setValues(value, 0, 100);
135  }
136  break;
137  }
138  }
139 
140  @Override
141  public void simpleWeaponSpeedChanged(final boolean simpleWeaponSpeed) {
142  // ignore
143  }
144 
145  @Override
146  public void titleChanged(@NotNull final String title) {
147  // ignore
148  }
149 
150  @Override
151  public void rangeChanged(@NotNull final String range) {
152  // ignore
153  }
154 
155  @Override
156  public void activeSkillChanged(@NotNull final String activeSkill) {
157  // ignore
158  }
159 
160  @Override
161  public void experienceChanged(final long exp) {
162  if (stat == Stats.C_STAT_EXP_NEXT_LEVEL) {
164  }
165  }
166 
167  @Override
168  public void experienceNextLevelChanged(final long expNextLevel) {
169  if (stat == Stats.C_STAT_EXP_NEXT_LEVEL) {
171  }
172  }
173 
174  };
175 
179  @NotNull
181 
182  @Override
183  public void itemAdded(@NotNull final CfItem item) {
184  // ignore
185  }
186 
187  @Override
188  public void itemMoved(@NotNull final CfItem item) {
189  // ignore
190  }
191 
192  @Override
193  public void itemChanged(@NotNull final CfItem item) {
194  // ignore
195  }
196 
197  @Override
198  public void itemRemoved(@NotNull final CfItem item) {
199  // ignore
200  }
201 
202  @Override
203  public void playerChanged(@Nullable final CfItem player) {
204  active = player != null;
205  }
206 
207  @Override
208  public void openContainerChanged(final int tag) {
209  // ignore
210  }
211 
212  };
213 
221  public StatGaugeUpdater(@NotNull final ExperienceTable experienceTable, final int stat, @NotNull final Stats stats, @NotNull final ItemSet itemSet) {
222  super(experienceTable, stat == Stats.CS_STAT_GOLEM_HP);
223  this.stat = stat;
224  this.stats = stats;
225  this.itemSet = itemSet;
226  this.stats.addCrossfireStatsListener(statsListener);
227  this.itemSet.addItemSetListener(itemSetListener);
228  }
229 
233  @Override
234  public void dispose() {
235  itemSet.removeItemSetListener(itemSetListener);
236  stats.removeCrossfireStatsListener(statsListener);
237  }
238 
242  private void updateExperienceNextLevel() {
243  final int level = stats.getStat(Stats.CS_STAT_LEVEL);
244  final long experience = stats.getExperience();
245  final int percents = getPercentsToNextLevel(level, experience);
246  setValues(percents, 0, 99, percents+"%", "Level: "+level+"<br>Experience:"+Formatter.formatLong(experience)+"<br>Next level:"+Formatter.formatLong(getExperienceToNextLevel(level, experience)));
247  }
248 
249 }
static final int CS_STAT_GOLEM_HP
The golem&#39;s hitpoints, 0 if no golem.
Definition: Stats.java:319
final ItemSet itemSet
The ItemSet instance to watch.
static String formatLong(final long value)
Returns a.
Definition: Formatter.java:44
void updateExperienceNextLevel()
Updates information for Stats#C_STAT_EXP_NEXT_LEVEL.
StatGaugeUpdater(@NotNull final ExperienceTable experienceTable, final int stat, @NotNull final Stats stats, @NotNull final ItemSet itemSet)
Creates a new instance.
int getPercentsToNextLevel(final int level, final long experience)
Returns the experience fraction of the current level.
static final int CS_STAT_RESIST_START
Beginning index of the resistances.
Definition: Stats.java:329
void removeCrossfireStatsListener(@NotNull final StatsListener statsListener)
Removes a StatsListener to be notified about stat changes.
Definition: Stats.java:778
void addItemSetListener(@NotNull final ItemSetListener listener)
Adds an ItemSetListener to be notified about changes.
Definition: ItemSet.java:93
static final int C_STAT_LOWFOOD
The Low Food indicator.
Definition: Stats.java:143
long getExperience()
Returns the amount of global experience.
Definition: Stats.java:722
static final int CS_STAT_HP
The Hit Points stat.
Definition: Stats.java:48
static final int CS_STAT_LEVEL
The Global Level stat.
Definition: Stats.java:108
static final int CS_STAT_MAXHP
The Maximum Hit Points stat.
Definition: Stats.java:53
static final int CS_STAT_FOOD
The Food stat.
Definition: Stats.java:138
static final int CS_STAT_SP
The Spell Points stat.
Definition: Stats.java:58
void removeItemSetListener(@NotNull final ItemSetListener listener)
Removes an ItemSetListener to be notified about changes.
Definition: ItemSet.java:101
static final int CS_STAT_GOLEM_MAXHP
The golem&#39;s maximum hitpoints, 0 if no golem.
Definition: Stats.java:324
Interface for listeners in ItemSet related events.
Utility class for formatting values into strings.
Definition: Formatter.java:30
final ExperienceTable experienceTable
The experience table to query.
final ItemSetListener itemSetListener
The listener to detect a changed player name.
static final int C_STAT_EXP_NEXT_LEVEL
The global experience needed to reach next level stat.
Definition: Stats.java:103
boolean active
Whether the low food event should be generated.
static final int CS_STAT_MAXGRACE
The Maximum Grace stat.
Definition: Stats.java:174
Interface for listeners interested in changes of Stats instances.
Updates the displayed values in a GUIGauge.
static final int LOWFOOD_LIMIT
The LOWFOOD indicator is turned on if the FOOD value falls below this value.
static final int C_STAT_POISONED
The "is poisoned" indicator.
Definition: Stats.java:440
static final int CS_STAT_RESIST_END
End index of the resistances.
Definition: Stats.java:334
long getExperienceToNextLevel(final int level, final long experience)
Returns the experience needed to reach next level.
final StatsListener statsListener
The StatsListener registered to be notified about stat changes.
Model class maintaining the CfItems known to the player.
Definition: ItemSet.java:43
void setValues(final int curValue, final int minValue, final int maxValue)
Updates the gauge values.
This is the representation of all the statistics of a player, like its speed or its experience...
Definition: Stats.java:43
static final int CS_STAT_MAXSP
The Maximum Spell Points stat.
Definition: Stats.java:63
The representation of a Crossfire Item, client-side.
Definition: CfItem.java:36
static final int CS_STAT_GRACE
The Grace stat.
Definition: Stats.java:169
int getStat(final int statNo)
Returns the numerical value of the given statistic.
Definition: Stats.java:615
A GaugeUpdater which monitors a stat value.