Skip to content

Commit

Permalink
Stuff and sniper (unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matqyou committed Nov 19, 2024
1 parent 618bbd2 commit a38434f
Show file tree
Hide file tree
Showing 31 changed files with 306 additions and 147 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10)
project(TrialAndError)

# Set the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Include directories
Expand Down Expand Up @@ -97,7 +97,9 @@ set(PROJECT_FILES
"src/game/entities/Crate.cpp"
"src/game/entities/Crate.h"
"src/game/entities/Error.cpp"
"src/game/entities/Error.h" src/Menu.cpp src/Menu.h)
"src/game/entities/Error.h"
"src/game/weapons/projectile/WeaponSniper.cpp"
"src/game/weapons/projectile/WeaponSniper.h")

# Add executable
add_executable(${PROJECT_NAME} ${PROJECT_FILES})
Expand Down
Binary file added bin/assets/images/entities/items/Sniper.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 bin/assets/images/weapons/Sniper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 0 additions & 25 deletions src/Menu.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions src/Menu.h

This file was deleted.

27 changes: 25 additions & 2 deletions src/game/GameWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void GameWorld::RemovePlayer(Player *player)

Entity *GameWorld::AddEntity(Entity *entity)
{
EntityType Enttype = entity->GetEntityType();
EntityType Enttype = entity->GetType();
Entity *&FirstType = m_FirstType[Enttype];
Entity *&LastType = m_LastType[Enttype];

Expand Down Expand Up @@ -166,13 +166,26 @@ Entity *GameWorld::AddEntity(Entity *entity)
m_Last = entity;
}

if (entity->GetType() == ENTTYPE_CHARACTER || entity->GetType() == ENTTYPE_CRATE) {
if (!m_FirstShootable) {
m_FirstShootable = entity;
m_LastShootable = entity;
entity->m_PrevShootable = nullptr;
entity->m_NextShootable = nullptr;
} else {
m_LastShootable->m_NextShootable = entity;
entity->m_PrevShootable = m_LastShootable;
m_LastShootable = entity;
}
}

return entity;
}

// ::RemoveEntity() doesn't reset entities Previous and Next entity pointers
void GameWorld::RemoveEntity(Entity *entity)
{
EntityType Type = entity->GetEntityType();
EntityType Type = entity->GetType();
Entity *&FirstType = m_FirstType[Type];
Entity *&LastType = m_LastType[Type];

Expand All @@ -195,6 +208,16 @@ void GameWorld::RemoveEntity(Entity *entity)
m_First = entity->m_Next;
if (m_Last == entity)
m_Last = entity->m_Prev;

// Remove entity from list of shootable
if (entity->m_PrevShootable)
entity->m_PrevShootable->m_NextShootable = entity->m_NextShootable;
if (entity->m_NextShootable)
entity->m_NextShootable->m_PrevShootable = entity->m_PrevShootable;
if (m_FirstShootable == entity)
m_FirstShootable = entity->m_NextShootable;
if (m_LastShootable == entity)
m_LastShootable = entity->m_PrevShootable;
}

