123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include "Ship.h"
- #include <Text.h>
- #include <Globals.h>
- #include <TastaturEreignis.h>
- #include <FrameworkMath.h>
- #include "Schuss.h"
- #include "Asteroid.h"
- #include <iostream>
- // 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, char tastenStände )
- {
- 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 -= breite + ship->zModel()->maxP.x * ship->getSize();
- if( minP.y > höhe && speed.y > 0 )
- pos.y -= höhe + ship->zModel()->maxP.y * ship->getSize();
- ship->setPosition( pos );
- if( ( tastenStände | 1 ) == tastenStände )
- {
- speed.x += (float)( 4.2 * cos( ship->getDrehung() ) ) * (float)zeit * 30;
- speed.y += (float)( 4.2 * sin( ship->getDrehung() ) ) * (float)zeit * 30;
- }
- else
- {
- float movementAngle = atan2( speed.y, speed.x );
- if( speed.x != 0 )
- speed.x += 1.2f * cos( movementAngle + (float)PI ) * (float)zeit * 30;
- if( speed.y != 0 )
- speed.y += 1.2f * sin( movementAngle + (float)PI ) * (float)zeit * 30;
- 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( ( tastenStände | 8 ) != tastenStände )
- {
- if( ( tastenStände | 2 ) == tastenStände )
- ship->addDrehung( (float)zeit * 2 );
- if( ( tastenStände | 4 ) == tastenStände )
- 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<float>( 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;
- }
|