From 41c80bc6f97783b8bcbef71b682fbc25c8d2c26c Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Thu, 9 Nov 2023 16:26:20 +0000 Subject: [PATCH 01/32] Setting up GitHub Classroom Feedback From dc08916899d0672b736bbfec88f0b072644fe875 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Wed, 15 Nov 2023 15:05:31 +0000 Subject: [PATCH 02/32] Adding degree function to mathlib and updating cmake --- CMakeLists.txt | 103 +++++++++++++++----- engine/engine.cpp | 2 +- lab_7_platformer/components/cmp_physics.cpp | 4 +- lib_maths/maths.h | 4 + 4 files changed, 84 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5305a1d..d42c6d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,55 +1,106 @@ -cmake_minimum_required(VERSION 3.21) -project(CMakeSFMLProject LANGUAGES CXX) +cmake_minimum_required(VERSION 3.9) +# Compiler flags +if (MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /EHsc" CACHE INTERNAL "" FORCE) +elseif(APPLE) + message("hello apple") + set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15 CACHE STRING "") +endif() +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +project(Platformer_for_wit) + +#### setup Directories #### +#Main output directory +set(OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/") +# Ouput all DLLs from all libs into main build folder +set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}) -include(FetchContent) -FetchContent_Declare(SFML - GIT_REPOSITORY https://github.com/SFML/SFML.git - GIT_TAG 2.6.x) -FetchContent_MakeAvailable(SFML) +add_custom_target(copy_resources ALL COMMAND ${CMAKE_COMMAND} + -E copy_directory + "${PROJECT_SOURCE_DIR}/res" + ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$/res +) +add_custom_target(copy_resources_rootdir ALL COMMAND ${CMAKE_COMMAND} + -E copy_directory + "${PROJECT_SOURCE_DIR}/res" + ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/res +) +#### Add External Dependencies #### +# SFML - graphics library +add_subdirectory("lib/sfml" EXCLUDE_FROM_ALL) +#include_directories("lib/sfml/include") +set(SFML_INCS "lib/sfml/include") +link_directories("${CMAKE_BINARY_DIR}/lib/sfml/lib") -# B2D - Box2D physics library -add_subdirectory("lib/b2d/Box2D") +# B2D - Box2D phyics library +add_subdirectory("lib/b2d/Box2D" EXCLUDE_FROM_ALL) #include_directories("lib/b2d/Box2D/") set(B2D_INCS "lib/b2d/Box2D/") link_directories("${CMAKE_BINARY_DIR}/lib/sfml/lib") - - #### Add Engine and our own library projects#### file(GLOB_RECURSE RESOURCE_FILES "res/*.*") -## Engine ## +#### file(GLOB_RECURSE SOURCE_FILES engine/*.cpp engine/*.h) add_library(lib_engine STATIC ${SOURCE_FILES} ${RESOURCE_FILES}) target_include_directories(lib_engine INTERFACE "${CMAKE_SOURCE_DIR}/engine/" PUBLIC SYSTEM ${B2D_INCS} ) target_link_libraries(lib_engine lib_tile_level_loader lib_maths lib_ecm Box2D sfml-graphics sfml-audio) +## Maths lib +add_library(lib_maths INTERFACE) +target_sources(lib_maths INTERFACE "${CMAKE_SOURCE_DIR}/lib_maths/maths.h") +target_include_directories(lib_maths INTERFACE "${CMAKE_SOURCE_DIR}/lib_maths" INTERFACE ${SFML_INCS}) + ## Tile loader lib file(GLOB_RECURSE SOURCE_FILES lib_tile_level_loader/*.cpp lib_tile_level_loader/*.h) -add_library(lib_tile_level_loader STATIC ${SOURCE_FILES}) +add_library(lib_tile_level_loader STATIC ${SOURCE_FILES} ${RESOURCE_FILES}) target_include_directories(lib_tile_level_loader INTERFACE "${CMAKE_SOURCE_DIR}/lib_tile_level_loader/" ) target_link_libraries(lib_tile_level_loader lib_maths sfml-graphics) -# Maths lib -add_library(lib_maths INTERFACE) -target_sources(lib_maths INTERFACE "${CMAKE_SOURCE_DIR}/lib_maths/maths.h") -target_include_directories(lib_maths INTERFACE "${CMAKE_SOURCE_DIR}/lib_maths" SYSTEM INTERFACE ${SFML_INCS}) - ## ECM lib file(GLOB_RECURSE SOURCE_FILES lib_ecm/*.cpp lib_ecm/*.h) add_library(lib_ecm STATIC ${SOURCE_FILES}) target_include_directories(lib_ecm INTERFACE "${CMAKE_SOURCE_DIR}/lib_ecm" ) target_link_libraries(lib_ecm PRIVATE lib_maths) +#### Add Practical Projects#### +#@@- -add_executable(CMakeSFMLProject src/main.cpp src/platformer.cpp) -target_link_libraries(CMakeSFMLProject PRIVATE sfml-graphics) -target_compile_features(CMakeSFMLProject PRIVATE cxx_std_17) -if (WIN32 AND BUILD_SHARED_LIBS) - add_custom_command(TARGET CMakeSFMLProject POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy $ $ COMMAND_EXPAND_LISTS) -endif() +## 6 - Platformer +file(GLOB_RECURSE SOURCES lab_7_platformer/*.cpp practical_6_platformer/*.h) +file(GLOB_RECURSE CMPNTS lab_7_platformer/components/*.cpp practical_6_platformer/components/*.h) +file(GLOB_RECURSE SCENES lab_7_platformer/scenes/*.cpp practical_6_platformer/scenes/*.h) +add_executable(PRACTICAL_7_PLATFORMER ${SOURCES} ${RESOURCE_FILES}) +source_group("components" FILES ${CMPNTS}) +source_group("resources" FILES ${RESOURCE_FILES}) +#target_include_directories(PRACTICAL_6_PLATFORMER SYSTEM PRIVATE ${SFML_INCS} ${B2D_INCS}) +target_link_libraries(PRACTICAL_7_PLATFORMER lib_engine) +set(EXECUTABLES ${EXECUTABLES} PRACTICAL_7_PLATFORMER) + +add_dependencies(PRACTICAL_7_PLATFORMER copy_resources copy_resources_rootdir) + + +set_target_properties(PRACTICAL_7_PLATFORMER + PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY + ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$(Configuration) +) + + + + + +#file(GLOB_RECURSE SOURCES lab_8_steering/*.cpp lab_8_steering/*.h) +#file(GLOB_RECURSE CMPNTS lab_8_steering/components/*.cpp lab_8_steering/components/*.h) +#file(GLOB_RECURSE SCENES lab_8_steering/scenes/*.cpp lab_8_steering/scenes/*.h) +#add_executable(LAB_8_STEERING ${SOURCES} ${CMPNTS} ${SCENES} ${RESOURCE_FILES} lab_8_steering/components/cmp_ai_steering.cpp lab_8_steering/components/cmp_ai_steering.h) +#target_link_libraries(LAB_8_STEERING lib_engine) +#set(EXECUTABLES ${EXECUTABLES} LAB_8_STEERING) + +#add_dependencies(LAB_8_STEERING copy_resources copy_resources_rootdir) -install(TARGETS CMakeSFMLProject) +#@@- \ No newline at end of file diff --git a/engine/engine.cpp b/engine/engine.cpp index 065ceac..2104b54 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -81,7 +81,7 @@ void Engine::Render(RenderWindow& window) { void Engine::Start(unsigned int width, unsigned int height, const std::string& gameName, Scene* scn) { - RenderWindow window(VideoMode(width, height), gameName); + RenderWindow window(VideoMode(Vector2u (width,height)), gameName); _gameName = gameName; _window = &window; Renderer::initialise(window); diff --git a/lab_7_platformer/components/cmp_physics.cpp b/lab_7_platformer/components/cmp_physics.cpp index 98978b3..269482c 100644 --- a/lab_7_platformer/components/cmp_physics.cpp +++ b/lab_7_platformer/components/cmp_physics.cpp @@ -130,8 +130,8 @@ bool PhysicsComponent::isTouching(const PhysicsComponent& pc, return false; } -std::vector PhysicsComponent::getTouching() const { - std::vector ret; +std::vector PhysicsComponent::getTouching() const { + std::vector ret; b2ContactEdge* edge = _body->GetContactList(); while (edge != NULL) { diff --git a/lib_maths/maths.h b/lib_maths/maths.h index 38be3c0..a34c5c3 100644 --- a/lib_maths/maths.h +++ b/lib_maths/maths.h @@ -47,6 +47,10 @@ template Vector2 Vcast(const Vector2& v) { return Vector2(static_cast(v.x), static_cast(v.y)); }; +static double degrees(double radians) { + return radians * 180.0 / (4.0 * atan(1.0)); +} + static double deg2rad(double degrees) { return degrees * 4.0 * atan(1.0) / 180.0; } From 2ba6c528a1150a71bbd6d9138ecd2d0fc1f44b89 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Mon, 20 Nov 2023 10:59:35 +0000 Subject: [PATCH 03/32] Update CMAKE - issue with rendering or player movement --- CMakeLists.txt | 107 +++++++++++++----- engine/engine.cpp | 2 +- lab_7_platformer/components/cmp_physics.cpp | 3 +- .../components/cmp_player_physics.cpp | 1 + lib_maths/maths.h | 8 +- 5 files changed, 87 insertions(+), 34 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d42c6d5..ed7fd7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,64 @@ -cmake_minimum_required(VERSION 3.9) +cmake_minimum_required(VERSION 3.21) # Compiler flags if (MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /EHsc" CACHE INTERNAL "" FORCE) elseif(APPLE) message("hello apple") - set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15 CACHE STRING "") + set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9 CACHE STRING "") endif() set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -project(Platformer_for_wit) +project(PRACTICAL_7_PLATFORMER) +include(FetchContent) + + +FetchContent_Declare( + sfml + GIT_REPOSITORY https://github.com/SFML/SFML.git + GIT_TAG 2.6.x +) +FetchContent_MakeAvailable(sfml) +# populate the lib directory with the sfml libraries +FetchContent_GetProperties(sfml) # can I get cmake to list out the names of the properties? A: yes, use the following line: get_cmake_property(_variableNames VARIABLES) +if(NOT sfml_POPULATED) + FetchContent_Populate(sfml) + add_subdirectory(${sfml_SOURCE_DIR} ${sfml_BINARY_DIR}) +endif() +get_cmake_property(_variableNames VARIABLES) +list(FILTER _variableNames INCLUDE REGEX "sfml") +message("variableNames: ${_variableNames}") + +foreach (_variableName ${_variableNames}) + message(STATUS "${_variableName}=${${_variableName}}") +endforeach() + +message ("SFML_SOURCE_DIR: ${sfml_SOURCE_DIR}") + +message ("SFML_SOURCE_DIR: ${sfml_SOURCE_DIR}") + +message("SFML_BINARY_DIR: ${sfml_BINARY_DIR}") + + +# The following downloads box2d into the build directory and builds it +FetchContent_Declare(box2d + GIT_REPOSITORY https://github.com/dooglz/Box2D.git + GIT_TAG v2.3.1) + +# We need finer control over the build of box2d so we set the following variables +set(BOX2D_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) +if(NOT box2d_POPULATED) + FetchContent_Populate(box2d) + add_subdirectory(${box2d_SOURCE_DIR}/Box2D ${box2d_BINARY_DIR}) +endif() + +get_cmake_property(_variableNamesbox VARIABLES) +list(FILTER _variableNamesbox INCLUDE REGEX "box") +message("_variableNamesbox: ${_variableNamesbox}") + +foreach (_variableName ${_variableNamesbox}) + message(STATUS "${_variableName}=${${_variableName}}") +endforeach() #### setup Directories #### #Main output directory @@ -22,27 +71,33 @@ add_custom_target(copy_resources ALL COMMAND ${CMAKE_COMMAND} "${PROJECT_SOURCE_DIR}/res" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$/res ) -add_custom_target(copy_resources_rootdir ALL COMMAND ${CMAKE_COMMAND} - -E copy_directory - "${PROJECT_SOURCE_DIR}/res" - ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/res -) +# _________________________________________________________________________________________________________ +# -- The alternative was to use submodule (for both sfml and box2d - below) from github and use that to download and populate the directory - We went with the more "straightforward" approach of using fetchcontent #### Add External Dependencies #### # SFML - graphics library -add_subdirectory("lib/sfml" EXCLUDE_FROM_ALL) +#add_subdirectory("lib/sfml" EXCLUDE_FROM_ALL) #include_directories("lib/sfml/include") -set(SFML_INCS "lib/sfml/include") -link_directories("${CMAKE_BINARY_DIR}/lib/sfml/lib") +#set(SFML_INCS "lib/sfml/include") +#link_directories("${CMAKE_BINARY_DIR}/lib/sfml/lib") # Does fetchcontent do this for me? A: yes, but I need to tell it to do so by adding the following line: FetchContent_MakeAvailable(sfml) # B2D - Box2D phyics library -add_subdirectory("lib/b2d/Box2D" EXCLUDE_FROM_ALL) +#add_subdirectory("lib/b2d/Box2D" EXCLUDE_FROM_ALL) #include_directories("lib/b2d/Box2D/") -set(B2D_INCS "lib/b2d/Box2D/") -link_directories("${CMAKE_BINARY_DIR}/lib/sfml/lib") +#set(B2D_INCS "lib/b2d/Box2D/") +#link_directories("${CMAKE_BINARY_DIR}/lib/sfml/lib") +# _________________________________________________________________________________________________________ + +# The following allows us to tell the compiler where the header files are for box2D. This is normally managed by fetchcontent but we need to do it manually for box2d - one of the problems in the build +set (B2D_INCS "${box2d_SOURCE_DIR}/Box2D") # note that fetchcontent will download the box2d source code into the box2d_SOURCE_DIR #### Add Engine and our own library projects#### +set (SFML_INCS "${sfml_SOURCE_DIR}/include") # note that fetchcontent will download the sfml source code into the sfml_SOURCE_DIR + +set (lib_box2d "${box2d_BINARY_DIR}/Box2D/libBox2D.a") # Again, not ideal, but we need to tell the compiler where the box2d library is. This is normally managed by fetchcontent but we need to do it manually for box2d - one of the problems in the build + + file(GLOB_RECURSE RESOURCE_FILES "res/*.*") #### file(GLOB_RECURSE SOURCE_FILES engine/*.cpp engine/*.h) @@ -78,11 +133,18 @@ file(GLOB_RECURSE SCENES lab_7_platformer/scenes/*.cpp practical_6_platformer/sc add_executable(PRACTICAL_7_PLATFORMER ${SOURCES} ${RESOURCE_FILES}) source_group("components" FILES ${CMPNTS}) source_group("resources" FILES ${RESOURCE_FILES}) -#target_include_directories(PRACTICAL_6_PLATFORMER SYSTEM PRIVATE ${SFML_INCS} ${B2D_INCS}) + + target_link_libraries(PRACTICAL_7_PLATFORMER lib_engine) set(EXECUTABLES ${EXECUTABLES} PRACTICAL_7_PLATFORMER) -add_dependencies(PRACTICAL_7_PLATFORMER copy_resources copy_resources_rootdir) + +if (WIN32 AND BUILD_SHARED_LIBS) + add_custom_command(TARGET PRACTICAL_7_PLATFORMER POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy $ $ COMMAND_EXPAND_LISTS) +endif() + +add_dependencies(PRACTICAL_7_PLATFORMER copy_resources) set_target_properties(PRACTICAL_7_PLATFORMER @@ -90,17 +152,4 @@ set_target_properties(PRACTICAL_7_PLATFORMER ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$(Configuration) ) - - - - -#file(GLOB_RECURSE SOURCES lab_8_steering/*.cpp lab_8_steering/*.h) -#file(GLOB_RECURSE CMPNTS lab_8_steering/components/*.cpp lab_8_steering/components/*.h) -#file(GLOB_RECURSE SCENES lab_8_steering/scenes/*.cpp lab_8_steering/scenes/*.h) -#add_executable(LAB_8_STEERING ${SOURCES} ${CMPNTS} ${SCENES} ${RESOURCE_FILES} lab_8_steering/components/cmp_ai_steering.cpp lab_8_steering/components/cmp_ai_steering.h) -#target_link_libraries(LAB_8_STEERING lib_engine) -#set(EXECUTABLES ${EXECUTABLES} LAB_8_STEERING) - -#add_dependencies(LAB_8_STEERING copy_resources copy_resources_rootdir) - #@@- \ No newline at end of file diff --git a/engine/engine.cpp b/engine/engine.cpp index 2104b54..888632b 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -81,7 +81,7 @@ void Engine::Render(RenderWindow& window) { void Engine::Start(unsigned int width, unsigned int height, const std::string& gameName, Scene* scn) { - RenderWindow window(VideoMode(Vector2u (width,height)), gameName); + RenderWindow window(VideoMode(width,height), gameName); _gameName = gameName; _window = &window; Renderer::initialise(window); diff --git a/lab_7_platformer/components/cmp_physics.cpp b/lab_7_platformer/components/cmp_physics.cpp index 269482c..6a57e88 100644 --- a/lab_7_platformer/components/cmp_physics.cpp +++ b/lab_7_platformer/components/cmp_physics.cpp @@ -96,7 +96,8 @@ void PhysicsComponent::render() {} void PhysicsComponent::impulse(const sf::Vector2f& i) { auto a = b2Vec2(i.x, i.y * -1.0f); - _body->ApplyLinearImpulseToCenter(a, true); + // _body->ApplyLinearImpulseToCenter (a, true); + _body->ApplyLinearImpulse (a, _body->GetWorldCenter(), true); } void PhysicsComponent::dampen(const sf::Vector2f& i) { diff --git a/lab_7_platformer/components/cmp_player_physics.cpp b/lab_7_platformer/components/cmp_player_physics.cpp index 67608e5..1515aa9 100644 --- a/lab_7_platformer/components/cmp_player_physics.cpp +++ b/lab_7_platformer/components/cmp_player_physics.cpp @@ -42,6 +42,7 @@ void PlayerPhysicsComponent::update(double dt) { Keyboard::isKeyPressed(Keyboard::Right)) { // Moving Either Left or Right if (Keyboard::isKeyPressed(Keyboard::Right)) { + std::cout << "RIGHT KEY" << std::endl; if (getVelocity().x < _maxVelocity.x) impulse({(float)(dt * _groundspeed), 0}); } else { diff --git a/lib_maths/maths.h b/lib_maths/maths.h index a34c5c3..b0aa819 100644 --- a/lib_maths/maths.h +++ b/lib_maths/maths.h @@ -6,6 +6,7 @@ #include // std::cout, std::fixed #include #include +#include namespace sf { @@ -47,9 +48,10 @@ template Vector2 Vcast(const Vector2& v) { return Vector2(static_cast(v.x), static_cast(v.y)); }; -static double degrees(double radians) { - return radians * 180.0 / (4.0 * atan(1.0)); -} +// TODO: check what this function should actually do +// Just a casting to avoid errors during Compilation +template +double degrees(T deg) { return (double)deg; } static double deg2rad(double degrees) { return degrees * 4.0 * atan(1.0) / 180.0; From 8549d68669d2b98a9f59d0fd23e2ae159ece1be0 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Wed, 22 Nov 2023 12:29:44 +0000 Subject: [PATCH 04/32] Update CMAKE --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ed7fd7c..55473b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,7 @@ set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}) add_custom_target(copy_resources ALL COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/res" - ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$/res + ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/res ) # _________________________________________________________________________________________________________ From 76f4bc64e5b09287bd010edbc25164c741059f4b Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Wed, 22 Nov 2023 12:59:44 +0000 Subject: [PATCH 05/32] Solved the problem by changing the window name; scene 2 implementation --- lab_7_platformer/platformer_main.cpp | 2 +- lab_7_platformer/scenes/scene_level2.cpp | 46 ++++++++++++++---------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lab_7_platformer/platformer_main.cpp b/lab_7_platformer/platformer_main.cpp index dae99e9..449a3f3 100644 --- a/lab_7_platformer/platformer_main.cpp +++ b/lab_7_platformer/platformer_main.cpp @@ -10,5 +10,5 @@ Level2Scene level2; Level3Scene level3; int main() { - Engine::Start(1280, 720, "Platformer",&menu); + Engine::Start(1280, 720, "game debug mode",&menu); } \ No newline at end of file diff --git a/lab_7_platformer/scenes/scene_level2.cpp b/lab_7_platformer/scenes/scene_level2.cpp index e92cd1e..cee2e98 100644 --- a/lab_7_platformer/scenes/scene_level2.cpp +++ b/lab_7_platformer/scenes/scene_level2.cpp @@ -7,6 +7,8 @@ #include "../game.h" #include #include +#include + using namespace std; using namespace sf; @@ -20,13 +22,16 @@ void Level2Scene::Load() { // Create player { // ********************************* - - - - - - + player = makeEntity(); + // Define Position + player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0])); + // Create a shape and assign it + auto s = player->addComponent(); + s->setShape(Vector2f(20.f, 30.f)); + s->getShape().setFillColor(Color::Magenta); + s->getShape().setOrigin(Vector2f (10.f, 15.f)); // ********************************* + player->addTag("player"); player->addComponent(Vector2f(20.f, 30.f)); } @@ -38,13 +43,14 @@ void Level2Scene::Load() { Vector2f(0, 24)); // ********************************* // Add HurtComponent - + enemy->addComponent(); // Add ShapeComponent, Red 16.f Circle - - - - + auto s = enemy->addComponent(); + s->setShape(16.f); + s->getShape().setFillColor(sf::Color::Red); + s->getShape().setOrigin(Vector2f (8.f,8.f)); // Add EnemyAIComponent + enemy->addComponent(); // ********************************* } @@ -64,17 +70,19 @@ void Level2Scene::Load() { // Add physics colliders to level tiles. { // ********************************* - - - - - - - - + auto walls = ls::findTiles(ls::WALL); + for (auto w : walls) + { + auto pos = ls::getTilePosition(w); + pos += Vector2f (20.f, 20.f); // Offset + auto e = makeEntity(); + e->setPosition(pos); + e->addComponent(false, Vector2f (40.f, 40.f)); + } // ********************************* } + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); cout << " Scene 2 Load Done" << endl; setLoaded(true); } From eb23ffec01682b85583d53195dffaa9122852582 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Mon, 27 Nov 2023 09:59:48 +0000 Subject: [PATCH 06/32] Implementation of Scene3 and finishing touches. --- lab_7_platformer/scenes/scene_level2.cpp | 2 +- lab_7_platformer/scenes/scene_level3.cpp | 40 +++++++++++++----------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lab_7_platformer/scenes/scene_level2.cpp b/lab_7_platformer/scenes/scene_level2.cpp index cee2e98..2d364ed 100644 --- a/lab_7_platformer/scenes/scene_level2.cpp +++ b/lab_7_platformer/scenes/scene_level2.cpp @@ -82,7 +82,7 @@ void Level2Scene::Load() { // ********************************* } - std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); cout << " Scene 2 Load Done" << endl; setLoaded(true); } diff --git a/lab_7_platformer/scenes/scene_level3.cpp b/lab_7_platformer/scenes/scene_level3.cpp index f4f07a9..a7cf4eb 100644 --- a/lab_7_platformer/scenes/scene_level3.cpp +++ b/lab_7_platformer/scenes/scene_level3.cpp @@ -5,6 +5,7 @@ #include "../components/cmp_bullet.h" #include #include +#include using namespace std; using namespace sf; @@ -18,33 +19,34 @@ void Level3Scene::Load() { // Create player { - // ********************************* - - - // pl->setPosition({100, 100}); - - - - - - - // ********************************* + player = makeEntity(); + player->setPosition({100, 100}); + auto s = player->addComponent(); + s->setShape(Vector2f(20.f, 30.f)); + s->getShape().setFillColor(Color::Magenta); + s->getShape().setOrigin(10.f,15.f); + + player->addTag("player"); + player->addComponent(Vector2f(20.f, 30.f)); } // Add physics colliders to level tiles. { // ********************************* - - - - - - - - + auto walls = ls::findTiles(ls::WALL); + for (auto w : walls) + { + auto pos = ls::getTilePosition(w); + pos += Vector2f(20.f, 20.f); + auto e = makeEntity(); + e->setPosition(pos); + e->addComponent(false, Vector2f(40.f, 40.f)); + } // ********************************* } + + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); cout << " Scene 3 Load Done" << endl; setLoaded(true); } From 84c116b6971e3aa3cfa1dd3b01516fe952dfd294 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Thu, 30 Nov 2023 13:42:22 +0000 Subject: [PATCH 07/32] Beginning of final game project, initial setup, built PlayerMoveComponent --- CMakeLists.txt | 28 ++++++ engine/engine.cpp | 1 + engine/engine.h | 1 + .../components/cmp_actor_movement.cpp | 87 +++++++++++++++++++ game_project/components/cmp_actor_movement.h | 80 +++++++++++++++++ game_project/components/cmp_input.cpp | 5 ++ game_project/components/cmp_input.h | 5 ++ game_project/components/cmp_sprite.cpp | 37 ++++++++ game_project/components/cmp_sprite.h | 40 +++++++++ game_project/components/cmp_text.cpp | 19 ++++ game_project/components/cmp_text.h | 22 +++++ game_project/game.h | 11 +++ game_project/main.cpp | 10 +++ game_project/scenes/blank.cpp | 53 +++++++++++ game_project/scenes/blank.h | 16 ++++ game_project/scenes/scene_menu.cpp | 28 ++++++ game_project/scenes/scene_menu.h | 13 +++ .../components/cmp_enemy_turret.cpp | 2 + lab_7_platformer/components/cmp_physics.cpp | 26 ------ .../components/cmp_player_physics.cpp | 9 +- lab_7_platformer/scenes/scene_level1.cpp | 12 ++- lab_9_deployment/main.cpp | 43 --------- lib_ecm/ecm.h | 3 +- lib_tile_level_loader/LevelSystem.cpp | 1 + res/blank_level.txt | 18 ++++ res/level_1.txt | 14 +-- 26 files changed, 499 insertions(+), 85 deletions(-) create mode 100644 game_project/components/cmp_actor_movement.cpp create mode 100644 game_project/components/cmp_actor_movement.h create mode 100644 game_project/components/cmp_input.cpp create mode 100644 game_project/components/cmp_input.h create mode 100644 game_project/components/cmp_sprite.cpp create mode 100644 game_project/components/cmp_sprite.h create mode 100644 game_project/components/cmp_text.cpp create mode 100644 game_project/components/cmp_text.h create mode 100644 game_project/game.h create mode 100644 game_project/main.cpp create mode 100644 game_project/scenes/blank.cpp create mode 100644 game_project/scenes/blank.h create mode 100644 game_project/scenes/scene_menu.cpp create mode 100644 game_project/scenes/scene_menu.h delete mode 100644 lab_9_deployment/main.cpp create mode 100644 res/blank_level.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 55473b1..46c081e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,4 +152,32 @@ set_target_properties(PRACTICAL_7_PLATFORMER ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$(Configuration) ) +file(GLOB_RECURSE SOURCES game_project/*.cpp game_project/*.h) +file(GLOB_RECURSE CMPNTS game_project/components/*.cpp game_project/components/*.h) +file(GLOB_RECURSE SCENES game_project/scenes/*.cpp game_project/scenes/*.h) +add_executable(GAME_PROJECT ${SOURCES} ${RESOURCE_FILES} + game_project/scenes/scene_menu.cpp + game_project/components/cmp_actor_movement.cpp + game_project/components/cmp_input.cpp + game_project/components/cmp_input.h) +source_group("components" FILES ${CMPNTS}) +source_group("resources" FILES ${RESOURCE_FILES}) + + +target_link_libraries(GAME_PROJECT lib_engine) +set(EXECUTABLES ${EXECUTABLES} GAME_PROJECT) + + +if (WIN32 AND BUILD_SHARED_LIBS) + add_custom_command(TARGET GAME_PROJECT POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy $ $ COMMAND_EXPAND_LISTS) +endif() + +add_dependencies(GAME_PROJECT copy_resources) + + +set_target_properties(GAME_PROJECT + PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY + ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$(Configuration) +) #@@- \ No newline at end of file diff --git a/engine/engine.cpp b/engine/engine.cpp index 888632b..e072480 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -175,6 +175,7 @@ sf::Vector2u Engine::getWindowSize() { return _window->getSize(); } sf::RenderWindow& Engine::GetWindow() { return *_window; } + namespace timing { // Return time since Epoc long long now() { diff --git a/engine/engine.h b/engine/engine.h index 870cbd5..942c96f 100644 --- a/engine/engine.h +++ b/engine/engine.h @@ -45,6 +45,7 @@ class Engine { static void Render(sf::RenderWindow& window); }; + namespace timing { // Return time since Epoc long long now(); diff --git a/game_project/components/cmp_actor_movement.cpp b/game_project/components/cmp_actor_movement.cpp new file mode 100644 index 0000000..4849a9c --- /dev/null +++ b/game_project/components/cmp_actor_movement.cpp @@ -0,0 +1,87 @@ +// +// Created by rysan on 29/11/23. +// + +#include "cmp_actor_movement.h" +#include +#include +#include + +void ActorMovementComponent::update(double dt) {} + +ActorMovementComponent::ActorMovementComponent(Entity *p) + : _speed(1.f), Component(p) {} + + +// Check if the current movement is valid +bool ActorMovementComponent::validMove(const sf::Vector2f & pos) +{ + return (LevelSystem::getTileAt(pos) != LevelSystem::WALL); +} + +void ActorMovementComponent::setSpeed(float speed) { _speed = speed; } + +/* Actual movement ------------------------------------- */ +// x == +1 -> right | y == +1 -> down +// x == -1 -> left | y == -1 -> up +/* ----------------------------------------------------- */ +void ActorMovementComponent::move(float x, float y) +{ + move(sf::Vector2f (x, y)); +} + +void ActorMovementComponent::move(const sf::Vector2f & pos) +{ + auto pp = _parent->getPosition() + ls::getOffset() + ls::getTileSize() * pos; + if (validMove(pp)) + _parent->setPosition(pp); +} + +PlayerMoveComponent::PlayerMoveComponent(Entity *p) : + ActorMovementComponent(p), _timer(0){} + +void PlayerMoveComponent::update(double dt) +{ + _timer-=dt; + if (_timer <= 0) + { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { move(0.f,-1.f); _timer=_speed; } + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { move(0.f, 1.f); _timer=_speed; } + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { move(-1.f, 0.f); _timer=_speed; } + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { move(1.f, 0.f); _timer=_speed; } + } +} + + +//CommandComponent::CommandComponent(Entity *p, sf::Keyboard::Key k) : +// ActorMovementComponent(p), k_(k) { } +// +//double CommandComponent::_timer = 0.f; +// +//void CommandComponent::update(double dt) {CommandComponent::_timer -= dt;} +// +//MoveUpComponent::MoveUpComponent(Entity *p, sf::Keyboard::Key k) : +// CommandComponent(p, k) {} +// +//void MoveUpComponent::update(double dt) +//{ +// if (CommandComponent::_timer <= 0) +// if(sf::Keyboard::isKeyPressed(k_)) +// { +// move(0.f,-1.f); +// CommandComponent::_timer = _speed; +// } +//} +// +//MoveDownComponent::MoveDownComponent(Entity *p, sf::Keyboard::Key k) : +// CommandComponent(p, k) {} +// +//void MoveDownComponent::update(double dt) +//{ +// if (CommandComponent::_timer <= 0) +// if(sf::Keyboard::isKeyPressed(k_)) +// { +// move(0.f,1.f); +// CommandComponent::_timer = _speed; +// } +//} diff --git a/game_project/components/cmp_actor_movement.h b/game_project/components/cmp_actor_movement.h new file mode 100644 index 0000000..38efd94 --- /dev/null +++ b/game_project/components/cmp_actor_movement.h @@ -0,0 +1,80 @@ +// +// Created by rysan on 29/11/23. +// +#pragma once +#include +#include +#include "SFML/Window/Keyboard.hpp" + +//class Command; +//class InputHandler; +//class MoveUpComponent; +//class MoveDownComponent; +//class MoveLeftComponent; +//class MoveRightComponent; + +class ActorMovementComponent : public Component { +protected: + bool validMove(const sf::Vector2f&); + float _speed; // Limit the amount of moves per second + +public: + void update(double dt) override; + void move(const sf::Vector2f&); + void move(float x, float y); + void render() override {} + void setSpeed(float speed); + explicit ActorMovementComponent(Entity* p); + ActorMovementComponent() = delete; +}; + +class PlayerMoveComponent : public ActorMovementComponent { +public: + void update(double dt) override; + void render() override {}; + explicit PlayerMoveComponent(Entity *p); + PlayerMoveComponent()=delete; + +private: + double _timer; +}; + +//class CommandComponent : public ActorMovementComponent { +//public: +// explicit CommandComponent(Entity *p, sf::Keyboard::Key k); +// CommandComponent() = delete; +// void update(double dt) override; +// void bindKey(sf::Keyboard::Key k); +// +//protected: +// sf::Keyboard::Key k_; +// static double _timer; +//}; // Command +// +//class MoveUpComponent : public CommandComponent { +//public: +// explicit MoveUpComponent(Entity *p, sf::Keyboard::Key k); +// MoveUpComponent() = delete; +// void update(double dt) override; +//}; +// +//class MoveDownComponent : public CommandComponent { +//public: +// explicit MoveDownComponent(Entity *p, sf::Keyboard::Key k); +// MoveDownComponent() = delete; +// void update(double dt) override; +//}; + +//class MoveRightComponent : public CommandComponent { +//public: +// explicit MoveRightComponent(Entity *p, sf::Keyboard::Key k); +// MoveRightComponent() = delete; +// void update(double dt) override; +//}; +// +//class MoveLeftComponent : public CommandComponent { +//public: +// explicit MoveLeftComponent(Entity *p, sf::Keyboard::Key k); +// MoveLeftComponent() = delete; +// void update(double dt) override; +//}; diff --git a/game_project/components/cmp_input.cpp b/game_project/components/cmp_input.cpp new file mode 100644 index 0000000..133500a --- /dev/null +++ b/game_project/components/cmp_input.cpp @@ -0,0 +1,5 @@ +// +// Created by rysan on 29/11/23. +// + +#include "cmp_input.h" diff --git a/game_project/components/cmp_input.h b/game_project/components/cmp_input.h new file mode 100644 index 0000000..2d7f915 --- /dev/null +++ b/game_project/components/cmp_input.h @@ -0,0 +1,5 @@ +// +// Created by rysan on 29/11/23. +// +#pragma once +#include "cmp_actor_movement.h" diff --git a/game_project/components/cmp_sprite.cpp b/game_project/components/cmp_sprite.cpp new file mode 100644 index 0000000..bc54015 --- /dev/null +++ b/game_project/components/cmp_sprite.cpp @@ -0,0 +1,37 @@ + +#include "cmp_sprite.h" +#include "system_renderer.h" + +using namespace std; + + +void SpriteComponent::setTexure(std::shared_ptr tex) +{ + _texture = tex; + _sprite->setTexture(*_texture); +} + + +SpriteComponent::SpriteComponent(Entity* p) + : Component(p), _sprite(make_shared()) {} + +void SpriteComponent::update(double dt) { + _sprite->setPosition(_parent->getPosition()); + _sprite->setRotation(sf::degrees(_parent->getRotation())); +} + +void SpriteComponent::render() { Renderer::queue(_sprite.get()); } + +void ShapeComponent::update(double dt) { + _shape->setPosition(_parent->getPosition()); + _shape->setRotation(sf::degrees(_parent->getRotation())); +} + +void ShapeComponent::render() { Renderer::queue(_shape.get()); } + +sf::Shape& ShapeComponent::getShape() const { return *_shape; } + +ShapeComponent::ShapeComponent(Entity* p) + : Component(p), _shape(make_shared()) {} + +sf::Sprite& SpriteComponent::getSprite() const { return *_sprite; } diff --git a/game_project/components/cmp_sprite.h b/game_project/components/cmp_sprite.h new file mode 100644 index 0000000..945080b --- /dev/null +++ b/game_project/components/cmp_sprite.h @@ -0,0 +1,40 @@ +#pragma once + +#include "ecm.h" +#include +#include + +class SpriteComponent : public Component { +protected: + std::shared_ptr _sprite; + std::shared_ptr _texture; +public: + SpriteComponent() = delete; + + explicit SpriteComponent(Entity* p); + void update(double dt) override; + void render() override; + + sf::Sprite& getSprite() const; + + + void setTexure(std::shared_ptr tex); +}; + +class ShapeComponent : public Component { +protected: + std::shared_ptr _shape; + // sf::Shape _shape; + +public: + ShapeComponent() = delete; + + explicit ShapeComponent(Entity* p); + + void update(double dt) override; + void render() override; + sf::Shape& getShape() const; + template void setShape(Targs... params) { + _shape.reset(new T(params...)); + } +}; \ No newline at end of file diff --git a/game_project/components/cmp_text.cpp b/game_project/components/cmp_text.cpp new file mode 100644 index 0000000..83d6b9e --- /dev/null +++ b/game_project/components/cmp_text.cpp @@ -0,0 +1,19 @@ +#include "cmp_text.h" +#include +#include + +void TextComponent::update(double dt) {} + +void TextComponent::render() { Renderer::queue(&_text); } + +TextComponent::TextComponent(Entity* const p, const std::string& str) + : Component(p), _string(str) { + _text.setString(_string); + _font = Resources::get("RobotoMono-Regular.ttf"); + _text.setFont(*_font); +} + +void TextComponent::SetText(const std::string& str) { + _string = str; + _text.setString(_string); +} diff --git a/game_project/components/cmp_text.h b/game_project/components/cmp_text.h new file mode 100644 index 0000000..c843510 --- /dev/null +++ b/game_project/components/cmp_text.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +class TextComponent : public Component { +public: + TextComponent() = delete; + explicit TextComponent(Entity* p, const std::string& str = ""); + void update(double dt) override; + + void render() override; + + ~TextComponent() override = default; + + void SetText(const std::string& str); + +protected: + std::shared_ptr _font; + std::string _string; + sf::Text _text; +}; diff --git a/game_project/game.h b/game_project/game.h new file mode 100644 index 0000000..439bd0d --- /dev/null +++ b/game_project/game.h @@ -0,0 +1,11 @@ +// +// Created by rysan on 29/11/23. +// +#pragma once + +#include "scenes/blank.h" +#include "scenes/scene_menu.h" + +extern MenuScene menu; +extern Blank blank; + diff --git a/game_project/main.cpp b/game_project/main.cpp new file mode 100644 index 0000000..019aa7e --- /dev/null +++ b/game_project/main.cpp @@ -0,0 +1,10 @@ +#include "engine.h" +#include "game.h" +#include "scenes/scene_menu.h" + +MenuScene menu; +Blank blank; + +int main() { + Engine::Start(1280, 720, "game debug mode", &menu); +} \ No newline at end of file diff --git a/game_project/scenes/blank.cpp b/game_project/scenes/blank.cpp new file mode 100644 index 0000000..e6ac610 --- /dev/null +++ b/game_project/scenes/blank.cpp @@ -0,0 +1,53 @@ +// +// Created by rysan on 29/11/23. +// + +#include "blank.h" +#include "../game.h" +#include "../components/cmp_sprite.h" +#include "../components/cmp_actor_movement.h" +#include +#include +#include + +static std::shared_ptr player; + +void Blank::Load() { + std::cout << ">> BLANK CANVAS LOADING <<" << std::endl; + + // Importing Level from file + ls::loadLevelFile("res/blank_level.txt", 40.f); + ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * 40.f))); + + { + player = makeEntity(); + player->setPosition(ls::getTilePosition(sf::Vector2ul(10,10)) + ls::getOffset()); + auto s = player->addComponent(); + s->setShape(sf::Vector2f(40.f, 40.f)); + s->getShape().setFillColor(sf::Color::Green); + s->getShape().setOrigin(sf::Vector2f (20.f,20.f)); + + player->addComponent(); + player->get_components()[0]->setSpeed(0.4f); + } + + setLoaded(true); + std::cout <<">> BLANK CANVAS LOADED <<" << std::endl; +} + +void Blank::UnLoad() { + std::cout << ">> BLANK CANVAS UNLOAD <<" << std::endl; + // REMOVE PLAYER OR ANYTHING IN THE CANVAS + player.reset(); + ls::unload(); + Scene::UnLoad(); +} + +void Blank::Update(const double& dt) { + Scene::Update(dt); +} + +void Blank::Render() { + ls::render(Engine::GetWindow()); + Scene::Render(); +} \ No newline at end of file diff --git a/game_project/scenes/blank.h b/game_project/scenes/blank.h new file mode 100644 index 0000000..5ef1126 --- /dev/null +++ b/game_project/scenes/blank.h @@ -0,0 +1,16 @@ +// +// Created by rysan on 29/11/23. +// +#pragma once + +#include "engine.h" + +class Blank : public Scene { +public: + void Load() override; + void UnLoad() override; + void Update(const double& dt) override; + void Render() override; +}; + + diff --git a/game_project/scenes/scene_menu.cpp b/game_project/scenes/scene_menu.cpp new file mode 100644 index 0000000..801cca9 --- /dev/null +++ b/game_project/scenes/scene_menu.cpp @@ -0,0 +1,28 @@ +#include "scene_menu.h" +#include "../components/cmp_text.h" +#include "../game.h" +#include +#include + +using namespace std; +using namespace sf; + +void MenuScene::Load() { + cout << "Menu Load \n"; + { + auto txt = makeEntity(); + auto t = txt->addComponent( + "Platformer\nPress Space to Start"); + } + setLoaded(true); +} + +void MenuScene::Update(const double& dt) { + // cout << "Menu Update "<SetRestitution(.9) FixtureDef.restitution = .2; } - - // An ideal Pod/capusle shape should be used for hte player, - // this isn't built into B2d, but we can combine two shapes to do so. - // This would allwo the player to go up steps - /* - BodyDef.bullet = true; - b2PolygonShape shape1; - shape1.SetAsBox(sv2_to_bv2(size).x * 0.5f, sv2_to_bv2(size).y * 0.5f); - { - b2PolygonShape poly ; - poly.SetAsBox(0.45f, 1.4f); - b2FixtureDef FixtureDefPoly; - - FixtureDefPoly.shape = &poly; - _body->CreateFixture(&FixtureDefPoly); - - } - { - b2CircleShape circle; - circle.m_radius = 0.45f; - circle.m_p.Set(0, -1.4f); - b2FixtureDef FixtureDefCircle; - FixtureDefCircle.shape = &circle; - _body->CreateFixture(&FixtureDefCircle); - } - */ } void PhysicsComponent::setFriction(float r) { _fixture->SetFriction(r); } diff --git a/lab_7_platformer/components/cmp_player_physics.cpp b/lab_7_platformer/components/cmp_player_physics.cpp index 1515aa9..9d6f712 100644 --- a/lab_7_platformer/components/cmp_player_physics.cpp +++ b/lab_7_platformer/components/cmp_player_physics.cpp @@ -25,10 +25,11 @@ bool PlayerPhysicsComponent::isGrounded() const { return true; } } - return false; } + + void PlayerPhysicsComponent::update(double dt) { const auto pos = _parent->getPosition(); @@ -51,7 +52,7 @@ void PlayerPhysicsComponent::update(double dt) { } } else { // Dampen X axis movement - dampen({0.9f, 1.0f}); + dampen({0.3f, 1.0f}); } // Handle Jump @@ -60,7 +61,7 @@ void PlayerPhysicsComponent::update(double dt) { if (_grounded) { setVelocity(Vector2f(getVelocity().x, 0.f)); teleport(Vector2f(pos.x, pos.y - 2.0f)); - impulse(Vector2f(0, -6.f)); + impulse(Vector2f(0, -8.f)); } } @@ -88,7 +89,7 @@ PlayerPhysicsComponent::PlayerPhysicsComponent(Entity* p, : PhysicsComponent(p, true, size) { _size = sv2_to_bv2(size, true); _maxVelocity = Vector2f(200.f, 400.f); - _groundspeed = 30.f; + _groundspeed = 50.f; _grounded = false; _body->SetSleepingAllowed(false); _body->SetFixedRotation(true); diff --git a/lab_7_platformer/scenes/scene_level1.cpp b/lab_7_platformer/scenes/scene_level1.cpp index 3127765..0ec8f3f 100644 --- a/lab_7_platformer/scenes/scene_level1.cpp +++ b/lab_7_platformer/scenes/scene_level1.cpp @@ -13,24 +13,31 @@ static shared_ptr player; void Level1Scene::Load() { cout << " Scene 1 Load" << endl; + // Import level from file *** ls::loadLevelFile("res/level_1.txt", 40.0f); + // Calculate a height offset auto ho = Engine::getWindowSize().y - (ls::getHeight() * 40.f); ls::setOffset(Vector2f(0, ho)); - // Create player + // Create Entities **** { + // Create the Player player = makeEntity(); + // Spawn point player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0])); + // Appearance auto s = player->addComponent(); s->setShape(Vector2f(20.f, 30.f)); s->getShape().setFillColor(Color::Magenta); s->getShape().setOrigin(Vector2f(10.f, 15.f)); + // Behaviour player->addComponent(Vector2f(20.f, 30.f)); } // Add physics colliders to level tiles. + // The collider is separate from the sprite and shape of the wall itself { auto walls = ls::findTiles(ls::WALL); for (auto w : walls) { @@ -43,7 +50,7 @@ void Level1Scene::Load() { } //Simulate long loading times - std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); cout << " Scene 1 Load Done" << endl; setLoaded(true); @@ -57,7 +64,6 @@ void Level1Scene::UnLoad() { } void Level1Scene::Update(const double& dt) { - if (ls::getTileAt(player->getPosition()) == ls::END) { Engine::ChangeScene((Scene*)&level2); } diff --git a/lab_9_deployment/main.cpp b/lab_9_deployment/main.cpp deleted file mode 100644 index 8d6254b..0000000 --- a/lab_9_deployment/main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "engine.h" -#include -#include -#include - -using namespace std; -using namespace sf; - -class MainScene : public Scene { - -public: - MainScene() = default; - ~MainScene() = default; - sf::Sprite sprite; - sf::Texture tex; - Vector2f target; - void Load() override { - tex = *Resources::load("dat_boi.png"); - target = {640, 360}; - sprite.setPosition(640, 360); - sprite.setOrigin(115, 64); - sprite.setTexture(tex); - } - - void Update(const double& dt) override { - Scene::Update(dt); - auto d = target - sprite.getPosition(); - if (length(d) < 64) { - target = {100.f + (rand() % 1000), 200.f + (rand() % 500)}; - } - sprite.setScale(d.x > 0.f ? -1.f : 1.f, 1.f); - sprite.move((float)dt * 100.0f * normalize(d)); - } - - void Render() override { Renderer::queue(&sprite); } -}; - -MainScene scene; - -int main() { - Engine::Start(1280, 720, "Deployed", &scene); - return 0; -} \ No newline at end of file diff --git a/lib_ecm/ecm.h b/lib_ecm/ecm.h index 8f58bf1..1675f94 100644 --- a/lib_ecm/ecm.h +++ b/lib_ecm/ecm.h @@ -34,8 +34,7 @@ struct EntityManager { void update(double dt); void render(); std::vector> find(const std::string& tag) const; - std::vector> - find(const std::vector& tags) const; + std::vector> find(const std::vector& tags) const; }; class Entity { diff --git a/lib_tile_level_loader/LevelSystem.cpp b/lib_tile_level_loader/LevelSystem.cpp index 574757b..b831089 100644 --- a/lib_tile_level_loader/LevelSystem.cpp +++ b/lib_tile_level_loader/LevelSystem.cpp @@ -54,6 +54,7 @@ void LevelSystem::loadLevelFile(const std::string& path, float tileSize) { if (w == 0) { // if we haven't written width yet w = i; // set width } else if (w != (widthCheck - 1)) { + std::cout << "non uniform width" << endl; throw string("non uniform width:" + to_string(h) + " ") + path; } widthCheck = 0; diff --git a/res/blank_level.txt b/res/blank_level.txt new file mode 100644 index 0000000..3b858eb --- /dev/null +++ b/res/blank_level.txt @@ -0,0 +1,18 @@ +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww diff --git a/res/level_1.txt b/res/level_1.txt index 1162c4f..9503fe7 100644 --- a/res/level_1.txt +++ b/res/level_1.txt @@ -1,3 +1,4 @@ +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww w w w w w w @@ -9,10 +10,9 @@ w w w w w w w w -w e w -w www w -w w w -w ww w -w s e w wwww w -w w n www w -wwwwwww wwwwwwwwwwww wwwwww ww +w w +w w +w s w +w w +w w +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww From 9398aa97d6ec49dc3fd4f621715ccb4f919c4089 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Thu, 30 Nov 2023 14:19:20 +0000 Subject: [PATCH 08/32] Movement timer does not reset anymore on collision --- .../components/cmp_actor_movement.cpp | 20 +++++++++++-------- game_project/components/cmp_actor_movement.h | 4 ++-- game_project/scenes/blank.cpp | 8 ++++++-- res/blank_level.txt | 10 +++++----- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/game_project/components/cmp_actor_movement.cpp b/game_project/components/cmp_actor_movement.cpp index 4849a9c..26dc6e6 100644 --- a/game_project/components/cmp_actor_movement.cpp +++ b/game_project/components/cmp_actor_movement.cpp @@ -25,16 +25,18 @@ void ActorMovementComponent::setSpeed(float speed) { _speed = speed; } // x == +1 -> right | y == +1 -> down // x == -1 -> left | y == -1 -> up /* ----------------------------------------------------- */ -void ActorMovementComponent::move(float x, float y) +bool ActorMovementComponent::move(float x, float y) { - move(sf::Vector2f (x, y)); + return move(sf::Vector2f (x, y)); } -void ActorMovementComponent::move(const sf::Vector2f & pos) +bool ActorMovementComponent::move(const sf::Vector2f & pos) { auto pp = _parent->getPosition() + ls::getOffset() + ls::getTileSize() * pos; - if (validMove(pp)) + bool valid = validMove(pp); + if (valid) _parent->setPosition(pp); + return valid; } PlayerMoveComponent::PlayerMoveComponent(Entity *p) : @@ -43,13 +45,15 @@ PlayerMoveComponent::PlayerMoveComponent(Entity *p) : void PlayerMoveComponent::update(double dt) { _timer-=dt; + bool moved = false; if (_timer <= 0) { - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { move(0.f,-1.f); _timer=_speed; } - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { move(0.f, 1.f); _timer=_speed; } - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { move(-1.f, 0.f); _timer=_speed; } - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { move(1.f, 0.f); _timer=_speed; } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) moved = move(0.f,-1.f); + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) moved = move(0.f, 1.f); + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) moved = move(-1.f, 0.f); + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) moved = move(1.f, 0.f); } + if (moved) _timer=_speed; } diff --git a/game_project/components/cmp_actor_movement.h b/game_project/components/cmp_actor_movement.h index 38efd94..26b09f2 100644 --- a/game_project/components/cmp_actor_movement.h +++ b/game_project/components/cmp_actor_movement.h @@ -20,8 +20,8 @@ class ActorMovementComponent : public Component { public: void update(double dt) override; - void move(const sf::Vector2f&); - void move(float x, float y); + bool move(const sf::Vector2f&); + bool move(float x, float y); void render() override {} void setSpeed(float speed); explicit ActorMovementComponent(Entity* p); diff --git a/game_project/scenes/blank.cpp b/game_project/scenes/blank.cpp index e6ac610..ed5d16b 100644 --- a/game_project/scenes/blank.cpp +++ b/game_project/scenes/blank.cpp @@ -18,17 +18,19 @@ void Blank::Load() { // Importing Level from file ls::loadLevelFile("res/blank_level.txt", 40.f); ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * 40.f))); + auto spawn_offset = sf::Vector2f(20.f,20.f); { player = makeEntity(); - player->setPosition(ls::getTilePosition(sf::Vector2ul(10,10)) + ls::getOffset()); + player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); + std::cout << "OF: " << ls::getOffset().x << " " << ls::getOffset().y << std::endl; auto s = player->addComponent(); s->setShape(sf::Vector2f(40.f, 40.f)); s->getShape().setFillColor(sf::Color::Green); s->getShape().setOrigin(sf::Vector2f (20.f,20.f)); player->addComponent(); - player->get_components()[0]->setSpeed(0.4f); + player->get_components()[0]->setSpeed(0.5f); } setLoaded(true); @@ -44,6 +46,8 @@ void Blank::UnLoad() { } void Blank::Update(const double& dt) { + if (ls::getTileAt(player->getPosition()) == ls::END) + Engine::ChangeScene((Scene*)&menu); Scene::Update(dt); } diff --git a/res/blank_level.txt b/res/blank_level.txt index 3b858eb..95577eb 100644 --- a/res/blank_level.txt +++ b/res/blank_level.txt @@ -3,12 +3,12 @@ w w w w w w w w +w e w +w w w w +w ww ww w w w -w w -w w -w w -w w -w w +w ww ww w +w s w w w w w w w w w From abca4f8654771e3422e38b1732ae29a75bd78646 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Thu, 30 Nov 2023 15:13:19 +0000 Subject: [PATCH 09/32] Drawing Grid --- lib_tile_level_loader/LevelSystem.cpp | 51 +++++++++++++++++++++------ lib_tile_level_loader/LevelSystem.h | 5 ++- res/blank_level.txt | 4 +-- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lib_tile_level_loader/LevelSystem.cpp b/lib_tile_level_loader/LevelSystem.cpp index b831089..8b677dc 100644 --- a/lib_tile_level_loader/LevelSystem.cpp +++ b/lib_tile_level_loader/LevelSystem.cpp @@ -4,19 +4,34 @@ using namespace std; using namespace sf; -std::map LevelSystem::_colours{ +std::map LevelSystem::_fillcolours{ {WALL, Color::White}, {END, Color::Red}}; +std::map LevelSystem::_edgecolours{ + {EMPTY, sf::Color(50,50,50)}, {WALL, sf::Color::White}, {END, sf::Color::Red}}; + sf::Color LevelSystem::getColor(LevelSystem::Tile t) { - auto it = _colours.find(t); - if (it == _colours.end()) { - _colours[t] = Color::Transparent; + auto it = _fillcolours.find(t); + if (it == _fillcolours.end()) { + _fillcolours[t] = Color::Transparent; } - return _colours[t]; + return _fillcolours[t]; +} + +sf::Color LevelSystem::getEdgeColor(LevelSystem::Tile t){ + auto it = _edgecolours.find(t); + if (it == _edgecolours.end()) + _edgecolours[t] = Color::Transparent; + return _edgecolours[t]; } void LevelSystem::setColor(LevelSystem::Tile t, sf::Color c) { - _colours[t] = c; + _fillcolours[t] = c; +} + +void LevelSystem::setEdgeColor(LevelSystem::Tile t, sf::Color c) +{ + _edgecolours[t] = c; } std::unique_ptr LevelSystem::_tiles; @@ -83,16 +98,20 @@ void LevelSystem::buildSprites(bool optimise) { sf::Vector2f p; sf::Vector2f s; sf::Color c; + sf::Color edge; }; vector tps; + vector e_tps; const auto tls = Vector2f(_tileSize, _tileSize); for (size_t y = 0; y < _height; ++y) { for (size_t x = 0; x < _width; ++x) { Tile t = getTile({x, y}); - if (t == EMPTY) { + if (t == EMPTY) + { + e_tps.push_back({getTilePosition({x,y}), tls, getColor(t), getEdgeColor(t)}); continue; } - tps.push_back({getTilePosition({x, y}), tls, getColor(t)}); + tps.push_back({getTilePosition({x, y}), tls, getColor(t), getEdgeColor(t)}); } } @@ -162,10 +181,22 @@ void LevelSystem::buildSprites(bool optimise) { auto s = make_unique(); s->setPosition(t.p); s->setSize(t.s); - s->setFillColor(Color::Red); s->setFillColor(t.c); + s->setOutlineColor(t.edge); + s->setOutlineThickness(-0.5f); // s->setFillColor(Color(rand()%255,rand()%255,rand()%255)); - _sprites.push_back(move(s)); + _sprites.push_back(std::move(s)); + } + + for (auto& t : e_tps) + { + auto s = make_unique(); + s->setPosition(t.p); + s->setSize(t.s); + s->setFillColor(t.c); + s->setOutlineColor(t.edge); + s->setOutlineThickness(-0.5f); + _sprites.push_back(std::move(s)); } cout << "Level with " << (_width * _height) << " Tiles, With " << nonempty diff --git a/lib_tile_level_loader/LevelSystem.h b/lib_tile_level_loader/LevelSystem.h index fe10cb7..f675367 100644 --- a/lib_tile_level_loader/LevelSystem.h +++ b/lib_tile_level_loader/LevelSystem.h @@ -41,8 +41,10 @@ class LevelSystem { static std::vector findTiles(Tile); static sf::Color getColor(Tile t); + static sf::Color getEdgeColor(LevelSystem::Tile t); static void setColor(Tile t, sf::Color c); + static void setEdgeColor(Tile t, sf::Color c); static void setOffset(const sf::Vector2f& _offset); @@ -61,7 +63,8 @@ class LevelSystem { static void buildSprites(bool optimise = true); static float _tileSize; // for rendering - static std::map _colours; + static std::map _fillcolours; + static std::map _edgecolours; private: LevelSystem() = delete; diff --git a/res/blank_level.txt b/res/blank_level.txt index 95577eb..f9de938 100644 --- a/res/blank_level.txt +++ b/res/blank_level.txt @@ -3,12 +3,12 @@ w w w w w w w w -w e w +w s e w w w w w w ww ww w w w w ww ww w -w s w w w +w w w w w w w w w w From 233f186594b42a0413cb137f47a5b5fbaa560173 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Thu, 30 Nov 2023 16:14:03 +0000 Subject: [PATCH 10/32] PLayer movement through the grid. --- CMakeLists.txt | 6 +-- .../components/cmp_actor_movement.cpp | 41 ++------------- game_project/components/cmp_actor_movement.h | 50 +------------------ game_project/scenes/blank.cpp | 14 +++++- res/blank_level.txt | 6 +-- 5 files changed, 23 insertions(+), 94 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 46c081e..d73185b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -155,11 +155,7 @@ set_target_properties(PRACTICAL_7_PLATFORMER file(GLOB_RECURSE SOURCES game_project/*.cpp game_project/*.h) file(GLOB_RECURSE CMPNTS game_project/components/*.cpp game_project/components/*.h) file(GLOB_RECURSE SCENES game_project/scenes/*.cpp game_project/scenes/*.h) -add_executable(GAME_PROJECT ${SOURCES} ${RESOURCE_FILES} - game_project/scenes/scene_menu.cpp - game_project/components/cmp_actor_movement.cpp - game_project/components/cmp_input.cpp - game_project/components/cmp_input.h) +add_executable(GAME_PROJECT ${SOURCES} ${RESOURCE_FILES}) source_group("components" FILES ${CMPNTS}) source_group("resources" FILES ${RESOURCE_FILES}) diff --git a/game_project/components/cmp_actor_movement.cpp b/game_project/components/cmp_actor_movement.cpp index 26dc6e6..cc1300a 100644 --- a/game_project/components/cmp_actor_movement.cpp +++ b/game_project/components/cmp_actor_movement.cpp @@ -9,8 +9,8 @@ void ActorMovementComponent::update(double dt) {} -ActorMovementComponent::ActorMovementComponent(Entity *p) - : _speed(1.f), Component(p) {} +ActorMovementComponent::ActorMovementComponent(Entity *p, float s) + : _speed(s), Component(p) {} // Check if the current movement is valid @@ -39,8 +39,9 @@ bool ActorMovementComponent::move(const sf::Vector2f & pos) return valid; } -PlayerMoveComponent::PlayerMoveComponent(Entity *p) : - ActorMovementComponent(p), _timer(0){} + +PlayerMoveComponent::PlayerMoveComponent(Entity *p, float s) : + ActorMovementComponent(p, s), _timer(0){} void PlayerMoveComponent::update(double dt) { @@ -57,35 +58,3 @@ void PlayerMoveComponent::update(double dt) } -//CommandComponent::CommandComponent(Entity *p, sf::Keyboard::Key k) : -// ActorMovementComponent(p), k_(k) { } -// -//double CommandComponent::_timer = 0.f; -// -//void CommandComponent::update(double dt) {CommandComponent::_timer -= dt;} -// -//MoveUpComponent::MoveUpComponent(Entity *p, sf::Keyboard::Key k) : -// CommandComponent(p, k) {} -// -//void MoveUpComponent::update(double dt) -//{ -// if (CommandComponent::_timer <= 0) -// if(sf::Keyboard::isKeyPressed(k_)) -// { -// move(0.f,-1.f); -// CommandComponent::_timer = _speed; -// } -//} -// -//MoveDownComponent::MoveDownComponent(Entity *p, sf::Keyboard::Key k) : -// CommandComponent(p, k) {} -// -//void MoveDownComponent::update(double dt) -//{ -// if (CommandComponent::_timer <= 0) -// if(sf::Keyboard::isKeyPressed(k_)) -// { -// move(0.f,1.f); -// CommandComponent::_timer = _speed; -// } -//} diff --git a/game_project/components/cmp_actor_movement.h b/game_project/components/cmp_actor_movement.h index 26b09f2..26d0cdd 100644 --- a/game_project/components/cmp_actor_movement.h +++ b/game_project/components/cmp_actor_movement.h @@ -6,12 +6,6 @@ #include #include "SFML/Window/Keyboard.hpp" -//class Command; -//class InputHandler; -//class MoveUpComponent; -//class MoveDownComponent; -//class MoveLeftComponent; -//class MoveRightComponent; class ActorMovementComponent : public Component { protected: @@ -24,7 +18,7 @@ class ActorMovementComponent : public Component { bool move(float x, float y); void render() override {} void setSpeed(float speed); - explicit ActorMovementComponent(Entity* p); + explicit ActorMovementComponent(Entity* p, float s); ActorMovementComponent() = delete; }; @@ -32,49 +26,9 @@ class PlayerMoveComponent : public ActorMovementComponent { public: void update(double dt) override; void render() override {}; - explicit PlayerMoveComponent(Entity *p); + explicit PlayerMoveComponent(Entity *p, float s = 1.f); PlayerMoveComponent()=delete; private: double _timer; }; - -//class CommandComponent : public ActorMovementComponent { -//public: -// explicit CommandComponent(Entity *p, sf::Keyboard::Key k); -// CommandComponent() = delete; -// void update(double dt) override; -// void bindKey(sf::Keyboard::Key k); -// -//protected: -// sf::Keyboard::Key k_; -// static double _timer; -//}; // Command -// -//class MoveUpComponent : public CommandComponent { -//public: -// explicit MoveUpComponent(Entity *p, sf::Keyboard::Key k); -// MoveUpComponent() = delete; -// void update(double dt) override; -//}; -// -//class MoveDownComponent : public CommandComponent { -//public: -// explicit MoveDownComponent(Entity *p, sf::Keyboard::Key k); -// MoveDownComponent() = delete; -// void update(double dt) override; -//}; - -//class MoveRightComponent : public CommandComponent { -//public: -// explicit MoveRightComponent(Entity *p, sf::Keyboard::Key k); -// MoveRightComponent() = delete; -// void update(double dt) override; -//}; -// -//class MoveLeftComponent : public CommandComponent { -//public: -// explicit MoveLeftComponent(Entity *p, sf::Keyboard::Key k); -// MoveLeftComponent() = delete; -// void update(double dt) override; -//}; diff --git a/game_project/scenes/blank.cpp b/game_project/scenes/blank.cpp index ed5d16b..0d5a512 100644 --- a/game_project/scenes/blank.cpp +++ b/game_project/scenes/blank.cpp @@ -20,6 +20,7 @@ void Blank::Load() { ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * 40.f))); auto spawn_offset = sf::Vector2f(20.f,20.f); + // PLAYER SPAWNING { player = makeEntity(); player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); @@ -29,8 +30,17 @@ void Blank::Load() { s->getShape().setFillColor(sf::Color::Green); s->getShape().setOrigin(sf::Vector2f (20.f,20.f)); - player->addComponent(); - player->get_components()[0]->setSpeed(0.5f); + player->addComponent(0.1f); + } + + // ENEMIES + { + auto enemy = makeEntity(); + enemy->setPosition(ls::getTilePosition(ls::findTiles(ls::ENEMY)[0]) + spawn_offset); + auto s = enemy->addComponent(); + s->setShape(sf::Vector2f(40.f, 40.f)); + s->getShape().setFillColor(sf::Color::Magenta); + s->getShape().setOrigin(sf::Vector2f (20.f, 20.f)); } setLoaded(true); diff --git a/res/blank_level.txt b/res/blank_level.txt index f9de938..ba80cac 100644 --- a/res/blank_level.txt +++ b/res/blank_level.txt @@ -3,8 +3,8 @@ w w w w w w w w -w s e w -w w w w +w e w +w sw w w w ww ww w w w w ww ww w @@ -13,6 +13,6 @@ w w w w w w w w -w w +w n w w w wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww From b7caa89c9f5e561b642987bce2ba19fcfed018dd Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Sat, 2 Dec 2023 14:56:35 +0000 Subject: [PATCH 11/32] Adjustments on LevelSystem for auto calculating the tile size depending on the level txt --- game_project/scenes/blank.cpp | 16 ++--- lib_tile_level_loader/LevelSystem.cpp | 4 +- res/blank_level.txt | 90 +++++++++++++++++++++------ 3 files changed, 83 insertions(+), 27 deletions(-) diff --git a/game_project/scenes/blank.cpp b/game_project/scenes/blank.cpp index 0d5a512..35dadf8 100644 --- a/game_project/scenes/blank.cpp +++ b/game_project/scenes/blank.cpp @@ -16,9 +16,11 @@ void Blank::Load() { std::cout << ">> BLANK CANVAS LOADING <<" << std::endl; // Importing Level from file - ls::loadLevelFile("res/blank_level.txt", 40.f); - ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * 40.f))); - auto spawn_offset = sf::Vector2f(20.f,20.f); + ls::loadLevelFile("res/blank_level.txt", float(Engine::getWindowSize().x)); + float tile_size = ls::getTileSize(); + ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); + auto spawn_offset = sf::Vector2f(tile_size/2, tile_size/2); + // PLAYER SPAWNING { @@ -26,9 +28,9 @@ void Blank::Load() { player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); std::cout << "OF: " << ls::getOffset().x << " " << ls::getOffset().y << std::endl; auto s = player->addComponent(); - s->setShape(sf::Vector2f(40.f, 40.f)); + s->setShape(sf::Vector2f(tile_size, tile_size)); s->getShape().setFillColor(sf::Color::Green); - s->getShape().setOrigin(sf::Vector2f (20.f,20.f)); + s->getShape().setOrigin(spawn_offset); player->addComponent(0.1f); } @@ -38,9 +40,9 @@ void Blank::Load() { auto enemy = makeEntity(); enemy->setPosition(ls::getTilePosition(ls::findTiles(ls::ENEMY)[0]) + spawn_offset); auto s = enemy->addComponent(); - s->setShape(sf::Vector2f(40.f, 40.f)); + s->setShape(sf::Vector2f(tile_size, tile_size)); s->getShape().setFillColor(sf::Color::Magenta); - s->getShape().setOrigin(sf::Vector2f (20.f, 20.f)); + s->getShape().setOrigin(spawn_offset); } setLoaded(true); diff --git a/lib_tile_level_loader/LevelSystem.cpp b/lib_tile_level_loader/LevelSystem.cpp index 8b677dc..3f0f523 100644 --- a/lib_tile_level_loader/LevelSystem.cpp +++ b/lib_tile_level_loader/LevelSystem.cpp @@ -43,8 +43,7 @@ Vector2f LevelSystem::_offset(0.0f, 30.0f); // Vector2f LevelSystem::_offset(0,0); vector> LevelSystem::_sprites; -void LevelSystem::loadLevelFile(const std::string& path, float tileSize) { - _tileSize = tileSize; +void LevelSystem::loadLevelFile(const std::string& path, float window_size) { size_t w = 0, h = 0; string buffer; @@ -86,6 +85,7 @@ void LevelSystem::loadLevelFile(const std::string& path, float tileSize) { _tiles = std::make_unique(w * h); _width = w; // set static class vars _height = h; + _tileSize = window_size/float(_width); std::copy(temp_tiles.begin(), temp_tiles.end(), &_tiles[0]); cout << "Level " << path << " Loaded. " << w << "x" << h << std::endl; buildSprites(); diff --git a/res/blank_level.txt b/res/blank_level.txt index ba80cac..b7251bc 100644 --- a/res/blank_level.txt +++ b/res/blank_level.txt @@ -1,18 +1,72 @@ -wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww -w w -w w -w w -w w -w e w -w sw w w -w ww ww w -w w -w ww ww w -w w w w -w w -w w -w w -w w -w n w -w w -wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +w w +w w +w w +w w +w e w +w sw w w +w ww ww w +w w +w ww ww w +w w w w +w w +w w +w w +w w +w n w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +w w +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww From 2f8876176a7a6a4bb9d325ad435a42bde217718a Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Sun, 3 Dec 2023 21:12:39 +0000 Subject: [PATCH 12/32] Implementation of neighbors function in Graph; Added method for finding coordinates of a tile. --- CMakeLists.txt | 3 +- game_project/ai/Graph.cpp | 47 +++++++++++++++++++++++++++ game_project/ai/Graph.h | 31 ++++++++++++++++++ game_project/main.cpp | 2 +- game_project/scenes/blank.cpp | 33 +++++++++++++++++-- game_project/scenes/blank.h | 1 + lib_tile_level_loader/LevelSystem.cpp | 6 ++++ lib_tile_level_loader/LevelSystem.h | 2 ++ 8 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 game_project/ai/Graph.cpp create mode 100644 game_project/ai/Graph.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d73185b..86e56e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -155,10 +155,11 @@ set_target_properties(PRACTICAL_7_PLATFORMER file(GLOB_RECURSE SOURCES game_project/*.cpp game_project/*.h) file(GLOB_RECURSE CMPNTS game_project/components/*.cpp game_project/components/*.h) file(GLOB_RECURSE SCENES game_project/scenes/*.cpp game_project/scenes/*.h) +file(GLOB_RECURSE AI game_project/ai/*.cpp game_project/ai/*.h) add_executable(GAME_PROJECT ${SOURCES} ${RESOURCE_FILES}) source_group("components" FILES ${CMPNTS}) source_group("resources" FILES ${RESOURCE_FILES}) - +source_group("ai" FILES ${AI}) target_link_libraries(GAME_PROJECT lib_engine) set(EXECUTABLES ${EXECUTABLES} GAME_PROJECT) diff --git a/game_project/ai/Graph.cpp b/game_project/ai/Graph.cpp new file mode 100644 index 0000000..38f1fc1 --- /dev/null +++ b/game_project/ai/Graph.cpp @@ -0,0 +1,47 @@ +// +// Created by rysan on 03/12/23. +// + +#include "Graph.h" + +// GRAPH --------------------------------------------------------------- +Graph::Graph(uint w, uint h) : _width(w), _height(h) {} + +std::array Graph::DIRS = { sf::Vector2f{1.f, 0.f}, // Right + sf::Vector2f{-1.f, 0.f}, // Left + sf::Vector2f{0.f, 1.f}, // Down + sf::Vector2f{0.f, -1.f} // UP +}; + +uint Graph::getHeight() { return _height; } +uint Graph::getWidth() { return _width; } + +void Graph::setHeight(uint h) { _height = h; } +void Graph::setWidth(uint w) { _width = w; } + +bool Graph::passable(sf::Vector2f coord) const +{ + return ls::getTile(sf::Vector2ul(coord)) != ls::WALL; +} + +bool Graph::in_bounds(sf::Vector2f coord) const +{ + return coord.x >= 0 && coord.x <= ls::getWidth() + && coord.y >= 0 && coord.y <= ls::getHeight(); +} + +std::vector Graph::neighbors(sf::Vector2ul me) const +{ + std::vector neighbors; + + for (auto dir : DIRS) + { + auto next = sf::Vector2f{me.x + dir.x, me.y + dir.y}; + if (in_bounds(next) && passable(next)) + neighbors.push_back(sf::Vector2ul(next)); + } + if (int(me.x + me.y) % 2 == 0) std::reverse(neighbors.begin(), neighbors.end()); + + return neighbors; +} +// GRAPH --------------------------------------------------------------- diff --git a/game_project/ai/Graph.h b/game_project/ai/Graph.h new file mode 100644 index 0000000..dbd0571 --- /dev/null +++ b/game_project/ai/Graph.h @@ -0,0 +1,31 @@ +// +// Created by rysan on 03/12/23. +// +#pragma once + +#include +#include +#include +#include + +struct Graph { + + uint _width; + uint _height; + + static std::array DIRS; + + Graph(uint w, uint h); + ~Graph() = default; + + bool passable(sf::Vector2f coord) const; + bool in_bounds(sf::Vector2f coord) const; + std::vector neighbors (sf::Vector2ul me) const; + + uint getHeight(); + uint getWidth(); + void setHeight(uint h); + void setWidth(uint w); +}; + + diff --git a/game_project/main.cpp b/game_project/main.cpp index 019aa7e..484b338 100644 --- a/game_project/main.cpp +++ b/game_project/main.cpp @@ -7,4 +7,4 @@ Blank blank; int main() { Engine::Start(1280, 720, "game debug mode", &menu); -} \ No newline at end of file +} diff --git a/game_project/scenes/blank.cpp b/game_project/scenes/blank.cpp index 35dadf8..7be9c61 100644 --- a/game_project/scenes/blank.cpp +++ b/game_project/scenes/blank.cpp @@ -6,22 +6,25 @@ #include "../game.h" #include "../components/cmp_sprite.h" #include "../components/cmp_actor_movement.h" +#include "../ai/Graph.h" #include #include #include static std::shared_ptr player; +// TESTING GRAPH NEIGHBORS ----------------------------- +static std::vector> arrows; +// TESTING GRAPH NEIGHBORS ----------------------------- void Blank::Load() { std::cout << ">> BLANK CANVAS LOADING <<" << std::endl; // Importing Level from file - ls::loadLevelFile("res/blank_level.txt", float(Engine::getWindowSize().x)); + ls::loadLevelFile("res/blank_level_small.txt", float(Engine::getWindowSize().x)); float tile_size = ls::getTileSize(); ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); auto spawn_offset = sf::Vector2f(tile_size/2, tile_size/2); - // PLAYER SPAWNING { player = makeEntity(); @@ -45,6 +48,20 @@ void Blank::Load() { s->getShape().setOrigin(spawn_offset); } + // TESTING GRAPH NEIGHBORS ----------------------------- + for (int i = 0; i < 4; i++) + { + auto a = makeEntity(); + auto s = a->addComponent(); + a->setVisible(false); + s->setShape(sf::Vector2f(tile_size/2, tile_size/2)); + s->getShape().setFillColor(sf::Color::Red); + s->getShape().setOrigin(10,10); + arrows.push_back(a); + } + // TESTING GRAPH NEIGHBORS ----------------------------- + + // Finishing touches setLoaded(true); std::cout <<">> BLANK CANVAS LOADED <<" << std::endl; } @@ -60,6 +77,18 @@ void Blank::UnLoad() { void Blank::Update(const double& dt) { if (ls::getTileAt(player->getPosition()) == ls::END) Engine::ChangeScene((Scene*)&menu); + + // TESTING GRAPH NEIGHBORS ----------------------------- + Graph g = Graph(ls::getWidth(), ls::getHeight()); + auto neigh = g.neighbors(ls::getTileCoord(player->getPosition())); + int i = -1; + for (auto a : arrows) a->setVisible(false); + for (auto n : neigh) + { + arrows[++i]->setPosition(ls::getTilePosition(sf::Vector2ul(n.x, n.y)) + sf::Vector2f(ls::getTileSize()/2, ls::getTileSize()/2)); + arrows[i]->setVisible(true); + } + // TESTING GRAPH NEIGHBORS ----------------------------- Scene::Update(dt); } diff --git a/game_project/scenes/blank.h b/game_project/scenes/blank.h index 5ef1126..43e556b 100644 --- a/game_project/scenes/blank.h +++ b/game_project/scenes/blank.h @@ -4,6 +4,7 @@ #pragma once #include "engine.h" +#include "../ai/Graph.h" class Blank : public Scene { public: diff --git a/lib_tile_level_loader/LevelSystem.cpp b/lib_tile_level_loader/LevelSystem.cpp index 3f0f523..cf7aaab 100644 --- a/lib_tile_level_loader/LevelSystem.cpp +++ b/lib_tile_level_loader/LevelSystem.cpp @@ -244,6 +244,12 @@ LevelSystem::Tile LevelSystem::getTileAt(Vector2f v) { return getTile(Vector2ul((v - _offset) / (_tileSize))); } +sf::Vector2ul LevelSystem::getTileCoord(Vector2f v) { + auto a = v - _offset; + if (a.x < 0 || a.y < 0) { std::cout << "TILE OUT OF RANGE!!" << std::endl; } + return Vector2ul((v - _offset) / _tileSize); +} + bool LevelSystem::isOnGrid(sf::Vector2f v) { auto a = v - _offset; if (a.x < 0 || a.y < 0) { diff --git a/lib_tile_level_loader/LevelSystem.h b/lib_tile_level_loader/LevelSystem.h index f675367..484726c 100644 --- a/lib_tile_level_loader/LevelSystem.h +++ b/lib_tile_level_loader/LevelSystem.h @@ -28,6 +28,8 @@ class LevelSystem { static Tile getTile(sf::Vector2ul); + static sf::Vector2ul getTileCoord(sf::Vector2f); + static Tile getTileAt(sf::Vector2f); static bool isOnGrid(sf::Vector2f); From bf3c6ba6754fc6380a1415eb2c1d35f3653e32a7 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Mon, 4 Dec 2023 18:55:53 +0000 Subject: [PATCH 13/32] BFS working for pathfinding. --- game_project/ai/Graph.h | 2 - .../components/cmp_actor_movement.cpp | 1 + game_project/components/cmp_actor_movement.h | 4 +- game_project/components/cmp_bfs_ai.cpp | 81 +++++++++++++++++++ game_project/components/cmp_bfs_ai.h | 29 +++++++ game_project/scenes/blank.cpp | 53 ++++++------ game_project/scenes/blank.h | 1 - lib_ecm/ecm.h | 1 + lib_maths/maths.h | 12 ++- lib_tile_level_loader/LevelSystem.cpp | 4 +- 10 files changed, 157 insertions(+), 31 deletions(-) create mode 100644 game_project/components/cmp_bfs_ai.cpp create mode 100644 game_project/components/cmp_bfs_ai.h diff --git a/game_project/ai/Graph.h b/game_project/ai/Graph.h index dbd0571..3f15997 100644 --- a/game_project/ai/Graph.h +++ b/game_project/ai/Graph.h @@ -3,13 +3,11 @@ // #pragma once -#include #include #include #include struct Graph { - uint _width; uint _height; diff --git a/game_project/components/cmp_actor_movement.cpp b/game_project/components/cmp_actor_movement.cpp index cc1300a..814cd4e 100644 --- a/game_project/components/cmp_actor_movement.cpp +++ b/game_project/components/cmp_actor_movement.cpp @@ -39,6 +39,7 @@ bool ActorMovementComponent::move(const sf::Vector2f & pos) return valid; } +// **** PLAYER **** PlayerMoveComponent::PlayerMoveComponent(Entity *p, float s) : ActorMovementComponent(p, s), _timer(0){} diff --git a/game_project/components/cmp_actor_movement.h b/game_project/components/cmp_actor_movement.h index 26d0cdd..3e187ae 100644 --- a/game_project/components/cmp_actor_movement.h +++ b/game_project/components/cmp_actor_movement.h @@ -18,7 +18,7 @@ class ActorMovementComponent : public Component { bool move(float x, float y); void render() override {} void setSpeed(float speed); - explicit ActorMovementComponent(Entity* p, float s); + explicit ActorMovementComponent(Entity* p, float s = 1.f); ActorMovementComponent() = delete; }; @@ -31,4 +31,4 @@ class PlayerMoveComponent : public ActorMovementComponent { private: double _timer; -}; +}; \ No newline at end of file diff --git a/game_project/components/cmp_bfs_ai.cpp b/game_project/components/cmp_bfs_ai.cpp new file mode 100644 index 0000000..9cc62d9 --- /dev/null +++ b/game_project/components/cmp_bfs_ai.cpp @@ -0,0 +1,81 @@ +// +// Created by rysan on 04/12/23. +// + +#include "cmp_bfs_ai.h" +#include +#include + +std::unordered_map breadth_first_search(Graph *graph, sf::Vector2ul start, sf::Vector2ul goal) +{ + std::queue frontier; + std::unordered_map came_from; + + frontier.push(start); + // Store the path to the goal for each tile + came_from[start] = start; + + while(!frontier.empty()) + { + sf::Vector2ul current = frontier.front(); + frontier.pop(); + + if(current == goal) { break; } // Found the target + + for(sf::Vector2ul next_dir : graph->neighbors(current)) { + if (came_from.find(next_dir) == came_from.end()) { + frontier.push(next_dir); + came_from[next_dir] = current; + } + } + } + return came_from; +} + +AIBFSComponent::AIBFSComponent(Entity *p, const std::shared_ptr& g, sf::Vector2ul goal) + : ActorMovementComponent(p, 0.8f), G_(g), goal_(goal), _mtimer(2.f), goal_changed(false) {} + +void AIBFSComponent::update(double dt) +{ + search_timer_ -= dt; + _mtimer -= dt; + sf::Vector2ul my_coord = ls::getTileCoord(_parent->getPosition()); + + // Search the target + if(search_timer_ <= 0 && goal_changed) + { + current_path_ = breadth_first_search(G_.get(), my_coord, goal_); + goal_changed = false; + } + // Moves towards it + if(_mtimer <= 0) + { + sf::Vector2ul next = goal_; + + while(true) + { + if(current_path_[next] != my_coord) + { + next = current_path_[next]; + } + else break; + } + + sf::Vector2f dir(0.f,0.f); + + if(next.y < my_coord.y) dir.y = -1.f; + if(next.y > my_coord.y) dir.y = 1.f; + if(next.x < my_coord.x) dir.x = -1.f; + if(next.x > my_coord.x) dir.x = 1.f; + + std::cout << "CP= " << next.x << next.y << std::endl; + + move(dir.x, dir.y); + _mtimer = _speed; + } +} + +void AIBFSComponent::setGoal(sf::Vector2ul new_goal) +{ + if (new_goal != goal_) { goal_ = new_goal; goal_changed=true; } +} diff --git a/game_project/components/cmp_bfs_ai.h b/game_project/components/cmp_bfs_ai.h new file mode 100644 index 0000000..e22593c --- /dev/null +++ b/game_project/components/cmp_bfs_ai.h @@ -0,0 +1,29 @@ +// +// Created by rysan on 04/12/23. +// +#pragma once + +#include +#include +#include "../ai/Graph.h" +#include "../components/cmp_actor_movement.h" + +class AIBFSComponent : public ActorMovementComponent { +public: + AIBFSComponent() = delete; + explicit AIBFSComponent(Entity *p, const std::shared_ptr& g, sf::Vector2ul goal); + ~AIBFSComponent() override = default; + + void update(double dt) override; + void setGoal(sf::Vector2ul new_goal); + void render() override {} + +private: + std::shared_ptr G_; + double search_timer_ = 1.5f; + double _mtimer; + sf::Vector2ul goal_; + bool goal_changed; + std::unordered_map current_path_; +}; + diff --git a/game_project/scenes/blank.cpp b/game_project/scenes/blank.cpp index 7be9c61..3b1d7c2 100644 --- a/game_project/scenes/blank.cpp +++ b/game_project/scenes/blank.cpp @@ -1,19 +1,18 @@ // // Created by rysan on 29/11/23. // - #include "blank.h" #include "../game.h" #include "../components/cmp_sprite.h" #include "../components/cmp_actor_movement.h" -#include "../ai/Graph.h" +#include "../components/cmp_bfs_ai.h" #include #include #include static std::shared_ptr player; // TESTING GRAPH NEIGHBORS ----------------------------- -static std::vector> arrows; +//static std::vector> arrows; // TESTING GRAPH NEIGHBORS ----------------------------- void Blank::Load() { @@ -25,6 +24,7 @@ void Blank::Load() { ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); auto spawn_offset = sf::Vector2f(tile_size/2, tile_size/2); + // PLAYER SPAWNING { player = makeEntity(); @@ -32,8 +32,8 @@ void Blank::Load() { std::cout << "OF: " << ls::getOffset().x << " " << ls::getOffset().y << std::endl; auto s = player->addComponent(); s->setShape(sf::Vector2f(tile_size, tile_size)); - s->getShape().setFillColor(sf::Color::Green); s->getShape().setOrigin(spawn_offset); + s->getShape().setFillColor(sf::Color::Green); player->addComponent(0.1f); } @@ -41,24 +41,30 @@ void Blank::Load() { // ENEMIES { auto enemy = makeEntity(); + auto G = std::make_shared(ls::getWidth(), ls::getHeight()); enemy->setPosition(ls::getTilePosition(ls::findTiles(ls::ENEMY)[0]) + spawn_offset); auto s = enemy->addComponent(); s->setShape(sf::Vector2f(tile_size, tile_size)); s->getShape().setFillColor(sf::Color::Magenta); s->getShape().setOrigin(spawn_offset); + + enemy->addComponent(); + enemy->addComponent(G, ls::getTileCoord(player->getPosition())); + + enemy->addTag("enemy"); } // TESTING GRAPH NEIGHBORS ----------------------------- - for (int i = 0; i < 4; i++) - { - auto a = makeEntity(); - auto s = a->addComponent(); - a->setVisible(false); - s->setShape(sf::Vector2f(tile_size/2, tile_size/2)); - s->getShape().setFillColor(sf::Color::Red); - s->getShape().setOrigin(10,10); - arrows.push_back(a); - } + //for (int i = 0; i < 4; i++) + //{ + // auto a = makeEntity(); + // auto s = a->addComponent(); + // a->setVisible(false); + // s->setShape(sf::Vector2f(tile_size/2, tile_size/2)); + // s->getShape().setFillColor(sf::Color::Red); + // s->getShape().setOrigin(10,10); + // arrows.push_back(a); + //} // TESTING GRAPH NEIGHBORS ----------------------------- // Finishing touches @@ -79,16 +85,17 @@ void Blank::Update(const double& dt) { Engine::ChangeScene((Scene*)&menu); // TESTING GRAPH NEIGHBORS ----------------------------- - Graph g = Graph(ls::getWidth(), ls::getHeight()); - auto neigh = g.neighbors(ls::getTileCoord(player->getPosition())); - int i = -1; - for (auto a : arrows) a->setVisible(false); - for (auto n : neigh) - { - arrows[++i]->setPosition(ls::getTilePosition(sf::Vector2ul(n.x, n.y)) + sf::Vector2f(ls::getTileSize()/2, ls::getTileSize()/2)); - arrows[i]->setVisible(true); - } + //Graph g = Graph(ls::getWidth(), ls::getHeight()); + //auto neigh = g.neighbors(ls::getTileCoord(player->getPosition())); + //int i = -1; + //for (auto a : arrows) a->setVisible(false); + //for (auto n : neigh) + //{ + // arrows[++i]->setPosition(ls::getTilePosition(sf::Vector2ul(n.x, n.y)) + sf::Vector2f(ls::getTileSize()/2, ls::getTileSize()/2)); + // arrows[i]->setVisible(true); + //} // TESTING GRAPH NEIGHBORS ----------------------------- + ents.find("enemy")[0]->get_components()[0]->setGoal(ls::getTileCoord(player->getPosition())); Scene::Update(dt); } diff --git a/game_project/scenes/blank.h b/game_project/scenes/blank.h index 43e556b..5ef1126 100644 --- a/game_project/scenes/blank.h +++ b/game_project/scenes/blank.h @@ -4,7 +4,6 @@ #pragma once #include "engine.h" -#include "../ai/Graph.h" class Blank : public Scene { public: diff --git a/lib_ecm/ecm.h b/lib_ecm/ecm.h index 1675f94..f260b6f 100644 --- a/lib_ecm/ecm.h +++ b/lib_ecm/ecm.h @@ -63,6 +63,7 @@ class Entity { // const sf::Vector2f& getPosition() const; + const sf::Vector2ul& getCoord() const; void setPosition(const sf::Vector2f& _position); diff --git a/lib_maths/maths.h b/lib_maths/maths.h index b0aa819..4b3ab80 100644 --- a/lib_maths/maths.h +++ b/lib_maths/maths.h @@ -8,6 +8,7 @@ #include #include + namespace sf { typedef Vector2 Vector2ul; @@ -84,4 +85,13 @@ std::string toStrDecPt(const uint16_t& dp, const T& i) { std::stringstream stream; stream << std::fixed << std::setprecision(dp) << i; return stream.str(); -} \ No newline at end of file +} + +// In order to implement the unordered_map +namespace std { + template <> struct hash { + std::size_t operator()(const sf::Vector2ul& id) const noexcept { + return std::hash()(id.x ^ (id.y << 16)); + } + }; +} diff --git a/lib_tile_level_loader/LevelSystem.cpp b/lib_tile_level_loader/LevelSystem.cpp index cf7aaab..d4cfaee 100644 --- a/lib_tile_level_loader/LevelSystem.cpp +++ b/lib_tile_level_loader/LevelSystem.cpp @@ -211,8 +211,8 @@ void LevelSystem::render(RenderWindow& window) { LevelSystem::Tile LevelSystem::getTile(sf::Vector2ul p) { if (p.x > _width || p.y > _height) { - throw string("Tile out of range: ") + to_string(p.x) + "," + - to_string(p.y) + ")"; + cout << "TILE OUT OF RANGE: " << p.x << "," << p.y << endl; + throw ""; } return _tiles[(p.y * _width) + p.x]; } From 221fbdd55cccfb22dcf5be313b27a9e6e3c5c500 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Mon, 4 Dec 2023 21:28:41 +0000 Subject: [PATCH 14/32] stable version with pathfinding done. --- game_project/components/cmp_actor_movement.cpp | 4 ++++ game_project/components/cmp_actor_movement.h | 1 + game_project/components/cmp_bfs_ai.cpp | 4 +--- game_project/scenes/blank.cpp | 5 ++--- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/game_project/components/cmp_actor_movement.cpp b/game_project/components/cmp_actor_movement.cpp index 814cd4e..0534917 100644 --- a/game_project/components/cmp_actor_movement.cpp +++ b/game_project/components/cmp_actor_movement.cpp @@ -39,6 +39,10 @@ bool ActorMovementComponent::move(const sf::Vector2f & pos) return valid; } +float ActorMovementComponent::getSpeed() const { + return _speed; +} + // **** PLAYER **** PlayerMoveComponent::PlayerMoveComponent(Entity *p, float s) : diff --git a/game_project/components/cmp_actor_movement.h b/game_project/components/cmp_actor_movement.h index 3e187ae..9c6c183 100644 --- a/game_project/components/cmp_actor_movement.h +++ b/game_project/components/cmp_actor_movement.h @@ -18,6 +18,7 @@ class ActorMovementComponent : public Component { bool move(float x, float y); void render() override {} void setSpeed(float speed); + float getSpeed() const; explicit ActorMovementComponent(Entity* p, float s = 1.f); ActorMovementComponent() = delete; }; diff --git a/game_project/components/cmp_bfs_ai.cpp b/game_project/components/cmp_bfs_ai.cpp index 9cc62d9..6055e91 100644 --- a/game_project/components/cmp_bfs_ai.cpp +++ b/game_project/components/cmp_bfs_ai.cpp @@ -48,7 +48,7 @@ void AIBFSComponent::update(double dt) goal_changed = false; } // Moves towards it - if(_mtimer <= 0) + if(_mtimer <= 0 && my_coord != goal_) { sf::Vector2ul next = goal_; @@ -68,8 +68,6 @@ void AIBFSComponent::update(double dt) if(next.x < my_coord.x) dir.x = -1.f; if(next.x > my_coord.x) dir.x = 1.f; - std::cout << "CP= " << next.x << next.y << std::endl; - move(dir.x, dir.y); _mtimer = _speed; } diff --git a/game_project/scenes/blank.cpp b/game_project/scenes/blank.cpp index 3b1d7c2..3e7b2ef 100644 --- a/game_project/scenes/blank.cpp +++ b/game_project/scenes/blank.cpp @@ -48,9 +48,8 @@ void Blank::Load() { s->getShape().setFillColor(sf::Color::Magenta); s->getShape().setOrigin(spawn_offset); - enemy->addComponent(); - enemy->addComponent(G, ls::getTileCoord(player->getPosition())); - + auto ai = enemy->addComponent(G, ls::getTileCoord(player->getPosition())); + ai->setSpeed(0.15f); enemy->addTag("enemy"); } From 4b1c64b522c2b3b7ebb6c60f389d895cdedfe834 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Mon, 11 Dec 2023 16:21:02 +0000 Subject: [PATCH 15/32] Pursuer Component, lvl1, lvl2 --- game_project/ai/Graph.cpp | 1 + game_project/components/cmp_pursuer_ai.cpp | 45 ++++++++ game_project/components/cmp_pursuer_ai.h | 22 ++++ game_project/game.h | 6 +- game_project/main.cpp | 3 +- game_project/scenes/blank.cpp | 104 ------------------ game_project/scenes/scene_lvl1.cpp | 79 +++++++++++++ game_project/scenes/{blank.h => scene_lvl1.h} | 2 +- game_project/scenes/scene_lvl2.cpp | 83 ++++++++++++++ game_project/scenes/scene_lvl2.h | 14 +++ game_project/scenes/scene_menu.cpp | 2 +- lib_ecm/ecm.h | 1 - res/lvls/lvl4.txt | 36 ++++++ 13 files changed, 288 insertions(+), 110 deletions(-) create mode 100644 game_project/components/cmp_pursuer_ai.cpp create mode 100644 game_project/components/cmp_pursuer_ai.h delete mode 100644 game_project/scenes/blank.cpp create mode 100644 game_project/scenes/scene_lvl1.cpp rename game_project/scenes/{blank.h => scene_lvl1.h} (86%) create mode 100644 game_project/scenes/scene_lvl2.cpp create mode 100644 game_project/scenes/scene_lvl2.h create mode 100644 res/lvls/lvl4.txt diff --git a/game_project/ai/Graph.cpp b/game_project/ai/Graph.cpp index 38f1fc1..bcc6df8 100644 --- a/game_project/ai/Graph.cpp +++ b/game_project/ai/Graph.cpp @@ -21,6 +21,7 @@ void Graph::setWidth(uint w) { _width = w; } bool Graph::passable(sf::Vector2f coord) const { + return ls::getTile(sf::Vector2ul(coord)) != ls::WALL; } diff --git a/game_project/components/cmp_pursuer_ai.cpp b/game_project/components/cmp_pursuer_ai.cpp new file mode 100644 index 0000000..c51c62b --- /dev/null +++ b/game_project/components/cmp_pursuer_ai.cpp @@ -0,0 +1,45 @@ +// +// Created by rysan on 11/12/23. +// + +#include "cmp_pursuer_ai.h" +#include +#include + +void PursuerAIComponent::update(double dt) +{ + _mtimer -= dt; + if (auto pl = _player.lock()) + { + if(_mtimer < 0) { + if (_trail.size() < 1024) _trail.push_back(ls::getTileCoord(pl->getPosition())); + else _trail.erase(_trail.begin()); + + if(_active) + { + sf::Vector2f dir(0.f, 0.f); + + sf::Vector2ul next = _trail[0]; + _trail.erase(_trail.begin()); + sf::Vector2ul my_coord = ls::getTileCoord(_parent->getPosition()); + + if (next.y < my_coord.y) dir.y = -1.f; + if (next.y > my_coord.y) dir.y = 1.f; + if (next.x < my_coord.x) dir.x = -1.f; + if (next.x > my_coord.x) dir.x = 1.f; + + move(dir.x, dir.y); + } + _mtimer = _speed; + } + } +} + +bool PursuerAIComponent::isActive() { return _active; } +void PursuerAIComponent::setActive(bool activate) { _active = activate; } + +PursuerAIComponent::PursuerAIComponent(Entity *p) : + ActorMovementComponent(p), _player(_parent->scene->ents.find("player")[0]), _active(false) + { + setSpeed(_player.lock()->GetCompatibleComponent()[0]->getSpeed()); + } \ No newline at end of file diff --git a/game_project/components/cmp_pursuer_ai.h b/game_project/components/cmp_pursuer_ai.h new file mode 100644 index 0000000..9a3edb9 --- /dev/null +++ b/game_project/components/cmp_pursuer_ai.h @@ -0,0 +1,22 @@ +// +// Created by rysan on 11/12/23. +// +#pragma once +#include "cmp_actor_movement.h" + +class PursuerAIComponent : public ActorMovementComponent { +protected: + std::weak_ptr _player; + std::vector _trail; + double _mtimer = 0; + bool _active; + +public: + void update(double dt) override; + void render() override {} + bool isActive(); + void setActive(bool activate); + explicit PursuerAIComponent(Entity* p); + PursuerAIComponent() = delete; +}; + diff --git a/game_project/game.h b/game_project/game.h index 439bd0d..2da54ac 100644 --- a/game_project/game.h +++ b/game_project/game.h @@ -3,9 +3,11 @@ // #pragma once -#include "scenes/blank.h" #include "scenes/scene_menu.h" +#include "scenes/scene_lvl1.h" +#include "scenes/scene_lvl2.h" extern MenuScene menu; -extern Blank blank; +extern SceneLVL1 lvl1; +extern SceneLVL2 lvl2; diff --git a/game_project/main.cpp b/game_project/main.cpp index 484b338..7d3fbdb 100644 --- a/game_project/main.cpp +++ b/game_project/main.cpp @@ -3,7 +3,8 @@ #include "scenes/scene_menu.h" MenuScene menu; -Blank blank; +SceneLVL1 lvl1; +SceneLVL2 lvl2; int main() { Engine::Start(1280, 720, "game debug mode", &menu); diff --git a/game_project/scenes/blank.cpp b/game_project/scenes/blank.cpp deleted file mode 100644 index 3e7b2ef..0000000 --- a/game_project/scenes/blank.cpp +++ /dev/null @@ -1,104 +0,0 @@ -// -// Created by rysan on 29/11/23. -// -#include "blank.h" -#include "../game.h" -#include "../components/cmp_sprite.h" -#include "../components/cmp_actor_movement.h" -#include "../components/cmp_bfs_ai.h" -#include -#include -#include - -static std::shared_ptr player; -// TESTING GRAPH NEIGHBORS ----------------------------- -//static std::vector> arrows; -// TESTING GRAPH NEIGHBORS ----------------------------- - -void Blank::Load() { - std::cout << ">> BLANK CANVAS LOADING <<" << std::endl; - - // Importing Level from file - ls::loadLevelFile("res/blank_level_small.txt", float(Engine::getWindowSize().x)); - float tile_size = ls::getTileSize(); - ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); - auto spawn_offset = sf::Vector2f(tile_size/2, tile_size/2); - - - // PLAYER SPAWNING - { - player = makeEntity(); - player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); - std::cout << "OF: " << ls::getOffset().x << " " << ls::getOffset().y << std::endl; - auto s = player->addComponent(); - s->setShape(sf::Vector2f(tile_size, tile_size)); - s->getShape().setOrigin(spawn_offset); - s->getShape().setFillColor(sf::Color::Green); - - player->addComponent(0.1f); - } - - // ENEMIES - { - auto enemy = makeEntity(); - auto G = std::make_shared(ls::getWidth(), ls::getHeight()); - enemy->setPosition(ls::getTilePosition(ls::findTiles(ls::ENEMY)[0]) + spawn_offset); - auto s = enemy->addComponent(); - s->setShape(sf::Vector2f(tile_size, tile_size)); - s->getShape().setFillColor(sf::Color::Magenta); - s->getShape().setOrigin(spawn_offset); - - auto ai = enemy->addComponent(G, ls::getTileCoord(player->getPosition())); - ai->setSpeed(0.15f); - enemy->addTag("enemy"); - } - - // TESTING GRAPH NEIGHBORS ----------------------------- - //for (int i = 0; i < 4; i++) - //{ - // auto a = makeEntity(); - // auto s = a->addComponent(); - // a->setVisible(false); - // s->setShape(sf::Vector2f(tile_size/2, tile_size/2)); - // s->getShape().setFillColor(sf::Color::Red); - // s->getShape().setOrigin(10,10); - // arrows.push_back(a); - //} - // TESTING GRAPH NEIGHBORS ----------------------------- - - // Finishing touches - setLoaded(true); - std::cout <<">> BLANK CANVAS LOADED <<" << std::endl; -} - -void Blank::UnLoad() { - std::cout << ">> BLANK CANVAS UNLOAD <<" << std::endl; - // REMOVE PLAYER OR ANYTHING IN THE CANVAS - player.reset(); - ls::unload(); - Scene::UnLoad(); -} - -void Blank::Update(const double& dt) { - if (ls::getTileAt(player->getPosition()) == ls::END) - Engine::ChangeScene((Scene*)&menu); - - // TESTING GRAPH NEIGHBORS ----------------------------- - //Graph g = Graph(ls::getWidth(), ls::getHeight()); - //auto neigh = g.neighbors(ls::getTileCoord(player->getPosition())); - //int i = -1; - //for (auto a : arrows) a->setVisible(false); - //for (auto n : neigh) - //{ - // arrows[++i]->setPosition(ls::getTilePosition(sf::Vector2ul(n.x, n.y)) + sf::Vector2f(ls::getTileSize()/2, ls::getTileSize()/2)); - // arrows[i]->setVisible(true); - //} - // TESTING GRAPH NEIGHBORS ----------------------------- - ents.find("enemy")[0]->get_components()[0]->setGoal(ls::getTileCoord(player->getPosition())); - Scene::Update(dt); -} - -void Blank::Render() { - ls::render(Engine::GetWindow()); - Scene::Render(); -} \ No newline at end of file diff --git a/game_project/scenes/scene_lvl1.cpp b/game_project/scenes/scene_lvl1.cpp new file mode 100644 index 0000000..281f7cd --- /dev/null +++ b/game_project/scenes/scene_lvl1.cpp @@ -0,0 +1,79 @@ +// +// Created by rysan on 29/11/23. +// +#include "scene_lvl1.h" +#include "../game.h" +#include "../components/cmp_sprite.h" +#include "../components/cmp_actor_movement.h" +#include "../components/cmp_bfs_ai.h" +#include +#include + +static std::shared_ptr player; + +void SceneLVL1::Load() { + std::cout << ">> LVL 1 LOADING <<" << std::endl; + + // Importing Level from file + ls::loadLevelFile("res/lvls/lvl1.txt", float(Engine::getWindowSize().x)); + float tile_size = ls::getTileSize(); + ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); + auto spawn_offset = sf::Vector2f(tile_size/2, tile_size/2); + + + // PLAYER SPAWNING + { + player = makeEntity(); + player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); + std::cout << "OF: " << ls::getOffset().x << " " << ls::getOffset().y << std::endl; + auto s = player->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setOrigin(spawn_offset); + s->getShape().setFillColor(sf::Color::Green); + + player->addComponent(0.1f); + } + + // ENEMIES + { + for(auto& t : ls::findTiles(ls::ENEMY)) + { + auto enemy = makeEntity(); + auto G = std::make_shared(ls::getWidth(), ls::getHeight()); + enemy->setPosition(ls::getTilePosition(t) + spawn_offset); + auto s = enemy->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setFillColor(sf::Color::Magenta); + s->getShape().setOrigin(spawn_offset); + + auto ai = enemy->addComponent(G, ls::getTileCoord(player->getPosition())); + ai->setSpeed(0.15f); + enemy->addTag("enemy"); + } + } + + // Finishing touches + setLoaded(true); + std::cout <<">> BLANK CANVAS LOADED <<" << std::endl; +} + +void SceneLVL1::UnLoad() { + std::cout << ">> BLANK CANVAS UNLOAD <<" << std::endl; + // REMOVE PLAYER OR ANYTHING IN THE CANVAS + player.reset(); + ls::unload(); + Scene::UnLoad(); +} + +void SceneLVL1::Update(const double& dt) { + for (const auto& e : ents.find("enemy")) + e->get_components()[0]->setGoal(ls::getTileCoord(player->getPosition())); + Scene::Update(dt); + if (ls::getTileAt(player->getPosition()) == ls::END) + Engine::ChangeScene((Scene*)&lvl2); +} + +void SceneLVL1::Render() { + ls::render(Engine::GetWindow()); + Scene::Render(); +} \ No newline at end of file diff --git a/game_project/scenes/blank.h b/game_project/scenes/scene_lvl1.h similarity index 86% rename from game_project/scenes/blank.h rename to game_project/scenes/scene_lvl1.h index 5ef1126..8fbd49b 100644 --- a/game_project/scenes/blank.h +++ b/game_project/scenes/scene_lvl1.h @@ -5,7 +5,7 @@ #include "engine.h" -class Blank : public Scene { +class SceneLVL1 : public Scene { public: void Load() override; void UnLoad() override; diff --git a/game_project/scenes/scene_lvl2.cpp b/game_project/scenes/scene_lvl2.cpp new file mode 100644 index 0000000..a9476d5 --- /dev/null +++ b/game_project/scenes/scene_lvl2.cpp @@ -0,0 +1,83 @@ +// +// Created by rysan on 11/12/23. +// + +#include "scene_lvl2.h" +#include "../game.h" +#include "../components/cmp_sprite.h" +#include "../components/cmp_pursuer_ai.h" +#include +#include + +static std::shared_ptr player; +double timer; + +void SceneLVL2::Load() +{ + std::cout << ">> LVL 2 LOADING <<" << std::endl; + + ls::loadLevelFile("res/lvls/lvl2.txt", float(Engine::getWindowSize().x)); + float tile_size = ls::getTileSize(); + ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); + sf::Vector2f spawn_offset (tile_size/2, tile_size/2); + + // PLAYER SPAWNING + { + player = makeEntity(); + player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); + std::cout << "OF: " << ls::getOffset().x << " " << ls::getOffset().y << std::endl; + auto s = player->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setOrigin(spawn_offset); + s->getShape().setFillColor(sf::Color::Green); + + player->addComponent(0.1f); + player->addTag("player"); + } + + + // ENEMIES + { + auto enemy = makeEntity(); + enemy->setPosition(player->getPosition()); + auto s = enemy->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setFillColor(sf::Color::Magenta); + s->getShape().setOrigin(spawn_offset); + + enemy->addComponent(); + enemy->addTag("enemy"); + } + + // Collectibles + + + setLoaded(true); + std::cout << " LVL 2 LOADED " << std::endl; +} + +void SceneLVL2::UnLoad() +{ + std::cout << ">> LVL 2 UNLOAD << " << std::endl; + player.reset(); + ls::unload(); + Scene::UnLoad(); +} + +void SceneLVL2::Update(const double &dt) +{ + timer+= dt; + auto enemy_ai = ents.find("enemy")[0]->get_components()[0]; + if(timer > 1 && !enemy_ai->isActive()) enemy_ai->setActive(true); + + if(ls::getTileAt(player->getPosition())==ls::END) Engine::ChangeScene((Scene*)&menu); + + Scene::Update(dt); +} + +void SceneLVL2::Render() +{ + ls::render(Engine::GetWindow()); + Scene::Render(); +} + diff --git a/game_project/scenes/scene_lvl2.h b/game_project/scenes/scene_lvl2.h new file mode 100644 index 0000000..4e3a38f --- /dev/null +++ b/game_project/scenes/scene_lvl2.h @@ -0,0 +1,14 @@ +// +// Created by rysan on 11/12/23. +// +#pragma once +#include "engine.h" + +class SceneLVL2 : public Scene { +public: + void Load() override; + void UnLoad() override; + void Update(const double& dt) override; + void Render() override; +}; + diff --git a/game_project/scenes/scene_menu.cpp b/game_project/scenes/scene_menu.cpp index 801cca9..9459815 100644 --- a/game_project/scenes/scene_menu.cpp +++ b/game_project/scenes/scene_menu.cpp @@ -21,7 +21,7 @@ void MenuScene::Update(const double& dt) { // cout << "Menu Update "< Date: Mon, 11 Dec 2023 16:21:35 +0000 Subject: [PATCH 16/32] Pursuer Component, lvl1, lvl2 --- res/lvls/lvl1.txt | 18 ++++++++++++++++++ res/lvls/lvl2.txt | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 res/lvls/lvl1.txt create mode 100644 res/lvls/lvl2.txt diff --git a/res/lvls/lvl1.txt b/res/lvls/lvl1.txt new file mode 100644 index 0000000..a093c79 --- /dev/null +++ b/res/lvls/lvl1.txt @@ -0,0 +1,18 @@ +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +w n n n n n n n n n w +w w +w w +w w +w w +w w +w w +w ee w +w s ee w +w w +w w +w w +w w +w w +w w +w n n n n n n n n n w +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww diff --git a/res/lvls/lvl2.txt b/res/lvls/lvl2.txt new file mode 100644 index 0000000..fcf2a3c --- /dev/null +++ b/res/lvls/lvl2.txt @@ -0,0 +1,18 @@ +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +w w +w w +w w +w w +w w +w w +w w +w ee w +w s ee w +w w +w w +w w +w w +w w +w n w +w w +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww From 81aa4fedc42e4633e6eb6c010e037d97e71fae07 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Mon, 11 Dec 2023 17:44:22 +0000 Subject: [PATCH 17/32] HurtComponent plus Pursuer, lvl2, changing scenes --- game_project/components/cmp_hurt_player.cpp | 17 +++++++++++++++++ game_project/components/cmp_hurt_player.h | 14 ++++++++++++++ game_project/main.cpp | 2 +- game_project/scenes/scene_lvl1.cpp | 12 +++++++++--- game_project/scenes/scene_lvl2.cpp | 16 +++++++++++++--- 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 game_project/components/cmp_hurt_player.cpp create mode 100644 game_project/components/cmp_hurt_player.h diff --git a/game_project/components/cmp_hurt_player.cpp b/game_project/components/cmp_hurt_player.cpp new file mode 100644 index 0000000..b154489 --- /dev/null +++ b/game_project/components/cmp_hurt_player.cpp @@ -0,0 +1,17 @@ +#include "cmp_hurt_player.h" +#include + +using namespace std; +using namespace sf; + +void HurtComponent::update(double dt) { + if (auto pl = _player.lock()) { + if (length(pl->getPosition() - _parent->getPosition()) < 25.0) { + pl->setForDelete(); + _parent->setForDelete(); + } + } +} + +HurtComponent::HurtComponent(Entity* p) + : Component(p), _player(_parent->scene->ents.find("player")[0]) {} diff --git a/game_project/components/cmp_hurt_player.h b/game_project/components/cmp_hurt_player.h new file mode 100644 index 0000000..24a3218 --- /dev/null +++ b/game_project/components/cmp_hurt_player.h @@ -0,0 +1,14 @@ +#pragma once +#include + +class HurtComponent : public Component { +protected: + void fire() const; + std::weak_ptr _player; + +public: + void update(double dt) override; + void render() override {} + explicit HurtComponent(Entity* p); + HurtComponent() = delete; +}; diff --git a/game_project/main.cpp b/game_project/main.cpp index 7d3fbdb..dc19a47 100644 --- a/game_project/main.cpp +++ b/game_project/main.cpp @@ -7,5 +7,5 @@ SceneLVL1 lvl1; SceneLVL2 lvl2; int main() { - Engine::Start(1280, 720, "game debug mode", &menu); + Engine::Start(1280, 720, "game debug mode", &lvl2); } diff --git a/game_project/scenes/scene_lvl1.cpp b/game_project/scenes/scene_lvl1.cpp index 281f7cd..a0682d6 100644 --- a/game_project/scenes/scene_lvl1.cpp +++ b/game_project/scenes/scene_lvl1.cpp @@ -6,8 +6,10 @@ #include "../components/cmp_sprite.h" #include "../components/cmp_actor_movement.h" #include "../components/cmp_bfs_ai.h" +#include "../components/cmp_hurt_player.h" #include #include +#include static std::shared_ptr player; @@ -32,6 +34,7 @@ void SceneLVL1::Load() { s->getShape().setFillColor(sf::Color::Green); player->addComponent(0.1f); + player->addTag("player"); } // ENEMIES @@ -46,19 +49,20 @@ void SceneLVL1::Load() { s->getShape().setFillColor(sf::Color::Magenta); s->getShape().setOrigin(spawn_offset); + enemy->addComponent(); auto ai = enemy->addComponent(G, ls::getTileCoord(player->getPosition())); ai->setSpeed(0.15f); enemy->addTag("enemy"); } } + std::cout <<">> LVL 1 LOADED <<" << std::endl; // Finishing touches setLoaded(true); - std::cout <<">> BLANK CANVAS LOADED <<" << std::endl; } void SceneLVL1::UnLoad() { - std::cout << ">> BLANK CANVAS UNLOAD <<" << std::endl; + std::cout << ">> LVL 1 UnLOAD <<" << std::endl; // REMOVE PLAYER OR ANYTHING IN THE CANVAS player.reset(); ls::unload(); @@ -68,9 +72,11 @@ void SceneLVL1::UnLoad() { void SceneLVL1::Update(const double& dt) { for (const auto& e : ents.find("enemy")) e->get_components()[0]->setGoal(ls::getTileCoord(player->getPosition())); - Scene::Update(dt); + if (!player->isAlive()) Engine::ChangeScene((Scene*)&lvl1); if (ls::getTileAt(player->getPosition()) == ls::END) Engine::ChangeScene((Scene*)&lvl2); + + Scene::Update(dt); } void SceneLVL1::Render() { diff --git a/game_project/scenes/scene_lvl2.cpp b/game_project/scenes/scene_lvl2.cpp index a9476d5..633e4c6 100644 --- a/game_project/scenes/scene_lvl2.cpp +++ b/game_project/scenes/scene_lvl2.cpp @@ -6,6 +6,7 @@ #include "../game.h" #include "../components/cmp_sprite.h" #include "../components/cmp_pursuer_ai.h" +#include "../components/cmp_hurt_player.h" #include #include @@ -21,6 +22,7 @@ void SceneLVL2::Load() ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); sf::Vector2f spawn_offset (tile_size/2, tile_size/2); + timer = 0; // PLAYER SPAWNING { player = makeEntity(); @@ -67,10 +69,18 @@ void SceneLVL2::UnLoad() void SceneLVL2::Update(const double &dt) { timer+= dt; - auto enemy_ai = ents.find("enemy")[0]->get_components()[0]; - if(timer > 1 && !enemy_ai->isActive()) enemy_ai->setActive(true); + { + auto e = ents.find("enemy")[0]; + if(timer > 1 && !e->get_components()[0]->isActive()) + { + e->get_components()[0]->setActive(true); + e->addComponent(); + } + } - if(ls::getTileAt(player->getPosition())==ls::END) Engine::ChangeScene((Scene*)&menu); + if (!player->isAlive()) Engine::ChangeScene((Scene*)&lvl2); + else if (ls::getTileAt(player->getPosition()) == ls::END) + Engine::ChangeScene((Scene*)&menu); Scene::Update(dt); } From bac0c3ea8d54d4e43e55a5016edad7aa77eb931f Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Tue, 12 Dec 2023 12:17:24 +0000 Subject: [PATCH 18/32] Collectable component --- game_project/components/cmp_collectable.cpp | 17 +++++++++++++++++ game_project/components/cmp_collectable.h | 15 +++++++++++++++ game_project/scenes/scene_lvl2.cpp | 17 +++++++++++++++-- lib_tile_level_loader/LevelSystem.cpp | 9 +++++---- res/lvls/lvl2.txt | 2 +- 5 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 game_project/components/cmp_collectable.cpp create mode 100644 game_project/components/cmp_collectable.h diff --git a/game_project/components/cmp_collectable.cpp b/game_project/components/cmp_collectable.cpp new file mode 100644 index 0000000..b25df66 --- /dev/null +++ b/game_project/components/cmp_collectable.cpp @@ -0,0 +1,17 @@ +// +// Created by rysan on 12/12/23. +// + +#include "cmp_collectable.h" +#include +#include + +CollectableComponent::CollectableComponent(Entity *p) : Component(p) {} + +void CollectableComponent::update(double dt) +{ + for(const auto& e : _parent->scene->ents.list) + if (ls::getTileCoord(e->getPosition()) == ls::getTileCoord(_parent->getPosition()) && e->getTags().find("collectable") == e->getTags().end()) + _parent->setForDelete(); +} + diff --git a/game_project/components/cmp_collectable.h b/game_project/components/cmp_collectable.h new file mode 100644 index 0000000..52df801 --- /dev/null +++ b/game_project/components/cmp_collectable.h @@ -0,0 +1,15 @@ +// +// Created by rysan on 12/12/23. +// +#pragma once + +#include + +class CollectableComponent : public Component{ +public: + CollectableComponent() = delete; + explicit CollectableComponent(Entity* p); + + void update(double dt) override; + void render() override {} +}; diff --git a/game_project/scenes/scene_lvl2.cpp b/game_project/scenes/scene_lvl2.cpp index 633e4c6..215534c 100644 --- a/game_project/scenes/scene_lvl2.cpp +++ b/game_project/scenes/scene_lvl2.cpp @@ -7,6 +7,7 @@ #include "../components/cmp_sprite.h" #include "../components/cmp_pursuer_ai.h" #include "../components/cmp_hurt_player.h" +#include "../components/cmp_collectable.h" #include #include @@ -27,7 +28,6 @@ void SceneLVL2::Load() { player = makeEntity(); player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); - std::cout << "OF: " << ls::getOffset().x << " " << ls::getOffset().y << std::endl; auto s = player->addComponent(); s->setShape(sf::Vector2f(tile_size, tile_size)); s->getShape().setOrigin(spawn_offset); @@ -52,7 +52,20 @@ void SceneLVL2::Load() } // Collectibles - + { + for(const auto& t : ls::findTiles(ls::WAYPOINT)) + { + auto collect = makeEntity(); + collect->setPosition(ls::getTilePosition(t) + sf::Vector2f (spawn_offset.x*1.22, spawn_offset.y*1.22)); + auto s = collect->addComponent(); + s->setShape(spawn_offset.x * 0.75); + s->getShape().setFillColor(sf::Color::Cyan); + s->getShape().setOrigin(spawn_offset); + + collect->addComponent(); + collect->addTag("collectable"); + } + } setLoaded(true); std::cout << " LVL 2 LOADED " << std::endl; diff --git a/lib_tile_level_loader/LevelSystem.cpp b/lib_tile_level_loader/LevelSystem.cpp index d4cfaee..42a4eb7 100644 --- a/lib_tile_level_loader/LevelSystem.cpp +++ b/lib_tile_level_loader/LevelSystem.cpp @@ -8,7 +8,8 @@ std::map LevelSystem::_fillcolours{ {WALL, Color::White}, {END, Color::Red}}; std::map LevelSystem::_edgecolours{ - {EMPTY, sf::Color(50,50,50)}, {WALL, sf::Color::White}, {END, sf::Color::Red}}; + {EMPTY, sf::Color(50,50,50)}, {WALL, sf::Color::White}, {END, sf::Color(100,0,0)}, {START, sf::Color(50,50,50)}, + {ENEMY, sf::Color(50,50,50)}, {WAYPOINT, sf::Color(50,50,50)}}; sf::Color LevelSystem::getColor(LevelSystem::Tile t) { auto it = _fillcolours.find(t); @@ -109,9 +110,9 @@ void LevelSystem::buildSprites(bool optimise) { if (t == EMPTY) { e_tps.push_back({getTilePosition({x,y}), tls, getColor(t), getEdgeColor(t)}); - continue; - } - tps.push_back({getTilePosition({x, y}), tls, getColor(t), getEdgeColor(t)}); + } //else if (t == WAYPOINT) e_tps.push_back({{getTilePosition({x,y}) + Vector2f(_tileSize * 0.25, _tileSize * 0.25)}, + // Vector2f(_tileSize *0.5, _tileSize * 0.5), getColor(t), getEdgeColor(t)}); + else tps.push_back({getTilePosition({x, y}), tls, getColor(t), getEdgeColor(t)}); } } diff --git a/res/lvls/lvl2.txt b/res/lvls/lvl2.txt index fcf2a3c..13220f8 100644 --- a/res/lvls/lvl2.txt +++ b/res/lvls/lvl2.txt @@ -3,7 +3,7 @@ w w w w w w w w -w w +w + w w w w w w ee w From d31cb913a824b2e63dd96d864ac8228a8527b26a Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Tue, 12 Dec 2023 12:32:50 +0000 Subject: [PATCH 19/32] Collectable component and increasespeed method on actor movement component --- game_project/components/cmp_actor_movement.cpp | 4 ++++ game_project/components/cmp_actor_movement.h | 1 + game_project/components/cmp_collectable.cpp | 8 ++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/game_project/components/cmp_actor_movement.cpp b/game_project/components/cmp_actor_movement.cpp index 0534917..95f2ace 100644 --- a/game_project/components/cmp_actor_movement.cpp +++ b/game_project/components/cmp_actor_movement.cpp @@ -43,6 +43,10 @@ float ActorMovementComponent::getSpeed() const { return _speed; } +void ActorMovementComponent::increaseSpeed(float speed) { + _speed -= speed; +} + // **** PLAYER **** PlayerMoveComponent::PlayerMoveComponent(Entity *p, float s) : diff --git a/game_project/components/cmp_actor_movement.h b/game_project/components/cmp_actor_movement.h index 9c6c183..5fafbce 100644 --- a/game_project/components/cmp_actor_movement.h +++ b/game_project/components/cmp_actor_movement.h @@ -18,6 +18,7 @@ class ActorMovementComponent : public Component { bool move(float x, float y); void render() override {} void setSpeed(float speed); + void increaseSpeed(float speed); float getSpeed() const; explicit ActorMovementComponent(Entity* p, float s = 1.f); ActorMovementComponent() = delete; diff --git a/game_project/components/cmp_collectable.cpp b/game_project/components/cmp_collectable.cpp index b25df66..15d37fc 100644 --- a/game_project/components/cmp_collectable.cpp +++ b/game_project/components/cmp_collectable.cpp @@ -3,7 +3,7 @@ // #include "cmp_collectable.h" -#include +#include "cmp_actor_movement.h" #include CollectableComponent::CollectableComponent(Entity *p) : Component(p) {} @@ -12,6 +12,10 @@ void CollectableComponent::update(double dt) { for(const auto& e : _parent->scene->ents.list) if (ls::getTileCoord(e->getPosition()) == ls::getTileCoord(_parent->getPosition()) && e->getTags().find("collectable") == e->getTags().end()) - _parent->setForDelete(); + { + _parent->setForDelete(); + if (e->getTags().find("enemy") != e->getTags().end()) e->GetCompatibleComponent()[0]->increaseSpeed(0.2); + } + } From 92d8111ce18f7bd2922f0120978b46a07e9179d7 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Tue, 12 Dec 2023 12:33:00 +0000 Subject: [PATCH 20/32] Collectable component and increasespeed method on actor movement component --- game_project/scenes/scene_lvl1.cpp | 2 +- game_project/scenes/scene_lvl2.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/game_project/scenes/scene_lvl1.cpp b/game_project/scenes/scene_lvl1.cpp index a0682d6..0d47a8b 100644 --- a/game_project/scenes/scene_lvl1.cpp +++ b/game_project/scenes/scene_lvl1.cpp @@ -73,7 +73,7 @@ void SceneLVL1::Update(const double& dt) { for (const auto& e : ents.find("enemy")) e->get_components()[0]->setGoal(ls::getTileCoord(player->getPosition())); if (!player->isAlive()) Engine::ChangeScene((Scene*)&lvl1); - if (ls::getTileAt(player->getPosition()) == ls::END) + if (ls::getTileAt(player->getPosition()) == ls::END && ents.find("collectable").empty()) Engine::ChangeScene((Scene*)&lvl2); Scene::Update(dt); diff --git a/game_project/scenes/scene_lvl2.cpp b/game_project/scenes/scene_lvl2.cpp index 215534c..c9af8ec 100644 --- a/game_project/scenes/scene_lvl2.cpp +++ b/game_project/scenes/scene_lvl2.cpp @@ -92,7 +92,7 @@ void SceneLVL2::Update(const double &dt) } if (!player->isAlive()) Engine::ChangeScene((Scene*)&lvl2); - else if (ls::getTileAt(player->getPosition()) == ls::END) + else if (ls::getTileAt(player->getPosition()) == ls::END && ents.find("collectable").empty()) Engine::ChangeScene((Scene*)&menu); Scene::Update(dt); From 77d834605251358e519448142286afea34c89146 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Tue, 12 Dec 2023 13:36:49 +0000 Subject: [PATCH 21/32] LVL1 done --- game_project/components/cmp_bfs_ai.cpp | 4 +- game_project/components/cmp_bfs_ai.h | 1 - game_project/game.h | 2 + game_project/main.cpp | 3 +- game_project/scenes/scene_lvl0_5.cpp | 85 ++++++++++++++++++++++++++ game_project/scenes/scene_lvl0_5.h | 16 +++++ game_project/scenes/scene_lvl1.cpp | 16 ++--- game_project/scenes/scene_lvl1.h | 8 +-- game_project/scenes/scene_menu.cpp | 2 +- res/lvls/lvl0_5.txt | 18 ++++++ res/lvls/lvl1.txt | 32 +++++----- res/lvls/lvl2.txt | 6 +- 12 files changed, 156 insertions(+), 37 deletions(-) create mode 100644 game_project/scenes/scene_lvl0_5.cpp create mode 100644 game_project/scenes/scene_lvl0_5.h create mode 100644 res/lvls/lvl0_5.txt diff --git a/game_project/components/cmp_bfs_ai.cpp b/game_project/components/cmp_bfs_ai.cpp index 6055e91..4812f9f 100644 --- a/game_project/components/cmp_bfs_ai.cpp +++ b/game_project/components/cmp_bfs_ai.cpp @@ -37,15 +37,13 @@ AIBFSComponent::AIBFSComponent(Entity *p, const std::shared_ptr& g, sf::V void AIBFSComponent::update(double dt) { - search_timer_ -= dt; _mtimer -= dt; sf::Vector2ul my_coord = ls::getTileCoord(_parent->getPosition()); // Search the target - if(search_timer_ <= 0 && goal_changed) + if(goal_changed) { current_path_ = breadth_first_search(G_.get(), my_coord, goal_); - goal_changed = false; } // Moves towards it if(_mtimer <= 0 && my_coord != goal_) diff --git a/game_project/components/cmp_bfs_ai.h b/game_project/components/cmp_bfs_ai.h index e22593c..090b656 100644 --- a/game_project/components/cmp_bfs_ai.h +++ b/game_project/components/cmp_bfs_ai.h @@ -20,7 +20,6 @@ class AIBFSComponent : public ActorMovementComponent { private: std::shared_ptr G_; - double search_timer_ = 1.5f; double _mtimer; sf::Vector2ul goal_; bool goal_changed; diff --git a/game_project/game.h b/game_project/game.h index 2da54ac..ac61f76 100644 --- a/game_project/game.h +++ b/game_project/game.h @@ -4,10 +4,12 @@ #pragma once #include "scenes/scene_menu.h" +#include "scenes/scene_lvl0_5.h" #include "scenes/scene_lvl1.h" #include "scenes/scene_lvl2.h" extern MenuScene menu; +extern SceneLVL0_5 lvl0_5; extern SceneLVL1 lvl1; extern SceneLVL2 lvl2; diff --git a/game_project/main.cpp b/game_project/main.cpp index dc19a47..d5a9f35 100644 --- a/game_project/main.cpp +++ b/game_project/main.cpp @@ -3,9 +3,10 @@ #include "scenes/scene_menu.h" MenuScene menu; +SceneLVL0_5 lvl0_5; SceneLVL1 lvl1; SceneLVL2 lvl2; int main() { - Engine::Start(1280, 720, "game debug mode", &lvl2); + Engine::Start(1280, 720, "game debug mode", &lvl1); } diff --git a/game_project/scenes/scene_lvl0_5.cpp b/game_project/scenes/scene_lvl0_5.cpp new file mode 100644 index 0000000..6711c38 --- /dev/null +++ b/game_project/scenes/scene_lvl0_5.cpp @@ -0,0 +1,85 @@ +// +// Created by rysan on 29/11/23. +// +#include "scene_lvl0_5.h" +#include "../game.h" +#include "../components/cmp_sprite.h" +#include "../components/cmp_actor_movement.h" +#include "../components/cmp_bfs_ai.h" +#include "../components/cmp_hurt_player.h" +#include +#include +#include + +static std::shared_ptr player; + +void SceneLVL0_5::Load() { + std::cout << ">> LVL 0.5 LOADING <<" << std::endl; + + // Importing Level from file + ls::loadLevelFile("res/lvls/lvl0_5.txt", float(Engine::getWindowSize().x)); + float tile_size = ls::getTileSize(); + ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); + auto spawn_offset = sf::Vector2f(tile_size/2, tile_size/2); + + + // PLAYER SPAWNING + { + player = makeEntity(); + player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); + std::cout << "OF: " << ls::getOffset().x << " " << ls::getOffset().y << std::endl; + auto s = player->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setOrigin(spawn_offset); + s->getShape().setFillColor(sf::Color::Green); + + player->addComponent(0.1f); + player->addTag("player"); + } + + // ENEMIES + { + for(auto& t : ls::findTiles(ls::ENEMY)) + { + auto enemy = makeEntity(); + auto G = std::make_shared(ls::getWidth(), ls::getHeight()); + enemy->setPosition(ls::getTilePosition(t) + spawn_offset); + auto s = enemy->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setFillColor(sf::Color::Magenta); + s->getShape().setOrigin(spawn_offset); + + enemy->addComponent(); + auto ai = enemy->addComponent(G, ls::getTileCoord(player->getPosition())); + ai->setSpeed(0.15f); + enemy->addTag("enemy"); + } + } + + std::cout <<">> LVL 1 LOADED <<" << std::endl; + // Finishing touches + setLoaded(true); +} + +void SceneLVL0_5::UnLoad() { + std::cout << ">> LVL 1 UnLOAD <<" << std::endl; + // REMOVE PLAYER OR ANYTHING IN THE CANVAS + player.reset(); + ls::unload(); + Scene::UnLoad(); +} + +void SceneLVL0_5::Update(const double& dt) { + for (const auto& e : ents.find("enemy")) + e->get_components()[0]->setGoal(ls::getTileCoord(player->getPosition())); + if (!player->isAlive()) Engine::ChangeScene((Scene*)&lvl0_5); + else if (ls::getTileAt(player->getPosition()) == ls::END && ents.find("collectable").empty()) + Engine::ChangeScene((Scene*)&lvl1); + + Scene::Update(dt); +} + +void SceneLVL0_5::Render() { + ls::render(Engine::GetWindow()); + Scene::Render(); +} \ No newline at end of file diff --git a/game_project/scenes/scene_lvl0_5.h b/game_project/scenes/scene_lvl0_5.h new file mode 100644 index 0000000..7b15b1d --- /dev/null +++ b/game_project/scenes/scene_lvl0_5.h @@ -0,0 +1,16 @@ +// +// Created by rysan on 29/11/23. +// +#pragma once + +#include "engine.h" + +class SceneLVL0_5 : public Scene { +public: + void Load() override; + void UnLoad() override; + void Update(const double& dt) override; + void Render() override; +}; + + diff --git a/game_project/scenes/scene_lvl1.cpp b/game_project/scenes/scene_lvl1.cpp index 0d47a8b..77d93a8 100644 --- a/game_project/scenes/scene_lvl1.cpp +++ b/game_project/scenes/scene_lvl1.cpp @@ -1,15 +1,16 @@ // -// Created by rysan on 29/11/23. +// Created by rysan on 12/12/23. // + #include "scene_lvl1.h" #include "../game.h" #include "../components/cmp_sprite.h" -#include "../components/cmp_actor_movement.h" -#include "../components/cmp_bfs_ai.h" +#include "../components/cmp_input.h" #include "../components/cmp_hurt_player.h" +#include "../components/cmp_bfs_ai.h" + #include #include -#include static std::shared_ptr player; @@ -61,7 +62,8 @@ void SceneLVL1::Load() { setLoaded(true); } -void SceneLVL1::UnLoad() { +void SceneLVL1::UnLoad() +{ std::cout << ">> LVL 1 UnLOAD <<" << std::endl; // REMOVE PLAYER OR ANYTHING IN THE CANVAS player.reset(); @@ -73,7 +75,7 @@ void SceneLVL1::Update(const double& dt) { for (const auto& e : ents.find("enemy")) e->get_components()[0]->setGoal(ls::getTileCoord(player->getPosition())); if (!player->isAlive()) Engine::ChangeScene((Scene*)&lvl1); - if (ls::getTileAt(player->getPosition()) == ls::END && ents.find("collectable").empty()) + else if (ls::getTileAt(player->getPosition()) == ls::END) Engine::ChangeScene((Scene*)&lvl2); Scene::Update(dt); @@ -82,4 +84,4 @@ void SceneLVL1::Update(const double& dt) { void SceneLVL1::Render() { ls::render(Engine::GetWindow()); Scene::Render(); -} \ No newline at end of file +} diff --git a/game_project/scenes/scene_lvl1.h b/game_project/scenes/scene_lvl1.h index 8fbd49b..769fc2e 100644 --- a/game_project/scenes/scene_lvl1.h +++ b/game_project/scenes/scene_lvl1.h @@ -1,16 +1,14 @@ // -// Created by rysan on 29/11/23. +// Created by rysan on 12/12/23. // #pragma once -#include "engine.h" +#include class SceneLVL1 : public Scene { public: void Load() override; void UnLoad() override; - void Update(const double& dt) override; + void Update(const double &dt) override; void Render() override; }; - - diff --git a/game_project/scenes/scene_menu.cpp b/game_project/scenes/scene_menu.cpp index 9459815..a42102c 100644 --- a/game_project/scenes/scene_menu.cpp +++ b/game_project/scenes/scene_menu.cpp @@ -21,7 +21,7 @@ void MenuScene::Update(const double& dt) { // cout << "Menu Update "< Date: Tue, 12 Dec 2023 19:15:23 +0000 Subject: [PATCH 22/32] LVLs 2 and 3 done --- .../components/cmp_actor_movement.cpp | 11 +- game_project/components/cmp_bfs_ai.cpp | 2 +- game_project/components/cmp_collectable.cpp | 2 +- game_project/components/cmp_input.cpp | 5 - game_project/components/cmp_input.h | 5 - game_project/game.h | 2 + game_project/main.cpp | 4 +- game_project/scenes/scene_lvl1.cpp | 1 - game_project/scenes/scene_lvl2.cpp | 46 ++++---- game_project/scenes/scene_lvl3.cpp | 111 ++++++++++++++++++ game_project/scenes/scene_lvl3.h | 14 +++ res/lvls/lvl2.txt | 16 +-- res/lvls/lvl3.txt | 27 +++++ 13 files changed, 198 insertions(+), 48 deletions(-) delete mode 100644 game_project/components/cmp_input.cpp delete mode 100644 game_project/components/cmp_input.h create mode 100644 game_project/scenes/scene_lvl3.cpp create mode 100644 game_project/scenes/scene_lvl3.h create mode 100644 res/lvls/lvl3.txt diff --git a/game_project/components/cmp_actor_movement.cpp b/game_project/components/cmp_actor_movement.cpp index 95f2ace..708385d 100644 --- a/game_project/components/cmp_actor_movement.cpp +++ b/game_project/components/cmp_actor_movement.cpp @@ -44,7 +44,8 @@ float ActorMovementComponent::getSpeed() const { } void ActorMovementComponent::increaseSpeed(float speed) { - _speed -= speed; + + if (_speed > 0.07 )_speed -= speed; } // **** PLAYER **** @@ -58,10 +59,10 @@ void PlayerMoveComponent::update(double dt) bool moved = false; if (_timer <= 0) { - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) moved = move(0.f,-1.f); - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) moved = move(0.f, 1.f); - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) moved = move(-1.f, 0.f); - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) moved = move(1.f, 0.f); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) moved = move(0.f,-1.f); + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) moved = move(0.f, 1.f); + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) moved = move(-1.f, 0.f); + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) moved = move(1.f, 0.f); } if (moved) _timer=_speed; } diff --git a/game_project/components/cmp_bfs_ai.cpp b/game_project/components/cmp_bfs_ai.cpp index 4812f9f..0c549be 100644 --- a/game_project/components/cmp_bfs_ai.cpp +++ b/game_project/components/cmp_bfs_ai.cpp @@ -33,7 +33,7 @@ std::unordered_map breadth_first_search(Graph *gra } AIBFSComponent::AIBFSComponent(Entity *p, const std::shared_ptr& g, sf::Vector2ul goal) - : ActorMovementComponent(p, 0.8f), G_(g), goal_(goal), _mtimer(2.f), goal_changed(false) {} + : ActorMovementComponent(p, 0.8f), G_(g), goal_(goal), _mtimer(2.f), goal_changed(true) {} void AIBFSComponent::update(double dt) { diff --git a/game_project/components/cmp_collectable.cpp b/game_project/components/cmp_collectable.cpp index 15d37fc..49d3f36 100644 --- a/game_project/components/cmp_collectable.cpp +++ b/game_project/components/cmp_collectable.cpp @@ -14,7 +14,7 @@ void CollectableComponent::update(double dt) if (ls::getTileCoord(e->getPosition()) == ls::getTileCoord(_parent->getPosition()) && e->getTags().find("collectable") == e->getTags().end()) { _parent->setForDelete(); - if (e->getTags().find("enemy") != e->getTags().end()) e->GetCompatibleComponent()[0]->increaseSpeed(0.2); + if (e->getTags().find("enemy") != e->getTags().end()) e->GetCompatibleComponent()[0]->increaseSpeed(0.05); } } diff --git a/game_project/components/cmp_input.cpp b/game_project/components/cmp_input.cpp deleted file mode 100644 index 133500a..0000000 --- a/game_project/components/cmp_input.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by rysan on 29/11/23. -// - -#include "cmp_input.h" diff --git a/game_project/components/cmp_input.h b/game_project/components/cmp_input.h deleted file mode 100644 index 2d7f915..0000000 --- a/game_project/components/cmp_input.h +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by rysan on 29/11/23. -// -#pragma once -#include "cmp_actor_movement.h" diff --git a/game_project/game.h b/game_project/game.h index ac61f76..92a0020 100644 --- a/game_project/game.h +++ b/game_project/game.h @@ -7,9 +7,11 @@ #include "scenes/scene_lvl0_5.h" #include "scenes/scene_lvl1.h" #include "scenes/scene_lvl2.h" +#include "scenes/scene_lvl3.h" extern MenuScene menu; extern SceneLVL0_5 lvl0_5; extern SceneLVL1 lvl1; extern SceneLVL2 lvl2; +extern SceneLVL3 lvl3; diff --git a/game_project/main.cpp b/game_project/main.cpp index d5a9f35..28fcc62 100644 --- a/game_project/main.cpp +++ b/game_project/main.cpp @@ -6,7 +6,9 @@ MenuScene menu; SceneLVL0_5 lvl0_5; SceneLVL1 lvl1; SceneLVL2 lvl2; +SceneLVL3 lvl3; +SceneLVL4 lvl4; int main() { - Engine::Start(1280, 720, "game debug mode", &lvl1); + Engine::Start(1280, 720, "game debug mode", &lvl3); } diff --git a/game_project/scenes/scene_lvl1.cpp b/game_project/scenes/scene_lvl1.cpp index 77d93a8..f21ff6c 100644 --- a/game_project/scenes/scene_lvl1.cpp +++ b/game_project/scenes/scene_lvl1.cpp @@ -5,7 +5,6 @@ #include "scene_lvl1.h" #include "../game.h" #include "../components/cmp_sprite.h" -#include "../components/cmp_input.h" #include "../components/cmp_hurt_player.h" #include "../components/cmp_bfs_ai.h" diff --git a/game_project/scenes/scene_lvl2.cpp b/game_project/scenes/scene_lvl2.cpp index c9af8ec..8e2045a 100644 --- a/game_project/scenes/scene_lvl2.cpp +++ b/game_project/scenes/scene_lvl2.cpp @@ -33,25 +33,11 @@ void SceneLVL2::Load() s->getShape().setOrigin(spawn_offset); s->getShape().setFillColor(sf::Color::Green); - player->addComponent(0.1f); + player->addComponent(0.12f); player->addTag("player"); } - - // ENEMIES - { - auto enemy = makeEntity(); - enemy->setPosition(player->getPosition()); - auto s = enemy->addComponent(); - s->setShape(sf::Vector2f(tile_size, tile_size)); - s->getShape().setFillColor(sf::Color::Magenta); - s->getShape().setOrigin(spawn_offset); - - enemy->addComponent(); - enemy->addTag("enemy"); - } - - // Collectibles + // Collectables { for(const auto& t : ls::findTiles(ls::WAYPOINT)) { @@ -67,6 +53,21 @@ void SceneLVL2::Load() } } + // ENEMIES + { + for (int i=0; i < 3; i++) + { + auto enemy = makeEntity(); + enemy->setPosition(player->getPosition()); + auto s = enemy->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setFillColor(sf::Color::Magenta); + s->getShape().setOrigin(spawn_offset); + + enemy->addComponent(); + enemy->addTag("enemy"); + } + } setLoaded(true); std::cout << " LVL 2 LOADED " << std::endl; } @@ -83,17 +84,20 @@ void SceneLVL2::Update(const double &dt) { timer+= dt; { - auto e = ents.find("enemy")[0]; - if(timer > 1 && !e->get_components()[0]->isActive()) + for(const auto& e : ents.find("enemy")) { - e->get_components()[0]->setActive(true); - e->addComponent(); + if(timer > 1 && !e->get_components()[0]->isActive()) + { + e->get_components()[0]->setActive(true); + e->addComponent(); + timer = 0; + } } } if (!player->isAlive()) Engine::ChangeScene((Scene*)&lvl2); else if (ls::getTileAt(player->getPosition()) == ls::END && ents.find("collectable").empty()) - Engine::ChangeScene((Scene*)&menu); + Engine::ChangeScene((Scene*)&lvl3); Scene::Update(dt); } diff --git a/game_project/scenes/scene_lvl3.cpp b/game_project/scenes/scene_lvl3.cpp new file mode 100644 index 0000000..4b2aeef --- /dev/null +++ b/game_project/scenes/scene_lvl3.cpp @@ -0,0 +1,111 @@ +// +// Created by rysan on 12/12/23. +// + +#include "scene_lvl3.h" + +#include "../game.h" +#include "../components/cmp_sprite.h" +#include "../components/cmp_pursuer_ai.h" +#include "../components/cmp_hurt_player.h" +#include "../components/cmp_collectable.h" +#include +#include + +static std::shared_ptr player; +static double timer; + +void SceneLVL3::Load() +{ + std::cout << ">> LVL 3 LOADING <<" << std::endl; + + ls::loadLevelFile("res/lvls/lvl3.txt", float(Engine::getWindowSize().x)); + float tile_size = ls::getTileSize(); + ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); + sf::Vector2f spawn_offset (tile_size/2, tile_size/2); + + timer = 0; + // PLAYER SPAWNING + { + player = makeEntity(); + player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); + auto s = player->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setOrigin(spawn_offset); + s->getShape().setFillColor(sf::Color::Green); + + player->addComponent(0.12f); + player->addTag("player"); + } + + // Collectibles + { + for(const auto& t : ls::findTiles(ls::WAYPOINT)) + { + auto collect = makeEntity(); + collect->setPosition(ls::getTilePosition(t) + sf::Vector2f (spawn_offset.x*1.22, spawn_offset.y*1.22)); + auto s = collect->addComponent(); + s->setShape(spawn_offset.x * 0.75); + s->getShape().setFillColor(sf::Color::Cyan); + s->getShape().setOrigin(spawn_offset); + + collect->addComponent(); + collect->addTag("collectable"); + } + } + + // ENEMIES + { + for (int i=0; i < 3; i++) + { + auto enemy = makeEntity(); + enemy->setPosition(player->getPosition()); + auto s = enemy->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setFillColor(sf::Color::Magenta); + s->getShape().setOrigin(spawn_offset); + + enemy->addComponent(); + enemy->addTag("enemy"); + } + } + setLoaded(true); + std::cout << " LVL 3 LOADED " << std::endl; +} + +void SceneLVL3::UnLoad() +{ + std::cout << ">> LVL 3 UNLOAD << " << std::endl; + player.reset(); + ls::unload(); + Scene::UnLoad(); +} + +void SceneLVL3::Update(const double &dt) +{ + timer+= dt; + { + for(const auto& e : ents.find("enemy")) + { + if(timer > 1 && !e->get_components()[0]->isActive()) + { + e->get_components()[0]->setActive(true); + e->addComponent(); + timer = 0; + } + } + } + + if (!player->isAlive()) Engine::ChangeScene((Scene*)&lvl3); + else if (ls::getTileAt(player->getPosition()) == ls::END && ents.find("collectable").empty()) + Engine::ChangeScene((Scene*)&menu); + + Scene::Update(dt); +} + +void SceneLVL3::Render() +{ + ls::render(Engine::GetWindow()); + Scene::Render(); +} + diff --git a/game_project/scenes/scene_lvl3.h b/game_project/scenes/scene_lvl3.h new file mode 100644 index 0000000..6721116 --- /dev/null +++ b/game_project/scenes/scene_lvl3.h @@ -0,0 +1,14 @@ +// +// Created by rysan on 12/12/23. +// +#pragma once +#include + +class SceneLVL3 : public Scene { +public: + void Load() override; + void UnLoad() override; + void Update(const double& dt) override; + void Render() override; +}; + diff --git a/res/lvls/lvl2.txt b/res/lvls/lvl2.txt index 9299b8d..ef3af1b 100644 --- a/res/lvls/lvl2.txt +++ b/res/lvls/lvl2.txt @@ -1,18 +1,18 @@ wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +ww w w w w w w w w w w w w w w w +ww w w w w w w w w w w w w w w w w w w w -w + w w w -w + w +w + + + w w w +w + + + + + + + + + ee w +w s + + + + + + + + + + ee w w w -w ee w -w s ee w +w + + + w w w -w + w w w -w + w -w w -w n w w w +w w w w w w w w w w w w w w w ww +w w w w w w w w w w w w w w w ww wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww diff --git a/res/lvls/lvl3.txt b/res/lvls/lvl3.txt new file mode 100644 index 0000000..a11b8b1 --- /dev/null +++ b/res/lvls/lvl3.txt @@ -0,0 +1,27 @@ +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +w www w +w + wwwwwwwwwwwwwwww wwwwwww + w +w w + wwww ww +w + s + w + w w wwwwwwwwwwww ww wwww +w +w w ++w www+++ w w www +w + w wwwww wwww www++++wwwww w + w +w +w w +++++ e wwwwwwwwww w w www +wwwww wwwwwwwwwwwwwwwww w wwwwww wwwwwww +w +w+++w w w w w www +w+ +w + + w wwwwwwwwwwwwwww wwwwwwww w +w +w w w + + + w w www +w + + w ww wwwww++ w w wwww w w +w +wwwww wwwwwwww w + w www +w+++w wwwwwwwwwwww + w wwwwwwww w w +w w w + w +++ w w +w wwwwwwwwwww w w + w + + wwwwwwwwwwww w www +w w + w w w + w + + w w +w w+www w w w + w +++ w wwwww w w++w w +w w + w w w w w+++w w w++w w +w wwwwwwwwwww w w wwwwwwwwwwww w+++w w wwww w +w + ww w + + + + w+++w w w w +w w+wwwwwwwwwwwwwwwwwwwwwwwwwww w w ww w +w w +w+ wwwwwwww+wwww ww w +w + wwwwwwwww wwww w + w+ + ++ +w +w w + + + w + ++ + +w +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww From 5292a754a3a62b6da34b0862ba6e016018998969 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Tue, 12 Dec 2023 20:49:20 +0000 Subject: [PATCH 23/32] LVL4 attempt, kill component done and LVL4 layout done --- game_project/components/cmp_kill.cpp | 16 ++++ game_project/components/cmp_kill.h | 16 ++++ game_project/game.h | 3 +- game_project/main.cpp | 2 +- game_project/scenes/scene_lvl2.cpp | 4 +- game_project/scenes/scene_lvl3.cpp | 2 +- game_project/scenes/scene_lvl4.cpp | 134 +++++++++++++++++++++++++++ game_project/scenes/scene_lvl4.h | 13 +++ res/lvls/lvl4.txt | 63 ++++++------- 9 files changed, 212 insertions(+), 41 deletions(-) create mode 100644 game_project/components/cmp_kill.cpp create mode 100644 game_project/components/cmp_kill.h create mode 100644 game_project/scenes/scene_lvl4.cpp create mode 100644 game_project/scenes/scene_lvl4.h diff --git a/game_project/components/cmp_kill.cpp b/game_project/components/cmp_kill.cpp new file mode 100644 index 0000000..60724fe --- /dev/null +++ b/game_project/components/cmp_kill.cpp @@ -0,0 +1,16 @@ +// +// Created by rysan on 12/12/23. +// + +#include "cmp_kill.h" +#include +#include + +KillComponent::KillComponent(Entity *p, std::string tag) : + Component(p), _target_tag(tag) {} + +void KillComponent::update(double dt) +{ + for(auto& e : _parent->scene->ents.find(_target_tag)) + if (ls::getTileCoord(e->getPosition()) == ls::getTileCoord(e->getPosition())) { e->setForDelete(); _parent->setForDelete(); } +} \ No newline at end of file diff --git a/game_project/components/cmp_kill.h b/game_project/components/cmp_kill.h new file mode 100644 index 0000000..897faa9 --- /dev/null +++ b/game_project/components/cmp_kill.h @@ -0,0 +1,16 @@ +// +// Created by rysan on 12/12/23. +// + +#include + +class KillComponent : public Component { +public: + void update(double dt) override; + void render() override {} + explicit KillComponent(Entity* p, std::string tag); + KillComponent() = delete; + +protected: + std::string _target_tag; +}; diff --git a/game_project/game.h b/game_project/game.h index 92a0020..82084e3 100644 --- a/game_project/game.h +++ b/game_project/game.h @@ -8,10 +8,11 @@ #include "scenes/scene_lvl1.h" #include "scenes/scene_lvl2.h" #include "scenes/scene_lvl3.h" +#include "scenes/scene_lvl4.h" extern MenuScene menu; extern SceneLVL0_5 lvl0_5; extern SceneLVL1 lvl1; extern SceneLVL2 lvl2; extern SceneLVL3 lvl3; - +extern SceneLVL4 lvl4; diff --git a/game_project/main.cpp b/game_project/main.cpp index 28fcc62..458f1ff 100644 --- a/game_project/main.cpp +++ b/game_project/main.cpp @@ -10,5 +10,5 @@ SceneLVL3 lvl3; SceneLVL4 lvl4; int main() { - Engine::Start(1280, 720, "game debug mode", &lvl3); + Engine::Start(1280, 720, "game debug mode", &lvl4); } diff --git a/game_project/scenes/scene_lvl2.cpp b/game_project/scenes/scene_lvl2.cpp index 8e2045a..ebf5d3e 100644 --- a/game_project/scenes/scene_lvl2.cpp +++ b/game_project/scenes/scene_lvl2.cpp @@ -12,7 +12,7 @@ #include static std::shared_ptr player; -double timer; +static double timer; void SceneLVL2::Load() { @@ -61,7 +61,7 @@ void SceneLVL2::Load() enemy->setPosition(player->getPosition()); auto s = enemy->addComponent(); s->setShape(sf::Vector2f(tile_size, tile_size)); - s->getShape().setFillColor(sf::Color::Magenta); + s->getShape().setFillColor(sf::Color::Yellow); s->getShape().setOrigin(spawn_offset); enemy->addComponent(); diff --git a/game_project/scenes/scene_lvl3.cpp b/game_project/scenes/scene_lvl3.cpp index 4b2aeef..7ec6954 100644 --- a/game_project/scenes/scene_lvl3.cpp +++ b/game_project/scenes/scene_lvl3.cpp @@ -62,7 +62,7 @@ void SceneLVL3::Load() enemy->setPosition(player->getPosition()); auto s = enemy->addComponent(); s->setShape(sf::Vector2f(tile_size, tile_size)); - s->getShape().setFillColor(sf::Color::Magenta); + s->getShape().setFillColor(sf::Color::Yellow); s->getShape().setOrigin(spawn_offset); enemy->addComponent(); diff --git a/game_project/scenes/scene_lvl4.cpp b/game_project/scenes/scene_lvl4.cpp new file mode 100644 index 0000000..63b8139 --- /dev/null +++ b/game_project/scenes/scene_lvl4.cpp @@ -0,0 +1,134 @@ +// +// Created by rysan on 12/12/23. +// + +#include "scene_lvl4.h" + +#include "../game.h" +#include "../components/cmp_bfs_ai.h" +#include "../components/cmp_sprite.h" +#include "../components/cmp_collectable.h" +#include "../components/cmp_hurt_player.h" +#include "../components/cmp_pursuer_ai.h" +#include "../components/cmp_kill.h" + +#include +#include + +static std::shared_ptr player; +static double timer; + +void SceneLVL4::Load() +{ + std::cout << ">> LVL 4 LOADING <<" << std::endl; + + ls::loadLevelFile("res/lvls/lvl4.txt", float(Engine::getWindowSize().x)); + float tile_size = ls::getTileSize(); + ls::setOffset(sf::Vector2f(0, Engine::getWindowSize().y - (ls::getHeight() * tile_size))); + sf::Vector2f spawn_offset (tile_size/2, tile_size/2); + timer = 0; + + // PLAYER SPAWNING + { + player = makeEntity(); + player->setPosition(ls::getTilePosition(ls::findTiles(ls::START)[0]) + spawn_offset); + auto s = player->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setOrigin(spawn_offset); + s->getShape().setFillColor(sf::Color::Green); + + player->addComponent(0.1f); + player->addTag("player"); + } + + // Collectables + { + for (const auto& t : ls::findTiles(ls::WAYPOINT)) + { + auto collect = makeEntity(); + collect->setPosition(ls::getTilePosition(t) + sf::Vector2f (spawn_offset.x * 1.22, spawn_offset.y * 1.22)); + auto s = collect->addComponent(); + s->setShape(spawn_offset.x * 0.75); + + s->getShape().setFillColor(sf::Color::Cyan); + s->getShape().setOrigin(spawn_offset); + + collect->addComponent(); + collect->addTag("collectable"); + } + } + + // ENEMIES + { + for (int i = 0; i < 4; i++) + { + auto enemy=makeEntity(); + enemy->setPosition(player->getPosition()); + auto s = enemy->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setFillColor(sf::Color::Yellow); + s->getShape().setOrigin(spawn_offset); + + auto ai = enemy->addComponent(); + ai->setActive(false); + enemy->addTag("enemy"); + enemy->addTag("enemy_p"); + } + auto G = std::make_shared(ls::getWidth(), ls::getHeight()); + int i=-1; + for (auto& t : ls::findTiles(ls::ENEMY)) + { + auto enemy = makeEntity(); + enemy->setPosition(ls::getTilePosition(t) + spawn_offset); + auto s = enemy->addComponent(); + s->setShape(sf::Vector2f(tile_size, tile_size)); + s->getShape().setFillColor(sf::Color::Magenta); + s->getShape().setOrigin(spawn_offset); + + enemy->addComponent(); + auto ai = enemy->addComponent(G, ls::findTiles(ls::WAYPOINT)[i++]); + ai->setSpeed(0.2f); + enemy->addTag("enemy"); enemy->addTag("enemy_b"); + } + } + + setLoaded(true); + std::cout << " LVL 4 LOADED " << std::endl; +} + +void SceneLVL4::UnLoad() +{ + std::cout << ">> LVL 4 UNLOAD <<" << std::endl; + player.reset(); + ls::unload(); + Scene::UnLoad(); +} + +void SceneLVL4::Update(const double &dt) +{ + timer+=dt; + { + // ENEMIES TYPE 1 - BFS + for (const auto &e: ents.find("enemy_b")) // In case there is no more collectables, they chase the player + if (ents.find("collectable").empty()) + e->get_components()[0]->setGoal(ls::getTileCoord(player->getPosition())); + + // ENEMIES TYPE 2 - Pursuer + for (const auto &e: ents.find("enemy_p")) + if (timer > 1 && !e->get_components()[0]->isActive()) + { + e->get_components()[0]->setActive(true); + //e->addComponent(); // Kill player + //e->addComponent("enemy_b"); // Kill other enemies + timer = 0; + } + } + + if (!player->isAlive()) Engine::ChangeScene((Scene*)& lvl4); + else if (ls::getTileAt(player->getPosition())== ls::END && ents.find("collectable").empty()) + Engine::ChangeScene((Scene*)&menu); + + Scene::Update(dt); +} + +void SceneLVL4::Render() { ls::render(Engine::GetWindow()); Scene::Render(); } diff --git a/game_project/scenes/scene_lvl4.h b/game_project/scenes/scene_lvl4.h new file mode 100644 index 0000000..405643d --- /dev/null +++ b/game_project/scenes/scene_lvl4.h @@ -0,0 +1,13 @@ +// +// Created by rysan on 12/12/23. +// +#pragma once +#include + +class SceneLVL4 : public Scene{ + void Load() override; + void UnLoad() override; + void Update(const double &dt) override; + void Render() override; +}; + diff --git a/res/lvls/lvl4.txt b/res/lvls/lvl4.txt index 29b06c5..0ff2318 100644 --- a/res/lvls/lvl4.txt +++ b/res/lvls/lvl4.txt @@ -1,36 +1,27 @@ -wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww -wns w w www ww w w w w ww ww -w w w ww www w w ww ww ww -ww ww wwww www wwwwwwwwwwwwwwwwwwwwwwwwww w www ww w wwww www ww -w w ww w w w ww w w w w w w w w w w ww -w w ww w w w ww w w w w w w w w w w ww -w www ww wwwww ww w w ww w w w wwww ww w www wwww w wwww w w w -w w w ww w ww w w ww w -w w w ww ww w w w -w ww ww w w ww wwwwww ww ww wwwwwwwwwww w w w wwwwww ww www w -w ww w w w w ww w w w ww w w -w w w w w w w w w ww w w -w ww www wwwww ww w wwwwwwwwwwww wwwwwwwwww wwww w www wwww w -w w w w w w w w w w w w w w w -w w w w w w w w w w w w w ww w w -w ww w w w ww w w ww w www www w ww w www w ww w w ww www w -w w w w w w w w ww w w w w w w w w ww w w w w w -w ww w w w w w w w w w w w w w w ww w w ww w w ww -w www ww www w w w w ww w w w w wwwwwwwwwwwwwwwwwww ww w w w -w w ww w w w w w w w w ww w w -w w w w w w w w ww w -ww ww wwwwww w wwwwww ww www www w w ww w w ww w wwww www ww -ww ww wwwwww w wwwwww ww www www w w ww w w ww w wwww www ww -w ww w w w w w ww www ww ww w -w w w w w w w w ww ww ww ww -ww ww wwwwww w w w wwwwwwwwwwww w w ww ww www w -w ww w w w w ww www ww ww ww -w w w w w w w w w wwww ww ww -w www ww w w ww wwww w ww w w w w wwwwwwwwww w w w w wwww w ww -w w w w w ww wwww w w w w w ww w w w w w ww -w w w w w ww wwww w ww w w w w ww w w w wwww w ww -w w w w w w w w w w w ww -w w w w w w w w w w weww -w www ww wwwww ww w w ww w w w w ww ww w www wwww w wwww w -w w w w w w w w w ww -wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +w s + + w +wwwwwww w w w www +w w w n w wew +w + w w w w w +w w w w w w +www wwwww wwwwwwwwwwwwwww w w w w +www w w w w w w w +w + www w w w w w w w +w www w n + w w w +w w w w w w w wwwww +w w w w w w w w w +w w w wwwwww wwwwwww w wwwwwwwwwwwwwwwwww n w +w+ w w w w w w w +w w wwwww w w w w www +w w w w + w + w w w +w w w w w w w w +w+ w w w w w w www +w wwwww w wwww wwww wwwwwww w w +w w w w www +wwwwwwwwww w w w +w w w w w w +w w wwwwwwwwwwwwwwwwwwwwwwwwwwwww wwwww w w +w w w +w wwwwwwwwwwwwwwwwwwwwww wwww w wwwwwww w +w w w w +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww From d3aff175d6410cffc6b364847adeccb9aa873955 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Wed, 13 Dec 2023 12:30:50 +0000 Subject: [PATCH 24/32] All lvls done. --- game_project/ai/Graph.cpp | 1 - .../components/cmp_actor_movement.cpp | 1 + game_project/components/cmp_bfs_ai.cpp | 1 + game_project/components/cmp_collectable.cpp | 1 + game_project/components/cmp_kill.cpp | 2 +- game_project/components/cmp_pursuer_ai.cpp | 1 + game_project/scenes/scene_lvl4.cpp | 29 ++++++++++++++++--- game_project/scenes/scene_lvl4.h | 1 + res/lvls/lvl4.txt | 16 +++++----- 9 files changed, 39 insertions(+), 14 deletions(-) diff --git a/game_project/ai/Graph.cpp b/game_project/ai/Graph.cpp index bcc6df8..38f1fc1 100644 --- a/game_project/ai/Graph.cpp +++ b/game_project/ai/Graph.cpp @@ -21,7 +21,6 @@ void Graph::setWidth(uint w) { _width = w; } bool Graph::passable(sf::Vector2f coord) const { - return ls::getTile(sf::Vector2ul(coord)) != ls::WALL; } diff --git a/game_project/components/cmp_actor_movement.cpp b/game_project/components/cmp_actor_movement.cpp index 708385d..a8882bd 100644 --- a/game_project/components/cmp_actor_movement.cpp +++ b/game_project/components/cmp_actor_movement.cpp @@ -46,6 +46,7 @@ float ActorMovementComponent::getSpeed() const { void ActorMovementComponent::increaseSpeed(float speed) { if (_speed > 0.07 )_speed -= speed; + else _speed = 0.07; } // **** PLAYER **** diff --git a/game_project/components/cmp_bfs_ai.cpp b/game_project/components/cmp_bfs_ai.cpp index 0c549be..ee8f098 100644 --- a/game_project/components/cmp_bfs_ai.cpp +++ b/game_project/components/cmp_bfs_ai.cpp @@ -44,6 +44,7 @@ void AIBFSComponent::update(double dt) if(goal_changed) { current_path_ = breadth_first_search(G_.get(), my_coord, goal_); + goal_changed = false; } // Moves towards it if(_mtimer <= 0 && my_coord != goal_) diff --git a/game_project/components/cmp_collectable.cpp b/game_project/components/cmp_collectable.cpp index 49d3f36..3d8f548 100644 --- a/game_project/components/cmp_collectable.cpp +++ b/game_project/components/cmp_collectable.cpp @@ -15,6 +15,7 @@ void CollectableComponent::update(double dt) { _parent->setForDelete(); if (e->getTags().find("enemy") != e->getTags().end()) e->GetCompatibleComponent()[0]->increaseSpeed(0.05); + if (e->getTags().find("player") != e->getTags().end()) e->GetCompatibleComponent()[0]->increaseSpeed(0.025); } } diff --git a/game_project/components/cmp_kill.cpp b/game_project/components/cmp_kill.cpp index 60724fe..60a84f6 100644 --- a/game_project/components/cmp_kill.cpp +++ b/game_project/components/cmp_kill.cpp @@ -12,5 +12,5 @@ KillComponent::KillComponent(Entity *p, std::string tag) : void KillComponent::update(double dt) { for(auto& e : _parent->scene->ents.find(_target_tag)) - if (ls::getTileCoord(e->getPosition()) == ls::getTileCoord(e->getPosition())) { e->setForDelete(); _parent->setForDelete(); } + if (ls::getTileCoord(_parent->getPosition()) == ls::getTileCoord(e->getPosition())) { e->setForDelete(); _parent->setForDelete(); } } \ No newline at end of file diff --git a/game_project/components/cmp_pursuer_ai.cpp b/game_project/components/cmp_pursuer_ai.cpp index c51c62b..5fb52e2 100644 --- a/game_project/components/cmp_pursuer_ai.cpp +++ b/game_project/components/cmp_pursuer_ai.cpp @@ -30,6 +30,7 @@ void PursuerAIComponent::update(double dt) move(dir.x, dir.y); } + setSpeed(pl->GetCompatibleComponent()[0]->getSpeed()); _mtimer = _speed; } } diff --git a/game_project/scenes/scene_lvl4.cpp b/game_project/scenes/scene_lvl4.cpp index 63b8139..57ffb24 100644 --- a/game_project/scenes/scene_lvl4.cpp +++ b/game_project/scenes/scene_lvl4.cpp @@ -12,6 +12,7 @@ #include "../components/cmp_pursuer_ai.h" #include "../components/cmp_kill.h" +#include #include #include @@ -86,7 +87,7 @@ void SceneLVL4::Load() s->getShape().setOrigin(spawn_offset); enemy->addComponent(); - auto ai = enemy->addComponent(G, ls::findTiles(ls::WAYPOINT)[i++]); + auto ai = enemy->addComponent(G, ls::findTiles(ls::WAYPOINT)[++i]); ai->setSpeed(0.2f); enemy->addTag("enemy"); enemy->addTag("enemy_b"); } @@ -104,22 +105,42 @@ void SceneLVL4::UnLoad() Scene::UnLoad(); } +// Returns the position of the closest entity to a point (euclidean distance) given its tag +sf::Vector2f SceneLVL4::closest_entity(sf::Vector2f me, std::string tag) +{ + sf::Vector2f closest; + double minimum = Engine::getWindowSize().x; + for (const auto& e : ents.find(tag)) + { + double current = sf::length(me - e->getPosition()); + if(current <= minimum) { closest = e->getPosition(); minimum = current; } + } + return closest; +} + void SceneLVL4::Update(const double &dt) { timer+=dt; { + // ENEMIES TYPE 1 - BFS for (const auto &e: ents.find("enemy_b")) // In case there is no more collectables, they chase the player - if (ents.find("collectable").empty()) + { + auto c = ents.find("collectable"); + // As soon as they get the max velocity, they will chase the player; + if (c.empty() || e->GetCompatibleComponent()[0]->getSpeed() < 0.08) e->get_components()[0]->setGoal(ls::getTileCoord(player->getPosition())); + else + e->get_components()[0]->setGoal(ls::getTileCoord(closest_entity(e->getPosition(), "collectable"))); + } // ENEMIES TYPE 2 - Pursuer for (const auto &e: ents.find("enemy_p")) if (timer > 1 && !e->get_components()[0]->isActive()) { e->get_components()[0]->setActive(true); - //e->addComponent(); // Kill player - //e->addComponent("enemy_b"); // Kill other enemies + e->addComponent(); // Kill player + e->addComponent("enemy_b"); // Kill other enemies timer = 0; } } diff --git a/game_project/scenes/scene_lvl4.h b/game_project/scenes/scene_lvl4.h index 405643d..a2657ce 100644 --- a/game_project/scenes/scene_lvl4.h +++ b/game_project/scenes/scene_lvl4.h @@ -9,5 +9,6 @@ class SceneLVL4 : public Scene{ void UnLoad() override; void Update(const double &dt) override; void Render() override; + sf::Vector2f closest_entity(sf::Vector2f me, std::string tag); }; diff --git a/res/lvls/lvl4.txt b/res/lvls/lvl4.txt index 0ff2318..d13aeca 100644 --- a/res/lvls/lvl4.txt +++ b/res/lvls/lvl4.txt @@ -1,27 +1,27 @@ wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww -w s + + w +w s + + + w wwwwwww w w w www w w w n w wew w + w w w w w -w w w w w w +w w + w w w w www wwwww wwwwwwwwwwwwwww w w w w www w w w w w w w w + www w w w w w w w -w www w n + w w w +w www w n + + w w w w w w w w w w wwwww -w w w w w w w w w -w w w wwwwww wwwwwww w wwwwwwwwwwwwwwwwww n w -w+ w w w w w w w +w w w w w w w w +w w w wwwwww wwwwwww+w+wwwwwwwwwwwwwwwwww n w +w+ w w w w + w w w w w wwwww w w w w www w w w w + w + w w w w w w w w w w w w+ w w w w w w www w wwwww w wwww wwww wwwwwww w w w w w w www -wwwwwwwwww w w w +wwwwwwwwww w w + w w w w w w w w w wwwwwwwwwwwwwwwwwwwwwwwwwwwww wwwww w w w w w w wwwwwwwwwwwwwwwwwwwwww wwww w wwwwwww w -w w w w +w + + + w w w wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww From 385806d54603baeb97cb832de40251b0a3752844 Mon Sep 17 00:00:00 2001 From: RysanDeluna Date: Wed, 13 Dec 2023 14:04:41 +0000 Subject: [PATCH 25/32] MenuScene with buttons --- .../components/cmp_actor_movement.cpp | 12 +-- game_project/components/cmp_button.cpp | 94 +++++++++++++++++++ game_project/components/cmp_button.h | 38 ++++++++ game_project/components/cmp_collectable.cpp | 12 ++- game_project/components/cmp_collectable.h | 4 + .../components/cmp_physics.cpp | 0 .../components/cmp_physics.h | 4 +- game_project/components/cmp_text.cpp | 1 + game_project/scenes/scene_guide.cpp | 5 + game_project/scenes/scene_guide.h | 11 +++ game_project/scenes/scene_lvl4.cpp | 3 +- game_project/scenes/scene_menu.cpp | 39 +++++++- game_project/scenes/scene_menu.h | 4 + 13 files changed, 211 insertions(+), 16 deletions(-) create mode 100644 game_project/components/cmp_button.cpp create mode 100644 game_project/components/cmp_button.h rename {lab_7_platformer => game_project}/components/cmp_physics.cpp (100%) rename {lab_7_platformer => game_project}/components/cmp_physics.h (89%) create mode 100644 game_project/scenes/scene_guide.cpp create mode 100644 game_project/scenes/scene_guide.h diff --git a/game_project/components/cmp_actor_movement.cpp b/game_project/components/cmp_actor_movement.cpp index a8882bd..918166b 100644 --- a/game_project/components/cmp_actor_movement.cpp +++ b/game_project/components/cmp_actor_movement.cpp @@ -45,8 +45,8 @@ float ActorMovementComponent::getSpeed() const { void ActorMovementComponent::increaseSpeed(float speed) { - if (_speed > 0.07 )_speed -= speed; - else _speed = 0.07; + if (_speed > 0.085 )_speed -= speed; + else _speed = 0.085; } // **** PLAYER **** @@ -60,10 +60,10 @@ void PlayerMoveComponent::update(double dt) bool moved = false; if (_timer <= 0) { - if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) moved = move(0.f,-1.f); - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) moved = move(0.f, 1.f); - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) moved = move(-1.f, 0.f); - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) moved = move(1.f, 0.f); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) moved = move(0.f,-1.f); + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) moved = move(0.f, 1.f); + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) moved = move(-1.f, 0.f); + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) moved = move(1.f, 0.f); } if (moved) _timer=_speed; } diff --git a/game_project/components/cmp_button.cpp b/game_project/components/cmp_button.cpp new file mode 100644 index 0000000..ee0e597 --- /dev/null +++ b/game_project/components/cmp_button.cpp @@ -0,0 +1,94 @@ +// Button component C++ file +#include"cmp_button.h" +#include +#include +#include +#include "engine.h" +#include "../game.h" + +using namespace sf; +using namespace std; + +unique_ptr