Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 3 food spawning #19

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion Linked-List-Snake/Linked-List-Snake.vcxproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
Expand Down Expand Up @@ -134,9 +134,23 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="source\Element\ElementService.cpp" />
<ClCompile Include="source\Element\Obstacle.cpp" />
<ClCompile Include="source\Food\FoodItem.cpp" />
<ClCompile Include="source\Food\FoodService.cpp" />
<ClCompile Include="source\Level\LevelController.cpp" />
<ClCompile Include="source\Level\LevelModel.cpp" />
<ClCompile Include="source\Level\LevelService.cpp" />
<ClCompile Include="source\Level\LevelView.cpp" />
<ClCompile Include="source\LinkedList\SingleLinkedList.cpp" />
<ClCompile Include="source\Player\BodyPart.cpp" />
<ClCompile Include="source\Player\PlayerService.cpp" />
<ClCompile Include="source\Player\SnakeController.cpp" />
<ClCompile Include="source\Time\TimeService.cpp" />
<ClCompile Include="source\UI\Credits\CreditsScreenUIController.cpp" />
<ClCompile Include="source\UI\GameplayUI\GameplayUIController.cpp" />
<ClCompile Include="source\UI\Instructions\InstructionsScreenUIController.cpp" />
<ClCompile Include="source\UI\LevelSelection\LevelSelectionUIController.cpp" />
<ClCompile Include="source\UI\UIElement\AnimatedImageView.cpp" />
<ClCompile Include="source\UI\UIElement\ButtonView.cpp" />
<ClCompile Include="source\Global\Config.cpp" />
Expand All @@ -148,14 +162,35 @@
<ClCompile Include="source\Global\ServiceLocator.cpp" />
<ClCompile Include="source\Sound\SoundService.cpp" />
<ClCompile Include="source\UI\SplashScreen\SplashScreenUIController.cpp" />
<ClCompile Include="source\UI\UIElement\RectangleShapeView.cpp" />
<ClCompile Include="source\UI\UIElement\TextView.cpp" />
<ClCompile Include="source\UI\UIService.cpp" />
<ClCompile Include="source\UI\UIElement\UIView.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\Element\ElementData.h" />
<ClInclude Include="include\Element\ElementService.h" />
<ClInclude Include="include\Element\Obstacle.h" />
<ClInclude Include="include\Food\FoodItem.h" />
<ClInclude Include="include\Food\FoodService.h" />
<ClInclude Include="include\Food\FoodType.h" />
<ClInclude Include="include\Level\LevelController.h" />
<ClInclude Include="include\Level\LevelData.h" />
<ClInclude Include="include\Level\LevelModel.h" />
<ClInclude Include="include\Level\LevelNumber.h" />
<ClInclude Include="include\Level\LevelService.h" />
<ClInclude Include="include\Level\LevelView.h" />
<ClInclude Include="include\LinkedList\Node.h" />
<ClInclude Include="include\LinkedList\SingleLinkedList.h" />
<ClInclude Include="include\Player\BodyPart.h" />
<ClInclude Include="include\Player\Direction.h" />
<ClInclude Include="include\Player\PlayerService.h" />
<ClInclude Include="include\Player\SnakeController.h" />
<ClInclude Include="include\Time\TimeService.h" />
<ClInclude Include="include\UI\Credits\CreditsScreenUIController.h" />
<ClInclude Include="include\UI\GameplayUI\GameplayUIController.h" />
<ClInclude Include="include\UI\Instructions\InstructionsScreenUIController.h" />
<ClInclude Include="include\UI\LevelSelection\LevelSelectionUIController.h" />
<ClInclude Include="include\UI\UIElement\AnimatedImageView.h" />
<ClInclude Include="include\UI\UIElement\ButtonView.h" />
<ClInclude Include="include\Global\Config.h" />
Expand All @@ -168,6 +203,7 @@
<ClInclude Include="include\Global\ServiceLocator.h" />
<ClInclude Include="include\Sound\SoundService.h" />
<ClInclude Include="include\UI\SplashScreen\SplashScreenUIController.h" />
<ClInclude Include="include\UI\UIElement\RectangleShapeView.h" />
<ClInclude Include="include\UI\UIElement\TextView.h" />
<ClInclude Include="include\UI\UIService.h" />
<ClInclude Include="include\UI\UIElement\UIView.h" />
Expand Down
22 changes: 22 additions & 0 deletions Linked-List-Snake/include/Element/ElementData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <SFML/System/Vector2.hpp>

namespace Element
{
enum class ElementType
{
OBSTACLE,
};

struct ElementData
{
ElementData(ElementType type, sf::Vector2i pos)
{
element_type = type;
position = pos;
}

ElementType element_type;
sf::Vector2i position;
};
}
28 changes: 28 additions & 0 deletions Linked-List-Snake/include/Element/ElementService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <vector>
#include <SFML/System/Vector2.hpp>

