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
|
/*
game/game.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include <vector>
#include "game/game.h"
#include "game/sector.h"
#include "game/ship.h"
#include "game/star.h"
#include "core/entity.h"
#include "sys/sys.h"
#include "math/mathlib.h"
#include "filesystem/filesystem.h"
namespace game
{
/// a player joins the game
void func_join(core::Player &player, std::stringstream &args)
{
if (player.control)
return;
Ship *ship = new Ship();
ship->location = math::Vector3f(0,0,0);
ship->label = "ship: <" + player.name + "> Micron Vector";
ship->owner = &player;
player.control = ship;
// net message
std::ostringstream osstream;
osstream << "msg info " << player.name << " joins the game\n";
core::net_broadcast(osstream, player.id);
// console message
con_print << player.name << " joins the game" << std::endl;
}
/// a player joins the spectators
void func_spectate(core::Player &player, std::stringstream &args)
{
if (!player.control)
return;
// net message
std::ostringstream osstream;
osstream << "msg info " << player.name << " spectates\n";
core::net_broadcast(osstream, player.id);
// console message
con_print << player.name << " spectates" << std::endl;
if (player.control) {
// player has only ship for now
core::entity::remove(player.control->id);
player.control = 0;
}
}
Game::Game() : core::GameInterface("Project::OSiRiON")
{
}
Game::~Game()
{
}
bool Game::init()
{
using math::Vector3f;
using math::Color;
//using filesystem::IniFile;
con_print << "Initializing game..." << std::endl;
/*
// read game.ini
IniFile f;
f.open("ini/game.ini");
while (f) {
f.getline();
if (f.got_key()) {
if (f.section() == "game") {
// game::name
if (f.got_key_string("name", name)); else
// game::label
if (f.got_key_string("label", label)); else
// game::author
if (f.got_key_string("author", author)); else
// unknown value
con_warn << f.name() << " unknown key '" << f.key() << "' at line " << f.line() << std::endl;
}
} else if (f.got_section("game")) {
} else if (f.got_section()) {
con_warn << f.name() << " unknown section '" << f.section() << "' at line " << f.line() << std::endl;
}
}
f.close();
con_print << name << std::endl;
con_print << "by " << author << std::endl;
// read world.ini
std::string tmp;
Sector *sector =0;
f.open("ini/world.ini");
while (f) {
f.getline();
if (f.got_key()) {
if (f.section() == "world") {
// world::name
if (f.got_key_string("name", tmp)); else
// world:label
if (f.got_key_string("label", tmp)); else
// unknown value
con_warn << f.name() << " unknown key '" << f.key() << "' at line " << f.line() << std::endl;
} else if (f.section() == "sector") {
// sector::name
if (f.got_key_string("name", tmp)) {
sector->name = tmp;
} else
// sector:label
if (f.got_key_string("label", tmp)) {
sector->label = tmp;
} else
// unknown value
con_warn << f.name() << " unknown key '" << f.key() << "' at line " << f.line() << std::endl;
}
} else if (f.got_section("world")) {
//con_debug << "[world] section" << std::endl;
} else if (f.got_section("sector")) {
sector = new Sector();
sectors.push_back(sector);
} else if (f.got_section()) {
con_warn << f.name() << " unknown section '" << f.section() << "' at line " << f.line() << std::endl;
}
}
f.close();
con_print << "Loading sectors..." << std::endl;
for (unsigned n =0; n < sectors.size(); n++)
con_print << " " << sectors[n]->label << " " << sectors[n]->name << std::endl;
*/
// set up some stuff in the game world
Star *star = new Star();
star->location = Vector3f(256.0f, 0.0f, 256.0f);
star->label = "star: Sabishi Hoshi";
core::Entity *cube = new core::Entity(core::entity::Solid & core::entity::Static);
cube->core_shape = core::entity::Cube;
cube->core_color = Color(0.0f, 0.8f, 0.0f);
cube->location = Vector3f(24.0f, 0.0f, -24.0f);
cube->label ="cube: Borg cube green";
cube->type = cube_enttype;
cube = new core::Entity(core::entity::Solid & core::entity::Static);
cube->core_shape = core::entity::Cube;
cube->core_color = Color(1.0f, 0.0f, 0.0f);
cube->location = Vector3f(16.0f, 0.0f, -16.0f);
cube->label ="cube: Borg cube red";
cube->type = cube_enttype;
core::Entity *sphere = new core::Entity(core::entity::Solid & core::entity::Static);
sphere->core_shape = core::entity::Sphere;
sphere->core_color = Color(0.8f, 0.8f, 0.0f);
sphere->location = Vector3f(0.0f, 0.0f, -32.0f);
sphere->label ="sphere: The Sphere";
core::Entity *axis = new core::Entity(core::entity::Static);
axis->core_shape = core::entity::Diamond;
axis->core_color = Color(1.0f, 1.0f, 0.0f);
axis->location = Vector3f(0, 0, 0);
axis->label = "axis: Origin";
// add game functions
core::func::add("join", func_join, core::func::Game);
core::func::add("spectate", func_spectate, core::func::Game);
return true;
}
void Game::shutdown()
{
con_print << "Shutting down game..." << std::endl;
core::func::remove("join");
core::func::remove("spectate");
// delete every sector object in the sectors vector
for (unsigned int n =0; n< sectors.size(); n++) {
delete sectors[n];
sectors[n] = 0;
}
// clear the sectors vector
sectors.clear();
}
void Game::frame(float seconds)
{
}
void Game::player_connect(core::Player &player)
{
std::stringstream args;
game::func_spectate(player, args);
}
void Game::player_disconnect(core::Player &player)
{
if (player.control) {
// player has only one ship for now
core::entity::remove(player.control->id);
player.control = 0;
}
}
} // namespace game
|