Commit 064f1d84 authored by pa's avatar pa

Changed class hierarchy

parent d715fbcd
......@@ -2,29 +2,39 @@
#include <SFML/Graphics.hpp>
#include "textureHolder.hpp"
#include "entity.hpp"
#include "sceneNode.hpp"
#include "category.hpp"
class Agent : public Entity{
class Agent : public SceneNode{
public :
enum Type{
qProjectile, tProjectile, Player, PlayerHit, Tower
qProjectile, tProjectile, Player, PlayerHit, Tower,
Sword_0, Sword_1, Sword_2, Sword_3, Sword_4, Sword_5
};
Textures::ID toTextureID(Type type);
public :
Agent(Type type);
void setTexture(TextureHolder& textures);
Agent(Type type, TextureHolder const& textures);
void setType(Type type);
Type getType() const{return mType;}
sf::Sprite& getSprite(){ return mSprite;}
const TextureHolder& getTexHolder() const{return mTextures;}
void setTexture();
sf::Texture& getTexture();
virtual void updateCurrent(sf::Time dt);
virtual sf::FloatRect getBounds() const;
virtual Category::Category identify() const;
virtual Category::Category identify() const{return Category::Agent;}
private :
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
Type mType;
sf::Sprite mSprite;
const TextureHolder& mTextures;
};
......@@ -9,17 +9,18 @@
class Alive : public Agent{
public :
Alive(TextureHolder& Textures, int HP);
enum State{
Normal, HitA, HitB
};
virtual Agent::Type typeOfState();
int getHP() const{return mHP;}
public :
Alive(Agent::Type type, TextureHolder const& Textures, int HP);
virtual Agent::Type typeOfState();
int getHP() const{return mHP;}
State getState() const{return mState;}
virtual void updateCurrent(sf::Time dt);
virtual void act(sf::Time dt);
void moveUp();
......@@ -34,15 +35,7 @@ public :
void hit();
void hitAnimation(sf::Time dt);
virtual void updateCurrent(sf::Time dt);
void keyBindings();
virtual Category::Category identify() const;
TextureHolder& getTexHolder(){
return mTextures;
}
virtual Category::Category identify() const{return Category::Alive;}
private :
State mState;
......@@ -53,9 +46,4 @@ private :
bool mLock;
sf::Time mLockTime;
bool mIsQAvailable;
sf::Time mQCD, mQElapse;
TextureHolder& mTextures;
};
......@@ -2,6 +2,6 @@
namespace Category{
enum Category{
SceneNode, SpriteNode, Entity, Agent, Alive, Player, Ennemy, Orbital
SceneNode, SpriteNode, Entity, Agent, Alive, Player, Ennemy, Orbital, Sword
};
}
......@@ -9,7 +9,7 @@
class Ennemy : public Alive{
public :
Ennemy(TextureHolder& Textures);
Ennemy(TextureHolder const& Textures);
virtual Agent::Type typeOfState() const;
void q_spell(float sense);
......
......@@ -3,7 +3,7 @@
#include "category.hpp"
class Entity : public SceneNode{
class Entity : public Agent{
public:
void setVelocity(sf::Vector2f velocity);
void setVelocity(float x, float y);
......@@ -12,7 +12,7 @@ public:
virtual void updateCurrent(sf::Time dt);
virtual Category::Category identify() const;
virtual Category::Category identify() const{return Category::Entity;}
private:
sf::Vector2f mVelocity;
......
......@@ -8,7 +8,7 @@
class Orbital : public Agent{
public :
Orbital(Agent::Type type , SpeedPatern* spP, bool ally);
Orbital(Agent::Type type , TextureHolder const& texture, SpeedPatern* spP, bool ally);
virtual void updateCurrent(sf::Time dt);
virtual Category::Category identify() const;
......
......@@ -4,21 +4,21 @@
#include "orbital.hpp"
#include "alive.hpp"
#include "category.hpp"
#include "sword.hpp"
class Player : public Alive{
public :
Player(TextureHolder& Textures);
Player(TextureHolder const& Textures);
void q_spell();
virtual void updateCurrent(sf::Time dt);
virtual void act(sf::Time dt);
void keyBindings();
virtual Category::Category identify() const;
virtual Category::Category identify() const{ return Category::Player;}
private :
bool mIsQAvailable;
......
......@@ -29,7 +29,7 @@ public :
sf::Transform getWorldTransform() const;
sf::Vector2f getWorldPosition() const;
virtual Category::Category identify() const;
virtual Category::Category identify() const{return Category::SceneNode;}
virtual sf::FloatRect getBounds() const;
bool collidesWith(SceneNode& node) const;
......
#pragma once
#include "sceneNode.hpp"
#include "agent.hpp"
#include "textureHolder.hpp"
#include "category.hpp"
class SpriteNode : public SceneNode{
class SpriteNode : public Agent{
public :
enum Type{
Floor
};
Textures::ID toTextureID(Type type);
public :
SpriteNode(Type type);
SpriteNode(Type type, const sf::IntRect& rect);
void setTexture(TextureHolder& textures);
virtual Category::Category identify() const;
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
private :
sf::Sprite mSprite;
Type mType;
SpriteNode(Agent::Type type, TextureHolder const& textures);
SpriteNode(Agent::Type type, TextureHolder const& textures, const sf::IntRect& rect);
virtual Category::Category identify() const{return Category::SpriteNode;}
};
#pragma once
#include <SFML/Graphics.hpp>
#include "agent.hpp"
#include "category.hpp"
class Sword : public Agent{
public :
enum State{Increasing, Decreading};
Sword(TextureHolder const& textures);
virtual void updateCurrent(sf::Time dt);
void nextType();
virtual Category::Category identify() const{return Category::Sword;}
private :
bool mIsIncreasing;
sf::Time mAnimationTime;
};
......@@ -8,11 +8,8 @@
namespace Textures{
enum ID{
Floor,
qProjectile,
tProjectile,
Player, PlayerHit,
Tower
Floor, qProjectile, tProjectile, Player, PlayerHit, Tower,
Sword_0, Sword_1, Sword_2, Sword_3, Sword_4, Sword_5
};
}
......@@ -33,6 +30,9 @@ private:
std::map<Identifier, std::unique_ptr<Ressource>> mRessourceMap;
};
// template class RessourceHolder<sf::Texture, Textures::ID>;
template class RessourceHolder<sf::Texture, Textures::ID>;
typedef RessourceHolder<sf::Texture, Textures::ID> TextureHolder;
......
#include "../headers/player.hpp"
Player::Player(TextureHolder const& textures) : Alive(Agent::Player,textures,8), mIsQAvailable(true){
mQCD = sf::seconds(8.f);
}
void Player::q_spell(){
if(mIsQAvailable){
mIsQAvailable = false;
std::unique_ptr<Sword> sword(new Sword(getTexHolder()));
attachChild(std::move(sword));
// sf::Vector2f positionSetter(75.f,0.f);
// sf::Transform transform; transform.rotate(120.f);
// for(int i = 0 ; i < 3 ; ++i){
// std::unique_ptr<Orbital>
// escortR(new Orbital(Agent::qProjectile,new RotationSpeedPatern(positionSetter,540.f), true));
// escortR->setPosition(positionSetter);
// escortR->setTexture(getTexHolder());
// attachChild(std::move(escortR));
// positionSetter = transform.transformPoint(positionSetter);
// }
}
}
void Player::updateCurrent(sf::Time dt){
// Q-SPELL
if(!mIsQAvailable){
if (mQElapse < mQCD) mQElapse += dt;
else {mIsQAvailable = true; mQElapse = sf::Time::Zero;}
}
Alive::updateCurrent(dt);
}
void Player::act(sf::Time dt){
keyBindings();
}
void Player::keyBindings(){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) moveLeft();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) moveRight();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) moveUp();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) moveDown();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) q_spell();
}
Category::Category Player::identify() const{
return Category::Player;
}
......@@ -13,18 +13,42 @@ Textures::ID Agent::toTextureID(Agent::Type type){
return Textures::ID::PlayerHit;
case Tower :
return Textures::ID::Tower;
case Sword_0 :
return Textures::ID::Sword_0;
case Sword_1 :
return Textures::ID::Sword_1;
case Sword_2 :
return Textures::ID::Sword_2;
case Sword_3 :
return Textures::ID::Sword_3;
case Sword_4 :
return Textures::ID::Sword_4;
case Sword_5 :
return Textures::ID::Sword_5;
default :
throw std::logic_error("Agent::toTextureID, non-exhaustive pattern-matching");
}
}
Agent::Agent(Agent::Type type) : mType(type){}
Agent::Agent(Agent::Type type, TextureHolder const& textures) : mType(type), mTextures(textures){
setTexture();
}
void Agent::setTexture(TextureHolder& textures){
mSprite.setTexture(textures.get(toTextureID(mType)));
void Agent::setTexture(){
mSprite.setTexture(mTextures.get(toTextureID(mType)));
mSprite.setOrigin(mSprite.getLocalBounds().width /2.f,
mSprite.getLocalBounds().height/2.f);
}
sf::Texture Agent::getTexture(){
return mTextures.get(toTextureID(mType));
}
void Agent::updateCurrent(sf::Time dt){
SceneNode::updateCurrent(dt);
}
void Agent::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const{
target.draw(mSprite,states);
}
......
#include "../headers/alive.hpp"
Alive::Alive(TextureHolder& textures, int HP) : Agent(Agent::Player), mTextures(textures), mHP(HP),
mState(Normal){
setTexture(mTextures);
}
Alive::Alive(Agent::Type type, TextureHolder const& textures, int HP)
:Agent(type,textures), mHP(HP), mState(Normal){}
void Alive::updateCurrent(sf::Time dt){
if(!isLocked()){
......@@ -55,7 +54,7 @@ void Alive::hitAnimation(sf::Time dt){
switch(mState){
case Normal :
setType(Agent::Player);
setTexture(mTextures);
setTexture();
break;
case HitA : case HitB :
......@@ -72,7 +71,7 @@ void Alive::hitAnimation(sf::Time dt){
break;
}
setType(typeOfState());
setTexture(mTextures);
setTexture();
}
void Alive::moveUp(){
......
#include "../headers/ennemy.hpp"
Ennemy::Ennemy(TextureHolder& textures) : Alive(textures,2), mIsQ(true), mIsW(true){
Ennemy::Ennemy(TextureHolder const& textures) : Alive(Agent::Tower,textures,2), mIsQ(true), mIsW(true){
mQCD = sf::seconds(3.f);
}
......@@ -21,9 +21,9 @@ void Ennemy::q_spell(float sense){
sf::Transform transform; transform.rotate(120.f);
for(int i = 0 ; i < 3 ; ++i){
std::unique_ptr<Orbital>
escortR(new Orbital(Agent::tProjectile,new HellSpeedPatern(positionSetter,sense*49.f,1.007f),false));
escortR(new Orbital(Agent::tProjectile, getTexHolder(),
new HellSpeedPatern(positionSetter,sense*49.f,1.007f),false));
escortR->setPosition(positionSetter);
escortR->setTexture(getTexHolder());
attachChild(std::move(escortR));
positionSetter = transform.transformPoint(positionSetter);
}
......
......@@ -17,7 +17,3 @@ sf::Vector2f& Entity::getVelocity(){
void Entity::updateCurrent(sf::Time dt){
move(mVelocity * dt.asSeconds());
}
Category::Category Entity::identify() const{
return Category::Entity;
}
#include "../headers/orbital.hpp"
#include <iostream>
Orbital::Orbital(Agent::Type type, SpeedPatern* spP, bool ally)
: Agent(type), mSpeedPatern(spP), mLifeSpan(sf::seconds(15.f)), mAlly(ally){
Orbital::Orbital(Agent::Type type, TextureHolder const& textures, SpeedPatern* spP, bool ally)
: Agent(type,textures), mSpeedPatern(spP), mLifeSpan(sf::seconds(15.f)), mAlly(ally){
}
void Orbital::updateCurrent(sf::Time dt){
......
#include "../headers/player.hpp"
Player::Player(TextureHolder& textures) : Alive(textures,8), mIsQAvailable(true){
Player::Player(TextureHolder const& textures) : Alive(Agent::Player,textures,8), mIsQAvailable(true){
mQCD = sf::seconds(8.f);
}
......@@ -8,16 +8,20 @@ void Player::q_spell(){
if(mIsQAvailable){
mIsQAvailable = false;
sf::Vector2f positionSetter(75.f,0.f);
sf::Transform transform; transform.rotate(120.f);
for(int i = 0 ; i < 3 ; ++i){
std::unique_ptr<Orbital>
escortR(new Orbital(Agent::qProjectile,new RotationSpeedPatern(positionSetter,540.f), true));
escortR->setPosition(positionSetter);
escortR->setTexture(getTexHolder());
attachChild(std::move(escortR));
positionSetter = transform.transformPoint(positionSetter);
}
std::unique_ptr<Sword> sword(new Sword(getTexHolder()));
attachChild(std::move(sword));
// sf::Vector2f positionSetter(75.f,0.f);
// sf::Transform transform; transform.rotate(120.f);
// for(int i = 0 ; i < 3 ; ++i){
// std::unique_ptr<Orbital>
// escortR(new Orbital(Agent::qProjectile,new RotationSpeedPatern(positionSetter,540.f), true));
// escortR->setPosition(positionSetter);
// escortR->setTexture(getTexHolder());
// attachChild(std::move(escortR));
// positionSetter = transform.transformPoint(positionSetter);
// }
}
}
......
#include "../headers/sceneNode.hpp"
SceneNode::SceneNode () : mParent(nullptr), isMarkedForRemoval(false){
}
SceneNode::SceneNode () : mParent(nullptr), isMarkedForRemoval(false){}
void SceneNode::attachChild(SceneNode::Ptr child){
......@@ -12,8 +11,8 @@ void SceneNode::attachChild(SceneNode::Ptr child){
SceneNode::Ptr SceneNode::detachChild(const SceneNode& node){
auto found = std::find_if(mChildren.begin(),mChildren.end(),
[&] (Ptr& p) -> bool {return p.get () == &node;}
);
[&] (Ptr& p) -> bool {return p.get () == &node;});
if(found == mChildren.end()) throw std::logic_error("SceneNode::detachChild - Failed to detach child");
Ptr result = std::move(*found);
......@@ -85,9 +84,4 @@ void SceneNode::checkSceneCollision(SceneNode& root, std::set<Pair>& target){
checkNodeCollision(root, target);
for(auto &child : root.mChildren)
checkSceneCollision(*child,target);
}
Category::Category SceneNode::identify() const{
return Category::SceneNode;
}
#include "../headers/spriteNode.hpp"
Textures::ID SpriteNode::toTextureID(SpriteNode::Type type){
switch(type){
case Floor :
return Textures::Floor;
default :
throw std::logic_error("SpriteNode::toTextureID - Non-exhaustive pattern matching");
}
SpriteNode::SpriteNode(Agent::Type type, TextureHolder const& textures)
: Agent(type,textures){
getTexture().setRepeated(true);
}
SpriteNode::SpriteNode(Type type)
: mType(type), mSprite(){}
SpriteNode::SpriteNode(Type type, const sf::IntRect& rect)
: mType(type), mSprite(){
mSprite.setTextureRect(rect);
}
void SpriteNode::setTexture(TextureHolder& textures){
mSprite.setTexture(textures.get(toTextureID(mType)));
SpriteNode::SpriteNode(Agent::Type type, TextureHolder const& textures, const sf::IntRect& rect)
: Agent(type,textures){
getSprite().setTextureRect(rect);
getTexture().setRepeated(true);
}
void SpriteNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const{
target.draw(mSprite,states);
}
Category::Category SpriteNode::identify() const{
return Category::SpriteNode;
SpriteNode::SpriteNode(Type type, const sf::IntRect& rect)
: mType(type), mSprite(){
getSprite.setTextureRect(rect);
}
#include "../headers/sword.hpp"
Sword::Sword(TextureHolder const& mTextures)
: Agent(Agent::Sword_0, mTextures), mAnimationTime(sf::milliseconds(150.f)), mIsIncreasing(true){
getSprite().setOrigin(20.f,20.f);
}
void Sword::nextType(){
switch(getType()){
case Agent::Sword_0 :
if(mIsIncreasing) setType(Agent::Sword_1); else stageForRemoval();
break;
case Agent::Sword_5 :
mIsIncreasing = false;
setType(Agent::Sword_4);
mAnimationTime = sf::milliseconds(500.f);
break;
default :
setType(static_cast<Agent::Type> (static_cast<int>(getType())+(mIsIncreasing? 1 : -1)));
mAnimationTime = sf::milliseconds(150.f);
break;
}
setTexture();
}
void Sword::updateCurrent(sf::Time dt){
Agent::updateCurrent(dt);
mAnimationTime -= dt;
if(mAnimationTime.asSeconds() <= 0.f)
nextType();
}
......@@ -46,6 +46,24 @@ std::string pathOfID(Textures::ID id){
case Textures::PlayerHit:
str = "res/player_hit.png";
break;
case Textures::Sword_0:
str = "res/sword_0.png";
break;
case Textures::Sword_1:
str = "res/sword_1.png";
break;
case Textures::Sword_2:
str = "res/sword_2.png";
break;
case Textures::Sword_3:
str = "res/sword_3.png";
break;
case Textures::Sword_4:
str = "res/sword_4.png";
break;
case Textures::Sword_5:
str = "res/sword_5.png";
break;
default :
throw std::logic_error("TextureHolder::pathOfID, Non-exhaustive pattern-matching");
}
......
......@@ -21,8 +21,12 @@ void World::loadTextures(){
mTextures.load(Textures::Tower, pathOfID(Textures::Tower));
mTextures.load(Textures::Player, pathOfID(Textures::Player));
mTextures.load(Textures::PlayerHit, pathOfID(Textures::PlayerHit));
mTextures.get(Textures::Floor).setRepeated(true);
mTextures.load(Textures::Sword_0, pathOfID(Textures::Sword_0));
mTextures.load(Textures::Sword_1, pathOfID(Textures::Sword_1));
mTextures.load(Textures::Sword_2, pathOfID(Textures::Sword_2));
mTextures.load(Textures::Sword_3, pathOfID(Textures::Sword_3));
mTextures.load(Textures::Sword_4, pathOfID(Textures::Sword_4));
mTextures.load(Textures::Sword_5, pathOfID(Textures::Sword_5));
}
void World::buildScene(){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment