#ifndef SHIP_CLASS #define SHIP_CLASS #include "ship.h" Ship::Ship () { x = 50; y = 50; vx = 0; vy = 0; fuel = 0; } Ship::Ship (float px, float py, float pvx, float pvy) { x = px; y = py; vx = pvx; vy = pvy; fuel = 0; } Ship::Ship (float px, float py, float pvx, float pvy, float pTime) { x = px; y = py; vx = pvx; vy = pvy; time = pTime; fuel = 0; } vector Ship::NewPosition (float theta, bool fire, float pTime) { float alpha; time += pTime; if (!fire) { x += vx * pTime; y += vy * pTime; } else while (pTime >= 1) { vx += cos(theta) * ACCEL; vy += sin(theta) * ACCEL; if (sqrt (vx * vx + vy * vy) > MAXSPEED) { alpha = atan (vy / vx); vx = MAXSPEED * cos (alpha); vy = MAXSPEED * sin (alpha); } // Otherwise, vx + vy already == MAXSPEED fuel += sqrt (vx * vx + vy * vy); x += vx; y += vy; pTime--; } if (x >= 0.0) { x = ((int)x % 100) + (x - (int) x); } else x = 100 - (abs ((int)x % 100) + (x - (int) x)); if (y >= 0.0) { y = ((int)y % 100) + (y - (int) y); } else y = 100 - (abs ((int)y % 100) + (y - (int) y)); return GetPosition (); } vector Ship::GetPosition () { vector v (x, y); return v; } vector Ship::GetVelocity () { vector v (vx, vy); return v; } point Ship::GetShape () { return GetPosition(); } float Ship::GetTime () { return time; } float abs (float x) { if (x < 0.0) return x * -1.0; else return x; } double abs (double x) { if (x < 0.0) return x * -1.0; else return x; } istream& operator >> (istream& in, Ship &s) { float f; vector pos, vel; in >> pos >> vel >> f; s.x = pos[0]; s.y = pos[1]; s.vx = vel[0]; s.vy = vel [1]; s.fuel = f; } ostream& operator << (ostream& out, const Ship &s) { out << s.GetPosition() << s.GetVelocity() << s.fuel; } #endif