00001 #include <SDL/SDL.h>
00002 #include <string.h>
00003 #include <iostream>
00004 #include <math.h>
00005
00006
00007 #include "ufo.h"
00008 #include "util.h"
00009 #include "nncmath.h"
00010
00011 #ifndef M_PI
00012 #define M_PI 3.14159265
00013 #endif
00014
00015 UFO::UFO(ObjectManager &om) : GameObject(om, "UFO")
00016 {
00017 vector <Surface *> v = Util_parseImage(UFO_IMG_FILE);
00018
00019
00020 sprite = new Sprite(v, "UFO");
00021 SetCollidable(true);
00022
00023
00024 SetDamagable(true);
00025 SetDamage (1);
00026
00027
00028 movingDir = NOT_MOVING;
00029 rpos.value = 0;
00030 sinex = 0;
00031 siney = 0;
00032 velocity_count = 0;
00033
00034
00035 lasttime = Util_GetTime();
00036 lastcirctime = Util_GetTime();
00037
00038
00039 currentPos = (position)Util_Random(MAX_POS);
00040 radius = Util_Random(50) + 25;
00041 notmoving_delay = Util_Random(1000) + 3000;
00042 velocity_delay = (Util_Random(1000) + 3000) / 400;
00043 circ_delay = Util_Random(15)+1;
00044 velocity = Util_Random(10) + 2;
00045
00046
00047 SetupPosition();
00048 SetupDirection();
00049
00050 }
00051
00052 UFO::~UFO()
00053 {
00054 }
00055
00056 void UFO::SetupPosition()
00057 {
00058 int nx[] = { 150, 1024-150, 150, 1024-150 };
00059 int ny[] = { 150, 150, 768-150, 768-150 };
00060 if(currentPos < MAX_POS) {
00061 x = (double)nx[(int)currentPos];
00062 y = (double)ny[(int)currentPos];
00063 SetPosition (x+sinex, y+siney);
00064 }
00065 }
00066
00067 void UFO::SetupDirection()
00068 {
00069 if(movingDir == NOT_MOVING) {
00070 if(currentPos == UPPER_LEFT || currentPos == LOWER_LEFT)
00071 sprite->SetFrame(3);
00072 else
00073 sprite->SetFrame(0);
00074 } else {
00075 switch(currentPos) {
00076 case UPPER_LEFT:
00077 if(movingDir == GOING_RIGHT)
00078 sprite->SetFrame(3);
00079 else
00080 sprite->SetFrame(4);
00081 break;
00082 case UPPER_RIGHT:
00083 if(movingDir == GOING_LEFT)
00084 sprite->SetFrame(0);
00085 else
00086 sprite->SetFrame(2);
00087 break;
00088 case LOWER_LEFT:
00089 if(movingDir == GOING_UP)
00090 sprite->SetFrame(5);
00091 else
00092 sprite->SetFrame(3);
00093 break;
00094 case LOWER_RIGHT:
00095 if(movingDir == GOING_UP)
00096 sprite->SetFrame(1);
00097 else
00098 sprite->SetFrame(0);
00099 break;
00100 default:
00101 sprite->SetFrame(1);
00102 break;
00103 }
00104 }
00105 }
00106
00107 void UFO::think()
00108 {
00109 int count;
00110
00111 if((count = Util_TimePassed(&lastcirctime, circ_delay)) != 0) {
00112 rpos.value += (0.05 * count/2);
00113
00114 sinex = NNCMath::Cos(rpos) * radius;
00115 siney = NNCMath::Sin(rpos) * radius;
00116 SetPosition(x+sinex,y+siney);
00117 lastcirctime = Util_GetTime();
00118 }
00119
00120 if(movingDir == NOT_MOVING) {
00121 if((count = Util_TimePassed(&lasttime, notmoving_delay)) != 0) {
00122 bool out;
00123 do {
00124 out = true;
00125
00126 movingDir = (direction)Util_Random(MAX_DIR);
00127 switch(movingDir) {
00128 case GOING_UP:
00129 if(currentPos == UPPER_LEFT || currentPos == UPPER_RIGHT)
00130 out = false;
00131 break;
00132 case GOING_DOWN:
00133 if(currentPos == LOWER_LEFT || currentPos == LOWER_RIGHT)
00134 out = false;
00135 break;
00136 case GOING_LEFT:
00137 if(currentPos == UPPER_LEFT || currentPos == LOWER_LEFT)
00138 out = false;
00139 break;
00140 case GOING_RIGHT:
00141 if(currentPos == UPPER_RIGHT || currentPos == LOWER_RIGHT)
00142 out = false;
00143 break;
00144 default: out = true; break;
00145 }
00146 } while(!out);
00147 SetupDirection();
00148 }
00149 } else {
00150 if((count = Util_TimePassed(&lasttime, velocity_delay)) != 0) {
00151 switch(movingDir) {
00152 case GOING_UP:
00153 y -= velocity*count;
00154 break;
00155 case GOING_DOWN:
00156 y += velocity*count;
00157 break;
00158 case GOING_RIGHT:
00159 x += velocity*count;
00160 break;
00161 case GOING_LEFT:
00162 x -= velocity*count;
00163 break;
00164 default: break;
00165 }
00166 SetPosition(x+sinex,y+siney);
00167 velocity_count += (int)(velocity*count);
00168 if(velocity_count > 468 && (movingDir == GOING_UP || movingDir == GOING_DOWN)) {
00169 velocity_count = 0;
00170 if(movingDir == GOING_UP) {
00171 if(currentPos == LOWER_RIGHT) {
00172 currentPos = UPPER_RIGHT;
00173 } else {
00174 currentPos = UPPER_LEFT;
00175 }
00176 } else if(movingDir == GOING_DOWN) {
00177 if(currentPos == UPPER_RIGHT) {
00178 currentPos = LOWER_RIGHT;
00179 } else {
00180 currentPos = LOWER_LEFT;
00181 }
00182 }
00183 movingDir = NOT_MOVING;
00184 SetupDirection();
00185 SetupPosition();
00186 } else if(velocity_count > 724 && (movingDir == GOING_LEFT || movingDir == GOING_RIGHT)) {
00187 velocity_count = 0;
00188 if(movingDir == GOING_RIGHT) {
00189 if(currentPos == UPPER_LEFT) {
00190 currentPos = UPPER_RIGHT;
00191 } else {
00192 currentPos = LOWER_RIGHT;
00193 }
00194 } else if(movingDir == GOING_LEFT) {
00195 if(currentPos == UPPER_RIGHT) {
00196 currentPos = UPPER_LEFT;
00197 } else {
00198 currentPos = LOWER_LEFT;
00199 }
00200 }
00201 movingDir = NOT_MOVING;
00202 SetupDirection();
00203 SetupPosition();
00204 }
00205
00206 }
00207 }
00208 }
00209
00210