File: world_template.h

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (296 lines) | stat: -rw-r--r-- 6,527 bytes parent folder | download
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
	$Id: world_template.h,v 1.22 2001/12/18 15:55:33 sphair Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	See http://www.clanlib.org
	------------------------------------------------------------------------
*/

//! clanNetwork="World Template"
//! header=network.h

#ifndef header_world_template
#define header_world_template

#include "../Core/System/cl_assert.h"
#include "../Core/System/system.h"
#include "../Core/IOData/inputsource_memory.h"
#include "../Core/IOData/outputsource_memory.h"
#include "netobject_channel.h"
#include "netobject.h"

template<class World>
//: Game object template.
class CL_GameObject
{
public:
	//: Predefined message types:
	enum EGameObjectMessageTypes
	{
		msgtype_full_update = 0,
		msgtype_tick_update = 1,
		msgtype_destroy     = 2,
		msgtype_user        = 1000 // reserve first 1000 entries for clanlib.
	};

//! Construction:
public:
	//: Server constructor.
	CL_GameObject(World *world, int object_type)
	: world(world), object_type(object_type), netobject(&world->get_netobject_channel())
	{
		common_init();
	}

	//: Client construtor.
	CL_GameObject(World *world, int object_type, const CL_NetObject &netobject)
	: world(world), object_type(object_type), netobject(netobject)
	{
		common_init();
	}

	//: Game Object Destructor
	virtual ~CL_GameObject() { return; }

//! Attributes:
public:
	//: Get World
	World *get_world() { return world; }

	//: Get destroy flag
	bool get_destroy_flag() { return destroy_flag; }

	//: Get netobject
	CL_NetObject &get_netobject() { return netobject; }

	//: Get object type
	int get_object_type() { return object_type; }

//! Operations:
public:
	//: Set destroy flag
	void set_destroy_flag(bool new_value = true) { destroy_flag = new_value; }

	//: Set tick rate
	void set_tick_rate(float time_per_tick) { tick_rate = time_per_tick; }

	//: Send full
	void send_full(const CL_NetComputer *dest = NULL)
	{
		CL_OutputSource_Memory output;
		output.write_int32(object_type);
		write_full_update(output);
		netobject.send(/*dest,*/ msgtype_full_update, output.get_data());
	}

	//: Send tick
	void send_tick(const CL_NetComputer *dest = NULL)
	{
		CL_OutputSource_Memory output;
		write_tick_update(output);
		netobject.send(/*dest,*/ msgtype_tick_update, output.get_data());
	}

	//: Send destroy
	void send_destroy(const CL_NetComputer *dest = NULL)
	{
		CL_OutputSource_Memory output;
		write_destroy(output);
		netobject.send(/*dest,*/ msgtype_destroy, output.get_data());
	}

	//: Recv full
	void recv_full(CL_InputSource &message)
	{
		int type = message.read_int32();
		cl_assert(type == object_type);
		read_full_update(message);
	}

	//: Recv tick
	void recv_tick(CL_InputSource &message)
	{
		read_tick_update(message);
	}

	//: Recv destroy
	void recv_destroy(CL_InputSource &message)
	{
		read_destroy(message);
	}

//! Overrideables:
public:
	//: Update
	virtual void update(float time_elapsed)
	{
		if (tick_rate > 0.0f)
		{
			tick_time += time_elapsed;

			while (tick_time >= tick_rate)
			{
				tick_time -= tick_rate;
				if (netobject.is_server()) send_tick();
			}
		}
	}

	//: Read full update
	virtual void read_full_update(CL_InputSource &message) { return; }

	//: Read tick update
	virtual void read_tick_update(CL_InputSource &message) { return; }

	//: Read destroy
	virtual void read_destroy(CL_InputSource &message) { set_destroy_flag(); }

	//: Write full update
	virtual void write_full_update(CL_OutputSource &message) { return; }

	//: Write tick update
	virtual void write_tick_update(CL_OutputSource &message) { return; }

	//: Write destroy
	virtual void write_destroy(CL_InputSource &message) { return; }

//! Implementation:
public:
	//: Common init
	void common_init()
	{
		destroy_flag = false;
		tick_rate = 0.0f; // disable tick updates per default.
		tick_time = 0.0f;

		slot_full = netobject.connect(
			msgtype_full_update,
			this, &CL_GameObject<World>::recv_full);

		slot_tick = netobject.connect(
			msgtype_tick_update,
			this, &CL_GameObject<World>::recv_tick);

		slot_destroy = netobject.connect(
			msgtype_destroy,
			this, &CL_GameObject<World>::recv_destroy);
	}

	//: The world
	World *world;

	//: Object Type
	int object_type;

	//: Destroy_flag
	bool destroy_flag;

	//: Netobject
	CL_NetObject netobject;

	//: Tick_rate
	float tick_rate;

	//: Tick_time
	float tick_time;

	//: Slot_full
	CL_Slot slot_full;

	//: Slot_tick
	CL_Slot slot_tick;

	//: Slot_destroy
	CL_Slot slot_destroy;
};

template<class GameObject>
//: World template.
class CL_World
{
//! Construction:
public:
	//: World Constructor
	CL_World(CL_NetSession *session, int object_netchannel)
	:
		session(session),
		netobject_channel(session, object_netchannel)
	{
		slot_create_object = netobject_channel.sig_create_object().connect(
			this, &CL_World<GameObject>::sig_create_object);
	}

	//: World Destructor
	virtual ~CL_World()
	{
	}

//! Attributes:
public:
	//: Get netobject channel
	CL_NetObjectChannel &get_netobject_channel() { return netobject_channel; }

	//: Get session
	CL_NetSession *get_session() { return session; }

	//: Get objects
	std::list<GameObject *> &get_objects() { return objects; }

//! Operations:
public:
	//: Add object
	void add_object(GameObject *object) { objects.push_back(object); }

	//: Remove object
	void remove_object(GameObject *object) { objects.remove(object); }

	//: Update
	void update(float delta_time)
	{
		std::list<GameObject *>::iterator it;
		std::list<GameObject *> remove_list;
		for (it = objects.begin(); it != objects.end(); it++)
		{
			(*it)->update(delta_time);
			if ((*it)->get_destroy_flag()) remove_list.push_back(*it);
		}
			
		for (it = remove_list.begin(); it != remove_list.end(); it++)
		{
			delete *it;
		}
	}

//! Overrideables:
protected:
	virtual void on_create_object(
		const CL_NetObject &netobj,
		int msgType,
		const std::string &message) { return; }

//! Implementation:
private:
	void sig_create_object(
		const CL_NetObject &netobj,
		int msgType,
		const std::string &message)
	{
		// this function is needed because I'm not sure how to connect a signal to a virtual
		// table function.
		on_create_object(netobj, msgType, message);
	}

	std::list<GameObject *> objects;
	CL_NetSession *session;
	CL_NetObjectChannel netobject_channel;
	CL_Slot slot_create_object;
};

#endif