Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

crosshair.cpp

Go to the documentation of this file.
00001 #include "crosshair.h"
00002 #include <math.h>
00003 #include "util.h"
00004 #include "nncmath.h"
00005 #include <iostream>
00006 
00007 using namespace std;
00008 
00009 Crosshair::Crosshair(ObjectManager &om) : GameObject(om, "Crosshair")
00010 {
00011   SetLayer (10);
00012 
00013   vector <Surface *> v = Util_parseImage(CROSSHAIR_IMG_FILE);
00014   sprite = new Sprite(v, "Crosshair");
00015 
00016   sprite->SetFrame(15);
00017 
00018   radius = 1;
00019 }
00020 
00021 Crosshair::~Crosshair() 
00022 {
00023 }
00024 
00025 int Crosshair::SetPosition(double x, double y) {
00026         if (!sprite) return 0;
00027 
00028         double oldx = Xpos;
00029         double oldy = Ypos;
00030         int xdif = abs((int)(oldx - x));
00031         int ydif = abs((int)(oldy - y));
00032         int currentFrame = sprite->GetFrame();
00033         static int counter = 0;
00034 
00035         // Call the game object default after 
00036         // the things we need.
00037         GameObject::SetPosition (x, y);
00038 
00039         return 1;
00040 }
00041 
00042 void Crosshair::think() {
00043         static nnctime old = Util_GetTime();
00044         static nnctime velTime = Util_GetTime();
00045         static double oldX = GetX();
00046         static double oldY = GetY();
00047         Uint32 ms = 50;
00048         int cFrame = sprite->GetFrame();
00049         static int maxFrame = sprite->CountFrames();
00050         int timePassed = Util_TimePassed(&old, ms);
00051         double newX = GetX();
00052         double newY = GetY();
00053         double diffX = newX - oldX;
00054         if (diffX < 0) diffX = -diffX;
00055         double diffY = newY - oldY;
00056         if (diffY < 0) diffY = -diffY;
00057         double distance = NNCMath::SRoot((diffX * diffX) + (diffY * diffY));
00058         nnctime time = Util_DiffTime(&velTime);
00059         double total_time = time.msecs + (time.secs / 1000);
00060         double velocity; 
00061         
00062         if (total_time != 0) velocity = distance / total_time;
00063         else velocity = 0.0;
00064 
00066         if (sprite) {
00067                 if (diffX < 10 && diffY < 10) {
00068                         if (timePassed > 0 && cFrame == 0) timePassed = 0;
00069                 //the crosshair hasn't moved much
00070                         switch(timePassed) {
00071                                 //see if 500 ms have passed and how many times it has passed
00072                                 case 0: break;
00073                                 default:
00074                                         for (int i = cFrame; i > cFrame - timePassed &&  i >= 0; i--)
00075                                                 (*sprite)--;
00076                                         break;
00077                         }       
00078                 }
00079 
00080                 else { //the crosshair has moved 
00081                         if (cFrame < maxFrame - 1) {
00082                                 //not on the last frame (we don't want it to loop the frames back to 0)
00083                                 if (velocity < 100) (*sprite)++;        
00084                                 else if (velocity < 200) {
00085                                         //increase by 2 frames
00086                                         for (int i = 0; i < 2 && cFrame + i < maxFrame - 1; i++) {
00087                                                 (*sprite)++;
00088                                         }
00089                                 }
00090                                 else if (velocity < 300) {
00091                                         //increase by 3 frames
00092                                         for (int i = 0; i < 3 && cFrame + i < maxFrame - 1; i++) {
00093                                                 (*sprite)++;
00094                                         }
00095                                 }
00096                                 else if (velocity < 400) {
00097                                         //increase by 4 frames
00098                                         for (int i = 0; i < 4 && cFrame + i < maxFrame - 1; i++) {
00099                                                 (*sprite)++;
00100                                         }
00101                                 }
00102                                 else if (velocity < 500) {
00103                                         //increase by 5 frames
00104                                         for (int i = 0; i < 5 && cFrame + i < maxFrame - 1; i++) {
00105                                                 (*sprite)++;
00106                                         }
00107                                 }
00108                                 else if (velocity < 600) {
00109                                         //increase by 6 frames
00110                                         for (int i = 0; i < 6 && cFrame + i < maxFrame - 1; i++) {
00111                                                 (*sprite)++;
00112                                         }
00113                                 }
00114                                 else if (velocity < 700) {
00115                                         //increase by 7 frames
00116                                         for (int i = 0; i < 7 && cFrame + i < maxFrame - 1; i++) {
00117                                                 (*sprite)++;
00118                                         }
00119                                 }
00120                                 else if (velocity < 800) {
00121                                         //increase by 8 frames
00122                                         for (int i = 0; i < 8 && cFrame + i < maxFrame - 1; i++) {
00123                                                 (*sprite)++;
00124                                         }
00125                                 }
00126                                 else if (velocity < 900) {
00127                                         //increase by 9 frames
00128                                         for (int i = 0; i < 9 && cFrame + i < maxFrame - 1; i++) {
00129                                                 (*sprite)++;
00130                                         }
00131                                 }
00132                                 else if (velocity < 1000) {
00133                                         //increase by 10 frames
00134                                         for (int i = 0; i < 10 && cFrame + i < maxFrame - 1; i++) {
00135                                                 (*sprite)++;
00136                                         }
00137                                 }
00138                                 else {
00139                                         //increase all the way
00140                                         for (int i = cFrame; i < maxFrame - 1; i++) {
00141                                                 (*sprite)++;
00142                                         }
00143                                 }
00144                         }
00145                 }
00146         }
00147         oldX = newX;
00148         oldY = newY;    
00149         velTime = Util_GetTime();
00150 }
00151 
00152 int Crosshair::Recenter()
00153 {
00154 #if 1 
00155         // crosshair ignores the subworld coordinates
00156         if (sprite) {
00157                 return sprite->SetPosition (GetX()-sprite->GetWidth()/2, GetY()-sprite->GetHeight()/2);
00158         }
00159         return 0;
00160 #else
00161         return GameObject::Recenter();
00162 #endif
00163 }
00164 

Generated on Sun Dec 8 12:02:19 2002 for nnc by doxygen1.3-rc1