Skip to content

Commit

Permalink
🔀 Merge pull request #95 from 6im0n/78-create-centipede-game
Browse files Browse the repository at this point in the history
78 create centipede game
  • Loading branch information
Njord201 authored Apr 6, 2024
2 parents 11d3da1 + adf8c81 commit 9df78ff
Show file tree
Hide file tree
Showing 22 changed files with 1,484 additions and 95 deletions.
41 changes: 25 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#Program Name
NAME = arcade
NAME_PACMAN = lib/arcade_pacman.so
NAME_CENTIPEDE = lib/arcade_centipede.so
NAME_SNAKE = lib/arcade_snake.so
NAME_NCURSES = lib/arcade_ncurses.so
NAME_SFML = lib/arcade_sfml.so
Expand All @@ -24,13 +24,22 @@ SRC_CORE = core/main.cpp \
classes/Color.cpp \
classes/Text.cpp \

PACMAN_SRC = src/pacman/pacman.cpp \
CENTIPEDE_SRC = src/centipede/Entities/Void.cpp \
src/centipede/Entities/Wall.cpp \
src/centipede/Entities/SnakeBody.cpp \
src/centipede/Entities/Player.cpp \
src/centipede/Entities/Bullet.cpp \
src/centipede/Score.cpp \
src/centipede/CentipedeGame.cpp \
src/centipede/Snake.cpp \
classes/Timer.cpp \
classes/Color.cpp \

SNAKE_SRC = src/snake/Entities/Void.cpp \
src/snake/Entities/Wall.cpp \
src/snake/Entities/SnakeBody.cpp \
src/snake/Entities/Food.cpp \
src/snake/Score.cpp \
src/snake/Score.cpp \
src/snake/SnakeGame.cpp \
src/snake/Snake.cpp \
classes/Timer.cpp \
Expand Down Expand Up @@ -65,7 +74,7 @@ SRC_TEST = tests/tests_color.cpp \

#Objects
OBJ_CORE = $(SRC_CORE:.cpp=.o)
OBJ_PACMAN = $(PACMAN_SRC:.cpp=.o)
OBJ_CENTIPEDE = $(CENTIPEDE_SRC:.cpp=.o)
OBJ_SNAKE = $(SNAKE_SRC:.cpp=.o)
OBJ_NCURSES = $(NCURSES_SRC:.cpp=.o)
OBJ_SFML = $(SFML_SRC:.cpp=.o)
Expand All @@ -75,23 +84,23 @@ OBJ_VULKAN = $(VULKAN_SRC:.cpp=.o)

#GCDA & GCNO
GCDA_CORE = $(SRC_CORE:.cpp=.gcda)
GCDA_PACMAN = $(PACMAN_SRC:.cpp=.gcda)
GCDA_CENTIPEDE = $(CENTIPEDE_SRC:.cpp=.gcda)
GCDA_SNAKE = $(SNAKE_SRC:.cpp=.gcda)
GCDA_NCURSES = $(NCURSES_SRC:.cpp=.gcda)
GCDA_SFML = $(SFML_SRC:.cpp=.gcda)
GCDA_SDL = $(SDL_SRC:.cpp=.gcda)
GCDA_VULKAN = $(VULKAN_SRC:.cpp=.gcda)
GCDA_TEST = $(SRC_TEST:.cpp=.gcda)
GCDA_FILES = $(GCDA_CORE) $(GCDA_PACMAN) $(GCDA_SNAKE) $(GCDA_NCURSES) $(GCDA_SFML) $(GCDA_SDL) $(GCDA_TEST)
GCDA_FILES = $(GCDA_CORE) $(GCDA_CENTIPEDE) $(GCDA_SNAKE) $(GCDA_NCURSES) $(GCDA_SFML) $(GCDA_SDL) $(GCDA_TEST)
GCNO_CORE = $(SRC_CORE:.cpp=.gcno)
GCNO_PACMAN = $(PACMAN_SRC:.cpp=.gcno)
GCNO_CENTIPEDE = $(CENTIPEDE_SRC:.cpp=.gcno)
GCNO_SNAKE = $(SNAKE_SRC:.cpp=.gcno)
GCNO_NCURSES = $(NCURSES_SRC:.cpp=.gcno)
GCNO_SFML = $(SFML_SRC:.cpp=.gcno)
GCNO_SDL = $(SDL_SRC:.cpp=.gcno)
GCNO_VULKAN = $(VULKAN_SRC:.cpp=.gcno)
GCNO_TEST = $(SRC_TEST:.cpp=.gcno)
GCNO_FILES = $(GCNO_CORE) $(GCNO_PACMAN) $(GCNO_SNAKE) $(GCNO_NCURSES) $(GCNO_SFML) $(GCNO_SDL) $(GCNO_TEST)
GCNO_FILES = $(GCNO_CORE) $(GCNO_CENTIPEDE) $(GCNO_SNAKE) $(GCNO_NCURSES) $(GCNO_SFML) $(GCNO_SDL) $(GCNO_TEST)

