Skip to content

Commit edbf0e1

Browse files
committed
add(Actor): ActorRespawnSystem and ActorRespawnTimer
1 parent 1ce78e5 commit edbf0e1

9 files changed

Lines changed: 116 additions & 3 deletions

File tree

game_server/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_PROJECT_NAME}-common)
66

77
file(GLOB_RECURSE SOURCES "*.cpp")
88
file(GLOB_RECURSE HEADERS "*.hpp")
9-
target_sources(${PROJECT_NAME} PRIVATE $<TARGET_OBJECTS:${CMAKE_PROJECT_NAME}-common> ${SOURCES} ${HEADERS})
9+
target_sources(${PROJECT_NAME} PRIVATE $<TARGET_OBJECTS:${CMAKE_PROJECT_NAME}-common> ${SOURCES} ${HEADERS} "game/systems/ActorRespawnSystem.cpp")

game_server/game/World.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "components/ActorStatus.hpp"
2323
#include "components/CharacterStatus.hpp"
2424
#include "components/DeletionTimer.hpp"
25+
#include "components/ActorRespawnTimer.hpp"
2526
#include "components/Loot.hpp"
2627
#include "components/NpcAppearance.hpp"
2728
#include "components/PlayerAppearance.hpp"
@@ -37,6 +38,7 @@
3738
#include "systems/ActorDeletionTimerSystem.hpp"
3839
#include "systems/ActorAbnormalEffectSystem.hpp"
3940
#include "systems/ActorStatsUpdateSystem.hpp"
41+
#include "systems/ActorRespawnSystem.hpp"
4042

4143
#include <l2cpp/CompileTimeConfig.hpp>
4244

@@ -59,6 +61,26 @@ static void addGremlin()
5961
gremlin->setPosX(gremlin->position().x + (count++ % 2 ? 35 : -35));
6062
}
6163

64+
static void addGoblin()
65+
{
66+
static u32 count = 1;
67+
68+
if (auto const goblin = World::addMonster(3))
69+
goblin->setPosX(goblin->position().x + (count++ % 2 ? 120 : -120));
70+
71+
72+
}
73+
74+
static void addImp()
75+
{
76+
static u32 count = 1;
77+
78+
if (auto const imp = World::addMonster(4))
79+
imp->setPosX(imp->position().x + (count++ % 2 ? 200 : -200));
80+
81+
82+
}
83+
6284
static void addDummy()
6385
{
6486
static u32 count = 1;
@@ -82,6 +104,8 @@ void World::init()
82104
registerSystem<ActorAutoRegenSystem>();
83105
registerSystem<ActorDeletionTimerSystem>();
84106
registerSystem<ActorAbnormalEffectSystem>();
107+
registerSystem<ActorRespawnSystem>();
108+
85109
// Must be last!
86110
registerSystem<ActorStatsUpdateSystem>();
87111

@@ -96,6 +120,10 @@ void World::init()
96120
addGremlin();
97121
addGremlin();
98122

123+
addGoblin();
124+
125+
addImp();
126+
99127
addDummy();
100128
addDummy();
101129
}
@@ -285,6 +313,15 @@ void World::unscheduleForDeletion(Actor & a)
285313
a.delComponent<DeletionTimer>();
286314
}
287315

