Skip to content

Commit

Permalink
Allow missile graphics to be on different levels
Browse files Browse the repository at this point in the history
  • Loading branch information
grantramsay committed Mar 1, 2020
1 parent fa63c7a commit 29e2302
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 32 deletions.
7 changes: 4 additions & 3 deletions apps/freeablo/faworld/gamelevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,12 @@ namespace FAWorld
{
for (const auto& missile : actor->getMissiles())
{
// Only display missiles for this (the currently displayed) level.
if (missile->getLevel() != this)
continue;
for (const auto& graphic : missile->getGraphics())
{
// Only display missile graphics for this (the currently displayed) level.
if (graphic->mLevel != this)
continue;

auto tmp = graphic->getCurrentFrame();
auto spriteGroup = tmp.first;
auto frame = tmp.second;
Expand Down
15 changes: 5 additions & 10 deletions apps/freeablo/faworld/missile/missile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
namespace FAWorld::Missile
{
Missile::Missile(MissileId missileId, Actor& creator, Misc::Point dest)
: mCreator(&creator), mMissileId(missileId), mLevel(creator.getLevel()), mSrcPoint(creator.getPos().current()), mAttr(Attributes::fromId(missileId))
: mCreator(&creator), mMissileId(missileId), mSrcPoint(creator.getPos().current()), mAttr(Attributes::fromId(missileId))
{
mAttr.mCreation(*this, dest);
mAttr.mCreation(*this, dest, creator.getLevel());

if (!missileData().mSoundEffect.empty())
Engine::ThreadManager::get()->playSound(missileData().mSoundEffect);
Expand All @@ -19,12 +19,8 @@ namespace FAWorld::Missile
Missile::Missile(FASaveGame::GameLoader& loader) : mMissileId(static_cast<MissileId>(loader.load<int32_t>())), mAttr(Attributes::fromId(mMissileId))
{
auto creatorId = loader.load<int32_t>();
auto levelIndex = loader.load<int32_t>();
auto world = loader.currentlyLoadingWorld;
loader.addFunctionToRunAtEnd([this, world, creatorId, levelIndex]() {
mCreator = world->getActorById(creatorId);
mLevel = world->getLevel(levelIndex);
});
loader.addFunctionToRunAtEnd([this, world, creatorId]() { mCreator = world->getActorById(creatorId); });

mSrcPoint = Misc::Point(loader);
mComplete = loader.load<bool>();
Expand All @@ -41,7 +37,6 @@ namespace FAWorld::Missile

saver.save(static_cast<int32_t>(mMissileId));
saver.save(mCreator->getId());
saver.save(mLevel->getLevelIndex());

mSrcPoint.save(saver);
saver.save(mComplete);
Expand Down Expand Up @@ -94,12 +89,12 @@ namespace FAWorld::Missile
auto curPoint = graphic->mCurPos.current();

// Check if actor is hit.
auto actor = mLevel->getActorAt(curPoint);
auto actor = graphic->mLevel->getActorAt(curPoint);
if (actor)
mAttr.mActorEngagement(*this, *graphic, *actor);

// Stop when walls are hit.
if (!actor && !mLevel->isPassable(curPoint, mCreator))
if (!actor && !graphic->mLevel->isPassable(curPoint, mCreator))
{
playImpactSound();
graphic->stop();
Expand Down
12 changes: 5 additions & 7 deletions apps/freeablo/faworld/missile/missile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace FAWorld::Missile
virtual void update();
virtual bool isComplete() const { return mComplete; }
MissileId getMissileId() const { return mMissileId; }
const GameLevel* getLevel() const { return mLevel; }
const std::vector<std::unique_ptr<MissileGraphic>>& getGraphics() const { return mGraphics; }

protected:
Expand All @@ -38,12 +37,12 @@ namespace FAWorld::Missile
{
public:
Creation() = delete;
typedef std::function<void(Missile& missile, Misc::Point dest)> Method;
typedef std::function<void(Missile& missile, Misc::Point dest, GameLevel* level)> Method;

static void singleFrame16Direction(Missile& missile, Misc::Point dest);
static void animated16Direction(Missile& missile, Misc::Point dest);
static void firewall(Missile& missile, Misc::Point dest);
static void basicAnimated(Missile& missile, Misc::Point dest);
static void singleFrame16Direction(Missile& missile, Misc::Point dest, GameLevel* level);
static void animated16Direction(Missile& missile, Misc::Point dest, GameLevel* level);
static void firewall(Missile& missile, Misc::Point dest, GameLevel* level);
static void basicAnimated(Missile& missile, Misc::Point dest, GameLevel* level);
};

class Movement
Expand Down Expand Up @@ -92,7 +91,6 @@ namespace FAWorld::Missile

Actor* mCreator;
MissileId mMissileId;
GameLevel* mLevel;
Misc::Point mSrcPoint;
Missile::Attributes mAttr;
std::vector<std::unique_ptr<MissileGraphic>> mGraphics;
Expand Down
16 changes: 8 additions & 8 deletions apps/freeablo/faworld/missile/missilecreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

namespace FAWorld::Missile
{
void Missile::Creation::singleFrame16Direction(Missile& missile, Misc::Point dest)
void Missile::Creation::singleFrame16Direction(Missile& missile, Misc::Point dest, GameLevel* level)
{
auto direction = (Vec2Fix(dest.x, dest.y) - Vec2Fix(missile.mSrcPoint.x, missile.mSrcPoint.y)).getDirection();
auto srcPos = Position(missile.mSrcPoint, direction);
int32_t direction16 = static_cast<int32_t>(direction.getDirection16());
missile.mGraphics.push_back(std::make_unique<MissileGraphic>("", missile.getGraphicsPath(0), direction16, srcPos));
missile.mGraphics.push_back(std::make_unique<MissileGraphic>("", missile.getGraphicsPath(0), direction16, srcPos, level));
}

void Missile::Creation::animated16Direction(Missile& missile, Misc::Point dest)
void Missile::Creation::animated16Direction(Missile& missile, Misc::Point dest, GameLevel* level)
{
auto direction = (Vec2Fix(dest.x, dest.y) - Vec2Fix(missile.mSrcPoint.x, missile.mSrcPoint.y)).getDirection();
auto srcPos = Position(missile.mSrcPoint, direction);
int32_t direction16 = static_cast<int32_t>(direction.getDirection16());
missile.mGraphics.push_back(std::make_unique<MissileGraphic>("", missile.getGraphicsPath(direction16), std::nullopt, srcPos));
missile.mGraphics.push_back(std::make_unique<MissileGraphic>("", missile.getGraphicsPath(direction16), std::nullopt, srcPos, level));
}

void Missile::Creation::firewall(Missile& missile, Misc::Point dest)
void Missile::Creation::firewall(Missile& missile, Misc::Point dest, GameLevel* level)
{
// Flames are placed at -5 -> +5 perpendicular to the clicked point, and
// two flames are placed at the clicked point (for double damage).
Expand All @@ -32,14 +32,14 @@ namespace FAWorld::Missile
for (int32_t i = 0; i < 6; i++)
{
missile.mGraphics.push_back(
std::make_unique<MissileGraphic>(missile.getGraphicsPath(0), missile.getGraphicsPath(1), std::nullopt, Position(point)));
std::make_unique<MissileGraphic>(missile.getGraphicsPath(0), missile.getGraphicsPath(1), std::nullopt, Position(point), level));
point = Misc::getNextPosByDir(point, dir);
}
}
}

void Missile::Creation::basicAnimated(Missile& missile, Misc::Point)
void Missile::Creation::basicAnimated(Missile& missile, Misc::Point, GameLevel* level)
{
missile.mGraphics.push_back(std::make_unique<MissileGraphic>("", missile.getGraphicsPath(0), std::nullopt, Position(missile.mSrcPoint)));
missile.mGraphics.push_back(std::make_unique<MissileGraphic>("", missile.getGraphicsPath(0), std::nullopt, Position(missile.mSrcPoint), level));
}
}
8 changes: 6 additions & 2 deletions apps/freeablo/faworld/missile/missilegraphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace FAWorld::Missile
{
MissileGraphic::MissileGraphic(std::string initialGraphicPath, std::string mainGraphicPath, std::optional<int32_t> singleFrame, Position position)
: mCurPos(position), mMainGraphicPath(mainGraphicPath), mSingleFrame(singleFrame)
MissileGraphic::MissileGraphic(std::string initialGraphicPath, std::string mainGraphicPath, std::optional<int32_t> singleFrame, Position position, GameLevel* level)
: mCurPos(position), mLevel(level), mMainGraphicPath(mainGraphicPath), mSingleFrame(singleFrame)
{
playAnimation(initialGraphicPath, FARender::AnimationPlayer::AnimationType::Once);
}
Expand All @@ -19,6 +19,9 @@ namespace FAWorld::Missile
mMainGraphicPath = loader.load<std::string>();
mSingleFrame = (mSingleFrame = loader.load<int32_t>()) == -1 ? std::nullopt : mSingleFrame;
mAnimationPlayer = FARender::AnimationPlayer(loader);
auto levelIndex = loader.load<int32_t>();
auto world = loader.currentlyLoadingWorld;
loader.addFunctionToRunAtEnd([this, world, levelIndex]() { mLevel = world->getLevel(levelIndex); });
mTicksSinceStarted = loader.load<Tick>();
mComplete = loader.load<bool>();
}
Expand All @@ -29,6 +32,7 @@ namespace FAWorld::Missile
saver.save(mMainGraphicPath);
saver.save(static_cast<int32_t>(mSingleFrame == std::nullopt ? -1 : *mSingleFrame));
mAnimationPlayer.save(saver);
saver.save(mLevel->getLevelIndex());
saver.save(mTicksSinceStarted);
saver.save(mComplete);
}
Expand Down
3 changes: 2 additions & 1 deletion apps/freeablo/faworld/missile/missilegraphic.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace FAWorld::Missile
public:
virtual ~MissileGraphic() = default;

MissileGraphic(std::string initialGraphicPath, std::string mainGraphicPath, std::optional<int32_t> singleFrame, Position position);
MissileGraphic(std::string initialGraphicPath, std::string mainGraphicPath, std::optional<int32_t> singleFrame, Position position, GameLevel* level);
MissileGraphic(FASaveGame::GameLoader& loader);

virtual void save(FASaveGame::GameSaver& saver);
Expand All @@ -29,6 +29,7 @@ namespace FAWorld::Missile
Tick getTicksSinceStarted() const { return mTicksSinceStarted; }

Position mCurPos;
GameLevel* mLevel;

protected:
void playAnimation(std::string path, FARender::AnimationPlayer::AnimationType animationType);
Expand Down
2 changes: 1 addition & 1 deletion apps/freeablo/faworld/missile/missilemovement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace FAWorld::Missile

void Missile::Movement::hoverOverCreator(Missile& missile, MissileGraphic& graphic)
{
// TODO: change level with actor.
graphic.mLevel = missile.mCreator->getLevel();
graphic.mCurPos = missile.mCreator->getPos();
}
}

0 comments on commit 29e2302

Please sign in to comment.