author | unC0Rr |
Wed, 01 May 2024 16:49:16 +0200 | |
changeset 16041 | caba603f461f |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
1 |
/* |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
4 |
* |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
9 |
* |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
* GNU General Public License for more details. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
* |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
18 |
*/ |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
|
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
package org.hedgewars.hedgeroid; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
|
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
import static org.hedgewars.hedgeroid.util.ObjectUtils.equal; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
|
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
import java.util.Collections; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
import java.util.LinkedList; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
import java.util.List; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
import java.util.Map; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
import java.util.TreeMap; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
|
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
import org.hedgewars.hedgeroid.RoomStateManager; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
import org.hedgewars.hedgeroid.Datastructures.MapRecipe; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
import org.hedgewars.hedgeroid.Datastructures.Scheme; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
import org.hedgewars.hedgeroid.Datastructures.Weaponset; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
|
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
/** |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
* Common base implementation for a roomstate that will call listeners on every |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
* change. The derived classes have to coordinate how state is changed to |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
* complete the implementation of the RoomStateManager interface. |
10017 | 40 |
* |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
* See {@link RoomStateManager} for a description of what this is for. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
*/ |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
public abstract class BasicRoomState implements RoomStateManager { |
10017 | 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 |
} |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
68 |
|
10017 | 69 |
public final Weaponset getWeaponset() { |
70 |
return weaponset; |
|
71 |
} |
|
72 |
||
73 |
public final Map<String, TeamInGame> getTeams() { |
|
74 |
return teams; |
|
75 |
} |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
76 |
|
10017 | 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 |
} |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
85 |
|
10017 | 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 |
} |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
94 |
|
10017 | 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 |
} |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
121 |
|
10017 | 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 |
} |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
161 |
} |