Files
DemoGame/src/Engine/Entity/Entity.cpp
2025-01-02 00:15:37 -05:00

89 lines
2.4 KiB
C++

#include "J3ML/J3ML.hpp"
#include "Engine/Entity/Entity.h"
#include "Engine/Globals.h"
using namespace J3ML;
void Entity::MoveX(float speed) {
position.x = position.x + (speed * Globals::Window->GetDeltaTime());
}
void Entity::MoveY(float speed) {
position.y = position.y + (speed * Globals::DeltaTime());
}
void Entity::MoveForward(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
}
void Entity::MoveBackward(float speed) {
speed = -speed;
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
}
void Entity::MoveLeft(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * -Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
}
void Entity::MoveRight(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * -Math::Cos(face_angle);
}
void Entity::SetRotation(float new_face_angle) {
face_angle = new_face_angle;
if (face_angle < 0)
face_angle = fmod(face_angle + Math::Pi * 2, Math::Pi * 2);
else if (face_angle >= 2 * M_PI)
face_angle = fmod(face_angle, Math::Pi * 2);
}
void Entity::Rotate(float speed) {
SetRotation(face_angle + speed * Globals::DeltaTime());
}
float Entity::GetRotation() const {
return face_angle;
}
Vector2 Entity::GetPosition() const {
return position;
}
bool Entity::AppendChild(Entity* entity) {
bool success = false;
if (!Globals::CurrentScene->EntityListContains(entity))
children.push_back(entity), success = true;
return success;
}
Entity::~Entity() {
for (auto* e : children)
delete e;
children = {};
}
void Entity::UpdateChildren() {
for (auto& e : children) {
e->Update();
if (!e->children.empty())
e->UpdateChildren();
}
}
void Entity::DestroyChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
delete *it, children.erase(it);
}
void Entity::RemoveChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
children.erase(it);
}