Skip to content

Commit

Permalink
Merge pull request NTUT-FUCK-PTSD#136 from HeavenManySugar/enemyPlus
Browse files Browse the repository at this point in the history
Refactor enemy classes and add Monkey enemy class
  • Loading branch information
HeavenManySugar authored May 14, 2024
2 parents fa0d3c9 + 6dbdbb9 commit 2a5c534
Show file tree
Hide file tree
Showing 32 changed files with 694 additions and 217 deletions.
Binary file added assets/entities/monkey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/entities/monkey_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions include/Dungeon/Enemies/Ghost.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class Ghost final : public Dungeon::Enemy {
private:
const glm::vec2 m_FrameSize = {24, 24};

std::size_t m_LastDistance;

bool m_Transparent = false;
};
} // namespace Enemies
Expand Down
2 changes: 0 additions & 2 deletions include/Dungeon/Enemies/GreenSlime.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class GreenSlime final : public Dungeon::Enemy {

void Move() override;

void Update() override;

private:
const glm::vec2 m_FrameSize = {26, 26};
};
Expand Down
32 changes: 32 additions & 0 deletions include/Dungeon/Enemies/Monkey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef MONKEY_H
#define MONKEY_H

#include "Dungeon/Enemy.h"

namespace Dungeon {
namespace Enemies {
class Monkey final : public Dungeon::Enemy {
public:
Monkey(const s_Enemy& u_Enemy, const std::shared_ptr<MapData> mapData);

void Move() override;

void AttackPlayer() override;

void UpdateProperties(const glm::vec2& direction);

void Update() override;

private:
void OnPlayer();
const glm::vec2 m_FrameSize = {25, 24};
std::vector<std::size_t> m_BackFrames;
std::vector<std::size_t> m_ShadowBackFrames;
std::size_t m_HealthGrapple;

bool m_Back = false;
};
} // namespace Enemies
} // namespace Dungeon

#endif // MONKEY_H
16 changes: 1 addition & 15 deletions include/Dungeon/Enemies/Shopkeeper.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
#ifndef FUCK_PTSD_2024_05_12_SHOPKEEPER_H
#define FUCK_PTSD_2024_05_12_SHOPKEEPER_H

#include <memory>
#include <random>
#include "Dungeon/Direction.h"
#include "Elements.h"
#include "Enemy.h"

namespace Dungeon::Enemies {
class Shopkeeper final : public Dungeon::Enemy {
public:
Shopkeeper(const s_Enemy& u_Enemy, const std::shared_ptr<MapData>& mapData);

void Move() override;

void Update() override;

private:
void UpdateFace(const glm::vec2& direction);

const glm::vec2 m_FrameSize = {47, 38};

std::random_device m_RandomDevice;
std::mt19937 m_RandomGenerator;
std::uniform_int_distribution<std::size_t> m_Distribution;
std::size_t m_StartIdx = 0;

std::vector<glm::vec2> m_Movement = {BOTTOM, RIGHT, TOP, LEFT};
};
} // namespace Dungeon::Enemies

Expand Down
25 changes: 20 additions & 5 deletions include/Dungeon/Enemy.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#ifndef ENEMY_H
#define ENEMY_H

#include <memory>
#include <set>

#include "Event/Object.h"
#include "Util/GameObject.hpp"

#include <memory>
#include "Dungeon/EMfwd.h"
#include "Dungeon/Elements.h"
#include "Event/Event.h"
#include "Game/Player.h"
#include "Settings/Animation.h"
#include "Settings/SpriteSheet.hpp"
#include "Settings/Window.hpp"
#include "UGameElement.h"

