File: CharacterBody.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (415 lines) | stat: -rw-r--r-- 10,612 bytes parent folder | download | duplicates (2)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Copyright (C) 2006-2010 - Frictional Games
 *
 * This file is part of HPL1 Engine.
 */

#ifndef HPL_CHARACTER_BODY_H
#define HPL_CHARACTER_BODY_H

#include "hpl1/engine/math/MathTypes.h"
#include "hpl1/engine/physics/PhysicsWorld.h"

namespace hpl {

class iPhysicsWorld;
class iCollideShape;
class iPhysicsBody;
class cCamera3D;
class iCharacterBody;
class iEntity3D;

enum eCharDir {
	eCharDir_Forward = 0,
	eCharDir_Right = 1,
	eCharDir_LastEnum = 2
};

class iCharacterBodyCallback {
public:
	virtual ~iCharacterBodyCallback() = default;
	virtual void OnHitGround(iCharacterBody *apCharBody, const cVector3f &avVel) = 0;
	virtual void OnGravityCollide(iCharacterBody *apCharBody, iPhysicsBody *apCollideBody,
								  cCollideData *apCollideData) = 0;
};

//------------------------------------------------

class cCharacterBodyRay : public iPhysicsRayCallback {
public:
	cCharacterBodyRay();

	void Clear();
	bool OnIntersect(iPhysicsBody *pBody, cPhysicsRayParams *apParams);

	float mfMinDist;
	bool mbCollide;
};

//------------------------------------------------

class cCharacterBodyCollideGravity : public iPhysicsWorldCollisionCallback {
public:
	cCharacterBodyCollideGravity();

	void OnCollision(iPhysicsBody *apBody, cCollideData *apCollideData);

	iCharacterBody *mpCharBody;
};

//------------------------------------------------

class cCharacterBodyCollidePush : public iPhysicsWorldCollisionCallback {
public:
	cCharacterBodyCollidePush();

	void OnCollision(iPhysicsBody *apBody, cCollideData *apCollideData);

	iCharacterBody *mpCharBody;
};

//-----------------------------------

kSaveData_BaseClass(iCharacterBody) {
	kSaveData_ClassInit(iCharacterBody) public : tString msName;

	float mfMass;
	bool mbGravityActive;
	float mfMaxGravitySpeed;

	bool mbActive;

	bool mbCollideCharacter;

	cVector3f mvPosition;
	cVector3f mvLastPosition;
	float mfMaxPosMoveSpeed[2];
	float mfMaxNegMoveSpeed[2];
	float mfMoveSpeed[2];
	float mfMoveAcc[2];
	float mfMoveDeacc[2];
	bool mbMoving[2];

	float mfPitch;
	float mfYaw;

	bool mbOnGround;

	float mfMaxPushMass;
	float mfPushForce;
	bool mbPushIn2D;

	cVector3f mvForce;
	cVector3f mvVelolcity;

	cVector3f mvSize;

	cMatrixf m_mtxMove;

	int mlEntityId;
	cMatrixf m_mtxEntityOffset;
	int mlEntitySmoothPosNum;

	float mfMaxStepHeight;
	float mfStepClimbSpeed;
	float mfClimbForwardMul;
	float mfClimbHeightAdd;
	bool mbClimbing;

	float mfGroundFriction;
	float mfAirFriction;

	int mlBodyId;

	cContainerList<int> mvExtraBodyIds;

	virtual iSaveObject *CreateSaveObject(cSaveObjectHandler * apSaveObjectHandler, cGame * apGame);
	virtual int GetSaveCreatePrio();
};

//------------------------------------------------

class iCharacterBody : public iSaveObject {
	typedef iSaveObject super;
	friend class cSaveData_iCharacterBody;
	friend class cCharacterBodyCollideGravity;

public:
	iCharacterBody(const tString &asName, iPhysicsWorld *apWorld, const cVector3f avSize);
	virtual ~iCharacterBody();

	const tString &GetName() { return msName; }

	///////////////////////////////////////
	// Properties

	float GetMass();
	void SetMass(float afMass);

	void SetActive(bool abX);
	bool IsActive() { return mbActive; }

	cVector3f GetSize();

	void SetCollideCharacter(bool abX);
	bool GetCollideCharacter() { return mbCollideCharacter; }

