SHARE
TWEET

Untitled

a guest Jun 27th, 2016 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Map.CPP:
  2.  
  3. #include <SFML/Graphics.hpp>
  4. #include "../Entities/Entity.h"
  5.  
  6. class TileMap: public sf::Drawable, public sf::Transformable {
  7. private:
  8.     std::vector<Entity> entities;
  9. public:
  10.  
  11.     Entity* addEntity(Entity player) {
  12.         entities.push_back(player);
  13.         return &entities[entities.size() - 1];
  14.     }
  15.  
  16.     void update(){
  17.         for (int i = 0; i < entities.size(); i++){
  18.             entities[i].update();
  19.         }
  20.     }
  21.  
  22.     virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
  23.         for (int i = 0; i < entities.size(); i++){
  24.             target.draw(entities[i]);
  25.         }
  26.     }
  27.  
  28. };
  29.  
  30.  
  31. DrawableEntity.h
  32.  
  33. /*
  34.  * DrawableEntity.h
  35.  *
  36.  *  Created on: Jun 25, 2016
  37.  *      Author: gaming
  38.  */
  39.  
  40. #ifndef SRC_ENTITIES_DRAWABLEENTITY_H_
  41. #define SRC_ENTITIES_DRAWABLEENTITY_H_
  42.  
  43. #include <SFML/Graphics/Drawable.hpp>
  44.  
  45. #include "Entity.h"
  46.  
  47. class DrawableEntity: public Entity, public sf::Drawable {
  48. public:
  49.     DrawableEntity();
  50.     virtual void update() override;
  51.     void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
  52.     sf::Vector2f getLocation();
  53.     virtual ~DrawableEntity();
  54. };
  55.  
  56. #endif /* SRC_ENTITIES_DRAWABLEENTITY_H_ */
  57.  
  58. DrawableEntity.cpp
  59.  
  60. /*
  61.  * DrawableEntity.cpp
  62.  *
  63.  *  Created on: Jun 25, 2016
  64.  *      Author: gaming
  65.  */
  66.  
  67. #include "DrawableEntity.h"
  68.  
  69. DrawableEntity::DrawableEntity() {
  70.     // TODO Auto-generated constructor stub
  71.  
  72. }
  73.  
  74. void DrawableEntity::update(){
  75.  
  76. }
  77.  
  78. sf::Vector2f DrawableEntity::getLocation() {
  79.     sf::Vector2f vec(x, y);
  80.     return vec;
  81. }
  82.  
  83. void DrawableEntity::draw(sf::RenderTarget& target, sf::RenderStates states) const {
  84.  
  85. }
  86.  
  87. DrawableEntity::~DrawableEntity() {
  88.  
  89. }
  90.  
  91. Player.h
  92.  
  93. #ifndef SRC_ENTITIES_PLAYER_H_
  94. #define SRC_ENTITIES_PLAYER_H_
  95. #include <SFML/Graphics.hpp>
  96. #include "../graphics/AnimatedSprite.h"
  97. #include "DrawableEntity.h"
  98.  
  99. using namespace sf;
  100.  
  101. class Player: public DrawableEntity {
  102. protected:
  103.     float moveSpeed;
  104.     int dir;
  105.     int lastDir;
  106.     AnimatedSprite up, down, left, right, idle;
  107.     AnimatedSprite *currentAnim;
  108.  
  109. public:
  110.     Texture texture;
  111.     Player(int x, int y);
  112.     void update() override;
  113.     virtual ~Player();
  114.  
  115.     virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
  116. };
  117.  
  118. #endif /* SRC_ENTITIES_PLAYER_H_ */
  119.  
  120. Player.cpp
  121.  
  122. /*
  123.  * Player.cpp
  124.  *
  125.  *  Created on: Jun 24, 2016
  126.  *      Author: gaming
  127.  */
  128.  
  129. #include "Player.h"
  130.  
  131. Player::Player(int x, int y) {
  132.     Player::x = x;
  133.     Player::y = y;
  134.     Player::moveSpeed = .5f;
  135.     Player::dir = 4;
  136.     Player::lastDir = 4;
  137.     Player::currentAnim = &idle;
  138.     if (!texture.loadFromFile("res/player.png")) {
  139.     }
  140.  
  141.     AnimatedSprite upNew(96, &texture); //Any shorter way of doing these two lines without haveing to create a new instance like this?
  142.     up = upNew;
  143.    
  144.     Player::height = 96;
  145.     Player::width = 96;
  146. }
  147.  
  148. void Player::update() {
  149.     float xChange, yChange;
  150.     xChange = 0;
  151.     yChange = 0;
  152.    
  153.     //handle input and direction changing of sprites
  154.  
  155.     currentAnim->update();
  156.     currentAnim->setLocation(x, y);
  157. }
  158.  
  159. void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const {
  160.     currentAnim->draw(target, states);
  161. }
  162.  
  163.  
  164. Player::~Player() {
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top