|
1 /* |
|
2 * Hedgewars, a free turn based strategy game |
|
3 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU General Public License |
|
7 * as published by the Free Software Foundation; either version 2 |
|
8 * of the License, or (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 |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 */ |
|
19 |
|
20 package org.hedgewars.hedgeroid; |
|
21 |
|
22 import static org.hedgewars.hedgeroid.util.ObjectUtils.equal; |
|
23 |
|
24 import java.util.Collections; |
|
25 import java.util.LinkedList; |
|
26 import java.util.List; |
|
27 import java.util.Map; |
|
28 import java.util.TreeMap; |
|
29 |
|
30 import org.hedgewars.hedgeroid.RoomStateManager; |
|
31 import org.hedgewars.hedgeroid.Datastructures.MapRecipe; |
|
32 import org.hedgewars.hedgeroid.Datastructures.Scheme; |
|
33 import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
|
34 import org.hedgewars.hedgeroid.Datastructures.Weaponset; |
|
35 |
|
36 /** |
|
37 * Common base implementation for a roomstate that will call listeners on every |
|
38 * change. The derived classes have to coordinate how state is changed to |
|
39 * complete the implementation of the RoomStateManager interface. |
|
40 * |
|
41 * See {@link RoomStateManager} for a description of what this is for. |
|
42 */ |
|
43 public abstract class BasicRoomState implements RoomStateManager { |
|
44 private final List<RoomStateManager.Listener> observers = new LinkedList<RoomStateManager.Listener>(); |
|
45 |
|
46 private boolean chief; |
|
47 private String gameStyle; |
|
48 private Scheme scheme; |
|
49 private MapRecipe map; |
|
50 private Weaponset weaponset; |
|
51 private Map<String, TeamInGame> teams = Collections.emptyMap(); |
|
52 |
|
53 public final MapRecipe getMapRecipe() { |
|
54 return map; |
|
55 } |
|
56 |
|
57 public final boolean getChiefStatus() { |
|
58 return chief; |
|
59 } |
|
60 |
|
61 public final Scheme getScheme() { |
|
62 return scheme; |
|
63 } |
|
64 |
|
65 public final String getGameStyle() { |
|
66 return gameStyle; |
|
67 } |
|
68 |
|
69 public final Weaponset getWeaponset() { |
|
70 return weaponset; |
|
71 } |
|
72 |
|
73 public final Map<String, TeamInGame> getTeams() { |
|
74 return teams; |
|
75 } |
|
76 |
|
77 public final void setWeaponset(Weaponset weaponset) { |
|
78 if(!equal(weaponset, this.weaponset)) { |
|
79 this.weaponset = weaponset; |
|
80 for(RoomStateManager.Listener observer : observers) { |
|
81 observer.onWeaponsetChanged(weaponset); |
|
82 } |
|
83 } |
|
84 } |
|
85 |
|
86 public final void setMapRecipe(MapRecipe map) { |
|
87 if(!equal(map, this.map)) { |
|
88 this.map = map; |
|
89 for(RoomStateManager.Listener observer : observers) { |
|
90 observer.onMapChanged(map); |
|
91 } |
|
92 } |
|
93 } |
|
94 |
|
95 public final void setGameStyle(String gameStyle) { |
|
96 if(!equal(gameStyle, this.gameStyle)) { |
|
97 this.gameStyle = gameStyle; |
|
98 for(RoomStateManager.Listener observer : observers) { |
|
99 observer.onGameStyleChanged(gameStyle); |
|
100 } |
|
101 } |
|
102 } |
|
103 |
|
104 public final void setScheme(Scheme scheme) { |
|
105 if(!equal(scheme, this.scheme)) { |
|
106 this.scheme = scheme; |
|
107 for(RoomStateManager.Listener observer : observers) { |
|
108 observer.onSchemeChanged(scheme); |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 public final void setChief(boolean chief) { |
|
114 if(chief != this.chief) { |
|
115 this.chief = chief; |
|
116 for(RoomStateManager.Listener observer : observers) { |
|
117 observer.onChiefStatusChanged(chief); |
|
118 } |
|
119 } |
|
120 } |
|
121 |
|
122 public final void putTeam(TeamInGame team) { |
|
123 TeamInGame oldEntry = teams.get(team.team.name); |
|
124 if(!equal(team, oldEntry)) { |
|
125 Map<String, TeamInGame> changedMap = new TreeMap<String, TeamInGame>(teams); |
|
126 changedMap.put(team.team.name, team); |
|
127 teams = Collections.unmodifiableMap(changedMap); |
|
128 for(RoomStateManager.Listener observer : observers) { |
|
129 observer.onTeamsChanged(teams); |
|
130 } |
|
131 } |
|
132 } |
|
133 |
|
134 public final void removeTeam(String teamname) { |
|
135 if(teams.containsKey(teamname)) { |
|
136 Map<String, TeamInGame> changedMap = new TreeMap<String, TeamInGame>(teams); |
|
137 changedMap.remove(teamname); |
|
138 teams = Collections.unmodifiableMap(changedMap); |
|
139 for(RoomStateManager.Listener observer : observers) { |
|
140 observer.onTeamsChanged(teams); |
|
141 } |
|
142 } |
|
143 } |
|
144 |
|
145 public final void setTeams(Map<String, TeamInGame> newTeams) { |
|
146 if(!newTeams.equals(teams)) { |
|
147 teams = Collections.unmodifiableMap(new TreeMap<String, TeamInGame>(newTeams)); |
|
148 for(RoomStateManager.Listener observer : observers) { |
|
149 observer.onTeamsChanged(teams); |
|
150 } |
|
151 } |
|
152 } |
|
153 |
|
154 public final void addListener(Listener observer) { |
|
155 observers.add(observer); |
|
156 } |
|
157 |
|
158 public final void removeListener(Listener observer) { |
|
159 observers.remove(observer); |
|
160 } |
|
161 } |