namespace Element
{
class Obstacle;
struct ElementData;

class ElementService
{
private:
std::vector<Obstacle*> obstacle_list;

void spawnObstacle(sf::Vector2i position, float cell_width, float cell_height);

public:
ElementService();
~ElementService();

void initialize();
void update();
void render();

const void spawnElements(std::vector<ElementData>& element_data_list, float cell_width, float cell_height);
std::vector<sf::Vector2i> getElementsPositionList();
};
}
30 changes: 30 additions & 0 deletions Linked-List-Snake/include/Element/Obstacle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "UI/UIElement/ImageView.h"

namespace Element
{
class Obstacle
{
private:
UI::UIElement::ImageView* obstacle_image;

sf::Vector2i grid_position;

float cell_width;
float cell_height;

void initializeObstacleImage();
sf::Vector2f getObstacleImagePosition();

public:
Obstacle();
~Obstacle();

void initialize(sf::Vector2i grid_pos, float width, float height);
void update();
void render();

sf::Vector2i getObstaclePosition();
};
}
37 changes: 37 additions & 0 deletions Linked-List-Snake/include/Food/FoodItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "UI/UIElement/ImageView.h"

namespace Food
{
enum class FoodType;

class FoodItem
{
private:
UI::UIElement::ImageView* food_image;

sf::Vector2i grid_position;

float cell_width;
float cell_height;

FoodType food_type;

void initializeFoodImage();
sf::String getFoodTexturePath();
sf::Vector2f getFoodImagePosition();

public:
static const int number_of_foods = 8;

FoodItem();
~FoodItem();

void initialize(sf::Vector2i grid_pos, float width, float height, FoodType type);
void update();
void render();

FoodType getFoodType();
};
}
61 changes: 61 additions & 0 deletions Linked-List-Snake/include/Food/FoodService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once
#include <SFML/System/Vector2.hpp>
#include <random>
#include <vector>

namespace Food
{
enum class FoodType;
class FoodItem;

enum FoodSpawningStatus
{
ACTIVE,
IN_ACTIVE,
};

class FoodService
{
private:
const float spawn_duration = 4.f;

float elapsed_duration;

FoodSpawningStatus current_spawning_status;
FoodItem* current_food_item;

float cell_width;
float cell_height;

// To generate random values.
std::default_random_engine random_engine;

// To give random seed to generator.
std::random_device random_device;

FoodItem* createFood(sf::Vector2i position, FoodType type);
void spawnFood();

sf::Vector2i getValidSpawnPosition();
sf::Vector2i getRandomPosition();
FoodType getRandomFoodType();

bool isValidPosition(std::vector<sf::Vector2i> position_data, sf::Vector2i food_position);

void destroyFood();
void updateElapsedDuration();
void handleFoodSpawning();
void reset();

public:
FoodService();
~FoodService();

void initialize();
void update();
void render();

void startFoodSpawning();
void stopFoodSpawning();
};
}
16 changes: 16 additions & 0 deletions Linked-List-Snake/include/Food/FoodType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

namespace Food
{
enum class FoodType
{
APPLE,
MANGO,
ORANGE,
PIZZA,
BURGER,
CHEESE,
POISION,
ALCOHOL,
};
}
12 changes: 12 additions & 0 deletions Linked-List-Snake/include/Global/ServiceLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#include "Event/EventService.h"
#include "UI/UIService.h"
#include "Sound/SoundService.h"
#include "Level/LevelService.h"
#include "Element/ElementService.h"
#include "Time/TimeService.h"
#include "Player/PlayerService.h"
#include "Food/FoodService.h"

namespace Global
{
Expand All @@ -13,6 +17,10 @@ namespace Global
Event::EventService* event_service;
Graphics::GraphicService* graphic_service;
Sound::SoundService* sound_service;
Level::LevelService* level_service;
Element::ElementService* element_service;
Player::PlayerService* player_service;
Food::FoodService* food_service;
UI::UIService* ui_service;
Time::TimeService* time_service;

Expand All @@ -32,7 +40,11 @@ namespace Global
Event::EventService* getEventService();
Graphics::GraphicService* getGraphicService();
Sound::SoundService* getSoundService();
Level::LevelService* getLevelService();
Element::ElementService* getElementService();
Player::PlayerService* getPlayerService();
UI::UIService* getUIService();
Food::FoodService* getFoodService();
Time::TimeService* getTimeService();
void deleteServiceLocator();
};
Expand Down
28 changes: 28 additions & 0 deletions Linked-List-Snake/include/Level/LevelController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <vector>
#include "LevelModel.h"

namespace Level
{
class LevelView;

class LevelController
{
private:
LevelModel* level_model;
LevelView* level_view;

public:
LevelController();
~LevelController();

void initialize();
void update();
void render();

float getCellWidth();
float getCellHeight();

const std::vector<Element::ElementData>& getElementDataList(int level_to_load);
};
}
18 changes: 18 additions & 0 deletions Linked-List-Snake/include/Level/LevelData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include "Level/LevelService.h"
#include "Element/ElementData.h"

namespace Level
{
struct LevelData
{
LevelData(LevelNumber ind, std::vector<Element::ElementData>* data_list)
{
level_index = ind;
element_data_list = data_list;
}

LevelNumber level_index;
std::vector<Element::ElementData>* element_data_list;
};
}
Loading