123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #include "Ship.h"
- #include <Text.h>
- #include <Globals.h>
- #include <TastaturEreignis.h>
- #include <FrameworkMath.h>
- #include "Schuss.h"
- #include "Asteroid.h"
- #include <iostream>
- #include <Textur2D.h>
- // Inhalt der Ship Klasse aus Ship.h
- // Konstruktor
- Ship::Ship( Model2DData* data, Vec2< float > p, Vec2< float > s, float r )
- : Model2DObject()
- {
- for( const Polygon2D& i : *data->polygons )
- {
- if( i.name->istGleich( "engine_l" ) )
- {
- stL = *i.schwerpunkt;
- Vertex l, r;
- for( int j = 0; j < 4; j++ )
- {
- if( i.tKordinaten->get( j ).y == 1.f )
- {
- if( i.tKordinaten->get( j ).x == 0.f )
- l = i.vertex->get( j );
- if( i.tKordinaten->get( j ).x == 1.f )
- r = i.vertex->get( j );
- }
- }
- kL = ((l + (r - l) * 0.5) - stL);
- }
- if( i.name->istGleich( "engine_r" ) )
- {
- stR = *i.schwerpunkt;
- Vertex l, r;
- for( int j = 0; j < 4; j++ )
- {
- if( i.tKordinaten->get( j ).y == 1.f )
- {
- if( i.tKordinaten->get( j ).x == 0.f )
- l = i.vertex->get( j );
- if( i.tKordinaten->get( j ).x == 1.f )
- r = i.vertex->get( j );
- }
- }
- kR = ((l + (r - l) * 0.5) - stR);
- }
- if( i.name->istGleich( "engine_m" ) )
- {
- stM = *i.schwerpunkt;
- Vertex l, r;
- for( int j = 0; j < 4; j++ )
- {
- if( i.tKordinaten->get( j ).y == 1.f )
- {
- if( i.tKordinaten->get( j ).x == 0.f )
- l = i.vertex->get( j );
- if( i.tKordinaten->get( j ).x == 1.f )
- r = i.vertex->get( j );
- }
- }
- kM = ((l + (r - l) * 0.5) - stM);
- }
- }
- rSpeed = 0;
- setModel( data );
- setDrehung( r );
- setPosition( p );
- setSpeed( s );
- tastenstände = 0;
- }
- // Destruktor
- Ship::~Ship()
- {}
- #define sgn( x ) x < 0 ? -1 : 1
- // nicht constant
- bool Ship::istGetroffen( Schuss* zSchuss )
- {
- if( getMasse() < 40 * 40 )
- return 0;
- if( istPunktInnen( zSchuss->getPosition() ) )
- {
- impuls( zSchuss->getPosition() - zSchuss->getSpeed(), zSchuss->getSpeed() * 0.3f );
- return 1;
- }
- return 0;
- }
- void Ship::setTexture( Textur2D* zFlammenM, Textur2D* zFlammenL, Textur2D* zFlammenR, Bild* textur )
- {
- Textur2D* txt = new Textur2D();
- txt->setTexturZ( textur );
- setTextur( txt, "ship" );
- setTextur( dynamic_cast<Textur2D*>(zFlammenL->getThis()), "engine_l" );
- setTextur( dynamic_cast<Textur2D*>(zFlammenR->getThis()), "engine_r" );
- setTextur( dynamic_cast<Textur2D*>(zFlammenM->getThis()), "engine_m" );
- }
- void Ship::setPRS( Vec2< float > p, Vec2< float > s, float r, float rSpeed )
- {
- setDrehung( r );
- setPosition( p );
- setSpeed( s );
- setDrehungSpeed( rSpeed );
- }
- void Ship::setTastenstände( char ts )
- {
- tastenstände = ts;
- }
- bool Ship::tick( const WeltInfo& info, double zeit )
- {
- if( (tastenstände | 1) == tastenstände )
- {
- impuls( getWorldPos( stM ), getWorldDir( kM ) * (float)zeit * 10 );
- }
- if( (tastenstände | 2) == tastenstände )
- {
- impuls( getWorldPos( stL ), getWorldDir( kL ) * ((float)zeit * 10) );
- }
- if( (tastenstände | 4) == tastenstände )
- {
- impuls( getWorldPos( stR ), getWorldDir( kR ) * ((float)zeit * 10) );
- }
- speed /= 1.01f;
- rSpeed /= 1.01f;
- return __super::tick( info, zeit );
- }
- // constant
- void Ship::save( Datei* zD ) const
- {
- zD->schreibe( (char*)&position.x, 4 );
- zD->schreibe( (char*)&position.y, 4 );
- zD->schreibe( (char*)&speed.x, 4 );
- zD->schreibe( (char*)&speed.y, 4 );
- float r = getDrehung();
- zD->schreibe( (char*)&r, 4 );
- zD->schreibe( (char*)&rSpeed, 4 );
- }
- Schuss* Ship::getSchuss() const
- {
- Vertex pos = position;
- Vertex sp = Vec2<float>( cos( getDrehung() ), sin( getDrehung() ) ) * 200 + getSpeed();
- while( istPunktInnen( pos ) )
- pos += sp / 60.0f;
- return new Schuss( pos, sp );
- }
|