123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #include "Welt2D.h"
- #include "Bild.h"
- using namespace Framework;
- Object2D::Object2D()
- {
- rSpeed = 0;
- rotation = 0;
- size = 1;
- ref = 1;
- }
- Object2D::~Object2D()
- {}
- void Object2D::explosion( Vertex worldPos, float intensity )
- {
- intensity /= ( position - worldPos ).getLengthSq();
- speed += ( position - worldPos ) * intensity;
- }
- void Object2D::impuls( Vertex start, Vertex speed )
- {}
- void Object2D::setPosition( Vertex pos )
- {
- position = pos;
- }
- void Object2D::setPosition( float x, float y )
- {
- position = Vertex( x, y );
- }
- void Object2D::setDrehung( float drehung )
- {
- rotation = drehung;
- while( rotation > PI * 2 )
- rotation -= (float)PI * 2;
- while( rotation < 0 )
- rotation += (float)PI * 2;
- }
- void Object2D::addDrehung( float drehung )
- {
- rotation += drehung;
- while( rotation > PI * 2 )
- rotation -= (float)PI * 2;
- while( rotation < 0 )
- rotation += (float)PI * 2;
- }
- void Object2D::setSize( float size )
- {
- this->size = size;
- }
- void Object2D::addSize( float size )
- {
- this->size += size;
- }
- bool Object2D::tick( double zeit )
- {
- rotation += rSpeed * (float)zeit;
- position += speed * (float)zeit;
- return rSpeed != 0 || speed != Vertex( 0, 0 );
- }
- Mat3< float > Object2D::getObjectMatrix() const
- {
- return Mat3<float>::translation( position ) * Mat3<float>::rotation( rotation ) * Mat3<float>::scaling( size );
- }
- Vertex Object2D::getPosition() const
- {
- return position;
- }
- float Object2D::getDrehung() const
- {
- return rotation;
- }
- float Object2D::getSize() const
- {
- return size;
- }
- Object2D *Object2D::getThis()
- {
- ref++;
- return this;
- }
- Object2D *Object2D::release()
- {
- if( !--ref )
- delete this;
- return 0;
- }
- Welt2D::Welt2D()
- {
- objects = new RCArray< Object2D >();
- ref = 1;
- }
- Welt2D::~Welt2D()
- {
- objects->release();
- }
- void Welt2D::addObject( Object2D *obj )
- {
- objects->add( obj );
- }
- void Welt2D::explosion( Vertex worldPos, float intensity, float maxRad )
- {
- maxRad = maxRad * maxRad;
- for( auto obj = objects->getArray(); obj.set; obj++ )
- {
- if( ( obj.var->getPosition() - worldPos ).getLengthSq() < maxRad )
- obj.var->explosion( worldPos, intensity );
- }
- }
- void impuls( Vertex start, Vertex speed )
- {}
- bool Welt2D::tick( double zeit )
- {
- bool ret = 0;
- for( auto obj = objects->getArray(); obj.set; obj++ )
- ret |= obj.var->tick( zeit );
- return ret;
- }
- void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj )
- {
- for( auto obj = objects->getArray(); obj.set; obj++ )
- {
- Rect< float > bnd = obj.var->getBoundingBox();
- Vertex topRight = Vertex( bnd.topLeft.y, bnd.bottomRight.x );
- Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
- bnd.bottomRight = kamMat * bnd.bottomRight;
- bnd.topLeft = kamMat * bnd.topLeft;
- topRight = kamMat * topRight;
- bottomLeft = kamMat * bottomLeft;
- if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < size.x ) ||
- ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < size.y ) ||
- ( bnd.topLeft.x >= 0 && bnd.topLeft.x < size.x ) ||
- ( bnd.topLeft.y >= 0 && bnd.topLeft.y < size.y ) ||
- ( topRight.x >= 0 && topRight.x < size.x ) ||
- ( topRight.y >= 0 && topRight.y < size.y ) ||
- ( bottomLeft.x >= 0 && bottomLeft.x < size.x ) ||
- ( bottomLeft.y >= 0 && bottomLeft.y < size.y ) )
- obj.var->render( kamMat, zRObj );
- }
- }
- Welt2D *Welt2D::getThis()
- {
- ref++;
- return this;
- }
- Welt2D *Welt2D::release()
- {
- if( !--ref )
- delete this;
- return 0;
- }
|