Skip to content

Commit 6db6ea1

Browse files
committed
-
1 parent b3fbf7b commit 6db6ea1

11 files changed

Lines changed: 109 additions & 49 deletions

gamedata/entities/weapons.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"base_hit_range": 4.5,
2121
"base_fire_rate": 2.0,
2222
"base_ammo_limit": 999,
23+
"base_damage": 1,
2324

2425
"hud_sprite": 30
2526
},
@@ -37,6 +38,7 @@
3738
"base_hit_range": 4.5,
3839
"base_fire_rate": 8.0,
3940
"base_ammo_limit": 999,
41+
"base_damage": 1,
4042

4143
"hud_sprite": 31
4244
},
@@ -46,13 +48,14 @@
4648
"fire_type": "projectile",
4749
// projectile data
4850
"projectile_type": "flame",
49-
"projectile_size": 0.075,
51+
"projectile_size": 0.05,
5052
"projectile_speed": 3.1,
5153
"projectile_object": 75,
5254
// base params
5355
"base_hit_range": 1.1,
5456
"base_fire_rate": 15.0,
5557
"base_ammo_limit": 999,
58+
"base_damage": 3,
5659

5760
"hud_sprite": 33
5861
},
@@ -69,6 +72,7 @@
6972
"base_hit_range": 20.0,
7073
"base_fire_rate": 1.0,
7174
"base_ammo_limit": 999,
75+
"base_damage": 100,
7276

7377
"hud_sprite": 32
7478
}

src/GameObjectsManager.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,25 @@ bool GameObjectsManager::CreateStartupObjects()
404404
}
405405
}
406406
return true;
407-
}
407+
}
408+
409+
int GameObjectsManager::GetBaseHitpointsForCar(eCarVType carType) const
410+
{
411+
// todo: move to settings
412+
// todo: find out correct values
413+
414+
switch (carType)
415+
{
416+
case eCarVType_Bus:
417+
case eCarVType_FrontOfJuggernaut:
418+
case eCarVType_BackOfJuggernaut:
419+
return 29;
420+
case eCarVType_Motorcycle:
421+
case eCarVType_StandardCar:
422+
return 16;
423+
case eCarVType_Train:
424+
case eCarVType_Tank:
425+
return 62;
426+
}
427+
return 10;
428+
}

src/GameObjectsManager.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,23 @@ class GameObjectsManager final: public cxx::noncopyable
5050
// Add new obstacle instance to map at specific location
5151
Obstacle* CreateObstacle(const glm::vec3& position, cxx::angle_t heading, GameObjectStyle* desc);
5252

53-
// find gameobject by its unique identifier
53+
// Find gameobject by its unique identifier
5454
// @param objectID: Unique identifier
5555
Vehicle* GetVehicleByID(GameObjectID objectID) const;
5656
Obstacle* GetObstacleByID(GameObjectID objectID) const;
5757
Decoration* GetDecorationByID(GameObjectID objectID) const;
5858
Pedestrian* GetPedestrianByID(GameObjectID objectID) const;
5959
GameObject* GetGameObjectByID(GameObjectID objectID) const;
6060

61-
// will immediately destroy gameobject, don't call this mehod during UpdateFrame
61+
// Get base hp points for specific car type
62+
// @param carType: Identifier
63+
int GetBaseHitpointsForCar(eCarVType carType) const;
64+
65+
// Will immediately destroy gameobject, don't call this mehod during UpdateFrame
6266
// @param object: Object to destroy
6367
void DestroyGameObject(GameObject* object);
6468

65-
// queue gameobject for deletion, it will be destroyed next frame
69+
// Queue gameobject for deletion, it will be destroyed next frame
6670
// @param object: Object to queue
6771
void MarkForDeletion(GameObject* object);
6872

src/PhysicsComponents.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ PhysicsBody::PhysicsBody(b2World* physicsWorld)
1414
, mPhysicsBody()
1515
, mPreviousPosition()
1616
, mSmoothPosition()
17-
, mPhysicsBodiesListNode(this)
1817
{
1918
debug_assert(physicsWorld);
2019
}

src/PhysicsComponents.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ class PhysicsBody: public cxx::noncopyable
9696
protected:
9797
b2World* mPhysicsWorld;
9898
b2Body* mPhysicsBody;
99-
cxx::intrusive_node<PhysicsBody> mPhysicsBodiesListNode;
10099
};
101100

102101
//////////////////////////////////////////////////////////////////////////

src/PhysicsManager.cpp

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,17 @@ void PhysicsManager::ProcessSimulationStep()
119119
mPhysicsWorld->Step(mSimulationStepTime, velocityIterations, positionIterations);
120120

121121
// process physics components
122-
for (PhysicsBody* currComponent: mCarsBodiesList)
122+
for (size_t i = 0, NumElements = mCarsBodiesList.size(); i < NumElements; ++i)
123123
{
124-
currComponent->SimulationStep();
124+
mCarsBodiesList[i]->SimulationStep();
125125
}
126-
for (PhysicsBody* currComponent: mPedsBodiesList)
126+
for (size_t i = 0, NumElements = mPedsBodiesList.size(); i < NumElements; ++i)
127127
{
128-
currComponent->SimulationStep();
128+
mPedsBodiesList[i]->SimulationStep();
129129
}
130-
for (PhysicsBody* currComponent: mProjectileBodiesList)
130+
for (size_t i = 0, NumElements = mProjectileBodiesList.size(); i < NumElements; ++i)
131131
{
132-
currComponent->SimulationStep();
132+
mProjectileBodiesList[i]->SimulationStep();
133133
}
134134

