Skip to content

Commit

Permalink
merge: pull request #25 from G-Epitech/rc-snake-game
Browse files Browse the repository at this point in the history
feat(games): add snake game
  • Loading branch information
TekMath authored Apr 3, 2024
2 parents e868873 + 4a8b3b2 commit 685dafc
Show file tree
Hide file tree
Showing 56 changed files with 1,718 additions and 99 deletions.
1 change: 1 addition & 0 deletions assets/snake/apple.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
O
Binary file added assets/snake/apple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/snake/head.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@@@@
Binary file added assets/snake/head.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/snake/tail.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
║═
~~~~
╝╚╗╔
Binary file added assets/snake/tail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/snake/wall.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#
Binary file added assets/snake/wall.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: 1 addition & 1 deletion core/src/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void Core::run()
this->_initWindow();
while (this->_window->isOpen()) {
auto currentTime = std::chrono::high_resolution_clock::now();
auto deltaTime = std::chrono::duration_cast<std::chrono::milliseconds>(previousTime - currentTime);
auto deltaTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - previousTime);
previousTime = currentTime;

this->_game->compute(deltaTime);
Expand Down
5 changes: 4 additions & 1 deletion core/src/loader/Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

Loader::Loader() {}

Loader::~Loader() {}
Loader::~Loader() {
this->_gamesLibraries.clear();
this->_graphicsLibraries.clear();
}