#flags
CXXFLAGS = -g -fno-gnu-unique -Wall -Wextra -Werror -std=c++20
Expand All @@ -115,15 +124,15 @@ all: core games graphicals

#-----------------Games Rules--------------------

games: $(NAME_PACMAN) $(NAME_SNAKE)
games: $(NAME_CENTIPEDE) $(NAME_SNAKE)

%.o: %.cpp
@$(CC) $(INC) $(CXXFLAGS) -fPIC -c -o $@ $< && \
$(call YELLOW,"🆗 $<") || \
$(call YELLOW,"❌ $<")

$(NAME_PACMAN) : $(OBJ_PACMAN)
@$(LINKER) -shared -fPIC -o $(NAME_PACMAN) $(OBJ_PACMAN) $(CXXFLAGS) && \
$(NAME_CENTIPEDE) : $(OBJ_CENTIPEDE)
@$(LINKER) -shared -o $(NAME_CENTIPEDE) $(OBJ_CENTIPEDE) $(CXXFLAGS) && \
$(call YELLOW,"✅ $@") || \
$(call YELLOW,"❌ $@")

Expand Down Expand Up @@ -172,7 +181,7 @@ $(NAME_SDL) : $(OBJ_SDL)

clean:
@rm -f $(OBJ_CORE)
@rm -f $(OBJ_PACMAN)
@rm -f $(OBJ_CENTIPEDE)
@rm -f $(OBJ_SNAKE)
@rm -f $(OBJ_NCURSES)
@rm -f $(OBJ_SFML)
Expand All @@ -181,7 +190,7 @@ clean:

fclean: clean
@rm -f $(NAME)
@rm -f $(NAME_PACMAN)
@rm -f $(NAME_CENTIPEDE)
@rm -f $(NAME_SNAKE)
@rm -f $(NAME_NCURSES)
@rm -f $(NAME_SFML)
Expand All @@ -197,13 +206,13 @@ tests_fclean:

re: fclean all

obj: $(OBJ_CORE) $(OBJ_PACMAN) $(OBJ_SNAKE) $(OBJ_NCURSES) $(OBJ_SFML) $(OBJ_SDL)
obj: $(OBJ_CORE) $(OBJ_CENTIPEDE) $(OBJ_SNAKE) $(OBJ_NCURSES) $(OBJ_SFML) $(OBJ_SDL)

test_obj: $(OBJ_TEST)
tests_obj: $(OBJ_TEST)

tests_run: fclean
$(MAKE) obj CXXFLAGS+=--coverage -lcriterion
$(MAKE) test_obj CXXFLAGS+=-lcriterion
$(MAKE) tests_obj CXXFLAGS+=-lcriterion
g++ -o unit_tests $(OBJ_TEST) $(CXXFLAGS) -lcriterion --coverage
./unit_tests
gcovr --exclude tests/
Expand Down
File renamed without changes.
226 changes: 226 additions & 0 deletions src/centipede/CentipedeGame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*
** EPITECH PROJECT, 2024
** Arcade
** File description:
** CentipedeGame
*/

