Skip to content

Commit 1aab12a

Browse files
committed
-
1 parent 21ddbb6 commit 1aab12a

35 files changed

Lines changed: 895 additions & 413 deletions

src/Carnage3D.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
<ClInclude Include="Box2D_Helpers.h" />
158158
<ClInclude Include="common_utils.h" />
159159
<ClInclude Include="Convert.h" />
160+
<ClInclude Include="DamageInfo.h" />
160161
<ClInclude Include="Decoration.h" />
161162
<ClInclude Include="Explosion.h" />
162163
<ClInclude Include="game_version.h" />
@@ -247,11 +248,13 @@
247248
<ClInclude Include="PixelsArray.h" />
248249
<ClInclude Include="TrimeshBuffer.h" />
249250
<ClInclude Include="Vehicle.h" />
251+
<ClInclude Include="VertexFormats.h" />
250252
<ClInclude Include="WeaponInfo.h" />
251253
</ItemGroup>
252254
<ItemGroup>
253255
<ClCompile Include="AiCharacterController.cpp" />
254256
<ClCompile Include="CommonTypes.cpp" />
257+
<ClCompile Include="DamageInfo.cpp" />
255258
<ClCompile Include="Decoration.cpp" />
256259
<ClCompile Include="enums_impl.cpp" />
257260
<ClCompile Include="Explosion.cpp" />

src/Carnage3D.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@
350350
<ClInclude Include="WeaponInfo.h">
351351
<Filter>Game</Filter>
352352
</ClInclude>
353+
<ClInclude Include="VertexFormats.h">
354+
<Filter>Game\Rendering</Filter>
355+
</ClInclude>
356+
<ClInclude Include="DamageInfo.h">
357+
<Filter>Game</Filter>
358+
</ClInclude>
353359
</ItemGroup>
354360
<ItemGroup>
355361
<ClCompile Include="stdafx.cpp">
@@ -552,6 +558,9 @@
552558
<ClCompile Include="WeaponInfo.cpp">
553559
<Filter>Game</Filter>
554560
</ClCompile>
561+
<ClCompile Include="DamageInfo.cpp">
562+
<Filter>Game</Filter>
563+
</ClCompile>
555564
</ItemGroup>
556565
<ItemGroup>
557566
<None Include="..\gamedata\config\sys_config.json.default">

