Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <SDL.h>
  4. #include <iostream>
  5. #include "Texture.h"
  6. #include "Animation.h"
  7. #include "Player.h"
  8.  
  9. class Tile
  10. {
  11. public:
  12.     Tile();
  13.     ~Tile();
  14.  
  15.     static enum State {Solid, Passive};
  16.  
  17.     void SetContent(std::string image,State state, float posX,float posY, SDL_Renderer *renderer);
  18.     void UnloadContent();
  19.     void Update(Player &e);
  20.     void Draw(SDL_Renderer *renderer, SDL_Rect tRect);
  21.  
  22. private:
  23.     Texture tTexture;
  24.     State state;
  25.  
  26.     float posX, posY;
  27. };
  28.  
  29.  
  30.  
  31. Tile.cpp
  32. #include "Tile.h"
  33.  
  34. Tile::Tile()
  35. {
  36. }
  37.  
  38. Tile::~Tile()
  39. {
  40. }
  41.  
  42. void Tile::SetContent(std::string image, State state, float posX, float posY, SDL_Renderer *renderer)
  43. {
  44.     this->posX = posX;
  45.     this->posY = posY;
  46.     this->tTexture.loadFromFile(image, renderer);
  47. }
  48. void Tile::UnloadContent()
  49. {
  50.     tTexture.free();
  51. }
  52. void Tile::Update(Player &e)
  53. {
  54.     FloatRect tileRect(posX,posY,32,32);
  55.  
  56.     if (e.rect->Collision(tileRect) && state == State::Solid)
  57.     {
  58.         if (e.rect->Right >= tileRect.Left && e.prevRect->Right <= tileRect.Left)
  59.         {
  60.             e.posX = tileRect.Left-32;
  61.         }
  62.         else if (e.rect->Left <= tileRect.Right && e.prevRect->Left >= tileRect.Right)
  63.         {
  64.             e.posX = tileRect.Right;
  65.         }
  66.         else if (e.rect->Bottom >= tileRect.Top && e.prevRect->Bottom<= tileRect.Top)
  67.         {
  68.             e.posY = tileRect.Top - 32;
  69.         }
  70.         else if (e.rect->Top <= tileRect.Bottom && e.prevRect->Top >= tileRect.Bottom)
  71.         {
  72.             e.posY = tileRect.Bottom;
  73.         }
  74.     }
  75. }
  76. void Tile::Draw(SDL_Renderer *renderer, SDL_Rect tRect)
  77. {
  78.     tTexture.render(posX, posY, renderer, &tRect);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement