diff --git a/CMakeLists.txt b/CMakeLists.txt index c7dd1d9c..b6d6ce8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,6 +156,17 @@ if (GRAPHICS) endforeach() endif() +# GRAPHICS TEST +if (GP_TESTS) + add_executable(GPTEST "tests/graphics/gui_phy_test.cpp" + ${HEADERS_GAME_ENGINE} + ${SOURCES_GAME_ENGINE} + ${SOURCES_COMMON} + ${HEADERS_COMMON} + ${SERVER_WITHOUT_MAIN}) + target_link_libraries(GPTEST SDL2main SDL2 SDL2_image SDL2_ttf SDL2_mixer) +endif() + if(PHYSICS) diff --git a/common/common_constants.h b/common/common_constants.h index fb14301a..9458dedc 100644 --- a/common/common_constants.h +++ b/common/common_constants.h @@ -1,6 +1,7 @@ #ifndef TP_FINAL_COMMON_CONSTANTS_H #define TP_FINAL_COMMON_CONSTANTS_H +#include #include // Game Settings diff --git a/game_engine/physics_engine/collision_manager.cpp b/game_engine/physics_engine/collision_manager.cpp index ec6cf7c3..9e7ffbb7 100644 --- a/game_engine/physics_engine/collision_manager.cpp +++ b/game_engine/physics_engine/collision_manager.cpp @@ -110,7 +110,7 @@ void CollisionManager::update() { // create to update specific object auto& obj = std::get<0>(*it); - if (obj != nullptr) { + if (obj != nullptr || !obj->is_active_object()) { auto& old_position_ref = std::get<1>(*it); obj->update_db(); diff --git a/game_engine/physics_engine/collision_manager.h b/game_engine/physics_engine/collision_manager.h index beefbecb..913a9bbd 100644 --- a/game_engine/physics_engine/collision_manager.h +++ b/game_engine/physics_engine/collision_manager.h @@ -23,9 +23,6 @@ class CollisionManager { std::vector>> grid; // grid: [x][y] stores a vector of shared pointers to CollisionObjects - // create a tuple of dynamic bodies that stores their shared pointer and their current position - std::vector, Vector2D>> dynamic_bodies; - void handle_out_of_bounds(std::shared_ptr obj); void place_object_in_grid(std::shared_ptr obj); void remove_object_from_grid(std::shared_ptr obj, Vector2D position); @@ -37,6 +34,7 @@ class CollisionManager { void detect_colisions(std::shared_ptr obj); public: + std::vector, Vector2D>> dynamic_bodies; CollisionManager(int levelWidth, int levelHeight); std::shared_ptr get_collision_object_at(int x, int y) const; diff --git a/server/game_logic/characters/character.cpp b/server/game_logic/characters/character.cpp index 65dd8578..f7d4b2ce 100644 --- a/server/game_logic/characters/character.cpp +++ b/server/game_logic/characters/character.cpp @@ -3,8 +3,6 @@ #include #include -#include "../collectables/collectable.h" - CharacterBody::CharacterBody(size_t id, const uint8_t& character, int x, int y, int w, int h, Vector2D velocity, size_t health, uint8_t state, size_t revive_cooldown): @@ -25,7 +23,7 @@ uint8_t CharacterBody::get_character() const { return character_reference; } uint8_t CharacterBody::get_state() const { return state; } -bool CharacterBody::is_alive() const { return health == 0; } +bool CharacterBody::is_dead() const { return health == 0; } //------- Setters -------- diff --git a/server/game_logic/characters/character.h b/server/game_logic/characters/character.h index b7a950a4..64a90bf7 100644 --- a/server/game_logic/characters/character.h +++ b/server/game_logic/characters/character.h @@ -10,6 +10,7 @@ #include "../../../common/common_constants.h" #include "../../../game_engine/physics_engine/collision_manager.h" #include "../../../game_engine/physics_engine/physics_object/dynamic_body.h" +#include "../collectables/collectable.h" class CharacterBody: public DynamicBody { protected: @@ -40,7 +41,7 @@ class CharacterBody: public DynamicBody { size_t get_health() const; uint8_t get_character() const; uint8_t get_state() const; - bool is_alive() const; + bool is_dead() const; //------- Setters -------- diff --git a/server/game_logic/characters/enemy.cpp b/server/game_logic/characters/enemy.cpp index 3ba2faac..38dd01e6 100644 --- a/server/game_logic/characters/enemy.cpp +++ b/server/game_logic/characters/enemy.cpp @@ -20,7 +20,7 @@ Enemy::Enemy(size_t id, const uint8_t& character, int attack_damage, int health, void Enemy::update_db() { - if (is_alive()) { + if (!is_dead()) { position += velocity; } } diff --git a/server/game_logic/characters/player.cpp b/server/game_logic/characters/player.cpp index d99327b8..28857fd4 100644 --- a/server/game_logic/characters/player.cpp +++ b/server/game_logic/characters/player.cpp @@ -5,10 +5,12 @@ #include "../weapons/guns.h" +#define MAX_FALL_SPEED 10 + Player::Player(size_t id, std::string name, const uint8_t& character, int x, int y, CollisionManager& collision_manager): CharacterBody(id, character, x, y, PLAYER_WIDTH, PLAYER_HEIGHT, - Vector2D(NO_SPEED, DEFAULT_SPEED_Y), MAX_HEALTH, STATE_IDLE_RIGHT, + Vector2D(NO_SPEED, MAX_FALL_SPEED), MAX_HEALTH, STATE_IDLE_RIGHT, REVIVE_COOLDOWN), name(std::move(name)), points(STARTING_POINTS), @@ -94,20 +96,28 @@ void Player::move_right() { } void Player::jump() { - on_floor = false; - velocity.y = -JUMP_SPEED; + if (on_floor) { + on_floor = false; + velocity.y = -JUMP_SPEED; + } } // ------------ Override Methods -------------- void Player::update_db() { - if (is_alive()) { // if the player is dead, then it shouldnt move + // print on_floor + std::cout << on_floor << std::endl; + + + if (is_dead()) { // if the player is dead, then it shouldnt move return; } if (!on_floor) { - velocity.y += GRAVITY; + if (velocity.y < MAX_FALL_SPEED) { + velocity.y += GRAVITY; + } } else { velocity.x -= FRICCTION * direction; @@ -119,5 +129,39 @@ void Player::update_db() { position += velocity; - // print_info(); + velocity.x = 0; + + print_info(); +} + +void Player::handle_colision(CollisionObject* other) { + + on_floor = true; + + CollisionFace face = is_touching(other); + + // cast to Collectable + Collectable* collectable = dynamic_cast(other); + + if (!collectable) { + std::cout << "NOT COLLECTABLE!" << std::endl; + } + + if (face == CollisionFace::LEFT || + face == CollisionFace::RIGHT) { // if im touching something on my side, then i cant + // move + // into it + velocity.x = 0; + } else if (face == CollisionFace::TOP) { // if i touch something on top, then i cant move into + std::cout << "TOUCHING TOP" << std::endl; + // it and i stop moving up + velocity.y = 0; + } else if (face == CollisionFace::BOTTOM) { + std::cout << "TOUCHING FLOOR" << std::endl; + velocity.y = 10; // set a small value to avoid getting stuck in the air while walking off + // platform + on_floor = true; + } else if (face == CollisionFace::NONE) { + on_floor = false; + } } diff --git a/server/game_logic/characters/player.h b/server/game_logic/characters/player.h index 102c3568..7c13b48e 100644 --- a/server/game_logic/characters/player.h +++ b/server/game_logic/characters/player.h @@ -37,6 +37,7 @@ class Player: public CharacterBody { //------- Overrided Methods -------- void update_db() override; + void handle_colision(CollisionObject* other) override; //------- Getters -------- @@ -63,6 +64,7 @@ class Player: public CharacterBody { void reset_intoxication(); bool is_player_intoxicated() const; + void decrease_intoxication_cooldown(); size_t get_intoxication_cooldown() const; @@ -75,7 +77,7 @@ class Player: public CharacterBody { //------- Movement Methods -------- - virtual void do_special_attack() = 0; + virtual void do_special_attack(); void move_left() override; void move_right() override; void jump() override; diff --git a/server/game_logic/platforms/box_platform.cpp b/server/game_logic/platforms/box_platform.cpp index b3697546..dcb2a360 100644 --- a/server/game_logic/platforms/box_platform.cpp +++ b/server/game_logic/platforms/box_platform.cpp @@ -6,9 +6,9 @@ #include "../../../game_engine/physics_engine/physics_object/dynamic_body.h" -BoxPLatoform::BoxPLatoform(int x, int y, int width, int height): StaticBody(x, y, width, height) {} +BoxPlatform::BoxPlatform(int x, int y, int width, int height): StaticBody(x, y, width, height) {} -void BoxPLatoform::handle_colision(CollisionObject* other) { +void BoxPlatform::handle_colision(CollisionObject* other) { CollisionFace face = this->is_touching(other); DynamicBody* dynamic_body = dynamic_cast(other); diff --git a/server/game_logic/platforms/box_platform.h b/server/game_logic/platforms/box_platform.h index 0ba34332..fe3b4cdc 100644 --- a/server/game_logic/platforms/box_platform.h +++ b/server/game_logic/platforms/box_platform.h @@ -1,6 +1,4 @@ -// -// Created by santi on 06/06/24. -// + #ifndef BOX_PLATFORM_H #define BOX_PLATFORM_H @@ -8,10 +6,10 @@ #include "../../../game_engine/physics_engine/physics_object/static_body.h" -class BoxPLatoform: public StaticBody { +class BoxPlatform: public StaticBody { public: - BoxPLatoform(int x, int y, int width, int height); + BoxPlatform(int x, int y, int width, int height); void handle_colision(CollisionObject* other) override; }; diff --git a/server/game_logic/weapons/bullet.cpp b/server/game_logic/weapons/bullet.cpp index 7efa75e6..383cd25f 100644 --- a/server/game_logic/weapons/bullet.cpp +++ b/server/game_logic/weapons/bullet.cpp @@ -45,7 +45,7 @@ void Bullet::handle_colision(CollisionObject* other) { if (character) { character->take_damage(bullet_damage); player_owner.add_points(BULLET_POINTS); - if (!character->is_alive()) { + if (character->is_dead()) { player_owner.add_points(BULLET_BONUS_POINTS); } } diff --git a/server/server_main.cpp b/server/server_main.cpp index 9df6d916..17e92b9b 100644 --- a/server/server_main.cpp +++ b/server/server_main.cpp @@ -2,6 +2,7 @@ #include "server.h" + int main(int argc, const char* argv[]) { try { int output = -1; diff --git a/tests/graphics/gui_phy_test.cpp b/tests/graphics/gui_phy_test.cpp index 8f09f4bf..8850a839 100644 --- a/tests/graphics/gui_phy_test.cpp +++ b/tests/graphics/gui_phy_test.cpp @@ -5,22 +5,22 @@ #include "../../game_engine/gui/canvas_object.h" #include "../../game_engine/gui/widgets/color_rect.h" #include "../../game_engine/physics_engine/collision_manager.h" -#include "../../game_engine/physics_engine/physics_object/dynamic_body.h" -#include "../../game_engine/physics_engine/physics_object/static_body.h" +#include "../../server/game_logic/characters/player.h" +#include "../../server/game_logic/platforms/box_platform.h" + +class PlayerTest: public Player, public engine::CanvasObject { -class Player: public DynamicBody, public engine::CanvasObject { private: - engine::ColorRect color_rect; - float base_speed = 10; - int jump_force = 25; - int gravity = 1; - float friction = 0.1f; - bool on_floor = true; - bool is_jumping = false; - int direction = 1; + engine::ColorRect& color_rect; public: + PlayerTest(size_t id, std::string name, const uint8_t& character, int x, int y, + CollisionManager& collision_manager, engine::ColorRect& color_rect): + Player(id, name, character, x, y, collision_manager), color_rect(color_rect) {} + + void draw(SDL_Renderer* renderer) override { color_rect.draw(renderer); } + // create a method that prints the position of the player and the velocity void print_position() { std::cout << "--------------------------------" << std::endl; @@ -30,53 +30,17 @@ class Player: public DynamicBody, public engine::CanvasObject { void update_color_rect() { color_rect.set_position(position.x, position.y); } - void update_db() override { - - if (!on_floor) { - velocity.y += gravity; - - } else { - velocity.x -= friction * direction; - } - - - position += velocity; - - print_position(); - } - - - void handle_colision(CollisionObject* other) override { - - if (is_touching_bool(*other)) { - velocity.y = 10; - on_floor = true; - } - } - - - void jump() { - on_floor = false; - velocity.y = -jump_force; - } - - Player(int x, int y, int width, int height, engine::ColorRect& color_rect): - DynamicBody(x, y, width, height, Vector2D(0, 10)), color_rect(color_rect) {} - - void on_key_press(SDL_Keycode key) override { + void on_key_press(const SDL_Keycode& key) override { switch (key) { case SDLK_a: - direction = -1; - velocity.x = -base_speed; + move_left(); break; case SDLK_d: - direction = 1; - velocity.x = base_speed; + move_right(); break; case SDLK_w: - if (on_floor) - jump(); + jump(); break; default: velocity.x = 0; @@ -84,20 +48,19 @@ class Player: public DynamicBody, public engine::CanvasObject { } } - void draw(SDL_Renderer* renderer) override { color_rect.draw(renderer); } - void set_position(int x, int y) override {} bool is_intersecting(SDL_Point& point) const override { return false; } bool is_intersecting(SDL_Rect& rect) const override { return false; } }; -class Platform: public StaticBody, public engine::CanvasObject { + +class PlatformTest: public BoxPlatform, public engine::CanvasObject { private: engine::ColorRect color_rect; public: - Platform(int x, int y, int width, int height, engine::ColorRect& color_rect): - StaticBody(x, y, width, height), color_rect(color_rect) {} + PlatformTest(int x, int y, int width, int height, engine::ColorRect& color_rect): + BoxPlatform(x, y, width, height), color_rect(color_rect) {} void draw(SDL_Renderer* renderer) override { color_rect.draw(renderer); } @@ -107,29 +70,31 @@ class Platform: public StaticBody, public engine::CanvasObject { bool is_intersecting(SDL_Rect& rect) const override { return false; } }; + int main() { engine::Window window(1600, 800, true, true); - auto renderer = window.getRenderer(); + auto renderer = window.get_renderer(); CollisionManager collision_manager(1600, 800); SDL_Rect rect = {0, 500, 1600, 400}; engine::ColorRect color_rect({255, 0, 0, 255}, rect); - auto platform = std::make_shared(0, 500, 1600, 400, color_rect); + auto platform = std::make_shared(0, 500, 1600, 400, color_rect); SDL_Rect rect_2 = {800, 300, 100, 50}; engine::ColorRect color_rect_2({0, 255, 0, 255}, rect_2); - auto platform_2 = std::make_shared(800, 300, 100, 50, color_rect_2); + auto platform_2 = std::make_shared(800, 300, 100, 50, color_rect_2); SDL_Rect rect_3 = {1000, 100, 100, 800}; engine::ColorRect color_rect_3({0, 255, 255, 255}, rect_3); - auto platform_3 = std::make_shared(1000, 100, 100, 800, color_rect_3); + auto platform_3 = std::make_shared(1000, 100, 100, 800, color_rect_3); SDL_Rect player_cube = {100, 200, 50, 50}; engine::ColorRect color_player({0, 255, 0, 255}, player_cube); - auto player = std::make_shared(100, 200, 50, 50, color_player); + auto player = + std::make_shared(1, "player", 1, 100, 200, collision_manager, color_player); - collision_manager.add_dynamic_body(player); + collision_manager.track_dynamic_body(player); collision_manager.add_object(platform); collision_manager.add_object(platform_2); collision_manager.add_object(platform_3);