forked from lockie/HiveGame
/
behaviour.cpp
239 lines (206 loc) · 5.99 KB
/
behaviour.cpp
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
#include "config.h"
#include <crystalspace.h>
#include <physicallayer/entity.h>
#include <physicallayer/propclas.h>
#include <propclass/linmove.h>
#include <propclass/prop.h>
#include "behaviour.h"
SCF_IMPLEMENT_IBASE (BehaviourLayer)
SCF_IMPLEMENTS_INTERFACE (iCelBlLayer)
SCF_IMPLEMENT_IBASE_END
BehaviourLayer::BehaviourLayer(iCelPlLayer* pl)
{
SCF_CONSTRUCT_IBASE(NULL);
BehaviourLayer::pl = pl;
}
BehaviourLayer::~BehaviourLayer()
{
SCF_DESTRUCT_IBASE ();
}
const char* BehaviourLayer::GetName() const
{
return "behaviourlayer";
}
iCelBehaviour* BehaviourLayer::CreateBehaviour (iCelEntity* entity,
const char* name)
{
iCelBehaviour* behave = NULL;
if(!strcmp(name, "level_behave"))
behave = new LevelBehaviour(entity, this, pl);
else if(!strcmp(name, "player_behave"))
behave = new PlayerBehaviour(entity, this, pl);
if(behave)
{
entity->SetBehaviour(behave);
behave->DecRef ();
}
return behave;
}
SCF_IMPLEMENT_IBASE (CommonBehaviour)
SCF_IMPLEMENTS_INTERFACE (iCelBehaviour)
SCF_IMPLEMENT_IBASE_END
CommonBehaviour::CommonBehaviour(iCelEntity* _entity, BehaviourLayer* _bl,
iCelPlLayer* _pl)
{
SCF_CONSTRUCT_IBASE(NULL);
entity = _entity;
bl = _bl;
pl = _pl;
}
CommonBehaviour::~CommonBehaviour()
{
SCF_DESTRUCT_IBASE();
}
iCelBlLayer* CommonBehaviour::GetBehaviourLayer() const
{
return bl;
}
void* CommonBehaviour::GetInternalObject()
{
return 0;
}
bool CommonBehaviour::SendMessage(const char* msg_id, iCelPropertyClass* pc,
celData& ret, iCelParameterBlock* params, ...)
{
va_list arg;
va_start (arg, params);
bool rc = SendMessageV(msg_id, pc, ret, params, arg);
va_end (arg);
return rc;
}
bool CommonBehaviour::SendMessageV (const char* msg_id, iCelPropertyClass* pc,
celData& ret, iCelParameterBlock* params, va_list arg)
{
csStringID id = pl->FetchStringID (msg_id);
return SendMessage (id, pc, ret, params, arg);
}
bool CommonBehaviour::SendMessage (csStringID, iCelPropertyClass*,
celData&, iCelParameterBlock*, va_list)
{
return false;
}
bool LevelBehaviour::SendMessage (csStringID msg_id, iCelPropertyClass* pc,
celData& ret, iCelParameterBlock* params, va_list arg)
{
return CommonBehaviour::SendMessage (msg_id, pc, ret, params, arg);
}
const char* LevelBehaviour::GetName () const
{
return "level_behave";
}
PlayerBehaviour::PlayerBehaviour(iCelEntity* entity, BehaviourLayer* bl,
iCelPlLayer* pl) : CommonBehaviour(entity, bl, pl)
{
id_pccommandinput_forward1 = pl->FetchStringID("pccommandinput_forward1");
id_pccommandinput_forward0 = pl->FetchStringID("pccommandinput_forward0");
id_pccommandinput_backward1 = pl->FetchStringID("pccommandinput_backward1");
id_pccommandinput_backward0 = pl->FetchStringID("pccommandinput_backward0");
id_pccommandinput_rotateleft1 = pl->FetchStringID("pccommandinput_rotateleft1");
id_pccommandinput_rotateleft0 = pl->FetchStringID("pccommandinput_rotateleft0");
id_pccommandinput_rotateright1 = pl->FetchStringID("pccommandinput_rotateright1");
id_pccommandinput_rotateright0 = pl->FetchStringID("pccommandinput_rotateright0");
id_pccommandinput_cammode1 = pl->FetchStringID("pccommandinput_cammode1");
id_pccommandinput_drop1 = pl->FetchStringID("pccommandinput_drop1");
id_pcinventory_addchild = pl->FetchStringID("pcinventory_addchild");
id_pcinventory_removechild = pl->FetchStringID("pcinventory_removechild");
}
const char* PlayerBehaviour::GetName() const
{
return "player_behave";
}
void PlayerBehaviour::GetActorMove()
{
if(!pcactormove)
{
pcactormove = CEL_QUERY_PROPCLASS_ENT(entity, iPcActorMove);
}
}
void PlayerBehaviour::GetInventory()
{
if(!pcinventory)
{
pcinventory = CEL_QUERY_PROPCLASS_ENT(entity, iPcInventory);
}
}
void PlayerBehaviour::GetMesh()
{
if(!pcmesh)
{
pcmesh = CEL_QUERY_PROPCLASS_ENT(entity, iPcMesh);
}
}
void PlayerBehaviour::ShowInventory()
{
size_t count = pcinventory->GetEntityCount();
size_t i;
for(i = 0 ; i < count ; i++)
{
iCelEntity* child = pcinventory->GetEntity(i);
printf (" child %d is '%s'\n", i, child->GetName());
}
}
void PlayerBehaviour::Drop()
{
GetInventory();
size_t count = pcinventory->GetEntityCount();
if(count <= 0)
{
printf("Inventory is empty!\n");
return;
}
iCelEntity* child = pcinventory->GetEntity(0);
pcinventory->RemoveEntity(child);
csRef<iPcLinearMovement> pclinmove = CEL_QUERY_PROPCLASS_ENT(
child, iPcLinearMovement);
if(pclinmove)
{
GetMesh ();
csVector3 pos = pcmesh->GetMesh()->GetMovable()->GetTransform()
.This2Other(csVector3 (0, 2, -2));
iSector* sector = pcmesh->GetMesh()->GetMovable()->GetSectors()->Get(0);
pclinmove->SetPosition(pos, 0, sector);
pclinmove->SetVelocity(csVector3 (0, .1f, 0));
csRef<iPcMesh> pcmesh_child = CEL_QUERY_PROPCLASS_ENT(child, iPcMesh);
if(pcmesh_child) pcmesh_child->Show();
}
}
bool PlayerBehaviour::SendMessage (csStringID msg_id, iCelPropertyClass* pc,
celData& ret, iCelParameterBlock* params, va_list arg)
{
GetActorMove ();
if(msg_id == id_pccommandinput_forward1)
pcactormove->Forward(true);
else if(msg_id == id_pccommandinput_forward0)
pcactormove->Forward(false);
else if(msg_id == id_pccommandinput_backward1)
pcactormove->Backward(true);
else if(msg_id == id_pccommandinput_backward0)
pcactormove->Backward(false);
else if(msg_id == id_pccommandinput_rotateleft1)
pcactormove->RotateLeft(true);
else if(msg_id == id_pccommandinput_rotateleft0)
pcactormove->RotateLeft(false);
else if(msg_id == id_pccommandinput_rotateright1)
pcactormove->RotateRight(true);
else if(msg_id == id_pccommandinput_rotateright0)
pcactormove->RotateRight(false);
else if(msg_id == id_pccommandinput_cammode1)
pcactormove->ToggleCameraMode ();
else if(msg_id == id_pccommandinput_drop1)
Drop();
else if(msg_id == id_pcinventory_addchild)
{
GetInventory();
printf("Got a new object! Objects in inventory:\n");
ShowInventory();
}
else if(msg_id == id_pcinventory_removechild)
{
GetInventory();
printf("Object removed from inventory! Objects in inventory:\n");
ShowInventory();
}
else
return CommonBehaviour::SendMessage(msg_id, pc, ret, params, arg);
return true;
}