game/sdlpp/sdlpp_renderobject.cpp

123 lines
3.4 KiB
C++

#include "sdlpp_renderobject.hpp"
namespace SDLPP {
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 );
}
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();
}
} // namespace SDLPP