File: client_world.cpp

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 (134 lines) | stat: -rw-r--r-- 3,491 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
#include <ClanLib/core.h>
#include <ClanLib/display.h>

#include "client_world.h"
#include "client_gameobject.h"
#include "client_fighter.h"
#include "client_wormhole.h"
#include "server_world.h"
#include "fighter.h"
#include "wormhole.h"

/////////////////////////////////////////////////////////////////////////////
// ClientWorld construction:

ClientWorld::ClientWorld(CL_NetSession *session, ServerWorld *server)
:
	World(session),
	server(server),
	max_tick_elapsed(0.05f)
{
	// TODO: use the gui-resource-manager
	resources = new CL_ResourceManager("resources.scr", false);
	background = new CL_Surface("Backgrounds/nebula1", resources);
}

ClientWorld::~ClientWorld()
{
	delete resources;
}

/////////////////////////////////////////////////////////////////////////////
// ClientWorld attributes:

CL_ResourceManager *ClientWorld::get_resources()
{
	return resources;
}

/////////////////////////////////////////////////////////////////////////////
// ClientWorld operations:

void ClientWorld::run()
{
	float last_time = CL_System::get_time();

	while (CL_Keyboard::get_keycode(CL_KEY_ESCAPE) == false)
	{
		// Update world:
		float delta_time = (CL_System::get_time() - last_time) / 1000.0f;
		last_time = CL_System::get_time();

		for (; delta_time > 0; delta_time -= max_tick_elapsed)
		{
			float elapsed = (delta_time > max_tick_elapsed) ? max_tick_elapsed : delta_time;

			if (server)
				server->update(elapsed);

			players.update(get_session());

			World::update(elapsed);

			std::list<ClientGameObject *>::iterator it;
			std::list<ClientGameObject *> remove_list;
			for (it = client_gameobjects.begin(); it != client_gameobjects.end(); it++)
			{
				(*it)->update(elapsed);
//				if ((*it)->get_destroy_flag()) remove_list.push_back(*it);
			}
		
			for (it = remove_list.begin(); it != remove_list.end(); it++)
			{
				delete *it;
			}
		}

		// Show world:
		background->put_screen(0,0);
		std::list<ClientGameObject *>::iterator it;
		for (it = client_gameobjects.begin(); it != client_gameobjects.end(); it++)
		{
			ClientGameObject *cur = *it;
			cur->show();
		}

		CL_Display::flip_display();

		CL_System::keep_alive();
	}
}

/////////////////////////////////////////////////////////////////////////////
// ClientWorld implementation:

void ClientWorld::on_create_object(
	const CL_NetObject &netobj,
	int msgType,
	const std::string &message)
{
	// can only create object if the update is a full update.
	if (msgType != GameObject::msgtype_full_update) return;

	// Solve chicken'n'egg problem. We want to be sure that player descriptions have
	// arrived before constructing objects for the same players. I wish this could be
	// done a little bit more nicely, but this will do the trick. -- mbn
	players.update(get_session());

	// read header of a full update message:
	CL_InputSource_Memory input(message);
	int object_type = input.read_int32();

	// and use that information to find out what object to create:
	GameObject *obj = NULL;
	ClientGameObject *client_obj = NULL;

	switch (object_type)
	{
	case GameObject::FIGHTER:
		client_obj = new ClientFighter(this, new Fighter(this, netobj));
		break;
	default:
		cl_assert(false); // unknown object.
	};

	if (obj == NULL && client_obj != NULL) obj = client_obj->get_gameobject();

	// ask the object to serialize itself:
	CL_InputSource_Memory dummy(message);
	obj->recv_full(dummy);
 
	// add it to the world:
	add_object(obj);
	if (client_obj != NULL) client_gameobjects.push_back(client_obj);
}