Advertisement
s1ay3r44

Daemons Character Movement and Animation Code

Oct 11th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. /////////////
  2. //ANIMATE
  3. /////////////
  4.  
  5. void Character::Animate(sf::RenderWindow& App, Game& Game)
  6. {
  7.     float offset =  128 * App.GetFrameTime();
  8.     if(PosX < MapX * 64)
  9.         {
  10.             PosX += offset;
  11.             Game.View.Move(offset, 0);
  12.         }
  13.     else if(PosX > MapX * 64)
  14.         {
  15.             PosX -= offset;
  16.             Game.View.Move(-offset, 0);
  17.         }
  18.     else if(PosY < MapY * 64)
  19.         {
  20.             PosY += offset;
  21.             Game.View.Move(0, offset);
  22.         }
  23.     else if(PosY > MapY * 64)
  24.         {
  25.             PosY -= offset;
  26.             Game.View.Move(0, -offset);
  27.         }
  28.     else if(PosX == MapX * 64 && PosY == MapY * 64)
  29.         {
  30.             Moving = false;
  31.             //PosX ++;
  32.             //MapX++;
  33.             //PosY++;
  34.             //MapY++;
  35.             //cout << "Moving == false";
  36.         }
  37.     Timer++;
  38.     if(Timer == 1 && Moving)
  39.         Direction++;
  40.     else if(Timer == 8 && Moving)
  41.         Direction--;
  42.     else if(Timer == 16 && Moving)
  43.         Direction += 2;
  44.     else if (Timer == 24 && Moving)
  45.         Direction -= 2;
  46.     if(Moving)
  47.      cout << Timer << " ";
  48.     //else if (Timer == 64)
  49.     //cout << PosX << " " << PosY << " " << MapX << " " << MapY << "\n";
  50.  
  51.  
  52.     return;
  53. }
  54.  
  55. //////////////
  56. //DISPLAY
  57. //////////////
  58.  
  59. void Character::Display(sf::RenderWindow& App, Game& Game)
  60. {
  61.     const sf::Input& Input = App.GetInput();
  62.     if(!Moving)
  63.     {
  64.         if(Input.IsKeyDown(sf::Key::W) || Input.IsKeyDown(sf::Key::Up))
  65.         {
  66.             if(Game.CollisionData[MapX + (MapY-1) * Game.MapWidth] != 1)
  67.                 MapY--;
  68.             Moving = true;
  69.             Timer = 0;
  70.             Direction = 3;
  71.         }
  72.          else if(Input.IsKeyDown(sf::Key::A) || Input.IsKeyDown(sf::Key::Left))
  73.         {
  74.             if(Game.CollisionData[(MapX-1) + MapY * Game.MapWidth] != 1)
  75.                 MapX--;
  76.             Moving = true;
  77.             Timer = 0;
  78.             Direction = 6;
  79.         }
  80.          else if(Input.IsKeyDown(sf::Key::S) || Input.IsKeyDown(sf::Key::Down))
  81.         {
  82.             if(Game.CollisionData[MapX + (MapY+1) * Game.MapWidth] != 1)
  83.                 MapY++;
  84.             Moving = true;
  85.             Timer = 0;
  86.             Direction = 0;
  87.         }
  88.          else if(Input.IsKeyDown(sf::Key::D) || Input.IsKeyDown(sf::Key::Right))
  89.         {
  90.             if(Game.CollisionData[(MapX+1) + MapY * Game.MapWidth] != 1)
  91.                 MapX++;
  92.             Moving = true;
  93.             Timer = 0;
  94.             Direction = 9;
  95.         }
  96.  
  97.     }
  98.  
  99.     Animate(App, Game);
  100.     CharacterSprite[Direction].SetPosition(PosX, PosY);
  101.     App.Draw(CharacterSprite[Direction]);
  102.  
  103.     return;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement