|
14 | 14 | import de.lessvoid.nifty.screen.Screen;
|
15 | 15 | import de.lessvoid.nifty.screen.ScreenController;
|
16 | 16 | import java.io.IOException;
|
| 17 | +import java.net.Socket; |
| 18 | +import java.net.UnknownHostException; |
17 | 19 | import java.util.ArrayList;
|
18 | 20 | import java.util.List;
|
19 | 21 | import java.util.logging.Level;
|
20 | 22 | import java.util.logging.Logger;
|
21 | 23 |
|
22 | 24 | public class GUIAppState extends BaseAppState implements ScreenController
|
23 | 25 | {
|
| 26 | + |
24 | 27 | private Nifty _niftyInstance;
|
25 | 28 | Element _scoreboardElement;
|
26 | 29 | private List<BaseAppState> _gameStates;
|
@@ -58,15 +61,16 @@ protected void initialize(Application app)
|
58 | 61 | _gameStates.add(new InputManagerAppState());
|
59 | 62 | _gameStates.add(new MapAppState());
|
60 | 63 | _gameStates.add(new PlayerAppState());
|
| 64 | + _gameStates.add(new BulletFactoryAppState()); |
61 | 65 | _gameStates.add(new TopDownCameraAppState());
|
62 | 66 | }
|
63 | 67 |
|
64 | 68 | /**
|
65 | 69 | * Handle join button onClick (only in hud-screen).
|
66 | 70 | */
|
67 |
| - public void joinButtonClicked() throws InterruptedException |
| 71 | + public void joinButtonClicked() |
68 | 72 | {
|
69 |
| - boolean canConnect = initFakeConnection(); |
| 73 | + boolean canConnect = hostAvailabilityCheck(); |
70 | 74 | if (canConnect)
|
71 | 75 | {
|
72 | 76 | attachGameStates();
|
@@ -145,35 +149,51 @@ public void updateTimer(long timeLeft)
|
145 | 149 | }
|
146 | 150 |
|
147 | 151 | /**
|
148 |
| - * It is a very bad way of verifying user's ip/port combination. I wan´t |
149 |
| - * able to check that while attaching the state. This is the only workaround |
150 |
| - * I have found and I am not proud of it. |
| 152 | + * It is a way of verifying user's ip/port combination. I wasn´t able to |
| 153 | + * check that while attaching the state. This is the only workaround I have |
| 154 | + * found and I am not proud of it. |
151 | 155 | *
|
152 | 156 | * @return value indicating whether it is possible to connect to given port
|
153 | 157 | */
|
154 |
| - private boolean initFakeConnection() |
| 158 | + public boolean hostAvailabilityCheck() |
155 | 159 | {
|
| 160 | + boolean available = true; |
| 161 | + Socket s; |
156 | 162 | try
|
157 | 163 | {
|
158 |
| - Client _clientInstance = Network.connectToServer(getTextIp(), getTextPort()); |
159 |
| - _clientInstance.start(); |
160 |
| - _clientInstance.close(); |
161 |
| - } catch (IOException ex) |
| 164 | + s = new Socket(getTextIp(), getTextPort()); |
| 165 | + if (s.isConnected()) |
| 166 | + { |
| 167 | + s.close(); |
| 168 | + } |
| 169 | + } catch (UnknownHostException e) |
| 170 | + { |
| 171 | + // unknown host |
| 172 | + available = false; |
| 173 | + } catch (IOException e) |
162 | 174 | {
|
163 |
| - Log("initFakeConnection failed"); |
164 |
| - ex.printStackTrace(); |
165 |
| - return false; |
| 175 | + // io exception, service probably not running |
| 176 | + available = false; |
| 177 | + } catch (NullPointerException e) |
| 178 | + { |
| 179 | + available = false; |
| 180 | + } finally |
| 181 | + { |
| 182 | + s = null; |
166 | 183 | }
|
167 |
| - return true; |
| 184 | + |
| 185 | + return available; |
168 | 186 | }
|
169 | 187 |
|
170 |
| - private void attachGameStates() throws InterruptedException |
| 188 | + private void attachGameStates() |
171 | 189 | {
|
172 | 190 | _gameStates.add(new NetworkAppState(getTextIp(), getTextPort()));
|
| 191 | + _gameStates.add(new SimulationAppState()); |
173 | 192 | for (BaseAppState state : _gameStates)
|
174 | 193 | {
|
175 | 194 | _application.getStateManager().attach(state);
|
176 | 195 | }
|
| 196 | + |
177 | 197 | }
|
178 | 198 |
|
179 | 199 | private void detachGameStates()
|
|
0 commit comments