Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
57 changes: 57 additions & 0 deletions Space-Invaders/PlayerService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "../Header/PlayerService.h"
#include "../Header/ServiceLocator.h"

PlayerService::PlayerService()
{
game_window = nullptr;
}

PlayerService::~PlayerService() = default;

//init
void PlayerService::initialize()
{
game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow();
initializePlayerSprite();
}

//take our players input in update, then set the position.
//order is important here
void PlayerService::update()
{
processPlayerInput();
player_sprite.setPosition(getPlayerPosition());
}

void PlayerService::render()
{
game_window->draw(player_sprite);
}

void PlayerService::processPlayerInput()
{
// Handle keyboard input
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
move(-1.0f * getMoveSpeed());
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
move(1.0f * getMoveSpeed());
}
}

void PlayerService::initializePlayerSprite()
{
if (player_texture.loadFromFile(player_texture_path))
{
player_sprite.setTexture(player_texture);
}
}

void PlayerService::move(float offsetX) {
position.x += offsetX;
}

//helper functions
sf::Vector2f PlayerService::getPlayerPosition() { return position; }
int PlayerService::getMoveSpeed() { return movement_speed; }

23 changes: 23 additions & 0 deletions Space-Invaders/Space-Invaders.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,30 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="gameservice\gameservice\gameservice.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="source\EventService.cpp" />
<ClCompile Include="source\gamesevice.cpp" />
<ClCompile Include="source\GraphicService.cpp" />
<ClCompile Include="source\PLayerController.cpp" />
<ClCompile Include="source\PlayerModel.cpp" />
<ClCompile Include="source\PlayerService.cpp" />
<ClCompile Include="source\PLayerView.cpp" />
<ClCompile Include="source\ServiceLocator.cpp" />
<ClCompile Include="source\Service_Locator.cpp" />
<ClCompile Include="source\TimeService.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="headers\ServiceLocator.h" />
<ClInclude Include="header\EventService.h" />
<ClInclude Include="header\GameService.h" />
<ClInclude Include="header\GraphicService.h" />
<ClInclude Include="header\header.h" />
<ClInclude Include="header\PlayerController.h" />
<ClInclude Include="header\PlayerModel.h" />
<ClInclude Include="header\PlayerService.h" />
<ClInclude Include="header\PlayerView.h" />
<ClInclude Include="header\TimeService.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
65 changes: 65 additions & 0 deletions Space-Invaders/Space-Invaders.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,70 @@
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\gamesevice.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="gameservice\gameservice\gameservice.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\GraphicService.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\Service_Locator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\EventService.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\PlayerService.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\TimeService.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\ServiceLocator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\PlayerModel.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\PLayerView.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\PLayerController.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="headers\ServiceLocator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="header\header.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="header\GraphicService.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="header\GameService.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="header\EventService.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="header\PlayerService.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="header\TimeService.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="header\PlayerModel.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="header\PlayerView.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="header\PlayerController.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
39 changes: 39 additions & 0 deletions Space-Invaders/gameservice/gameservice/gameservice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

// ServiceLocator Class Summary: This class manages access to various services in the application.
// include relevant headers files

class ServiceLocator
{
private:
// Private Attributes:
// - event_service: Manages event-related functionalities.
// - graphic_service: Handles graphics-related tasks.
// ..........................
// ..........................

// Private Constructor and Destructor:

// Constructor for initializing the ServiceLocator.
ServiceLocator();

// Destructor for cleaning up resources upon object deletion.
~ServiceLocator();

// Private Methods:
void createServices(); // Creates instances of all services.
void clearAllServices(); // Deletes and deallocates memory for all services.

public:
// Public Methods:
static ServiceLocator* getInstance(); // Provides a method to access the unique ServiceLocator instance (object). We will discuss this later.

void initialize(); // Initializes the ServiceLocator.
void update(); // Updates all services.
void render(); // Renders using the services.

// Methods to Get Specific Services:
//EventService* getEventService(); // Retrieve the EventService instance
//GraphicService* getGraphicService(); // Retrieve the GraphicService instance

};
32 changes: 32 additions & 0 deletions Space-Invaders/header/EventService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>

class EventService
{
private:
sf::Event game_event; //event var
sf::RenderWindow* game_window; //ptr to our game window

bool isGameWindowOpen();
bool gameWindowWasClosed(); //for the condition we already had - the title bar cross.
bool hasQuitGame(); //for our new 'ESC' condition






public:
EventService();
~EventService();

void initialize();
void update();
void processEvents(); // while window is open we will check for events
bool pressedEscapeKey();
bool isKeyboardEvent();
bool pressedLeftKey();

bool pressedRightKey();
};
7 changes: 7 additions & 0 deletions Space-Invaders/header/GraphicService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class GraphicService {
public:
void initialize();

private:
static const int frame_rate = 60;
};
26 changes: 26 additions & 0 deletions Space-Invaders/header/PlayerController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#pragma once
#include <SFML/Graphics.hpp>
#include "../Player/PlayerModel.h"
#include "../Player/PlayerView.h"

class PlayerController
{
private:
PlayerView* player_view;
PlayerModel* player_model;

void processPlayerInput();
void moveLeft();
void moveRight();

public:
PlayerController();
~PlayerController();

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

sf::Vector2f getPlayerPosition();
};
43 changes: 43 additions & 0 deletions Space-Invaders/header/PlayerModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once
#include <SFML/Graphics.hpp>

enum class PlayerState //Our Enum
{
ALIVE,
DEAD,
// we will add more states later
};

class PlayerModel
{
private:
const sf::Vector2f initial_player_position = sf::Vector2f(500.f, 500.f);

sf::Vector2f player_position;
PlayerState player_state; //Declaration
int player_score;

public:
const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f);
const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f);

const float player_movement_speed = 200.0f;

PlayerModel();
~PlayerModel();

void initialize();
void reset();

sf::Vector2f getPlayerPosition();
void setPlayerPosition(sf::Vector2f position);

int getPlayerScore();
void setPlayerScore(int score);

//new getter and setter
PlayerState getPlayerState();
void setPlayerState(PlayerState state);


};
26 changes: 26 additions & 0 deletions Space-Invaders/header/PlayerService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#pragma once


#pragma once
#include <SFML/Graphics.hpp>
#include "../../Header/Player/PlayerController.h"
class PlayerService
{

private:

PlayerController* player_controller

public:

PlayerService();
~PlayerService();

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



};
29 changes: 29 additions & 0 deletions Space-Invaders/header/PlayerView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include "../../Header/Player/PlayerController.h"
#include <SFML/Graphics.hpp>

class PlayerView
{
private:

const sf::String player_texture_path = "assets/textures/player_ship.png";
const float player_sprite_width = 60.f;
const float player_sprite_height = 60.f;

sf::RenderWindow* game_window;

sf::Texture player_texture;
sf::Sprite player_sprite;

void initializePlayerSprite();
void scalePlayerSprite();
PlayerController* player_controller;
public:
PlayerView();
~PlayerView();

void initialize();
void update();
void render();
void initialize(PlayerController* controller);
};
Loading