namespace Dungeon {
// Abstract class
Expand Down Expand Up @@ -41,6 +43,9 @@ class Enemy : public Object, public Util::GameObject {
bool GetShadow() const { return m_Shadow; }
std::size_t GetDamage() const { return m_Damage; }
std::size_t GetHealth() const { return m_Health; }
std::size_t GetMaxHealth() const {
return 2 * m_HealthBar->GetChildren().size();
}
std::size_t GetCoin() const { return m_Coin; }
const glm::vec2& GetWillMovePosition() const { return m_WillMovePosition; }
bool GetVisible() const { return m_Visible; }
Expand Down Expand Up @@ -70,9 +75,16 @@ class Enemy : public Object, public Util::GameObject {

virtual void UpdateHeart(const glm::vec2& pixelPos);

std::shared_ptr<Util::Image> m_FullHeart;
std::shared_ptr<Util::Image> m_EmptyHeart;
std::vector<Util::GameElement*> m_HeartList;
void ChangeHealthBar(const std::size_t health);

// Return the set of directions that the enemy can move to
std::set<Player::Direction> GetRelativeDirectionSet(
const glm::vec2& direction
);

std::shared_ptr<Util::Image> m_FullHeart;
std::shared_ptr<Util::Image> m_EmptyHeart;
std::shared_ptr<Util::GameObject> m_HealthBar;

std::shared_ptr<MapData> m_MapData;

Expand All @@ -84,6 +96,9 @@ class Enemy : public Object, public Util::GameObject {
std::unique_ptr<Animation> m_Animation;

std::size_t m_AnimationType = 0;
bool m_KnockbackAble = false;
bool m_Dead = false;
bool m_UnnecssaryAnimation = false;

private:
glm::vec2 m_GamePosition;
Expand Down
3 changes: 3 additions & 0 deletions include/Dungeon/EnemyFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Dungeon/Enemies/BlueSlime.h"
#include "Dungeon/Enemies/Ghost.h"
#include "Dungeon/Enemies/GreenSlime.h"
#include "Dungeon/Enemies/Monkey.h"
#include "Dungeon/Enemies/OrangeSlime.h"
#include "Dungeon/Enemies/Shopkeeper.h"
#include "Dungeon/Enemies/Skeleton.h"
Expand All @@ -29,6 +30,8 @@ class EnemyFactory final {
case 6: return std::make_shared<Enemies::Bat>(u_Enemy, mapData);
case 7: return std::make_shared<Enemies::Bat>(u_Enemy, mapData);
case 8: return std::make_shared<Enemies::Bat>(u_Enemy, mapData);
case 9: return std::make_shared<Enemies::Monkey>(u_Enemy, mapData);
case 10: return std::make_shared<Enemies::Monkey>(u_Enemy, mapData);
case 11: return std::make_shared<Enemies::Ghost>(u_Enemy, mapData);
case 12: return std::make_shared<Enemies::Zombie>(u_Enemy, mapData);
case 600: return std::make_shared<Enemies::DragonGreen>(u_Enemy, mapData);
Expand Down
16 changes: 12 additions & 4 deletions include/Dungeon/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@
#include "Dungeon/MiniMap.h"
#include "Event/Event.h"
#include "Event/Object.h"
#include "Game/Player.h"
#include "Settings/Camera.h"
#include "Settings/GameElement.h"

namespace Dungeon {
class Map final : public Object, public Util::GameObject {
public:
Map(const std::shared_ptr<Camera> camera,
const std::shared_ptr<Player> player,
const std::string& path,
const std::size_t levelNum = 1);
~Map();

void InitEvent();

bool IsAvailable() { return m_Available; }
bool LoadLevel(const std::size_t levelNum);
bool IsAvailable() { return m_Available; }
bool LoadLevel(
const std::size_t levelNum,
const std::shared_ptr<Player> player
);
std::size_t GetLevelNum() const;

std::size_t GamePostion2MapIndex(const glm::ivec2& position) const;
Expand All @@ -48,7 +54,6 @@ class Map final : public Object, public Util::GameObject {
static glm::ivec2 GetLevelIndexMin() { return m_Level->GetLevelIndexMin(); }
static glm::ivec2 GetLevelIndexMax() { return m_Level->GetLevelIndexMax(); }

void PlayerMove(const glm::vec2& position);
void AddItem(const std::size_t position, const std::shared_ptr<Item> item);
void RemoveItem(const std::size_t position);

Expand Down Expand Up @@ -91,7 +96,6 @@ class Map final : public Object, public Util::GameObject {
std::shared_ptr<MapData> m_MapData; // Use map index to store MapDate
std::shared_ptr<Camera> m_Camera;

bool m_TempoAttack = false;
bool m_OverlayRed = false;
unsigned long m_OverlayRedTime = 0;

Expand All @@ -102,6 +106,10 @@ class Map final : public Object, public Util::GameObject {
std::size_t m_TempoIndex = 0;

Event::Remover m_Event;

std::shared_ptr<GameElement> m_TileHead = std::make_shared<GameElement>();
std::shared_ptr<GameElement> m_EnemyHead = std::make_shared<GameElement>();
std::shared_ptr<GameElement> m_ItemHead = std::make_shared<GameElement>();
};

} // namespace Dungeon
Expand Down
15 changes: 9 additions & 6 deletions include/Dungeon/MapData.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#ifndef MAP_DATA_H
#define MAP_DATA_H

#include <memory>
#include <vector>

#include "Dungeon/EMfwd.h"
#include "Dungeon/Item.h"
#include "Dungeon/Tile.h"
#include "Game/Player.h"

namespace Dungeon {
class MapData final {
public:
MapData(
const glm::vec2& levelIndexMin,
const glm::vec2& levelIndexMax,
const glm::vec2& size
const glm::vec2& levelIndexMin,
const glm::vec2& levelIndexMax,
const glm::vec2& size,
const std::shared_ptr<Player> player
);

// tile
Expand Down Expand Up @@ -49,12 +52,11 @@ class MapData final {
glm::ivec2 GetLevelIndexMax() const;
glm::ivec2 GetLevelIndexMin() const;
glm::ivec2 GetSize() const;
glm::vec2 GetPlayerPosition();
glm::vec2 GetPlayerPosition() const;

void SetLevelIndexMax(const glm::ivec2& levelIndexMax);
void SetLevelIndexMin(const glm::ivec2& levelIndexMin);
void SetSize(const glm::ivec2& size);
void SetPlayerPosition(const glm::vec2& playerPosition);

std::size_t GamePosition2MapIndex(const glm::ivec2& position) const;

Expand All @@ -72,6 +74,7 @@ class MapData final {
bool IsPositionPlayerAct(const glm::vec2& position) const;

float Heuristic(const glm::vec2& start, const glm::vec2& end);
std::shared_ptr<Player> GetPlayer() const;

private:
static bool EnemyCompare(
Expand All @@ -89,7 +92,7 @@ class MapData final {
std::vector<std::shared_ptr<Enemy>> m_EnemyQueue;
std::vector<std::shared_ptr<Item>> m_Items;

glm::vec2 m_PlayerPosition = {0.0f, 0.0f};
std::shared_ptr<Player> m_Player;
};
} // namespace Dungeon

Expand Down
7 changes: 7 additions & 0 deletions include/Event/EventArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ EVENTPP_MAKE_EVENT(
(std::size_t, GetDamage)
);

EVENTPP_MAKE_EVENT(
EnemyRemoveEventArgs,
EventArgs,
EventType::EnemyRemove,
(std::size_t, GetMapIndex)
);

#endif // EVENT_ARGS_H
1 change: 1 addition & 0 deletions include/Event/EventType.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum class EventType {
AttackPlayer,
PlayerMove,
EnemyMove,
EnemyRemove,
};

#endif // EVENT_TYPE_H
6 changes: 5 additions & 1 deletion include/Game/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ class Player : public Object {
std::shared_ptr<GameElement> GetGameElement();
[[nodiscard]]
glm::vec2 GetGamePosition() const;
glm::vec2 GetTranslation() const;
std::shared_ptr<GameElement> GetWindowElement();
Direction GetFaceTo() const;
Direction GetDirection() const;
float GetZIndex() const;
std::size_t GetHealth() const;

// set player's settings
Expand Down Expand Up @@ -102,7 +105,8 @@ class Player : public Object {
glm::vec2 BodySize;
std::shared_ptr<SpriteSheet> HeadImage;
std::shared_ptr<SpriteSheet> BodyImage;
Direction m_FaceTo = Direction::RIGHT;
Direction m_Direction = Direction::RIGHT;
Direction m_BeforeFaceTo = Direction::RIGHT;

// game object
std::shared_ptr<GameElement> m_Body;
Expand Down
2 changes: 2 additions & 0 deletions include/Settings/GameElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class GameElement : public Util::GameObject {
bool GetVisibility() const {
return m_Visible;
}

void ClearChildren() { m_Children.clear(); }
};

#endif // I_LOVE_PTSD_ROLE_H
5 changes: 2 additions & 3 deletions include/Settings/SpriteSheet.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SPRITESHEET_HPP
#define SPRITESHEET_HPP

#include "Util/Logger.hpp"
#include "pch.hpp"

#include <exception>
Expand Down Expand Up @@ -146,9 +147,7 @@ class SpriteSheet final : public Core::Drawable {
*/
void SetDrawRect(const SDL_Rect displayRect);

void SetFrames(const std::vector<std::size_t>& frames) {
m_Frames = frames;
}
void SetFrames(const std::vector<std::size_t>& frames);

void SetAlpha(const Uint8 alpha);

Expand Down
Loading

0 comments on commit 2a5c534

Please sign in to comment.