game/sdlpp/sdlpp_renderobject.cpp

154 lines
4.2 KiB
C++
Raw Normal View History

2020-11-21 19:57:40 +00:00
#include "sdlpp_renderobject.hpp"
2021-01-31 20:48:48 +00:00
#include <cmath>
2020-11-21 19:57:40 +00:00
namespace SDLPP {
2021-01-31 20:48:48 +00:00
void RenderObject::setPos( double x, double y ) {
og_x = x;
og_y = y;
updateSizeAndPosition();
}
void RenderObject::setPos( const std::pair< double, double > &pos ) {
setPos( pos.first, pos.second );
}
std::pair< double, double > RenderObject::getPos() const {
return { og_x, og_y };
}
2020-11-21 19:57:40 +00:00
bool RenderObject::colidesWith( const RenderObject &other ) const {
if ( !hasCollisions() || !other.hasCollisions() || getHidden() ||
other.getHidden() ) {
return false;
}
for ( const auto &x : collisions ) {
for ( const auto &y : other.getCollisions() ) {
if ( x->colidesWith( *y ) )
return true;
}
}
return false;
}
bool RenderObject::hasCollisions() const {
return !collisions.empty();
}
const std::vector< std::shared_ptr< CollisionPolygon > > &
RenderObject::getCollisions() const {
return collisions;
}
void RenderObject::setTexture( const std::shared_ptr< Texture > &t ) {
texture = t;
}
void RenderObject::setTexture( const std::string &img_path ) {
texture = std::make_shared< Texture >( renderer, img_path );
}
void RenderObject::setTexture( Font &font, const std::string &text,
const std::string &color,
const std::string &outline_color,
int outline_size ) {
texture = std::make_shared< Texture >( renderer, font, text, color,
outline_color, outline_size );
}
void RenderObject::unsetTexture() {
texture.reset();
}
void RenderObject::unsetColor() {
polygon.reset();
}
// per second, relative to window width
void RenderObject::setMovementSpeed( double speed ) {
movementSpeed = speed;
}
void RenderObject::addMovement( int x, int y ) {
movementDirection.first += x;
movementDirection.second += y;
}
void RenderObject::resetMovementX() {
movementDirection.first = 0;
}
void RenderObject::resetMovementY() {
movementDirection.second = 0;
}
void RenderObject::clearColided() {
colidedWith.clear();
}
void RenderObject::addColided( std::shared_ptr< RenderObject > &obj ) {
colidedWith.push_back( obj );
}
std::vector< std::shared_ptr< RenderObject > > &RenderObject::getColidedWith() {
return colidedWith;
}
void RenderObject::setId( uint64_t input_id ) {
id = input_id;
}
uint64_t RenderObject::getId() {
return id;
}
void RenderObject::setHidden( bool hid ) {
hidden = hid;
}
bool RenderObject::getHidden() const {
return hidden;
}
void RenderObject::destroy() {
setHidden( true );
kill = true;
}
bool RenderObject::getKilled() {
return kill;
}
void RenderObject::setColiderColor( const std::string &color ) {
colider_color = getColorsHEX( color );
}
2021-01-31 20:48:48 +00:00
void RenderObject::move( int ticks ) {
if ( permanent )
return;
auto addx =
static_cast< double >( movementSpeed * movementDirection.first ) *
( static_cast< double >( ticks ) / 1000 );
auto addy =
static_cast< double >( movementSpeed * movementDirection.second ) *
( static_cast< double >( ticks ) / 1000 );
if ( std::isnan( addx ) || std::isnan( addy ) )
return;
og_x += addx;
og_y += addy;
custom_move( ticks );
updateSizeAndPosition();
}
2020-11-21 19:57:40 +00:00
void RenderObject::setPermanent( bool perm ) {
permanent = perm;
setStatic( perm );
}
bool RenderObject::getPermanent() const {
return permanent;
}
bool RenderObject::isStatic() {
return is_static;
}
void RenderObject::setStatic( bool stat ) {
is_static = stat;
}
std::shared_ptr< Renderer > RenderObject::getRenderer() const {
return renderer;
}
void RenderObject::setSceneID( int id ) {
scene_id = id;
}
void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) {
other->collisions.clear();
for ( auto &colider : collisions ) {
other->collisions.push_back( colider->copySelf() );
}
other->texture.reset();
if ( texture ) {
other->texture = std::make_shared< Texture >( *texture );
}
other->polygon.reset();
if ( polygon ) {
other->polygon = polygon->copySelf();
}
other->colidedWith.clear();
}
2020-11-21 19:57:40 +00:00
} // namespace SDLPP