aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/olacraft/network/GameClient.java
blob: 2312c3d9ed7711960a6845c489a90e51142d7606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package ru.olamedia.olacraft.network;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

import ru.olamedia.game.GameManager;
import ru.olamedia.liveEntity.LiveEntity;
import ru.olamedia.olacraft.game.Game;
import ru.olamedia.olacraft.network.packet.ConnectionPacket;
import ru.olamedia.olacraft.network.packet.ConnectionRequestPacket;
import ru.olamedia.olacraft.network.packet.IPacket;
import ru.olamedia.olacraft.network.packet.IPacketListener;
import ru.olamedia.olacraft.network.packet.LiveEntityLocationUpdatePacket;
import ru.olamedia.olacraft.network.packet.SpawnPacket;
import ru.olamedia.olacraft.network.packet.SpawnRequestPacket;
import ru.olamedia.olacraft.network.packet.WorldInfoPacket;
import ru.olamedia.olacraft.scene.GameScene;
import ru.olamedia.olacraft.world.WorldInfo;
import ru.olamedia.olacraft.world.dataProvider.CachedChunkDataProvider;
import ru.olamedia.olacraft.world.dataProvider.LocalChunkDataProvider;
import ru.olamedia.olacraft.world.dataProvider.RemoteChunkDataProvider;
import ru.olamedia.olacraft.world.provider.WorldProvider;

public class GameClient extends ConnectionStateListener implements IPacketListener {
	private WorldProvider worldProvider;
	private GameScene scene;

	private Client client = new Client(10 * 1024 * 1024, 10 * 1024 * 1024);
	private String hostname = "127.0.0.1";

	@Override
	public void onChangeState(ConnectionState state) {
		if (state.isConnected()) {
			GameManager.instance.hideMainMenu();
			client.sendTCP(new ConnectionRequestPacket());
			//
			// provider.load(0, 0, 0);
			// provider.load(1, 2, 3);
		}
	}

	/*
	 * public AbstractChunkDataProvider getChunkDataProvider() {
	 * return worldProvider.getChunkDataProvider();
	 * }
	 */

	public WorldProvider getWorldProvider() {
		return worldProvider;
	}

	private List<ConnectionStateListener> stateListeners = new ArrayList<ConnectionStateListener>();
	private List<IPacketListener> packetListeners = new ArrayList<IPacketListener>();
	private ExecutorService threadPool = Executors.newFixedThreadPool(1);

	public GameClient() {
		// INIT WORLD
		worldProvider = new WorldProvider();
		worldProvider.setChunkDataProvider(new CachedChunkDataProvider(new RemoteChunkDataProvider(this)));
		//worldProvider.setChunkDataProvider(new CachedChunkDataProvider(new LocalChunkDataProvider(worldProvider.getInfo().name)));
		// CREATE SCENE
		scene = new GameScene(worldProvider);
		Kryo kryo = client.getKryo();
		Network.registerPackets(kryo);
		addStateListener(this);
		addPacketListener(this);
		client.addListener(new Listener.ThreadedListener(new Listener() {

			@Override
			public void received(Connection connection, Object object) {
				if (object instanceof IPacket) {
					dispatchPacket(connection, (IPacket) object);
				}
			}

			@Override
			public void disconnected(Connection connection) {
				super.disconnected(connection);
				dispatchState(ConnectionState.STATE_DISCONNECTED);
			}
		}, threadPool));
		client.start();
	}

	public void addStateListener(ConnectionStateListener listener) {
		stateListeners.add(listener);
	}

	private void dispatchState(ConnectionState state) {
		for (ConnectionStateListener l : stateListeners) {
			l.onChangeState(state);
		}
	}

	private void dispatchPacket(Connection connection, IPacket p) {
		for (IPacketListener l : packetListeners) {
			l.onPacket(connection, p);
		}
	}

	public void connect() {
		new Thread("Connect") {
			public void run() {
				try {
					dispatchState(ConnectionState.STATE_CONNECTING);
					client.connect(5000, hostname, Game.port);
					dispatchState(ConnectionState.STATE_CONNECTED);
				} catch (IOException ex) {
					dispatchState(ConnectionState.STATE_DISCONNECTED);
				}
			}
		}.start();
	}

	public void close() {
		client.close();
	}

	public void send(IPacket p) {
		client.sendTCP(p);
	}

	public void addPacketListener(IPacketListener listener) {
		packetListeners.add(listener);
	}

	public boolean isConnected() {
		return client.isConnected();
	}

	private static boolean DEBUG = true;

	private void debug(String s) {
		if (DEBUG) {
			System.out.println("[GameClient] " + s);
		}
	}

	@Override
	public void onPacket(Connection connection, IPacket p) {
		debug("received " + p.getClass().getName());
		// 1. receive ConnectionPacket, send SpawnRequestPacket
		if (p instanceof ConnectionPacket) {
			Game.instance.player.setConnectionId(((ConnectionPacket) p).connectionId);
			send(new SpawnRequestPacket());
		}
		if (p instanceof WorldInfoPacket) {
			worldProvider.setInfo(((WorldInfoPacket) p).info);
		}
		// 2. Receive SpawnPacket
		if (p instanceof SpawnPacket) {
			LiveEntity entity;
			if (((SpawnPacket) p).connectionId == client.getID()) {
				// me
				entity = Game.instance.player;
			} else {
				// another player
				entity = new LiveEntity();
			}
			entity.setX(((SpawnPacket) p).x);
			entity.setY(((SpawnPacket) p).y);
			entity.setZ(((SpawnPacket) p).z);
			entity.setConnectionId(((SpawnPacket) p).connectionId);
			scene.registerLiveEntity(entity);
			if (((SpawnPacket) p).connectionId == client.getID()) {
				// me
				scene.registerPlayer(entity);
			}
		}
		if (p instanceof LiveEntityLocationUpdatePacket) {
			LiveEntity entity = scene.getLiveEntity(((LiveEntityLocationUpdatePacket) p).connectionId);
			if (null != entity) {
				entity.setX(((LiveEntityLocationUpdatePacket) p).x);
				entity.setY(((LiveEntityLocationUpdatePacket) p).y);
				entity.setZ(((LiveEntityLocationUpdatePacket) p).z);
			}
		}
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public void dispose() {
		client.close();
		client.stop();
		// threadPool.awaitTermination(1000, TimeUnit.MILLISECONDS);
		threadPool.shutdownNow();
	}

	public GameScene getScene() {
		return scene;
	}

	public WorldInfo getWorldInfo() {
		return worldProvider.getInfo();
	}

}