#include "CentipedeGame.hpp"
#include "Entities/Wall.hpp"
#include "Entities/Void.hpp"
#include "Entities/Bullet.hpp"
#include "includes/Keys.hpp"
#include "includes/Direction.hpp"

#include <algorithm>
#include <cmath>

#include <iostream>

void generateWallLine(std::vector<std::shared_ptr<Arcade::IEntity>> &line, int y, int size)
{
for (int i = 0; i < size; i++) {
std::shared_ptr<Arcade::Wall> wall = std::make_shared<Arcade::Wall>(GET_POSXY_AREA(i, y));
line.push_back(wall);
}
}

void generateLine(std::vector<std::shared_ptr<Arcade::IEntity>> &line, int y, int size)
{
std::shared_ptr<Arcade::Wall> wall = std::make_shared<Arcade::Wall>(GET_POSXY_AREA(0, y));
line.push_back(wall);
for (int i = 1; i < size - 1; i++) {
std::shared_ptr<Arcade::Void> void_case = std::make_shared<Arcade::Void>(GET_POSXY_AREA(i, y));
line.push_back(void_case);
}
std::shared_ptr<Arcade::Wall> wall2 = std::make_shared<Arcade::Wall>(GET_POSXY_AREA(size - 1, y));
line.push_back(wall2);
}

void generateMap(std::vector<std::vector<std::shared_ptr<Arcade::IEntity>>> &map)
{
for (int i = 0; i < AREA_GAME_HEIGHT; i++) {
std::vector<std::shared_ptr<Arcade::IEntity>> line;
map.push_back(line);
}
generateWallLine(map[0], 0, AREA_GAME_WIDTH);
for (int i = 1; i < AREA_GAME_HEIGHT; i++) {
generateLine(map[i], i, AREA_GAME_WIDTH);
}
generateWallLine(map[AREA_GAME_HEIGHT - 1], AREA_GAME_HEIGHT, AREA_GAME_WIDTH);
}

Arcade::CentipedeGame::CentipedeGame()
{
_score = std::make_shared<Score>();
_player = std::make_shared<Player>();
_timer = Timer();
_nbKills = 0;
srand(time(NULL));
}

int Arcade::CentipedeGame::startGame()
{
generateMap(_map);
_snakes.push_back(Snake(D_RIGHT, 9, {GET_POSXY_AREA(1, 1)}, false));
_score.get()->resetScore();
_timer.reset();
return 0;
}

int Arcade::CentipedeGame::stopGame()
{
_entities.clear();
_snakes.clear();
_map.clear();
_score.get()->resetScore();
_bullet = nullptr;
_nbKills = 0;
return 0;
}

int Arcade::CentipedeGame::getScore()
{
return _score.get()->getScore();
}

int Arcade::CentipedeGame::simulate()
{
_entities.clear();
for (auto &line : _map) {
for (auto &entity : line) {
_entities.push_back(entity);
}
}
if (_bullet != nullptr) {
_entities.push_back(_bullet);
auto resultMove = _bullet->moveBullet(_timer.getElapsedTime(), _snakes, _map);
if (resultMove[0] == 2) {
killSnake(_snakes[resultMove[3]], _bullet->getPos());
if (_nbKills == 20) {
return -1;
}
}
if (resultMove[0] == 3) {
Arcade::IEntity *mapElement = _map[_bullet->getPos()[POSY] - START_HEIGHT][_bullet->getPos()[POSX] - START_WIDTH].get();
Wall *wall = dynamic_cast<Wall *>(mapElement);
wall->hit();
if (wall->getLife() == 0) {
_map[_bullet->getPos()[POSY] - START_HEIGHT][_bullet->getPos()[POSX] - START_WIDTH] = std::make_shared<Arcade::Void>(_bullet->getPos()[POSX], _bullet->getPos()[POSY]);
}
}
if (resultMove[0] == 1 || resultMove[0] == 2 || resultMove[0] == 3) {
_bullet = nullptr;
}
}
for (std::size_t i = 0; i < _snakes.size(); i++) {
auto resultMove = _snakes[i].moveSnake(_map, *_player.get());
if (resultMove == -1) {
_snakes.erase(_snakes.begin() + i);
_score.get()->decrementScore(10);
} else if (resultMove == -2) {
return -1;
}
}
if (_snakes.empty()) {
_snakes.push_back(Snake(D_RIGHT, 9, {GET_POSXY_AREA(1, 1)}, false));
}
for (auto &snake : _snakes) {
for (auto snakeBody : snake.getSnake()) {
_entities.push_back(snakeBody);
}
}
_entities.push_back(_player);
return 0;
}

void Arcade::CentipedeGame::killSnake(Snake snakeKilled, std::vector<std::size_t> pos)
{
int id = snakeKilled.getId();
short size = 0;
std::vector<std::size_t> posSnake = {0, 0};
for (std::size_t i = 0; i < _snakes.size(); i++) {
if (_snakes[i] == snakeKilled) {
size = _snakes[i].getSnake().size();
posSnake = _snakes[i].getSnake().front()->getPos();
_snakes.erase(_snakes.begin() + i);
_score.get()->incrementScore();
break;
}
}
Direction dir = snakeKilled.getDirection();
Direction dirOpposite = D_RIGHT;
if (dir == D_RIGHT)
dirOpposite = D_LEFT;
_map[pos[POSY] - START_HEIGHT][pos[POSX] - START_WIDTH] = std::make_shared<Arcade::Wall>(pos[POSX], pos[POSY]);
if (size > 2) {
_snakes.push_back(Snake(dirOpposite, (std::size_t)floor(size / 2), {pos[POSX], pos[POSY]}, true, snakeKilled.getId()));
_snakes.push_back(Snake(dir, (std::size_t)floor(size / 2), {posSnake[POSX], posSnake[POSY]}, true, snakeKilled.getId()));
}
if (!isIdPresent(id)) {
_nbKills++;
}
}

void Arcade::CentipedeGame::catchKeyEvent(int key)
{
if (key == Arcade::Keys::LEFT) {
_player.get()->move(Direction::D_LEFT);
} else if (key == Arcade::Keys::RIGHT) {
_player.get()->move(Direction::D_RIGHT);
} else if (key == Arcade::Keys::UP) {
_player.get()->move(Direction::D_UP);
} else if (key == Arcade::Keys::DOWN) {
_player.get()->move(Direction::D_DOWN);
} else if (key == Arcade::Keys::SPACE) {
_player.get()->shoot(_bullet, _timer);
}
}

void Arcade::CentipedeGame::catchMousePosition(int x, int y)
{
(void)x;
(void)y;
}

std::vector<std::shared_ptr<Arcade::IEntity>> Arcade::CentipedeGame::getEntities()
{
return _entities;
}

std::vector<std::shared_ptr<Arcade::IText>> Arcade::CentipedeGame::getTexts()
{
_texts.clear();
_texts.push_back(_score);
return _texts;
}

std::vector<std::shared_ptr<Arcade::ISound>> Arcade::CentipedeGame::getSounds()
{
return _sounds;
}

bool Arcade::CentipedeGame::isIdPresent(int id)
{
auto it = std::find_if(_snakes.begin(), _snakes.end(),
[id](const Snake& snake) {
return snake.getId() == id;
});
return it != _snakes.end();
}

extern "C"
{
__attribute__((constructor))
void constructor()
{
}
__attribute__((destructor))
void destructor()
{
}
Arcade::CentipedeGame *loadGameInstance()
{
return new Arcade::CentipedeGame();
}
}
Loading

0 comments on commit 9df78ff

Please sign in to comment.