From 4ef47aed62364f9cf1474864e5cf94232b4477af Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 19 Dec 2016 20:12:23 +0000 Subject: Changed entity ownership model to use smart pointers --- src/Mobs/Monster.cpp | 74 ++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 40 deletions(-) (limited to 'src/Mobs/Monster.cpp') diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index ecda6e724..6dc03c7e3 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -996,7 +996,7 @@ void cMonster::UnsafeUnsetTarget() -cPawn * cMonster::GetTarget () +cPawn * cMonster::GetTarget() { return m_Target; } @@ -1005,29 +1005,25 @@ cPawn * cMonster::GetTarget () -cMonster * cMonster::NewMonsterFromType(eMonsterType a_MobType) +std::unique_ptr cMonster::NewMonsterFromType(eMonsterType a_MobType) { auto & Random = GetRandomProvider(); - cMonster * toReturn = nullptr; // Create the mob entity switch (a_MobType) { case mtMagmaCube: { - toReturn = new cMagmaCube(1 << Random.RandInt(2)); // Size 1, 2 or 4 - break; + return cpp14::make_unique(1 << Random.RandInt(2)); // Size 1, 2 or 4 } case mtSlime: { - toReturn = new cSlime(1 << Random.RandInt(2)); // Size 1, 2 or 4 - break; + return cpp14::make_unique(1 << Random.RandInt(2)); // Size 1, 2 or 4 } case mtSkeleton: { // TODO: Actual detection of spawning in Nether - toReturn = new cSkeleton(false); - break; + return cpp14::make_unique(false); } case mtVillager: { @@ -1038,8 +1034,7 @@ cMonster * cMonster::NewMonsterFromType(eMonsterType a_MobType) VillagerType = 0; } - toReturn = new cVillager(static_cast(VillagerType)); - break; + return cpp14::make_unique(static_cast(VillagerType)); } case mtHorse: { @@ -1055,42 +1050,41 @@ cMonster * cMonster::NewMonsterFromType(eMonsterType a_MobType) HorseType = 0; } - toReturn = new cHorse(HorseType, HorseColor, HorseStyle, HorseTameTimes); - break; + return cpp14::make_unique(HorseType, HorseColor, HorseStyle, HorseTameTimes); } - case mtBat: toReturn = new cBat(); break; - case mtBlaze: toReturn = new cBlaze(); break; - case mtCaveSpider: toReturn = new cCaveSpider(); break; - case mtChicken: toReturn = new cChicken(); break; - case mtCow: toReturn = new cCow(); break; - case mtCreeper: toReturn = new cCreeper(); break; - case mtEnderDragon: toReturn = new cEnderDragon(); break; - case mtEnderman: toReturn = new cEnderman(); break; - case mtGhast: toReturn = new cGhast(); break; - case mtGiant: toReturn = new cGiant(); break; - case mtGuardian: toReturn = new cGuardian(); break; - case mtIronGolem: toReturn = new cIronGolem(); break; - case mtMooshroom: toReturn = new cMooshroom(); break; - case mtOcelot: toReturn = new cOcelot(); break; - case mtPig: toReturn = new cPig(); break; - case mtRabbit: toReturn = new cRabbit(); break; - case mtSheep: toReturn = new cSheep(); break; - case mtSilverfish: toReturn = new cSilverfish(); break; - case mtSnowGolem: toReturn = new cSnowGolem(); break; - case mtSpider: toReturn = new cSpider(); break; - case mtSquid: toReturn = new cSquid(); break; - case mtWitch: toReturn = new cWitch(); break; - case mtWither: toReturn = new cWither(); break; - case mtWolf: toReturn = new cWolf(); break; - case mtZombie: toReturn = new cZombie(false); break; // TODO: Infected zombie parameter - case mtZombiePigman: toReturn = new cZombiePigman(); break; + case mtBat: return cpp14::make_unique(); + case mtBlaze: return cpp14::make_unique(); + case mtCaveSpider: return cpp14::make_unique(); + case mtChicken: return cpp14::make_unique(); + case mtCow: return cpp14::make_unique(); + case mtCreeper: return cpp14::make_unique < cCreeper>(); + case mtEnderDragon: return cpp14::make_unique(); + case mtEnderman: return cpp14::make_unique(); + case mtGhast: return cpp14::make_unique(); + case mtGiant: return cpp14::make_unique(); + case mtGuardian: return cpp14::make_unique(); + case mtIronGolem: return cpp14::make_unique(); + case mtMooshroom: return cpp14::make_unique(); + case mtOcelot: return cpp14::make_unique(); + case mtPig: return cpp14::make_unique(); + case mtRabbit: return cpp14::make_unique(); + case mtSheep: return cpp14::make_unique(); + case mtSilverfish: return cpp14::make_unique(); + case mtSnowGolem: return cpp14::make_unique(); + case mtSpider: return cpp14::make_unique(); + case mtSquid: return cpp14::make_unique(); + case mtWitch: return cpp14::make_unique(); + case mtWither: return cpp14::make_unique(); + case mtWolf: return cpp14::make_unique(); + case mtZombie: return cpp14::make_unique(false); // TODO: Infected zombie parameter + case mtZombiePigman: return cpp14::make_unique(); default: { ASSERT(!"Unhandled mob type whilst trying to spawn mob!"); + return nullptr; } } - return toReturn; } -- cgit v1.2.3