src/DamageInfo.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include "stdafx.h"
2+
#include "DamageInfo.h"
3+
4+
void DamageInfo::SetDamageFromFall(float fallHeight)
5+
{
6+
mDamageCause = eDamageCause_Gravity;
7+
mFallHeight = fallHeight;
8+
// no hitpoints damage
9+
}
10+
11+
void DamageInfo::SetDamageFromElectricity()
12+
{
13+
mDamageCause = eDamageCause_Electricity;
14+
// no hitpoints damage
15+
}
16+
17+
void DamageInfo::SetDamageFromFire(int hitpoints, GameObject* object)
18+
{
19+
mDamageCause = eDamageCause_Burning;
20+
mHitPoints = hitpoints;
21+
mSourceObject = object;
22+
}
23+
24+
void DamageInfo::SetDamageFromWeapon(const WeaponInfo& weaponInfo, GameObject* object)
25+
{
26+
int hitpoints = weaponInfo.mBaseDamage;
27+
28+
if (weaponInfo.IsBulletDamage())
29+
{
30+
SetDamageFromBullet(hitpoints, object);
31+
return;
32+
}
33+
34+
if (weaponInfo.IsFireDamage())
35+
{
36+
SetDamageFromFire(hitpoints, object);
37+
return;
38+
}
39+
40+
if (weaponInfo.IsMelee())
41+
{
42+
SetDamageFromPunch(hitpoints, object);
43+
return;
44+
}
45+
46+
if (weaponInfo.IsExplosionDamage())
47+
{
48+
SetDamageFromExplosion(hitpoints, object);
49+
return;
50+
}
51+
52+
debug_assert(false);
53+
// unknown damage type
54+
}
55+
56+
void DamageInfo::SetDamageFromWater(int hitpoints)
57+
{
58+
mDamageCause = eDamageCause_Drowning;
59+
mHitPoints = hitpoints;
60+
}
61+
62+
void DamageInfo::SetDamageFromCarCrash(const glm::vec3& contactPoint, float contactImpulse, GameObject* object)
63+
{
64+
mDamageCause = eDamageCause_CarCrash;
65+
mContactPoint = contactPoint;
66+
mContactImpulse = contactImpulse;
67+
mSourceObject = object;
68+
}
69+
70+
void DamageInfo::SetDamageFromExplosion(int hitpoints, GameObject* object)
71+
{
72+
mDamageCause = eDamageCause_Explosion;
73+
mHitPoints = hitpoints;
74+
mSourceObject = object;
75+
}
76+
77+
void DamageInfo::SetDamageFromBullet(int hitpoints, GameObject* object)
78+
{
79+
mDamageCause = eDamageCause_Bullet;
80+
mHitPoints = hitpoints;
81+
mSourceObject = object;
82+
}
83+
84+
void DamageInfo::SetDamageFromPunch(int hitpoints, GameObject* object)
85+
{
86+
mDamageCause = eDamageCause_Punch;
87+
mHitPoints = hitpoints;
88+
mSourceObject = object;
89+
}
90+
91+
void DamageInfo::Clear()
92+
{
93+
mDamageCause = eDamageCause_Punch;
94+
95+
mContactPoint.x = 0.0f;
96+
mContactPoint.y = 0.0f;
97+
mContactPoint.z = 0.0f;
98+
mNormal.x = 0.0f;
99+
mNormal.y = 0.0f;
100+
101+
mSourceObject = nullptr;
102+
mHitPoints = 0;
103+
mContactImpulse = 0.0f;
104+
mFallHeight = 0.0f;
105+
}

src/DamageInfo.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
3+
#include "GameDefs.h"
4+
#include "WeaponInfo.h"
5+
6+
// Game object damage type
7+
enum eDamageCause
8+
{
9+
eDamageCause_Gravity, // fall from height
10+
eDamageCause_Electricity, // rails
11+
eDamageCause_Burning,
12+
eDamageCause_Drowning,
13+
eDamageCause_CarCrash,
14+
eDamageCause_Explosion,
15+
eDamageCause_Bullet,
16+
eDamageCause_Punch,
17+
};
18+
19+
// Game object damage information
20+
struct DamageInfo
21+
{
22+
public:
23+
DamageInfo() = default;
24+
25+
// @param object: Source object, it is optional
26+
void SetDamageFromFall(float fallHeight);
27+
void SetDamageFromElectricity();
28+
void SetDamageFromFire(int hitpoints, GameObject* object);
29+
void SetDamageFromWeapon(const WeaponInfo& weaponInfo, GameObject* object);
30+
void SetDamageFromWater(int hitpoints);
31+
void SetDamageFromCarCrash(const glm::vec3& contactPoint, float contactImpulse, GameObject* object);
32+
void SetDamageFromExplosion(int hitpoints, GameObject* object);
33+
void SetDamageFromBullet(int hitpoints, GameObject* object);
34+
void SetDamageFromPunch(int hitpoints, GameObject* object);
35+
36+
void Clear();
37+
38+
public:
39+
// depending on cause of damage object reaction may vary
40+
eDamageCause mDamageCause = eDamageCause_Punch;
41+
42+
// has meaning only if car crash
43+
glm::vec3 mContactPoint;
44+
glm::vec2 mNormal;
45+
46+
// object which cause damage, optional
47+
GameObject* mSourceObject = nullptr;
48+
49+
float mContactImpulse = 0.0f; // has meaning only if car crash
50+
float mFallHeight = 0.0f; // has meaning only if fall
51+
52+
int mHitPoints = 0;
53+
};

src/Decoration.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#include "TimeManager.h"
44
#include "SpriteManager.h"
55

