275 lines
No EOL
5.2 KiB
C++
Executable file
275 lines
No EOL
5.2 KiB
C++
Executable file
#include "headers/gameobject.hpp"
|
|
|
|
namespace asloengine
|
|
{
|
|
|
|
GameObject::GameObject(sf::String _name)
|
|
{
|
|
|
|
name = _name;
|
|
|
|
position = sf::Vector2f(0, 0);
|
|
scale = sf::Vector2f(1, 1);
|
|
origin = sf::Vector2f(0, 0);
|
|
rotation = 0;
|
|
|
|
}
|
|
|
|
GameObject::~GameObject() {}
|
|
|
|
void GameObject::start(sf::RenderWindow *window)
|
|
{
|
|
|
|
render_window = window;
|
|
|
|
alive = true;
|
|
|
|
LOG("GameObject '" + name + "' has been Started");
|
|
|
|
on_start();
|
|
|
|
}
|
|
|
|
void GameObject::update(float delta_time)
|
|
{
|
|
|
|
if (alive)
|
|
{
|
|
on_update(delta_time);
|
|
if (main_property_changed)
|
|
{
|
|
on_main_property_update();
|
|
main_property_changed = false;
|
|
}
|
|
on_post_update(delta_time);
|
|
}
|
|
|
|
}
|
|
|
|
void GameObject::destroy()
|
|
{
|
|
|
|
if (alive)
|
|
{
|
|
on_destroy();
|
|
|
|
alive = false;
|
|
|
|
LOG("GameObject '" + name + "' has been Destroyed");
|
|
|
|
delete this;
|
|
}
|
|
|
|
}
|
|
|
|
sf::Vector2f GameObject::calculate_child_position(sf::Vector2f& offset, sf::Vector2f& scale, float rotation)
|
|
{
|
|
|
|
float radians = DEG_TO_RAD * (rotation + 90);
|
|
float sinus = sin(radians);
|
|
float cosinus = cos(radians);
|
|
|
|
sf::Vector2f position_vec
|
|
{
|
|
offset.x * scale.x * sinus + offset.y * scale.y * cosinus,
|
|
offset.x * scale.x * -cosinus + offset.y * scale.y * sinus
|
|
};
|
|
|
|
return position_vec;
|
|
|
|
}
|
|
|
|
sf::Vector2f GameObject::calculate_child_position_centered(
|
|
sf::FloatRect& parent_rect, sf::FloatRect& child_rect, sf::Vector2f& offset, sf::Vector2f& scale, float rotation
|
|
)
|
|
{
|
|
|
|
offset.x += parent_rect.width / 2 - child_rect.width / 2;
|
|
offset.y += parent_rect.height / 2 - child_rect.height / 2;
|
|
|
|
return calculate_child_position(offset, scale, rotation);
|
|
|
|
}
|
|
|
|
sf::View GameObject::set_crop(sf::Vector2f _position, sf::Vector2f _size)
|
|
{
|
|
|
|
return set_crop(_position.x, _position.y, _size.x, _size.y);
|
|
|
|
}
|
|
|
|
sf::View GameObject::set_crop(float _left, float _top, float _width, float _height)
|
|
{
|
|
|
|
sf::Vector2u window = render_window->getSize();
|
|
|
|
sf::View last_view = render_window->getView();
|
|
sf::View view = last_view;
|
|
|
|
view.reset(sf::FloatRect(_left, _top, _width, _height));
|
|
view.setViewport(sf::FloatRect(_left / window.x, _top / window.y, _width / window.x, _height / window.y));
|
|
render_window->setView(view);
|
|
|
|
return last_view;
|
|
|
|
}
|
|
|
|
void GameObject::set_position(sf::Vector2f& _position)
|
|
{
|
|
|
|
position = _position;
|
|
|
|
main_property_changed = true;
|
|
|
|
}
|
|
|
|
void GameObject::set_scale(sf::Vector2f& _scale)
|
|
{
|
|
|
|
scale = _scale;
|
|
|
|
main_property_changed = true;
|
|
|
|
}
|
|
|
|
void GameObject::set_origin(sf::Vector2f& _origin)
|
|
{
|
|
|
|
origin = _origin;
|
|
|
|
main_property_changed = true;
|
|
|
|
}
|
|
|
|
void GameObject::set_rotation(float _rotation)
|
|
{
|
|
|
|
rotation = _rotation;
|
|
|
|
main_property_changed = true;
|
|
|
|
}
|
|
|
|
void GameObject::set_position(float _position_x, float _position_y)
|
|
{
|
|
|
|
position.x = _position_x;
|
|
position.y = _position_y;
|
|
|
|
main_property_changed = true;
|
|
|
|
}
|
|
|
|
void GameObject::set_scale(float _scale_x, float _scale_y)
|
|
{
|
|
|
|
scale.x = _scale_x;
|
|
scale.y = _scale_y;
|
|
|
|
main_property_changed = true;
|
|
|
|
}
|
|
|
|
void GameObject::set_origin(float _origin_x, float _origin_y)
|
|
{
|
|
|
|
origin.x = _origin_x;
|
|
origin.y = _origin_y;
|
|
|
|
main_property_changed = true;
|
|
|
|
}
|
|
|
|
void GameObject::move(sf::Vector2f& _move)
|
|
{
|
|
|
|
sf::Vector2f vec = position + _move;
|
|
set_position(vec);
|
|
|
|
}
|
|
|
|
void GameObject::rescale(sf::Vector2f& _rescale)
|
|
{
|
|
|
|
sf::Vector2f vec = scale + _rescale;
|
|
set_scale(vec);
|
|
|
|
}
|
|
|
|
void GameObject::translate_origin(sf::Vector2f& _translate_origin)
|
|
{
|
|
|
|
sf::Vector2f vec = origin + _translate_origin;
|
|
set_origin(vec);
|
|
|
|
}
|
|
|
|
void GameObject::rotate(float _rotate)
|
|
{
|
|
|
|
set_rotation(rotation + _rotate);
|
|
|
|
}
|
|
|
|
void GameObject::move(float _move_x, float _move_y)
|
|
{
|
|
|
|
set_position(position.x + _move_x, position.y + _move_y);
|
|
|
|
}
|
|
|
|
void GameObject::rescale(float _rescale_x, float _rescale_y)
|
|
{
|
|
|
|
set_scale(scale.x + _rescale_x, scale.y + _rescale_y);
|
|
|
|
}
|
|
|
|
void GameObject::translate_origin(float _translate_origin_x, float _translate_origin_y)
|
|
{
|
|
|
|
set_origin(origin.x + _translate_origin_x, origin.y + _translate_origin_y);
|
|
|
|
}
|
|
|
|
sf::Vector2f GameObject::get_position() const
|
|
{
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
sf::Vector2f GameObject::get_scale() const
|
|
{
|
|
|
|
return scale;
|
|
|
|
}
|
|
|
|
sf::Vector2f GameObject::get_origin() const
|
|
{
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
float GameObject::get_rotation() const
|
|
{
|
|
|
|
return rotation;
|
|
|
|
}
|
|
|
|
void GameObject::draw() {}
|
|
|
|
void GameObject::on_main_property_update() {}
|
|
|
|
void GameObject::on_start() {}
|
|
|
|
void GameObject::on_update(float delta_time) {}
|
|
|
|
void GameObject::on_post_update(float delta_time) {}
|
|
|
|
void GameObject::on_destroy() {}
|
|
|
|
} |