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
Jump to file
Failed to load files.
Loading
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; }

20 changes: 19 additions & 1 deletion Space-Invaders/Space-Invaders.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +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\gameservice.h" />
<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
58 changes: 56 additions & 2 deletions Space-Invaders/Space-Invaders.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,66 @@
<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\gameservice.h">
<ClInclude Include="headers\ServiceLocator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="headers\ServiceLocator.h">
<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>
Expand Down
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);
};
32 changes: 32 additions & 0 deletions Space-Invaders/header/TimeService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#pragma once
#include <chrono>

/*
// The TimeService class helps keep track of time in game and calculate delta time.
// Utilizes the <chrono> library to calculate delta time.
*/
class TimeService
{
private:

// A point in time which indicates the starting time of previous frame.
std::chrono::time_point<std::chrono::steady_clock> previous_time;
// No need to worry about the syntax of this variable too much right now
// It'll be explained in detail at a later time.

float delta_time; //to store the detla time

void updateDeltaTime(); // method to update time
float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time
void updatePreviousTime(); // finally update the current time to be previous time

public:

//lifecycle methods
void initialize();
void update();

//getter
float getDeltaTime();
};
30 changes: 30 additions & 0 deletions Space-Invaders/header/servicelocator.h
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@

#pragma once
#include "../header/GraphicService.h"
#include "../header/EventService.h"

class ServiceLocator
{
private:
// Private Attributes:
GraphicService* graphic_service;

// Private Constructor and Destructor:
ServiceLocator();
// Constructor for initializing the ServiceLocator.
~ServiceLocator(); // Destructor for cleaning up resources upon object deletion.

// 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).
void initialize(); // Initializes the ServiceLocator.
void update(); // Updates all services.
void render(); // Renders using the services.

// Methods to Get Specific Services:
GraphicService* getGraphicService();

};
Loading