feat: adding entity, aircraft and scenenode classes

This commit is contained in:
PolishPigeon 2022-01-23 19:13:38 +01:00 committed by KRZYSZTOF RUDNICKI
parent 4a8e139daa
commit 416c9cf617
9 changed files with 140 additions and 2 deletions

View File

@ -0,0 +1,9 @@
#ifndef AIRCRAFT_CPP
#define AIRCRAFT_CPP
Aircraft::Aircraft(Type type) : mType(type)
{
}
#endif

View File

@ -0,0 +1,23 @@
#ifndef AIRCRAFT_HPP
#define AIRCRAFT_HPP
#include "entity.hpp"
class Aircraft : public Entity
{
public:
enum Type
{
Eagle,
Raptor
};
public:
explicit Aircraft(Type type);
private:
Type mType;
};
#include "aircraft.cpp"
#endif

Binary file not shown.

View File

@ -0,0 +1,20 @@
#ifndef ENTITY_CPP
#define ENTITY_CPP
void Entity::SetVelocity(sf::Vector2f velocity)
{
mVelocity = velocity;
}
void Entity::setVelocity(float velocityX, float velocityY)
{
mVelocity.x = velocityX;
mVelocity.y = velocityY;
}
sf::Vector2f Entity::getVelocity() const
{
return mVelocity;
}
#endif

View File

@ -0,0 +1,18 @@
#ifndef ENTITY_HPP
#define ENTITY_HPP
class Entity
{
public:
void SetVelocity(sf::Vector2f velocity);
void setVelocity(float velocityX, float velocityY);
sf::Vector2f getVelocity() const;
private:
sf::Vector2f mVelocity; // default ocnstructor initializes this vector to a zero vector
};
#include "entity.cpp"
#endif

View File

@ -5,6 +5,9 @@
#include <SFML/Graphics.hpp>
#include "constants.hpp"
#include "resources.hpp"
#include "entity.hpp"
#include "aircraft.hpp"
#include "scenenode.hpp"
#include "basic.cpp"
@ -32,9 +35,7 @@ class Game
Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mTexture(), mPlayer()
{
//ResourceHolder textures;
mTexture.load(Textures::Airplane, PATH_TO_PLAYER_TEXTURE);
mPlayer.setTexture(mTexture.get(Textures::Airplane));
mPlayer.setPosition(PLAYER_X_POSITION, PLAYER_Y_POSITION);
}

Binary file not shown.

View File

@ -0,0 +1,33 @@
#ifndef SCENE_NODE_CPP
#define SCENE_NODE_CPP
#include "scenenode.hpp"
void SceneNode::attachChild(ScenePointer child) // takes ownership of the scene node
{
child -> mParent = this;
mChildren.push_back(std::move(child));
}
SceneNode::ScenePointer SceneNode::detachChild(const SceneNode& node) // finds node, releases it and returns it to caller
{
auto found = std::find_if
(
mChildren.begin(), mChildren.end(),
[&] (ScenePointer& p) -> bool { return p.get() == &node; }
);
// This is lambda expression
// [&] (ScenePointer& p) -> bool { return p.get() == &node; }
// [&] - how many and in what way will the lambda expression have access to the variables in surrounding scope, [] - no variables [&] - all variables by reference [=] - all variables by value
// (ScenePointer& p) - parameters passed to the function
// -> bool - return type
// function body encolsed in {}
assert(found != mChildren.end()); // We check validity of the iterator of the found element
ScenePointer result = std::move(*found); // we move the found node out of the container to result
result -> mParent = nullptr; // node's parent is set to null pointer
mChildren.erase(found); // we erase this element from the container
return result; // and we return the pointer to the node
}
#endif

View File

@ -0,0 +1,34 @@
#ifndef SCENE_NODE_HPP
#define SCENE_NODE_HPP
class SceneNode : public sf::Transformable, public sf::Drawable, private sf::NonCopyable
// we derrive from transformable - to be able to store and modify position, rotation and scale
// we derrive from drawable - to be able to draw it on screen
// we derrive from noncopyable - so that copy constructor and copy assignemnt operators are disabled
// This is used to create scene graph (tree data structure) in order to manage transform hierarchies
{
public:
typedef std::unique_ptr<SceneNode> ScenePointer; // element types must be complete types and we do not want to manage memory ourselves so we use std::unique_ptr
public:
SceneNode();
void attachChild(ScenePointer child);
ScenePointer detachChild(const SceneNode& node);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; // we override draw() function of sf::Drawable
// Virtual functions are member functions whose behavior can be overridden in derived classes
// draw() function allows our class to be used like this:
/*
sf::RenderWindow window(...);
SceneNode::ScenePointer node(...);
window.draw(*node);
*/
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; // draws only the current object, and not the children
private:
std::vector<ScenePointer> mChildren;
SceneNode* mParent;
};
#include "scenenode.cpp"
#endif