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

course project #28

Open
wants to merge 13 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
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ add_subdirectory ("lesson_22")
add_subdirectory ("lesson_23")
add_subdirectory ("lesson_26")
add_subdirectory ("lesson_27")

add_subdirectory ("battle_tanks/common_logic")
add_subdirectory ("battle_tanks/client")
add_subdirectory ("battle_tanks/server")
5 changes: 4 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++",
"CMAKE_TOOLCHAIN_FILE": "vcpkg_r_d_cpp_gamedev/scripts/buildsystems/vcpkg.cmake"
},
"condition": {
"type": "equals",
Expand Down
28 changes: 28 additions & 0 deletions battle_tanks/client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
project(battle_tanks_client)

file(GLOB_RECURSE SOURCES "./*.cpp")

include_directories(${PROJECT_SOURCE_DIR}/headers)

find_package(SFML COMPONENTS system window graphics CONFIG REQUIRED)

add_executable (${PROJECT_NAME} ${SOURCES})

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/game_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)
endif()

target_include_directories(${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../external/headers
${CMAKE_CURRENT_SOURCE_DIR}/../common_logic/headers
)

target_link_libraries(${PROJECT_NAME}
PRIVATE
bt-common
sfml-graphics
sfml-window
)
49 changes: 49 additions & 0 deletions battle_tanks/client/battle_tanks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <chrono>
#include <memory>

#include "SFML/System/Clock.hpp"
#include "SFML/Graphics.hpp"

#include "game/game_objects/player_game_object.hpp"
#include "game/sfml_game.hpp"
#include "network/player_actions.hpp"


int main()
{
auto window = std::make_shared<sf::RenderWindow>(
sf::VideoMode(
bt::game_entity_consts::game_field_size.x,
bt::game_entity_consts::game_field_size.y
),
"Battle Tanks"
);
//window->setFramerateLimit(60);

sf::Clock clock;

const auto game = std::make_unique<bt::sfml_game>(
window,
bt::physics_consts::pixels_per_meters
);

while (window->isOpen())
{
const float delta_time = clock.restart().asSeconds();
sf::Event event;
while (window->pollEvent(event))
{
if (event.type == sf::Event::Closed ||
(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::Escape))
{
window->close();
}
game->handle_event(event);
}

game->update(delta_time);
}

return 0;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
196 changes: 196 additions & 0 deletions battle_tanks/client/game_data/atlases/map_forest_tileset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
{
"id": 1,
"name": "forest tileset",
"atlas_path": "game_data/atlases/map_forest_tileset.png",
"textures": [
{
"id": 4,
"x": 1,
"y": 1,
"w": 16,
"h": 16
},
{
"id": 5,
"x": 18,
"y": 1,
"w": 16,
"h": 16
},
{
"id": 6,
"x": 35,
"y": 1,
"w": 16,
"h": 16
},
{
"id": 7,
"x": 1,
"y": 18,
"w": 16,
"h": 16
},
{
"id": 8,
"x": 18,
"y": 18,
"w": 16,
"h": 16
},
{
"id": 9,
"x": 35,
"y": 18,
"w": 16,
"h": 16
},
{
"id": 10,
"x": 1,
"y": 35,
"w": 16,
"h": 16
},
{
"id": 11,
"x": 18,
"y": 35,
"w": 16,
"h": 16
},
{
"id": 12,
"x": 35,
"y": 35,
"w": 16,
"h": 16
},
{
"id": 13,
"x": 52,
"y": 1,
"w": 16,
"h": 16
},
{
"id": 14,
"x": 69,
"y": 1,
"w": 16,
"h": 16
},
{
"id": 15,
"x": 86,
"y": 1,
"w": 16,
"h": 16
},
{
"id": 16,
"x": 52,
"y": 18,
"w": 16,
"h": 16
},
{
"id": 17,
"x": 69,
"y": 18,
"w": 16,
"h": 16
},
{
"id": 18,
"x": 86,
"y": 18,
"w": 16,
"h": 16
},
{
"id": 19,
"x": 52,
"y": 35,
"w": 16,
"h": 16
},
{
"id": 20,
"x": 69,
"y": 35,
"w": 16,
"h": 16
},
{
"id": 21,
"x": 86,
"y": 35,
"w": 16,
"h": 16
},
{
"id": 22,
"x": 1,
"y": 52,
"w": 16,
"h": 16
},
{
"id": 23,
"x": 18,
"y": 52,
"w": 16,
"h": 16
},
{
"id": 24,
"x": 35,
"y": 52,
"w": 16,
"h": 16
},
{
"id": 25,
"x": 1,
"y": 69,
"w": 16,
"h": 16
},
{
"id": 26,
"x": 18,
"y": 69,
"w": 16,
"h": 16
},
{
"id": 27,
"x": 35,
"y": 69,
"w": 16,
"h": 16
},
{
"id": 28,
"x": 1,
"y": 86,
"w": 16,
"h": 16
},
{
"id": 29,
"x": 18,
"y": 86,
"w": 16,
"h": 16
},
{
"id": 30,
"x": 35,
"y": 86,
"w": 16,
"h": 16
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added battle_tanks/client/game_data/atlases/rock_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added battle_tanks/client/game_data/atlases/tank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added battle_tanks/client/game_data/atlases/tower.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
6 changes: 6 additions & 0 deletions battle_tanks/client/game_data/screens/game_screen_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"hp_text_color_thresholds": {
"wounded": 50,
"dying": 25
}
}
53 changes: 53 additions & 0 deletions battle_tanks/client/headers/dev/debug_draw.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <memory>

#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Config.hpp>

#include <Box2D/Box2D.h>

class SFMLDebugDraw : public b2Draw
{
private:
std::shared_ptr<sf::RenderTarget> m_window;
//float m_pixels_per_meter;
static float pixels_per_meter;

public:
SFMLDebugDraw(const std::shared_ptr<sf::RenderTarget>& window, float pixels_per_meters);

/// Convert Box2D's OpenGL style color definition[0-1] to SFML's color definition[0-255], with optional alpha byte[Default - opaque]
static sf::Color GLColorToSFML(const b2Color& color, sf::Uint8 alpha = 255)
{
return sf::Color(static_cast<sf::Uint8>(color.r * 255), static_cast<sf::Uint8>(color.g * 255), static_cast<sf::Uint8>(color.b * 255), alpha);
}

/// Convert Box2D's vector to SFML vector [Default - scales the vector up by SCALE constants amount]
static sf::Vector2f B2VecToSFVec(const b2Vec2& vector, bool scaleToPixels = true)
{
return sf::Vector2f(vector.x * (scaleToPixels ? pixels_per_meter : 1.f), vector.y * (scaleToPixels ? pixels_per_meter : 1.f));
}

/// Draw a closed polygon provided in CCW order.
void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);

/// Draw a solid closed polygon provided in CCW order.
void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);

/// Draw a circle.
void DrawCircle(const b2Vec2& center, float radius, const b2Color& color);

/// Draw a solid circle.
void DrawSolidCircle(const b2Vec2& center, float radius, const b2Vec2& axis, const b2Color& color);

/// Draw a line segment.
void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);

/// Draw a transform. Choose your own length scale.
void DrawTransform(const b2Transform& xf);

void DrawPoint(const b2Vec2& p, float size, const b2Color& color) override;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <memory>

#include "game/game_objects/game_object.hpp"
#include "physics/physics_body_factory.hpp"
#include "renderer/textures.hpp"
#include "utils/uuid.hpp"


namespace bt
{
//TODO:: create object poll for such objects
class bullet_game_object : public bt::game_object
{
public:
bullet_game_object(
const bt::uuid id,
const std::weak_ptr<physics_body_factory>& ph_body_factory,
const std::shared_ptr<bt::texture_holder>& render_data
);

virtual ~bullet_game_object() override = default;

public:
virtual void restore_from_frame(const bt::game_object_frame_restorer& restorer) override;
virtual void update(float delta_time) override;

protected:
virtual void create_render_object() override;
virtual void create_game_object_entity() override;

private:

std::shared_ptr<bt::texture_holder> render_data_{ nullptr };
const std::weak_ptr<physics_body_factory> ph_body_factory_;
};
}
Loading