135135
FixedStepGravity();
@@ -162,7 +162,7 @@ PedPhysicsBody* PhysicsManager::CreatePhysicsBody(Pedestrian* object, const glm:
162162
PedPhysicsBody* physicsObject = mPedsBodiesPool.create(mPhysicsWorld, object);
163163
physicsObject->SetPosition(position, rotationAngle);
164164

165-
mPedsBodiesList.insert(&physicsObject->mPhysicsBodiesListNode);
165+
mPedsBodiesList.push_back(physicsObject);
166166
return physicsObject;
167167
}
168168

@@ -174,7 +174,7 @@ CarPhysicsBody* PhysicsManager::CreatePhysicsBody(Vehicle* object, const glm::ve
174174
CarPhysicsBody* physicsObject = mCarsBodiesPool.create(mPhysicsWorld, object);
175175
physicsObject->SetPosition(position, rotationAngle);
176176

177-
mCarsBodiesList.insert(&physicsObject->mPhysicsBodiesListNode);
177+
mCarsBodiesList.push_back(physicsObject);
178178
return physicsObject;
179179
}
180180

@@ -185,7 +185,7 @@ ProjectilePhysicsBody* PhysicsManager::CreatePhysicsBody(Projectile* object, con
185185
ProjectilePhysicsBody* physicsObject = mProjectileBodiesPool.create(mPhysicsWorld, object);
186186
physicsObject->SetPosition(position, rotationAngle);
187187

188-
mProjectileBodiesList.insert(&physicsObject->mPhysicsBodiesListNode);
188+
mProjectileBodiesList.push_back(physicsObject);
189189
return physicsObject;
190190
}
191191

@@ -258,42 +258,24 @@ void PhysicsManager::CreateMapCollisionShape()
258258
void PhysicsManager::DestroyPhysicsBody(PedPhysicsBody* object)
259259
{
260260
debug_assert(object);
261-
if (mPedsBodiesList.contains(&object->mPhysicsBodiesListNode))
262-
{
263-
mPedsBodiesList.remove(&object->mPhysicsBodiesListNode);
264-
}
265-
else
266-
{
267-
debug_assert(false);
268-
}
261+
cxx::erase_elements(mPedsBodiesList, object);
262+
269263
mPedsBodiesPool.destroy(object);
270264
}
271265

272266
void PhysicsManager::DestroyPhysicsBody(CarPhysicsBody* object)
273267
{
274268
debug_assert(object);
275-
if (mCarsBodiesList.contains(&object->mPhysicsBodiesListNode))
276-
{
277-
mCarsBodiesList.remove(&object->mPhysicsBodiesListNode);
278-
}
279-
else
280-
{
281-
debug_assert(false);
282-
}
269+
cxx::erase_elements(mCarsBodiesList, object);
270+
283271
mCarsBodiesPool.destroy(object);
284272
}
285273

286274
void PhysicsManager::DestroyPhysicsBody(ProjectilePhysicsBody* object)
287275
{
288276
debug_assert(object);
289-
if (mProjectileBodiesList.contains(&object->mPhysicsBodiesListNode))
290-
{
291-
mProjectileBodiesList.remove(&object->mPhysicsBodiesListNode);
292-
}
293-
else
294-
{
295-
debug_assert(false);
296-
}
277+
cxx::erase_elements(mProjectileBodiesList, object);
278+
297279
mProjectileBodiesPool.destroy(object);
298280
}
299281

@@ -450,11 +432,10 @@ void PhysicsManager::FixedStepGravity()
450432
// todo: cleanup this mess
451433

452434
// cars
453-
for (PhysicsBody* currPhysicsBody: mCarsBodiesList)
435+
for (size_t i = 0, NumElements = mCarsBodiesList.size(); i < NumElements; ++i)
454436
{
455-
CarPhysicsBody* physicsComponent = static_cast<CarPhysicsBody*>(currPhysicsBody);
456-
Vehicle* currCar = physicsComponent->mReferenceCar;
457437

438+
CarPhysicsBody* physicsComponent = static_cast<CarPhysicsBody*>(mCarsBodiesList[i]);
458439
if (physicsComponent->mWaterContact)
459440
continue;
460441

@@ -484,9 +465,9 @@ void PhysicsManager::FixedStepGravity()
484465
}
485466