shared::types::LibraryType Loader::_getLibraryGetter(const std::string &filepath, std::shared_ptr<DLLoader> dlLoader) {
shared::types::LibraryTypeGetter getter = nullptr;
Expand Down
17 changes: 17 additions & 0 deletions games/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
target_sources(${PROJECT_NAME} PUBLIC
game/AGame.cpp
game/AGame.hpp
entity/AEntity.cpp
entity/AEntity.hpp
components/AComponent.cpp
components/AComponent.hpp
components/PositionableComponent.cpp
components/PositionableComponent.hpp
components/ADisplayableComponent.cpp
components/ADisplayableComponent.hpp
components/CollidableComponent.hpp
components/CollidableComponent.cpp
components/TextureComponent.hpp
components/TextureComponent.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..)
24 changes: 24 additions & 0 deletions games/common/components/AComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
** EPITECH PROJECT, 2024
** AComponent.cpp
** File description:
** AComponent class
*/

#include <iostream>
#include "AComponent.hpp"

using namespace arcade::games::common::components;

AComponent::AComponent(shared::games::components::ComponentType type, shared::games::entity::IEntity &entity) : _parent(
entity) {
this->_type = type;
}

const shared::games::components::ComponentType AComponent::getType() const noexcept {
return this->_type;
}

const shared::games::entity::IEntity &AComponent::getEntity() noexcept {
return this->_parent;
}
44 changes: 44 additions & 0 deletions games/common/components/AComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
** EPITECH PROJECT, 2024
** AComponent.hpp
** File description:
** AComponent class
*/

#pragma once

#include "shared/games/components/IComponent.hpp"

namespace arcade::games::common::components {
class AComponent;
}

class arcade::games::common::components::AComponent : public virtual shared::games::components::IComponent {
public:
~AComponent() override = default;

/**
* @brief Get the type of the component
*
* @return Type of the component
*/
const shared::games::components::ComponentType getType() const noexcept override;

/**
* @brief Get the parent entity of the component
*
* @return Entity of the component
*/
const shared::games::entity::IEntity &getEntity() noexcept override;

protected:
/**
* @brief Create a component
*
* @param entity Entity parent of the component
*/
explicit AComponent(shared::games::components::ComponentType type, shared::games::entity::IEntity &entity);

shared::games::entity::IEntity &_parent;
shared::games::components::ComponentType _type;
};
30 changes: 30 additions & 0 deletions games/common/components/ADisplayableComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
** EPITECH PROJECT, 2024
** ADisplayableComponent.cpp
** File description:
** ADisplayableComponent class
*/

#include "ADisplayableComponent.hpp"

using namespace arcade::games::common::components;
using namespace shared::games;

ADisplayableComponent::ADisplayableComponent(entity::IEntity &entity, Vector2u size, unsigned int zindex)
: PositionableComponent(entity),
_size(size), _zindex(zindex) {
}

Vector2u &ADisplayableComponent::getSize() noexcept {
return this->_size;
}

unsigned int &ADisplayableComponent::getZIndex() noexcept {
return this->_zindex;
}

void ADisplayableComponent::onMousePress(std::shared_ptr<IGame> ctx) {}

void ADisplayableComponent::onMouseHover(std::shared_ptr<IGame> ctx) {}

void ADisplayableComponent::onMouseRelease(std::shared_ptr<IGame> ctx) {}
62 changes: 62 additions & 0 deletions games/common/components/ADisplayableComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
** EPITECH PROJECT, 2024
** ADisplayableComponent.hpp
** File description:
** ADisplayableComponent class
*/

#pragma once

#include "shared/games/components/IDisplayableComponent.hpp"
#include "PositionableComponent.hpp"

namespace arcade::games::common::components {
class ADisplayableComponent;
}

class arcade::games::common::components::ADisplayableComponent : public virtual shared::games::components::IDisplayableComponent, public PositionableComponent {
public:
~ADisplayableComponent() override = default;

/**
* @brief Get size of the entity (tiles)
*
*/
Vector2u &getSize() noexcept override;

/**
* @brief Get Z index that is usefull for display prioroty
*
*/
unsigned int &getZIndex() noexcept override;

/**
* @brief On click event handler for the entity
* @param ctx Context of the game
*/
void onMousePress(std::shared_ptr<shared::games::IGame> ctx) override;

/**
* @brief On release event handler for the entity
* @param ctx Context of the game
*/
void onMouseRelease(std::shared_ptr<shared::games::IGame> ctx) override;

/**
* @brief On hover event handler for the entity
* @param ctx Context of the game
*/
void onMouseHover(std::shared_ptr<shared::games::IGame> ctx) override;

protected:
/**
* Create a displayable component
* @param entity
* @param size
* @param zindex
*/
explicit ADisplayableComponent(shared::games::entity::IEntity &entity, Vector2u size, unsigned int zindex);

Vector2u _size;
unsigned int _zindex;
};
21 changes: 21 additions & 0 deletions games/common/components/CollidableComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
** EPITECH PROJECT, 2024
** CollidableComponent.cpp
** File description:
** CollidableComponent class
*/

#include "CollidableComponent.hpp"
#include <iostream>

using namespace arcade::games::common::components;

CollidableComponent::CollidableComponent(shared::games::entity::IEntity &entity, onCollideFunction function) : PositionableComponent(entity), _collideFunction(function) {
this->_type = shared::games::components::COLLIDABLE;
}

void CollidableComponent::onCollide(std::shared_ptr<shared::games::IGame> ctx,
std::shared_ptr<ICollidableComponent> target) {
if (this->_collideFunction)
this->_collideFunction(ctx, target);
}
38 changes: 38 additions & 0 deletions games/common/components/CollidableComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
** EPITECH PROJECT, 2024
** CollidableComponent.hpp
** File description:
** CollidableComponent class
*/

#pragma once

#include "shared/games/components/ICollidableComponent.hpp"
#include "PositionableComponent.hpp"

namespace arcade::games::common::components {
class CollidableComponent;
}

class arcade::games::common::components::CollidableComponent : public virtual shared::games::components::ICollidableComponent, public PositionableComponent {
public:
typedef void (*onCollideFunction)(std::shared_ptr<shared::games::IGame> ctx, std::shared_ptr<ICollidableComponent> target);

~CollidableComponent() override = default;

/**
* @brief Create a position component
* @param entity
*/
explicit CollidableComponent(shared::games::entity::IEntity &entity, onCollideFunction function);

/**
* @brief On collide event handler for the component
* @param ctx Context of the game
* @param target Target entity
*/
void onCollide(std::shared_ptr<shared::games::IGame> ctx, std::shared_ptr<ICollidableComponent> target) override;

protected:
onCollideFunction _collideFunction;
};
23 changes: 23 additions & 0 deletions games/common/components/PositionableComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2024
** PositionableComponent.cpp
** File description:
** PositionableComponent class
*/

#include "PositionableComponent.hpp"

using namespace arcade::games::common::components;
using namespace shared::games::components;

PositionableComponent::PositionableComponent(shared::games::entity::IEntity &entity) : AComponent(POSITIONABLE, entity),
_position(0, 0), _size(1, 1) {
}

shared::types::Vector2i &PositionableComponent::getPosition() noexcept {
return this->_position;
}

shared::types::Vector2u &PositionableComponent::getSize() noexcept {
return this->_size;
}
43 changes: 43 additions & 0 deletions games/common/components/PositionableComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
** EPITECH PROJECT, 2024
** PositionableComponent.hpp
** File description:
** PositionableComponent class
*/

#pragma once

#include "shared/games/components/IPositionableComponent.hpp"
#include "AComponent.hpp"

namespace arcade::games::common::components {
class PositionableComponent;
}

class arcade::games::common::components::PositionableComponent
: public AComponent, public virtual shared::games::components::IPositionableComponent {
public:
~PositionableComponent() override = default;

/**
* @brief Create a position component
* @param entity
*/
explicit PositionableComponent(shared::games::entity::IEntity &entity);

/**
* @brief Get position of the entity (tiles)
*
*/
shared::types::Vector2i &getPosition() noexcept override;

/**
* @brief Get size of the entity (tiles)
*
*/
shared::types::Vector2u &getSize() noexcept override;

protected:
shared::types::Vector2i _position;
shared::types::Vector2u _size;
};
22 changes: 22 additions & 0 deletions games/common/components/TextureComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
** EPITECH PROJECT, 2024
** TextureComponent.cpp
** File description:
** TextureComponent class
*/

#include "TextureComponent.hpp"

using namespace arcade::games::common::components;

TextureComponent::TextureComponent(shared::games::entity::IEntity &entity,
Vector2u size, unsigned int zindex,
shared::games::components::TextureProps &props)
: ADisplayableComponent(entity, size, zindex),
_textureProps(props) {
this->_type = shared::games::components::TEXTURE;
}

shared::games::components::TextureProps &TextureComponent::getTextureProps() noexcept {
return this->_textureProps;
}
Loading

0 comments on commit 685dafc

Please sign in to comment.