6-
Decoration::Decoration(GameObjectID id, GameObjectInfo* desc)
6+
Decoration::Decoration(GameObjectID id, GameObjectInfo* gameObjectDesc)
77
: GameObject(eGameObjectClass_Decoration, id)
8-
, mGameObjectDesc(desc)
98
{
10-
debug_assert(mGameObjectDesc);
9+
if (gameObjectDesc)
10+
{
11+
mAnimationState.mAnimDesc = gameObjectDesc->mAnimationData;
12+
}
1113
}
1214

1315
Decoration::~Decoration()
@@ -24,7 +26,6 @@ void Decoration::UpdateFrame()
2426
if (mAnimationState.AdvanceAnimation(deltaTime))
2527
{
2628
gSpriteManager.GetSpriteTexture(mObjectID, mAnimationState.GetCurrentFrame(), 0, mDrawSprite);
27-
mDrawSprite.SetOriginToCenter();
2829
}
2930

3031
if (mLifeDuration > 0 && !mAnimationState.IsAnimationActive())
@@ -37,22 +38,29 @@ void Decoration::DrawDebug(DebugRenderer& debugRender)
3738
{
3839
}
3940

40-
void Decoration::Spawn(const glm::vec3& startPosition, cxx::angle_t startRotation)
41+
void Decoration::Spawn(const glm::vec3& position, cxx::angle_t heading)
4142
{
42-
debug_assert(mGameObjectDesc);
43+
SetTransform(position, heading);
4344

44-
mDrawSprite.mPosition.x = startPosition.x;
45-
mDrawSprite.mPosition.y = startPosition.z;
46-
mDrawSprite.mHeight = startPosition.y;
47-
mDrawSprite.mRotateAngle = startRotation;
48-
49-
mAnimationState.Clear();
50-
mAnimationState.mAnimDesc = mGameObjectDesc->mAnimationData;
45+
mAnimationState.ClearState();
5146
mAnimationState.PlayAnimation(eSpriteAnimLoop_FromStart);
5247
}
5348

54-
void Decoration::SetLifeDuration(int numAnimationCycles)
49+
void Decoration::SetLifeDuration(int numCycles)
50+
{
51+
mLifeDuration = numCycles;
52+
mAnimationState.SetMaxRepeatCycles(numCycles);
53+
}
54+
55+
void Decoration::SetTransform(const glm::vec3& position, cxx::angle_t heading)
5556
{
56-
mLifeDuration = numAnimationCycles;
57-
mAnimationState.SetMaxRepeatCycles(numAnimationCycles);
57+
mPosition = position;
58+
mRotation = heading;
59+
60+
cxx::angle_t decoRotation = heading - cxx::angle_t::from_degrees(SPRITE_ZERO_ANGLE);
61+
62+
mDrawSprite.mPosition.x = position.x;
63+
mDrawSprite.mPosition.y = position.z;
64+
mDrawSprite.mHeight = position.y;
65+
mDrawSprite.mRotateAngle = decoRotation;
5866
}

src/Decoration.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class Decoration final: public GameObject
66
{
77
friend class GameObjectsManager;
88

9+
public:
10+
// readonly
11+
glm::vec3 mPosition;
12+
cxx::angle_t mRotation;
13+
914
public:
1015
Decoration(GameObjectID id, GameObjectInfo* desc);
1116
~Decoration();
@@ -16,13 +21,15 @@ class Decoration final: public GameObject
1621
void DrawDebug(DebugRenderer& debugRender) override;
1722

1823
// Setup initial state when spawned or respawned on level
19-
void Spawn(const glm::vec3& startPosition, cxx::angle_t startRotation);
24+
void Spawn(const glm::vec3& position, cxx::angle_t heading);
25+
26+
// Setup current position and rotation
27+
void SetTransform(const glm::vec3& position, cxx::angle_t heading);
2028

2129
void SetLifeDuration(int numAnimationCycles);
2230

2331
private:
2432
SpriteAnimation mAnimationState;
25-
GameObjectInfo* mGameObjectDesc = nullptr;
2633

2734
int mLifeDuration = 0; // number of animation cycles before decoration will be deleted, or 0 for endless lifetime
2835
};

0 commit comments

Comments
 (0)