486467
// pedestrians
487-
for (PhysicsBody* currPhysicsBody: mPedsBodiesList)
468+
for (size_t i = 0, NumElements = mPedsBodiesList.size(); i < NumElements; ++i)
488469
{
489-
PedPhysicsBody* physicsComponent = static_cast<PedPhysicsBody*>(currPhysicsBody);
470+
PedPhysicsBody* physicsComponent = static_cast<PedPhysicsBody*>(mPedsBodiesList[i]);
490471
Pedestrian* currPedestrian = physicsComponent->mReferencePed;
491472

492473
if (currPedestrian->mCurrentCar)

src/PhysicsManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class PhysicsManager final: private b2ContactListener
9797
cxx::object_pool<ProjectilePhysicsBody> mProjectileBodiesPool;
9898

9999
// bodies lists
100-
cxx::intrusive_list<PhysicsBody> mPedsBodiesList;
101-
cxx::intrusive_list<PhysicsBody> mCarsBodiesList;
102-
cxx::intrusive_list<PhysicsBody> mProjectileBodiesList;
100+
std::vector<PhysicsBody*> mPedsBodiesList;
101+
std::vector<PhysicsBody*> mCarsBodiesList;
102+
std::vector<PhysicsBody*> mProjectileBodiesList;
103103
};
104104

105105
extern PhysicsManager gPhysics;

src/Projectile.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ void Projectile::UpdateFrame()
9494
hitEffect->SetLifeDuration(1);
9595
}
9696
}
97+
98+
if (mContactObject && mContactObject->IsVehicleClass())
99+
{
100+
Vehicle* carObject = static_cast<Vehicle*>(mContactObject);
101+
carObject->ReceiveDamageFromProjectile(mWeaponInfo->mBaseDamage);
102+
}
97103
}
98104
}
99105
}

src/Vehicle.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "SpriteManager.h"
99
#include "Pedestrian.h"
1010
#include "TimeManager.h"
11+
#include "GameObjectsManager.h"
1112

1213
Vehicle::Vehicle(GameObjectID id) : GameObject(eGameObjectClass_Car, id)
1314
, mPhysicsBody()
@@ -45,6 +46,8 @@ void Vehicle::Spawn(const glm::vec3& startPosition, cxx::angle_t startRotation)
4546
}
4647

4748
mDead = false;
49+
mHitpoints = gGameObjectsManager.GetBaseHitpointsForCar(mCarStyle->mVType);
50+
4851
mDamageDeltaBits = 0;
4952
mChassisSpriteIndex = gGameMap.mStyleData.GetCarSpriteIndex(mCarStyle->mVType, mCarStyle->mSprNum); // todo: handle bike fallen state
5053

@@ -53,6 +56,17 @@ void Vehicle::Spawn(const glm::vec3& startPosition, cxx::angle_t startRotation)
5356

5457
void Vehicle::UpdateFrame()
5558
{
59+
if (mDead)
60+
return;
61+
62+
// check if car dead
63+
mDead = (mHitpoints <= 0);
64+
if (mDead)
65+
{
66+
Explode();
67+
return;
68+
}
69+
5670
UpdateDeltaAnimations();
5771

5872
UpdateDriving();
@@ -510,6 +524,34 @@ void Vehicle::ReceiveDamageFromWater()
510524
}
511525
}
512526

527+
void Vehicle::ReceiveDamageFromProjectile(int damage)
528+
{
529+
if (mDead)
530+
return;
531+
532+
debug_assert(damage > 0);
533+
534+
if (damage > 0)
535+
{
536+
mHitpoints -= damage;
537+
}
538+
}
539+
540+
void Vehicle::Explode()
541+
{
542+
glm::vec3 explosionPos = mPhysicsBody->GetPosition();
543+
explosionPos.y = mDrawHeight + 0.2f; // todo: magic numbers
544+
545+
Explosion* explosion = gGameObjectsManager.CreateExplosion(explosionPos);
546+
debug_assert(explosion);
547+
548+
// kill passengers inside
549+
for (Pedestrian* currentPed: mPassengers)
550+
{
551+
currentPed->Die(ePedestrianDeathReason_BlownUp, nullptr);
552+
}
553+
}
554+
513555
bool Vehicle::HasHardTop() const
514556
{
515557
if ((mCarStyle->mConvertible == eCarConvertible_HardTop || mCarStyle->mConvertible == eCarConvertible_HardTopAnimated) &&

src/Vehicle.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ class Vehicle final: public GameObject
3838
// setup initial state when spawned or respawned on level
3939
void Spawn(const glm::vec3& startPosition, cxx::angle_t startRotation);
4040

41-
// todo: implement car damage system
41+
// process damages
4242
void ReceiveDamageFromWater();
43+
void ReceiveDamageFromProjectile(int damage);
4344

4445
// adds passenger into the car
4546
// @param pedestrian: Pedestrian, cannot be null
@@ -91,6 +92,7 @@ class Vehicle final: public GameObject
9192
bool IsSeatPresent(eCarSeat carSeat) const;
9293

9394
private:
95+
void Explode();
9496
void UpdateDriving();
9597
void ComputeDrawHeight(const glm::vec3& position);
9698
void SetupDeltaAnimations();
@@ -104,4 +106,5 @@ class Vehicle final: public GameObject
104106
SpriteDeltaBits mDamageDeltaBits;
105107

106108
int mChassisSpriteIndex = 0;
109+
int mHitpoints = 20;
107110
};

0 commit comments

Comments
 (0)