Skip to content

Commit

Permalink
testing graphics and physics
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiSev committed Jun 8, 2024
1 parent b1c76c5 commit 02c3481
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 86 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions common/common_constants.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef TP_FINAL_COMMON_CONSTANTS_H
#define TP_FINAL_COMMON_CONSTANTS_H

#include <cstddef>
#include <cstdint>

// Game Settings
Expand Down
2 changes: 1 addition & 1 deletion game_engine/physics_engine/collision_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions game_engine/physics_engine/collision_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class CollisionManager {
std::vector<std::vector<std::shared_ptr<CollisionObject>>>
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<std::tuple<std::shared_ptr<DynamicBody>, Vector2D>> dynamic_bodies;

void handle_out_of_bounds(std::shared_ptr<DynamicBody> obj);
void place_object_in_grid(std::shared_ptr<CollisionObject> obj);
void remove_object_from_grid(std::shared_ptr<CollisionObject> obj, Vector2D position);
Expand All @@ -37,6 +34,7 @@ class CollisionManager {
void detect_colisions(std::shared_ptr<DynamicBody> obj);

public:
std::vector<std::tuple<std::shared_ptr<DynamicBody>, Vector2D>> dynamic_bodies;
CollisionManager(int levelWidth, int levelHeight);

std::shared_ptr<CollisionObject> get_collision_object_at(int x, int y) const;
Expand Down
4 changes: 1 addition & 3 deletions server/game_logic/characters/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <cstdint>
#include <utility>

#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):
Expand All @@ -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 --------

Expand Down
3 changes: 2 additions & 1 deletion server/game_logic/characters/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 --------

Expand Down
2 changes: 1 addition & 1 deletion server/game_logic/characters/enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
56 changes: 50 additions & 6 deletions server/game_logic/characters/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand All @@ -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<Collectable*>(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;
}
}
4 changes: 3 additions & 1 deletion server/game_logic/characters/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Player: public CharacterBody {
//------- Overrided Methods --------

void update_db() override;
void handle_colision(CollisionObject* other) override;

//------- Getters --------

Expand All @@ -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;

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions server/game_logic/platforms/box_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DynamicBody*>(other);
Expand Down
8 changes: 3 additions & 5 deletions server/game_logic/platforms/box_platform.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
//
// Created by santi on 06/06/24.
//


#ifndef BOX_PLATFORM_H
#define BOX_PLATFORM_H

#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;
};

Expand Down
2 changes: 1 addition & 1 deletion server/game_logic/weapons/bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions server/server_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "server.h"


int main(int argc, const char* argv[]) {
try {
int output = -1;
Expand Down
Loading

0 comments on commit 02c3481

Please sign in to comment.