Advertisement
thecplusplusguy

player.h

Nov 17th, 2012
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include <SDL/SDL_thread.h>
  3. #include <iostream>
  4. #include <vector>  
  5. #include <cstdlib> 
  6. #include <ctime>
  7. #include <cmath>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. class Monster{
  13.     public:
  14.         Monster(const char* typeOfMob,bool boss,int x, int y);
  15.         int calcDamage();
  16.         void update(int delta,int playerX, int playerY, int chanx,int chany);
  17.        
  18.         bool hitPlayer;
  19.     private:
  20.         float mobSpeed;
  21.         double posX,posY;
  22.         int damageMax;
  23.         int damageMin;
  24.         bool isBoss;
  25.         int deltaTime;
  26.         SDL_Surface* mobImage;
  27. };
  28.  
  29. Monster::Monster(const char* typeOfMob, bool boss, int x, int y)
  30. {
  31.     isBoss = boss;
  32.     posX = x;
  33.     posY = y;
  34.     hitPlayer = false;
  35.    
  36.     if(typeOfMob == "goblin")
  37.     {
  38.         damageMin = 5;
  39.         damageMax = 10;
  40.         mobSpeed = 1.f;
  41.         mobImage = SDL_LoadBMP("Content/Images/Player/up.bmp");
  42.     }else
  43.     {
  44.         damageMin = 5;
  45.         damageMax = 10;
  46.         mobSpeed = 1.f; //I increase the speed
  47.         mobImage = SDL_LoadBMP("Content/Images/Mobs/goblin.bmp");
  48.     }
  49.    
  50. }
  51.  
  52. void Monster::update(int delta,int playerX, int playerY, int chanx,int chany)
  53. {
  54.         //pointing to the player
  55.     double x=SDL_GetVideoSurface()->w/2-(playerX+posX);
  56.     double y=SDL_GetVideoSurface()->h/2-(playerY+posY);
  57.    
  58.     //normalize
  59.     double length=sqrt(x*x+y*y);
  60.     x/=length;
  61.     y/=length;
  62.    
  63.     //move on the vector
  64.     posX+=x*mobSpeed;
  65.     posY+=y*mobSpeed;
  66.    
  67.     SDL_Rect tmprect = {posX+playerX,posY+playerY}; //make sure the monster is static on the screen, by adding the camera coordinate to it's position
  68.    
  69.     SDL_BlitSurface(mobImage,NULL,SDL_GetVideoSurface(),&tmprect);
  70. }
  71.  
  72. int Monster::calcDamage()
  73. {
  74.     return rand()%damageMax+damageMin;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement