Skip to content

Commit

Permalink
omegalulfruit added
Browse files Browse the repository at this point in the history
  • Loading branch information
nydragon committed Apr 10, 2022
1 parent 9f29022 commit a9e2e8b
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/games/nibbler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SRC = src/GameCore.cpp \
src/Player.cpp \
src/Terrain.cpp \
src/Fruit.cpp \
src/MegaFruit.cpp \
src/Item.cpp \
src/make.cpp \
../../shared/mapParser.cpp \
Expand Down
4 changes: 2 additions & 2 deletions src/games/nibbler/src/Fruit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

#include "Fruit.hpp"
#include "Player.hpp"
#include "../include/NibblerMacros.hpp"

Nibbler::Fruit::Fruit(int x, int y, IGraphicsLib **gfx)
: Item(x, y, ITEM_FRUIT1, gfx)
{
}

void Nibbler::Fruit::effect(Player *nibbler)
void Nibbler::Fruit::effect(Player *nibbler, int *score)
{
nibbler->appendSegment();
*score = *score + 1;
}
2 changes: 1 addition & 1 deletion src/games/nibbler/src/Fruit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ namespace Nibbler
public:
Fruit(int x, int y, IGraphicsLib **gfx);

void effect(Player *nibbler) override;
void effect(Player *nibbler, int *score) override;
};
}
3 changes: 2 additions & 1 deletion src/games/nibbler/src/Item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <utility>
#include "../../../shared/IGraphicsLib.hpp"
#include "../include/NibblerMacros.hpp"

namespace Nibbler {
class Player;
Expand All @@ -24,7 +25,7 @@ namespace Nibbler {

bool itemCollision(int x, int y) const;

virtual void effect(Player *nibbler) = 0;
virtual void effect(Player *nibbler, int *score) = 0;

void relocate(int x, int y);

Expand Down
13 changes: 10 additions & 3 deletions src/games/nibbler/src/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


#include "Level.hpp"
#include "MegaFruit.hpp"

Nibbler::Level::Level(IGraphicsLib **gfx, size_t level)
{
Expand All @@ -28,7 +29,8 @@ Nibbler::Level::Level(IGraphicsLib **gfx, size_t level)

this->_state = LEVEL::RUNNING;

this->generateItems(ITEM_FRUIT1, this->_gameHeight * this->_gameWidth * 0.1);
this->generateItems(ITEM_FRUIT1, this->_gameHeight * this->_gameWidth * 0.05);
this->generateItems(ITEM_FRUIT2, this->_gameHeight * this->_gameWidth * 0.02);
}

void Nibbler::Level::draw()
Expand Down Expand Up @@ -83,10 +85,9 @@ void Nibbler::Level::checkItemCollision()

for (Item *item: this->_items) {
if (item->itemCollision(head.x, head.y)) {
item->effect(this->_nibbler);
item->effect(this->_nibbler, &this->_score);
std::pair<int, int> coords = this->randomLocation();
item->relocate(coords.first, coords.second);
this->_score++;
break;
}
}
Expand All @@ -101,6 +102,12 @@ void Nibbler::Level::generateItems(int item_id, size_t amount)
switch (item_id) {
case ITEM_FRUIT1:
item = new Fruit(coords.first, coords.second, this->_gfx);
break;
case ITEM_FRUIT2:
item = new MegaFruit(coords.first, coords.second, this->_gfx);
break;
default:
break;
}

this->_items.push_back(item);
Expand Down
20 changes: 20 additions & 0 deletions src/games/nibbler/src/MegaFruit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
** EPITECH PROJECT, 2022
** Project
** File description:
** Description
*/

#include "MegaFruit.hpp"
#include "Player.hpp"

Nibbler::MegaFruit::MegaFruit(int x, int y, IGraphicsLib **gfx)
: Item(x, y, ITEM_FRUIT2, gfx)
{
}

void Nibbler::MegaFruit::effect(Nibbler::Player *nibbler, int *score)
{
nibbler->popSegment();
*score = *score + 2;
}
23 changes: 23 additions & 0 deletions src/games/nibbler/src/MegaFruit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2022
** Project
** File description:
** Description
*/

#pragma once

#include "Item.hpp"

namespace Nibbler
{
class Player;

class MegaFruit : virtual public Item
{
public:
MegaFruit(int x, int y, IGraphicsLib **gfx);

void effect(Player *nibbler, int *score) override;
};
}
6 changes: 6 additions & 0 deletions src/games/nibbler/src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ void Nibbler::Player::appendSegment()
this->_body.push_back({.x = this->_pastTile.x, .y = this->_pastTile.y});
}

void Nibbler::Player::popSegment()
{
if (this->_body.size() > 1)
this->_body.pop_front();
}

void Nibbler::Player::turn(int xOffset, int yOffset)
{
if (!(xOffset == 0 || yOffset == 0)) {
Expand Down
7 changes: 6 additions & 1 deletion src/games/nibbler/src/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

namespace Nibbler
{
typedef struct {
typedef struct
{
int x;
int y;
} segment_t;
Expand All @@ -29,6 +30,8 @@ namespace Nibbler

void appendSegment();

void popSegment();

void turn(int xOffset, int yOffset);

int moveForward();
Expand All @@ -38,7 +41,9 @@ namespace Nibbler
segment_t tail();

int getTileOrientation(int bodyIndex) const;

int getCornerOrientation(int bodyIndex) const;

void draw();

bool nibblerCollision(int x, int y);
Expand Down

0 comments on commit a9e2e8b

Please sign in to comment.