	void SetTestCollision(bool abX) { mbTestCollision = abX; }
	bool GetTestCollision() { return mbTestCollision; }

	void SetMaxPositiveMoveSpeed(eCharDir aDir, float afX);
	float GetMaxPositiveMoveSpeed(eCharDir aDir);
	void SetMaxNegativeMoveSpeed(eCharDir aDir, float afX);
	float GetMaxNegativeMoveSpeed(eCharDir aDir);

	void SetMoveSpeed(eCharDir aDir, float afX);
	float GetMoveSpeed(eCharDir aDir);
	void SetMoveAcc(eCharDir aDir, float afX);
	float GetMoveAcc(eCharDir aDir);
	void SetMoveDeacc(eCharDir aDir, float afX);
	float GetMoveDeacc(eCharDir aDir);

	cVector3f GetVelocity(float afFrameTime);

	void SetPosition(const cVector3f &avPos, bool abSmooth = false);
	const cVector3f &GetPosition();
	const cVector3f &GetLastPosition();
	void SetFeetPosition(const cVector3f &avPos, bool abSmooth = false);
	cVector3f GetFeetPosition();

	void SetYaw(float afX);
	void AddYaw(float afX);
	float GetYaw();
	void SetPitch(float afX);
	void AddPitch(float afX);
	float GetPitch();

	cVector3f GetForward();
	cVector3f GetRight();
	cVector3f GetUp();

	cMatrixf &GetMoveMatrix();

	void SetGravityActive(bool abX);
	bool GravityIsActive();
	void SetMaxGravitySpeed(float afX);
	float GetMaxGravitySpeed();

	bool GetCustomGravityActive();
	void SetCustomGravityActive(bool abX);
	void SetCustomGravity(const cVector3f &avCustomGravity);
	cVector3f GetCustomGravity();

	void SetMaxPushMass(float afX) { mfMaxPushMass = afX; }
	void SetPushForce(float afX) { mfPushForce = afX; }
	float GetMaxPushMass() { return mfMaxPushMass; }
	float GetPushForce() { return mfPushForce; }

	bool GetPushIn2D() { return mbPushIn2D; }
	void SetPushIn2D(bool abX) { mbPushIn2D = abX; }

	void AddForceVelocity(const cVector3f &avVel) { mvVelolcity += avVel; }
	void SetForceVelocity(const cVector3f &avVel) { mvVelolcity = avVel; }
	cVector3f GetForceVelocity() { return mvVelolcity; }

	int AddExtraSize(const cVector3f &avSize);
	void SetActiveSize(int alNum);

	///////////////////////////////////////
	// Actions
	void SetForce(const cVector3f &avForce);
	void AddForce(const cVector3f &avForce);
	cVector3f GetForce();

	void Move(eCharDir aDir, float afMul, float afTimeStep);

	void Update(float afTimeStep);

	///////////////////////////////////////
	// Other
	void SetCamera(cCamera3D *apCam);
	cCamera3D *GetCamera();
	void SetCameraPosAdd(const cVector3f &avAdd);
	cVector3f GetCameraPosAdd();
	void SetCameraSmoothPosNum(int alNum) { mlCameraSmoothPosNum = alNum; }
	int GetCameraSmoothPosNum() { return mlCameraSmoothPosNum; }

	void SetEntity(iEntity3D *apEntity);
	iEntity3D *GetEntity();

	void SetEntityOffset(const cMatrixf &a_mtxOffset);
	const cMatrixf &GetEntityOffset();

	void SetEntityPostOffset(const cMatrixf &a_mtxOffset);
	const cMatrixf &GetEntityPostOffset();

	void SetEntitySmoothPosNum(int alNum) { mlEntitySmoothPosNum = alNum; }
	int GetEntitySmoothPosNum() { return mlEntitySmoothPosNum; }

	void SetUserData(void *apUserData) { mpUserData = apUserData; }
	void *GetUserData() { return mpUserData; }

	void SetCallback(iCharacterBodyCallback *apCallback) { mpCallback = apCallback; }

	void SetEnableNearbyBodies(bool abX) { mbEnableNearbyBodies = abX; }
	bool GetEnableNearbyBodies() { return mbEnableNearbyBodies; }

	iPhysicsBody *GetBody() { return mpBody; }
	iCollideShape *GetShape();

