-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc52390
commit b7a4529
Showing
11 changed files
with
383 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "CollisionComponent.hpp" | ||
#include "GameObject.hpp" | ||
#include "PhysicsComponent.hpp" | ||
|
||
CollisionComponent::CollisionComponent() {} | ||
|
||
bool CollisionComponent::GetAffectedByCollision() { return this->affected_by_collision; } | ||
float CollisionComponent::GetRestitution() { return this->restitution; } | ||
|
||
void CollisionComponent::SetAffectedByCollision(bool affected_by_collision) { | ||
this->affected_by_collision = affected_by_collision; | ||
} | ||
void CollisionComponent::SetRestitution(float restitution) { this->restitution = restitution; } | ||
|
||
std::unordered_set<GameObject *> CollisionComponent::GetColliders() { return this->colliders; } | ||
void CollisionComponent::AddCollider(GameObject *game_object) { | ||
this->colliders.insert(game_object); | ||
} | ||
void CollisionComponent::RemoveCollider(GameObject *game_object) { | ||
this->colliders.erase(game_object); | ||
} | ||
|
||
void CollisionComponent::HandleCollisions(GameObject *game_object) { | ||
if (this->GetColliders().size() > 0) { | ||
for (GameObject *collider : this->GetColliders()) { | ||
int obj_x = static_cast<int>( | ||
std::round(game_object->GetComponent<PhysicsComponent>()->GetPosition().x)); | ||
int obj_y = static_cast<int>( | ||
std::round(game_object->GetComponent<PhysicsComponent>()->GetPosition().y)); | ||
|
||
int col_x = static_cast<int>( | ||
std::round(collider->GetComponent<PhysicsComponent>()->GetPosition().x)); | ||
int col_y = static_cast<int>( | ||
std::round(collider->GetComponent<PhysicsComponent>()->GetPosition().y)); | ||
|
||
int obj_width = game_object->GetComponent<PhysicsComponent>()->GetSize().width; | ||
int obj_height = game_object->GetComponent<PhysicsComponent>()->GetSize().height; | ||
int col_width = collider->GetComponent<PhysicsComponent>()->GetSize().width; | ||
int col_height = collider->GetComponent<PhysicsComponent>()->GetSize().height; | ||
|
||
int left_overlap = (obj_x + obj_width) - col_x; | ||
int right_overlap = (col_x + col_width) - obj_x; | ||
int top_overlap = (obj_y + obj_height) - col_y; | ||
int bottom_overlap = (col_y + col_height) - obj_y; | ||
|
||
int min_overlap = std::min(std::min(left_overlap, right_overlap), | ||
std::min(top_overlap, bottom_overlap)); | ||
|
||
int pos_x = 0, pos_y = 0; | ||
if (min_overlap == left_overlap) { | ||
pos_x = col_x - obj_width; | ||
pos_y = obj_y; | ||
} else if (min_overlap == right_overlap) { | ||
pos_x = col_x + col_width; | ||
pos_y = obj_y; | ||
} else if (min_overlap == top_overlap) { | ||
pos_x = obj_x; | ||
pos_y = col_y - obj_height; | ||
} else if (min_overlap == bottom_overlap) { | ||
pos_x = obj_x; | ||
pos_y = col_y + col_height; | ||
} | ||
|
||
game_object->GetComponent<PhysicsComponent>()->SetPosition( | ||
Position{float(pos_x), float(pos_y)}); | ||
|
||
float vel_x = game_object->GetComponent<PhysicsComponent>()->GetVelocity().x; | ||
float vel_y = game_object->GetComponent<PhysicsComponent>()->GetVelocity().y; | ||
if (min_overlap == left_overlap || min_overlap == right_overlap) { | ||
vel_x *= -this->GetRestitution(); | ||
} | ||
if (min_overlap == top_overlap || min_overlap == bottom_overlap) { | ||
vel_y *= -this->GetRestitution(); | ||
} | ||
game_object->GetComponent<PhysicsComponent>()->SetVelocity(Velocity{vel_x, vel_y}); | ||
} | ||
} | ||
} | ||
|
||
void CollisionComponent::Update(GameObject *game_object, int64_t delta) { | ||
this->HandleCollisions(game_object); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "NetworkComponent.hpp" | ||
#include "GameObject.hpp" | ||
|
||
void NetworkComponent::Update(GameObject *game_object, int64_t delta) {} | ||
|
||
std::string NetworkComponent::GetPlayerAddress() { return this->player_address; } | ||
NetworkRole NetworkComponent::GetOwner() { return this->owner; } | ||
|
||
void NetworkComponent::SetPlayerAddress(std::string player_address) { | ||
this->player_address = player_address; | ||
} | ||
void NetworkComponent::SetOwner(NetworkRole owner) { this->owner = owner; } |
Oops, something went wrong.