Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7ed23d0
Added sceleton for Sound functionality
bartez33a Sep 20, 2023
70598c5
adapted CMakeLists.txt to sound functionality
bartez33a Sep 21, 2023
9a26461
Implemented class Sound
bartez33a Sep 21, 2023
0539b3c
Implemented tank shot sound
bartez33a Sep 27, 2023
b69d332
Merge pull request #1 from wrazik/main
bartez33a Sep 29, 2023
cd595cf
merged with branch Sound
bartez33a Oct 5, 2023
492d8ea
removed tank_shot.flac
bartez33a Oct 10, 2023
6462307
added Sounds to .gitattributes
bartez33a Oct 10, 2023
ab79367
corrections after review
bartez33a Oct 10, 2023
4367764
added res/Sounds/
bartez33a Oct 12, 2023
34379e6
Removed notes.md - these were supposed to be my local notes
bartez33a Oct 12, 2023
f74ae4a
Delete res/Sounds/tank_shot.flac
bartez33a Oct 12, 2023
9de4aa8
added default ctor to Sound class to make possible compilation of test
bartez33a Oct 12, 2023
4edb9c3
added tank_shot.flac
bartez33a Oct 12, 2023
b3507c5
corrected formatting in Board.cpp
bartez33a Oct 12, 2023
12914dd
Update macos_clang.yaml
bartez33a Oct 12, 2023
fca57e1
Update macos_clang.yaml
bartez33a Oct 12, 2023
93161fc
formatted TankTests.cpp
bartez33a Oct 12, 2023
20f180d
Merge pull request #4 from wrazik/main
bartez33a Oct 14, 2023
3b3ffcf
corrected main.cpp
bartez33a Oct 14, 2023
9eadc84
Merge pull request #5 from wrazik/main
bartez33a Oct 16, 2023
1cf7a47
Created skeleton for GameManager, MainMenu, added Notes
bartez33a Oct 18, 2023
3a8e3d0
Added more notes
bartez33a Oct 21, 2023
6786c7e
Created classes Button, MenuLevel
bartez33a Oct 26, 2023
510ca1a
- added MainMenu, Button and MenuLevel to CMakeLists
bartez33a Oct 26, 2023
aaaa309
- added SFML Notes
bartez33a Oct 26, 2023
f32aeb4
- Corrected skeleton of Button and MenuLevel
bartez33a Oct 26, 2023
1baaa1d
- Corrected implementation of skeleton
bartez33a Oct 26, 2023
510f697
- Added MainMenu to CMakeLists
bartez33a Oct 26, 2023
39e81d7
- Improvements
bartez33a Oct 26, 2023
b427c5b
Added MenuItem base class for Button class
bartez33a Nov 2, 2023
9ebe97e
Adapted to MenuItem class
bartez33a Nov 2, 2023
106b9cf
Renamed MainMenu.h to MainMenu.hpp
bartez33a Nov 2, 2023
aa48ccf
Added text to Button
bartez33a Nov 2, 2023
98edbb3
Improved implementation
bartez33a Nov 4, 2023
db368b8
merged with branch main_menu
bartez33a Nov 11, 2023
9b4665a
Delete Notes directory
bartez33a Nov 11, 2023
5fd491a
Merge pull request #6 from bartez33a/bartez33a-patch-1
bartez33a Nov 11, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/.DS_Store
.editorconfig
.vscode
.cache
build
Expand Down
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ add_library(
src/Trace.cpp
src/Players/DummyPlayer.cpp
src/Players/KeyboardPlayer.cpp
src/Sound.cpp)
src/Sound.cpp
src/MainMenu.cpp
src/GameManager.cpp
src/MenuLevel.cpp
src/Button.cpp
src/MenuItem.cpp
)

target_include_directories(tank_bot_fight_lib PRIVATE src)

Expand Down
44 changes: 15 additions & 29 deletions src/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
#include "Tank/TankFactory.hpp"
#include "TracesHandler.hpp"