	iPhysicsBody *GetExtraBody(size_t alIdx) { return mvExtraBodies[alIdx]; }

	bool IsOnGround();

	float GetMaxStepSize() { return mfMaxStepHeight; }
	void SetMaxStepSize(float afSize) { mfMaxStepHeight = afSize; }

	void SetStepClimbSpeed(float afX) { mfStepClimbSpeed = afX; }
	float GetStepClimbSpeed() { return mfStepClimbSpeed; }

	void SetAccurateClimbing(bool abX) { mbAccurateClimbing = abX; }
	bool GetAccurateClimbing() { return mbAccurateClimbing; }

	void SetClimbForwardMul(float afX) { mfClimbForwardMul = afX; }
	float GetClimbForwardMul() { return mfClimbForwardMul; }

	void SetClimbHeightAdd(float afX) { mfClimbHeightAdd = afX; }
	float GetClimbHeightAdd() { return mfClimbHeightAdd; }

	void SetGroundFriction(float afX) { mfGroundFriction = afX; }
	float GetGroundFriction() { return mfGroundFriction; }

	void SetAirFriction(float afX) { mfAirFriction = afX; }
	float GetAirFriction() { return mfAirFriction; }

	bool IsClimbing() { return mbClimbing; }

	void SetAttachedBody(iPhysicsBody *apBody);
	iPhysicsBody *GetAttachedBody() { return mpAttachedBody; }

	// O=nly sue when you know what you are doing, Update calls these
	void UpdateMoveMarix();

	void UpdateCamera();
	void UpdateEntity();

	void UpdateAttachment();

	// SaveObject implementation
	virtual iSaveData *CreateSaveData();
	virtual void SaveToSaveData(iSaveData *apSaveData);
	virtual void LoadFromSaveData(iSaveData *apSaveData);
	virtual void SaveDataSetup(cSaveObjectHandler *apSaveObjectHandler, cGame *apGame);

protected:
	tString msName;

	float mfMass;

	bool mbActive;

	bool mbCollideCharacter;

	bool mbTestCollision;

	bool mbGravityActive;
	float mfMaxGravitySpeed;
	bool mbCustomGravity;
	cVector3f mvCustomGravity;

	cVector3f mvPosition;
	cVector3f mvLastPosition;

	float mfMaxPosMoveSpeed[2];
	float mfMaxNegMoveSpeed[2];

	float mfMoveSpeed[2];
	float mfMoveAcc[2];
	float mfMoveDeacc[2];
	bool mbMoving[2];

	float mfPitch;
	float mfYaw;

	bool mbOnGround;

	float mfMaxPushMass;
	float mfPushForce;
	bool mbPushIn2D;

	float mfCheckStepClimbCount;
	float mfCheckStepClimbInterval;

	cVector3f mvForce;
	cVector3f mvVelolcity;

	cVector3f mvSize;

	cMatrixf m_mtxMove;

	cCamera3D *mpCamera;
	cVector3f mvCameraPosAdd;
	int mlCameraSmoothPosNum;
	tVector3fList mlstCameraPos;

	iEntity3D *mpEntity;
	cMatrixf m_mtxEntityOffset;
	cMatrixf m_mtxEntityPostOffset;
	int mlEntitySmoothPosNum;
	tVector3fList mlstEntityPos;

	float mfGroundFriction;
	float mfAirFriction;

	void *mpUserData;

	iPhysicsBody *mpAttachedBody;
	cMatrixf m_mtxAttachedPrevMatrix;
	bool mbAttachmentJustAdded;

	iCharacterBodyCallback *mpCallback;

	cCharacterBodyRay *mpRayCallback;

	cCharacterBodyCollideGravity *mpCollideCallbackGravity;
	cCharacterBodyCollidePush *mpCollideCallbackPush;

	float mfMaxStepHeight;
	float mfStepClimbSpeed;
	float mfClimbForwardMul;
	float mfClimbHeightAdd;
	bool mbClimbing;
	bool mbAccurateClimbing;

	bool mbEnableNearbyBodies;

	iPhysicsBody *mpBody;
	iPhysicsWorld *mpWorld;

	Common::Array<iPhysicsBody *> mvExtraBodies;
};

} // namespace hpl

#endif // HPL_CHARACTER_BODY_H