#include "Ship.h" #include #include #include #include #include "Schuss.h" #include "Asteroid.h" // Inhalt der Ship Klasse aus Ship.h // Konstruktor Ship::Ship( Model2DData *data, Bild *textur, Vec2< float > p, Vec2< float > s, float r ) { ship = new Model2D(); ship->setStyle( Model2D::Style::Sichtbar | Model2D::Style::Textur ); ship->setModel( data ); ship->setTextur( textur ); ship->setDrehung( r ); pos = p; speed = s; ref = 1; } // Destruktor Ship::~Ship() { ship->release(); } // nicht constant bool Ship::tick( double zeit, int breite, int höhe ) { Vertex minP = (Vertex)ship->zModel()->minP * ship->getSize() + ship->getPosition(); Vertex maxP = (Vertex)ship->zModel()->maxP * ship->getSize() + ship->getPosition(); if( maxP.x < 0 && speed.x < 0 ) pos.x = breite + ship->zModel()->maxP.x * ship->getSize(); if( maxP.y < 0 && speed.y < 0 ) pos.y = höhe + ship->zModel()->maxP.y * ship->getSize(); if( minP.x > breite && speed.x > 0 ) pos.x = 0 - ship->zModel()->maxP.x * ship->getSize(); if( minP.y > höhe && speed.y > 0 ) pos.y = 0 - ship->zModel()->maxP.y * ship->getSize(); ship->setPosition( pos ); if( getTastenStand( T_Oben ) || getTastenStand( 'w' ) || getTastenStand( 'W' ) ) { speed.x += (float)( 4.2 * cos( ship->getDrehung() ) ); speed.y += (float)( 4.2 * sin( ship->getDrehung() ) ); } else { float movementAngle = atan2( speed.y, speed.x ); if( speed.x != 0 ) speed.x += 1.2f * cos( movementAngle + (float)PI ); if( speed.y != 0 ) speed.y += 1.2f * sin( movementAngle + (float)PI ); if( speed.x < 6.f && speed.x > -6.f ) speed.x = 0; if( speed.y < 6.f && speed.y > -6.f ) speed.y = 0; } if( !getTastenStand( T_Space ) ) { if( getTastenStand( T_Rechts ) || getTastenStand( 'd' ) || getTastenStand( 'D' ) ) ship->addDrehung( (float)zeit * 2 ); if( getTastenStand( T_Links ) || getTastenStand( 'a' ) || getTastenStand( 'A' ) ) ship->addDrehung( (float)-zeit * 2 ); } if( speed.x || speed.y ) { pos += speed * (float)zeit; ship->setPosition( pos ); } return ship->tick( zeit ); } void Ship::render( Bild &zRObj ) { ship->render( zRObj ); } // constant void Ship::save( Datei *zD ) const { zD->schreibe( (char*)&pos.x, 4 ); zD->schreibe( (char*)&pos.y, 4 ); zD->schreibe( (char*)&speed.x, 4 ); zD->schreibe( (char*)&speed.y, 4 ); float r = ship->getDrehung(); zD->schreibe( (char*)&r, 4 ); } Schuss *Ship::getSchuss() const { return new Schuss( pos, Vec2( cos( ship->getDrehung() ), sin( ship->getDrehung() ) ) * 200 ); } bool Ship::istTod( Asteroid *zA ) const { return ship->istModelInnen( zA->zModel() ); } Punkt Ship::getKamPos( int breite, int höhe ) const { Punkt ret; ret.x = (int)pos.x - 400; ret.y = (int)pos.y - 250; if( ret.x < 0 ) ret.x = 0; if( ret.y < 0 ) ret.y = 0; if( ret.x > breite - 800 ) ret.x = breite - 800; if( ret.y > höhe - 500 ) ret.y = höhe - 500; return ret; } Punkt Ship::getPos() const { return (Punkt)pos; } // Reference Counting Ship *Ship::getThis() { ref++; return this; } Ship *Ship::release() { ref--; if( !ref ) delete this; return 0; }