Board::Board()
: mWindow(sf::VideoMode(WIDTH, HEIGHT), "TankBotFight"),
mBackground(mStore),
mTankExplodeSound("explosion.flac") {
Board::Board(sf::RenderWindow& window)
: mWindow{window}, mBackground(mStore), mTankExplodeSound{"explosion.flac"} {
constexpr float TANK_X = WIDTH / 2.0f;
constexpr float TANK_Y = 50.f;
constexpr float TANK2_X = WIDTH / 2.0f;
Expand All @@ -31,7 +29,6 @@ Board::Board()
void Board::register_missile(const Missle& missile) { mMissles.push_back(missile); }

void Board::draw() {
mWindow.clear();
mBackground.draw(mWindow);
if (mKeyboardPlayer) {
mKeyboardPlayer->draw(mWindow);
Expand All @@ -46,34 +43,23 @@ void Board::draw() {
animation.draw(mWindow);
};
display_speed();
mWindow.display();
}

void Board::run() {
while (mWindow.isOpen()) {
sf::Event event{};
while (mWindow.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
mWindow.close();
}
void Board::play(const sf::Event& event) {
if (mKeyboardPlayer) {
mKeyboardPlayer->handle_events(event);
}

if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
mWindow.close();
}
if (mKeyboardPlayer) {
mKeyboardPlayer->handle_events(event);
}
}
if (mDummyPlayer) {
mDummyPlayer->update();
}
if (mKeyboardPlayer) {
mKeyboardPlayer->update();
}
draw();
remove_missles();
update_players();
if (mDummyPlayer) {
mDummyPlayer->update();
}

if (mKeyboardPlayer) {
mKeyboardPlayer->update();
}
draw();
remove_missles();
update_players();
}

void Board::display_speed() {
Expand Down
6 changes: 3 additions & 3 deletions src/Board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#include "background/Background.hpp"
class Board {
public:
Board();
Board(sf::RenderWindow& window);

void register_missile(const Missle& missile);
void register_animation(const Animation& animation);
void run();
void play(const sf::Event& event);

private:
void remove_missles();
Expand All @@ -23,7 +23,7 @@ class Board {
void draw();

TextureStore mStore;
sf::RenderWindow mWindow;
sf::RenderWindow& mWindow;
Background mBackground;
std::unique_ptr<KeyboardPlayer> mKeyboardPlayer;
std::unique_ptr<DummyPlayer> mDummyPlayer;
Expand Down
54 changes: 54 additions & 0 deletions src/Button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "Button.hpp"

Button::Button(std::string text, sf::Vector2f top_left_corner, unsigned int width,
unsigned int height, void (*callback)(void), ButtonType button_type,
MenuLevel* next_level)
: mTextContent{text},
mCallback{callback},
MenuItem{top_left_corner},
mWidth{width},
mHeight{height},
mButtonType{button_type},
mNextLevel{next_level} {}

void Button::draw(sf::RenderWindow& window, const sf::Font& font) {
// draw rectangle
sf::RectangleShape rectangle(sf::Vector2f(mWidth, mHeight));

// draw rectangle outline
if (mIsSelected) {
rectangle.setOutlineThickness(10.0f);
rectangle.setOutlineColor(sf::Color(255, 0, 0));
} else {
rectangle.setOutlineThickness(0.0f);
}

// set text
mText.setFont(font);
mText.setCharacterSize(24);
mText.setColor(sf::Color::Red);
mText.setString(mTextContent);

// move objects in window and draw
rectangle.setPosition(mPosition);
window.draw(rectangle);

// center text
mText.setOrigin(mText.getGlobalBounds().getSize() / 2.f + mText.getLocalBounds().getPosition());
mText.setPosition(rectangle.getPosition() + (rectangle.getSize() / 2.f));
window.draw(mText);
}

void Button::select() { mIsSelected = true; }

void Button::deselect() { mIsSelected = false; }

void Button::click() {
if (mButtonType == ButtonType::Callback && mIsSelected) {
mCallback();
}
}

ButtonType Button::get_button_type() const { return mButtonType; }

MenuLevel* Button::get_next_level() const { return mNextLevel; }
34 changes: 34 additions & 0 deletions src/Button.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <SFML/Graphics.hpp>
#include <string>
#include <vector>

#include "MenuItem.hpp"
#include "MenuLevel.hpp"

enum class ButtonType { Callback, LevelChanger };

class MenuLevel;

class Button : MenuItem {
public:
Button(std::string text, sf::Vector2f top_left_corner, unsigned int width, unsigned int height,
void (*callback)(void), ButtonType button_type, MenuLevel* next_level = nullptr);
void draw(sf::RenderWindow& window, const sf::Font& font);
void select();
void deselect();
void click();
ButtonType get_button_type() const;
MenuLevel* get_next_level() const;

private:
std::string mTextContent;
sf::Text mText;
unsigned int mWidth;
unsigned int mHeight;
void (*mCallback)(void);
bool mIsSelected = false;
MenuLevel* mNextLevel;
ButtonType mButtonType;
};
97 changes: 97 additions & 0 deletions src/GameManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "GameManager.hpp"

#include "Size.hpp"

// define static variables
bool GameManager::request_state_change = false;
GameManagerState GameManager::desired_state = GameManagerState::InvalidState;

// Button callbacks:
void main_menu_start_callback(void) {
GameManager::request_state_change = true;
GameManager::desired_state = GameManagerState::Started;
}
void main_menu_exit_callback(void) {
GameManager::request_state_change = true;
GameManager::desired_state = GameManagerState::Exit;
}

// define menus
extern MenuLevel options;

const unsigned int buttonWidth = WIDTH / 5;
const unsigned int buttonHeight = HEIGHT / 10;
constexpr unsigned int verticalSpacing = 20;

MenuLevel main_menu{
{{"Start", sf::Vector2f((WIDTH - buttonWidth) / 2, verticalSpacing + buttonHeight * 1),
buttonWidth, buttonHeight, main_menu_start_callback, ButtonType::Callback, nullptr},
{"Options", sf::Vector2f((WIDTH - buttonWidth) / 2, verticalSpacing + buttonHeight * 3),
buttonWidth, buttonHeight, nullptr, ButtonType::LevelChanger, &options},
{"Exit", sf::Vector2f((WIDTH - buttonWidth) / 2, verticalSpacing + buttonHeight * 5),
buttonWidth, buttonHeight, main_menu_exit_callback, ButtonType::Callback, nullptr}}};

MenuLevel options{{"Back", sf::Vector2f(WIDTH / 2 - buttonWidth, 2 * buttonHeight + 20),
buttonWidth, buttonHeight, nullptr, ButtonType::LevelChanger, &main_menu}};

GameManager::GameManager()
: mWindow{sf::VideoMode(WIDTH, HEIGHT), "TankBotFight"},
mBoard{mWindow},
mMainMenu{mWindow, {&main_menu, &options}, &main_menu} {}

void GameManager::start() {
while (mWindow.isOpen()) {
sf::Event event;
while (mWindow.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
mGameManagerState = GameManagerState::Exit;
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
mGameManagerState = GameManagerState::Exit;
}
}

// clear the window with black color
mWindow.clear(sf::Color::Black);

performStateMachine(event);

// end the current frame
mWindow.display();
}
}

void GameManager::performStateMachine(const sf::Event& event) {
switch (mGameManagerState) {
case GameManagerState::MainMenu:
mMainMenu.process_and_draw(event);
break;
case GameManagerState::Started:
mBoard.play(event);
break;
case GameManagerState::Exit:
mWindow.close();
break;
}

// check if state change is requested
if (request_state_change) {
request_state_change = false;
transitState();
}
}

void GameManager::transitState() {
switch (mGameManagerState) {
case GameManagerState::MainMenu:
if (desired_state == GameManagerState::Started) {
mGameManagerState = GameManagerState::Started;
} else if (desired_state == GameManagerState::Exit) {
mGameManagerState = GameManagerState::Exit;
}
break;
case GameManagerState::Started:
// TODO -> pause game
break;
}
}
34 changes: 34 additions & 0 deletions src/GameManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <SFML/Graphics.hpp>

#include "Board.hpp"
#include "MainMenu.hpp"

enum class GameManagerState {
MainMenu,
Started,
Exit,
InvalidState,
};

class GameManager {
public:
GameManager();
void start();
void performStateMachine(const sf::Event& event);
void transitState();

private:
// objects
GameManagerState mGameManagerState = GameManagerState::MainMenu;
sf::RenderWindow mWindow;
MainMenu mMainMenu;
Board mBoard;
static bool request_state_change;
static GameManagerState desired_state;

// friends: buttons' callbacks
friend void main_menu_start_callback(void);
friend void main_menu_exit_callback(void);
};
23 changes: 23 additions & 0 deletions src/MainMenu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "MainMenu.hpp"

#include "Files.hpp"

MainMenu::MainMenu(sf::RenderWindow& window, std::initializer_list<MenuLevel*> menu_level_ptrs,
MenuLevel* current_level)
: mWindow{window}, mMenuLevelPtrs{menu_level_ptrs}, mCurrentLevel{current_level} {
mFont.loadFromFile(files::asset_path() + "DejaVuSans.ttf");
}

void MainMenu::process_and_draw(const sf::Event& event) {
// check if menu should be changed
if (mCurrentLevel->is_level_change_requested()) {
mCurrentLevel->reset_level_request();
mCurrentLevel = mCurrentLevel->get_next_level();
}

// process inputs
mCurrentLevel->processEvents(event);

// draw menu
mCurrentLevel->draw(mWindow, mFont);
}
19 changes: 19 additions & 0 deletions src/MainMenu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>

#include "Button.hpp"
#include "MenuLevel.hpp"

class MainMenu {
public:
MainMenu(sf::RenderWindow& window, std::initializer_list<MenuLevel*> menu_level_ptrs,
MenuLevel* current_level);
void process_and_draw(const sf::Event& event);

private:
sf::RenderWindow& mWindow;
std::vector<MenuLevel*> mMenuLevelPtrs;
MenuLevel* mCurrentLevel = nullptr;
sf::Font mFont;
};
3 changes: 3 additions & 0 deletions src/MenuItem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "MenuItem.hpp"

MenuItem::MenuItem(sf::Vector2f position) : mPosition{position} {}
Loading