316+
void World::scheduleNpcRespawn(Actor& deadActor, u32 npcId, ClockDuration delay)
317+
{
318+
auto& timer = deadActor.getOrAddComponent<ActorRespawnTimer>();
319+
timer.npcId = npcId;
320+
timer.respawnDelay = delay;
321+
timer.elapsedSinceDeath = ClockDuration::zero();
322+
timer.spawnPosition = deadActor.position();
323+
}
324+
288325
auto World::inGameTime() -> std::chrono::minutes
289326
{
290327
if constexpr (Config::isDebugMode)

game_server/game/World.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class World
5757
static void scheduleForDeletion(Actor &, ClockDuration timeFromNow = ClockDuration::zero());
5858
static void unscheduleForDeletion(Actor &);
5959

60+
static void scheduleNpcRespawn(Actor& deadActor, u32 npcId, ClockDuration delay);
61+
6062
static auto inGameTime() -> std::chrono::minutes;
6163

6264
static auto subscribeToTarget(GameObjectId targetId, Actor const & listener) -> OptRef<Actor>;

game_server/game/actor/Actor.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "../components/Position.hpp"
1616
#include "../components/SkillDirectory.hpp"
1717
#include "../components/Stats.hpp"
18+
#include "../components/NpcAppearance.hpp"
1819

1920
// ReSharper disable once CppUnusedIncludeDirective
2021
#include <l2cpp/details/Pimpl.hpp>
@@ -205,6 +206,15 @@ void Actor::die()
205206
delComponent<ActorAutoRegen>();
206207
delComponent<AttackStanceTimer>();
207208

209+
210+
if (auto const app = component<NpcAppearance>())
211+
{
212+
u32 tId = app->templateId();
213+
auto const delay = std::chrono::seconds(2);
214+
World::scheduleNpcRespawn(*this, tId, delay);
215+
}
216+
217+
208218
fire onDied();
209219
}
210220

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
#pragma once
3+
#include "../ecs/Component.hpp"
4+
#include <l2cpp/Typedefs.hpp>
5+
#include "../components/Position.hpp"
6+
7+
struct ActorRespawnTimer : public Component {
8+
u32 npcId;
9+
ClockDuration respawnDelay;
10+
ClockDuration elapsedSinceDeath{ ClockDuration::zero() };
11+
Position spawnPosition;
12+
};

game_server/game/components/NpcAppearance.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ namespace
1010

1111
NpcAppearance::NpcAppearance() noexcept
1212
: _id(baseId + 1) // Gremlin
13+
, _templateId(1) // Gremlin
1314
, _titleColor(0)
1415
, _nameIsVisible(true)
1516
{}
1617

1718
auto NpcAppearance::id() const -> u32 { return _id; }
19+
auto NpcAppearance::templateId() const -> u32 { return _templateId; }
1820
auto NpcAppearance::titleColor() const -> u32 { return _titleColor; }
1921
bool NpcAppearance::nameIsVisible() const { return _nameIsVisible; }
2022

21-
void NpcAppearance::setId(u32 const id) { _id = id < baseId ? baseId + id : id; }
23+
void NpcAppearance::setId(u32 const id)
24+
{
25+
_templateId = id < baseId ? id : id - baseId;
26+
27+
_id = id < baseId ? baseId + id : id;
28+
}
29+
2230
void NpcAppearance::setTitleColor(u32 const color) { _titleColor = color; }
2331
void NpcAppearance::setNameIsVisible(bool const visible) { _nameIsVisible = visible; }

game_server/game/components/NpcAppearance.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class NpcAppearance : public ActorAppearance
1313

1414
public:
1515
auto id() const -> u32;
16+
auto templateId() const -> u32;
1617
auto titleColor() const -> u32;
1718
bool nameIsVisible() const;
1819

@@ -22,6 +23,6 @@ class NpcAppearance : public ActorAppearance
2223
void setNameIsVisible(bool visible);
2324

2425
private:
25-
u32 _id, _titleColor;
26+
u32 _id, _templateId, _titleColor;
2627
bool _nameIsVisible;
2728
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "ActorRespawnSystem.hpp"
2+
3+
4+
#include "../actor/Actor.hpp"
5+
#include "../actor/Monster.hpp"
6+
#include "../components/ActorRespawnTimer.hpp"
7+
#include "../World.hpp"
8+
#include "../../network/packets/server/status/NpcStatusUpdatePacket.hpp"
9+
using namespace Network::Packet::Server;
10+
11+
void ActorRespawnSystem::updateImpl(ClockDuration elapsed, Actor& actor)
12+
{
13+
auto timer = actor.component<ActorRespawnTimer>();
14+
if (!timer)
15+
{
16+
return;
17+
}
18+
19+
timer->elapsedSinceDeath += elapsed;
20+
21+
if (timer->elapsedSinceDeath >= timer->respawnDelay)
22+
{
23+
if (auto const npc = World::addNpc(timer->npcId))
24+
{
25+
npc->setPosition(timer->spawnPosition);
26+
27+
World::broadcastAround(npc, NpcStatusUpdatePacket(npc));
28+
}
29+
30+
actor.delComponent<ActorRespawnTimer>();
31+
}
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#pragma once
3+
4+
// Project includes
5+
#include "../ecs/ActorSystem.hpp"
6+
7+
8+
struct ActorRespawnSystem : public ActorSystem
9+
{
10+
void updateImpl(ClockDuration elapsed, Actor& actor) override;
11+
};

0 commit comments

Comments
 (0)