void GameWorld::DestroyPlayerByController(GameController *DeletedController) const
Expand Down
3 changes: 3 additions & 0 deletions src/game/GameWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GameWorld {
Player* m_FirstPlayer, * m_LastPlayer;
Entity* m_FirstType[NUM_ENTTYPES]{ }, * m_LastType[NUM_ENTTYPES]{ };
Entity* m_First, * m_Last;
Entity* m_FirstShootable, * m_LastShootable;
unsigned long long m_CurrentTick;

// Cool scrolling background cap
Expand Down Expand Up @@ -59,6 +60,8 @@ class GameWorld {
[[nodiscard]] unsigned int GetNextPlayerIndex() const;
[[nodiscard]] Entity* FirstEntity() const { return m_First; }
[[nodiscard]] Entity* LastEntity() const { return m_Last; }
[[nodiscard]] Entity* FirstShootable() const { return m_FirstShootable; }
[[nodiscard]] Entity* LastShootable() const { return m_LastShootable; }
[[nodiscard]] Entity* FirstEntityType(EntityType entity_type) const { return m_FirstType[entity_type]; }
[[nodiscard]] Entity* LastEntityType(EntityType entity_type) const { return m_LastType[entity_type]; }
[[nodiscard]] Character* FirstCharacter() const { return (Character*)(FirstEntityType(ENTTYPE_CHARACTER)); }
Expand Down
15 changes: 12 additions & 3 deletions src/game/entities/Crate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ Crate::Crate(GameWorld* world,
else if (RandomFloat <= 8 / 8.0f) { typeID = DANGEROUS_RECOIL; }
}

Crate::~Crate() = default;
Crate::~Crate()
{
Character *Char = m_World->FirstCharacter();
for (; Char; Char = (Character *)Char->NextType())
{
Hook *TargetHook = Char->GetHook();
if (TargetHook->m_GrabbedEntity == this)
TargetHook->Unhook();
}
}

void Crate::DamageCrate(double Damage) {
void Crate::Damage(double value) {
Sound* BoxHitSound = ms_BoxSound;
m_World->GameWindow()->Assets()->SoundHandler()->PlaySound(BoxHitSound);
m_Health -= Damage;
m_Health -= value;
if (m_Health < 10) m_Texture = &ms_TextureBreakingBox2;
else if (m_Health < 20) m_Texture = &ms_TextureBreakingBox1;
}
Expand Down
5 changes: 2 additions & 3 deletions src/game/entities/Crate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ enum DropType {
NUM_DROPS
};

class Crate : public Entity {
class Crate : public Entity, public virtual HasHealth {
protected:
DropType m_Type;
int typeID;
double m_Health;
Texture** m_Texture;

public:
Expand All @@ -39,7 +38,7 @@ class Crate : public Entity {
[[nodiscard]] DropType Type() const { return m_Type; }

// Manipulating
void DamageCrate(double Damage);
void Damage(double value) override;

// Ticking
void Tick() override;
Expand Down
30 changes: 24 additions & 6 deletions src/game/entities/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
//

#include "Entity.h"
#include <format>

void EntityCore::Accelerate(double x, double y) {
Vel.x += x;
Vel.y += y;
}

HasHealth::HasHealth() {

}

void HasHealth::Damage(double value) {
m_Health -= value;
}

void HasHealth::Heal(double value) {
m_Health += value;
}

Entity::Entity(GameWorld* world,
EntityFormFactor form_factor,
EntityType entity_type,
Expand All @@ -25,6 +38,8 @@ Entity::Entity(GameWorld* world,
m_NextType = nullptr;
m_Prev = nullptr;
m_Next = nullptr;
m_PrevShootable = nullptr;
m_NextShootable = nullptr;
m_EntityType = entity_type;
m_Alive = true;
m_Core = *m_pUnknownCore;
Expand All @@ -37,7 +52,6 @@ Entity::Entity(GameWorld* world,

m_World->AddEntity(this);
m_Core.sizeRatio = (m_Core.Size.x + m_Core.Size.y) / 4.0;

}

Entity::~Entity() {
Expand Down Expand Up @@ -83,13 +97,17 @@ void Entity::TickWalls() {
}
}

bool Entity::PointCollides(double x, double y) const {
bool Entity::PointCollides(const Vec2d& point) const {
double w2 = m_Core.Size.x / 2.0;
double h2 = m_Core.Size.y / 2.0;
return !(x < m_Core.Pos.x - w2 ||
x > m_Core.Pos.x + w2 ||
y < m_Core.Pos.y - h2 ||
y > m_Core.Pos.y + h2);
return !(point.x < m_Core.Pos.x - w2 ||
point.x > m_Core.Pos.x + w2 ||
point.y < m_Core.Pos.y - h2 ||
point.y > m_Core.Pos.y + h2);
}

Entity::operator const char*() const {
return std::format("Entity(%i)", (int)m_EntityType).c_str();
}

// Add some velocity to this characters
Expand Down
27 changes: 23 additions & 4 deletions src/game/entities/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

struct EntityCore {
Vec2d Pos, Size, Vel;
double BaseDamping{ }, sizeRatio;;
double BaseDamping{}, sizeRatio{};

// Manipulating
void Accelerate(double x, double y);
Expand All @@ -20,13 +20,31 @@ struct DirectionalEntityCore : public EntityCore {
DirectionalEntityCore() : EntityCore() { }
};

class HasHealth {
protected:
double m_Health, m_MaxHealth, m_LastHealth;

public:
HasHealth();

// Getting
[[nodiscard]] double Health() const { return m_Health; }
[[nodiscard]] double MaxHealth() const { return m_MaxHealth; }
[[nodiscard]] double LastHealth() const { return m_LastHealth; }

// Setting
virtual void Damage(double value);
virtual void Heal(double value);
};

class Entity {
protected:
friend class GameWorld;
friend class Hook;
GameWorld* m_World;
Entity* m_PrevType, * m_NextType;
Entity* m_Prev, * m_Next;
Entity* m_PrevShootable, * m_NextShootable;
EntityCore* m_pUnknownCore, * m_pLastUnknownCore;
EntityCore& m_Core, & m_LastCore;
EntityType m_EntityType;
Expand All @@ -49,7 +67,7 @@ class Entity {

// Getting
[[nodiscard]] GameWorld* World() const { return m_World; }
[[nodiscard]] EntityType GetEntityType() const { return m_EntityType; }
[[nodiscard]] EntityType GetType() const { return m_EntityType; }
[[nodiscard]] Entity* Next() const { return m_Next; }
[[nodiscard]] Entity* Prev() const { return m_Prev; }
[[nodiscard]] Entity* NextType() const { return m_NextType; }
Expand All @@ -59,7 +77,8 @@ class Entity {
[[nodiscard]] bool IsAlive() const { return m_Alive; }

// Generating
[[nodiscard]] bool PointCollides(double x, double y) const;
[[nodiscard]] bool PointCollides(const Vec2d& point) const;
[[nodiscard]] virtual operator const char*() const;

// Manipulating
void Accelerate(const Vec2d& direction);
Expand Down Expand Up @@ -89,4 +108,4 @@ class DirectionalEntity : public Entity {
// Getting
[[nodiscard]] DirectionalEntityCore& GetDirectionalCore() const { return m_DirectionalCore; }
[[nodiscard]] DirectionalEntityCore& GetLastDirectionalCore() const { return m_LastDirectionalCore; }
};
};
Loading

0 comments on commit a38434f

Please sign in to comment.