Commit 68689404 authored by pa's avatar pa

Did the collision not bad

parent eff534b0
......@@ -33,6 +33,3 @@
- [ ] Different kinds of weapons
* Limit to 2 weapons
- [ ] Heals and consomable
......@@ -15,7 +15,7 @@ public :
virtual Agent::Type typeOfState();
public :
Alive(Agent::Type type, TextureHolder const& Textures, int HP);
Alive(Agent::Type type, TextureHolder const& Textures, int HP, Entity::Side side);
int getHP() const{return mHP;}
State getState() const{return mState;}
......@@ -37,7 +37,10 @@ public :
virtual Category::Category identify() const{return Category::Alive;}
virtual void onCollideWith(SceneNode& collider);
private :
Entity::Side mSide;
State mState;
int mHP;
......
......@@ -2,6 +2,6 @@
namespace Category{
enum Category{
SceneNode, SpriteNode, Entity, Agent, Alive, Player, Ennemy, Orbital, Sword, Door
SceneNode, SpriteNode, Entity, Agent, Alive, Player, Ennemy, Orbital, Sword, Door, Collectible
};
}
#pragma once
#include <SFML/Graphics.hpp>
#include "textureHolder.hpp"
#include "category.hpp"
#include "agent.hpp"
class Collectible : public Agent{
public :
enum Type {Sword, Poison, Health, Dagger};
static Agent::Type typeOf(Type type);
static Type typeIn(Agent::Type type);
public :
Collectible(TextureHolder const& tex, Type type);
Type getType(){return mType;}
virtual Category::Category identify() const{return Category::Collectible;}
virtual void onCollideWith(SceneNode& collider);
private :
Type mType;
};
......@@ -6,18 +6,21 @@
class Entity : public Agent{
public:
Entity(Agent::Type type, TextureHolder const& textures);
public :
enum Side{ None, Player, Ennemy };
void setVelocity(sf::Vector2f velocity);
void setVelocity(float x, float y);
public:
Entity(Agent::Type type, TextureHolder const& textures);
sf::Vector2f& getVelocity();
void setVelocity(sf::Vector2f velocity);
void setVelocity(float x, float y);
virtual void updateCurrent(sf::Time dt);
sf::Vector2f& getVelocity();
virtual Category::Category identify() const{return Category::Entity;}
virtual void updateCurrent(sf::Time dt);
private:
sf::Vector2f mVelocity;
virtual Category::Category identify() const{return Category::Entity;}
private:
sf::Vector2f mVelocity;
};
......@@ -5,18 +5,19 @@
#include "entity.hpp"
#include "speedPatern.hpp"
#include "category.hpp"
#include "alive.hpp"
class Orbital : public Entity{
public :
Orbital(Agent::Type type , TextureHolder const& texture, SpeedPatern* spP, bool ally);
Orbital(Agent::Type type , TextureHolder const& texture, SpeedPatern* spP, Entity::Side ally);
virtual void updateCurrent(sf::Time dt);
virtual Category::Category identify() const{return Category::Orbital;}
virtual bool getOwner() const{ return mAlly; }
virtual Entity::Side getSide() const{ return Entity::None; }
private :
bool mAlly;
Entity::Side mSide;
SpeedPatern* mSpeedPatern;
sf::Time mLifeSpan;
};
......@@ -26,6 +26,8 @@ public :
virtual Category::Category identify() const{ return Category::Player;}
virtual void onCollideWith(SceneNode& collider);
private :
bool mIsQAvailable;
sf::Time mQCD, mQElapse;
......
......@@ -8,6 +8,7 @@
#include "ennemy.hpp"
#include "player.hpp"
#include "door.hpp"
#include "collectible.hpp"
#include <fstream>
#include <sstream>
......
......@@ -39,6 +39,7 @@ public :
bool collidesWith(SceneNode& node) const;
void checkNodeCollision(SceneNode& node, std::set<Pair>& target);
void checkSceneCollision(SceneNode& root, std::set<Pair>& target);
virtual void onCollideWith(SceneNode& collider);
private :
std::vector<Ptr> mChildren;
......
#include "../headers/alive.hpp"
Alive::Alive(Agent::Type type, TextureHolder const& textures, int HP)
:Entity(type,textures), mHP(HP), mState(Normal){}
Alive::Alive(Agent::Type type, TextureHolder const& textures, int HP, Entity::Side side)
:Entity(type,textures), mHP(HP), mState(Normal), mSide(side){}
void Alive::updateCurrent(sf::Time dt){
......@@ -78,3 +78,10 @@ void Alive::moveLeft(){
void Alive::moveRight(){
getVelocity() += sf::Vector2f(400.f,0.f);
}
void Alive::onCollideWith(SceneNode& collider){
if(collider.identify() == Category::Orbital)
if(static_cast<Orbital&>(collider).getSide() != mSide){
hit();
}
}
#include "../headers/collectible.hpp"
Agent::Type Collectible::typeOf(Collectible::Type type){
switch(type){
default : return Agent::SwordFloor;
}
}
Collectible::Type Collectible::typeIn(Agent::Type type){
switch(type){
default : return Collectible::Sword;
}
}
Collectible::Collectible(TextureHolder const& tex, Collectible::Type type) : Agent(typeOf(type),tex), mType(type) {}
void Collectible::onCollideWith(SceneNode& collider){
switch(collider.identify()){
case Category::Player :
stageForRemoval();
break;
}
}
#include "../headers/ennemy.hpp"
Ennemy::Ennemy(TextureHolder const& textures) : Alive(Agent::Tower,textures,2), mIsQ(true), mIsW(true){
Ennemy::Ennemy(TextureHolder const& textures) : Alive(Agent::Tower,textures,2,Entity::Ennemy), mIsQ(true), mIsW(true){
mQCD = sf::seconds(4.f);
markForReset();
}
......@@ -22,7 +22,7 @@ void Ennemy::q_spell(float sense){
sf::Transform transform; transform.rotate(120.f);
for(int i = 0 ; i < 3 ; ++i){
Orbital* orb = new Orbital(Agent::tProjectile, getTexHolder(),
new HellSpeedPatern(positionSetter,sense*94.f,1.005f),false);
new HellSpeedPatern(positionSetter,sense*94.f,1.005f),Entity::Ennemy);
orb->setPosition(positionSetter);
attachChild(orb);
positionSetter = transform.transformPoint(positionSetter);
......
#include "../headers/orbital.hpp"
#include <iostream>
Orbital::Orbital(Agent::Type type, TextureHolder const& textures, SpeedPatern* spP, bool ally)
: Entity(type,textures), mSpeedPatern(spP), mLifeSpan(sf::seconds(15.f)), mAlly(ally){
Orbital::Orbital(Agent::Type type, TextureHolder const& textures, SpeedPatern* spP, Entity::Side ally)
: Entity(type,textures), mSpeedPatern(spP), mLifeSpan(sf::seconds(15.f)), mSide(ally){
}
void Orbital::updateCurrent(sf::Time dt){
......
......@@ -14,7 +14,7 @@ Agent::Type Player::typeOfState(){
}
Player::Player(TextureHolder const& textures)
: Alive(Agent::Player,textures,8), mIsQAvailable(true), mIsWAvailable(true), mW(false){
: Alive(Agent::Player,textures,8,Entity::Player), mIsQAvailable(true), mIsWAvailable(true), mW(false){
mQCD = sf::seconds(8.f);
mWCD = sf::seconds(1.f);
}
......@@ -27,7 +27,7 @@ void Player::q_spell(){
sf::Transform transform; transform.rotate(120.f);
for(int i = 0 ; i < 3 ; ++i){
Orbital* orb = new Orbital(Agent::qProjectile, getTexHolder(), new
RotationSpeedPatern(positionSetter,540.f), true);
RotationSpeedPatern(positionSetter,540.f), Entity::Player);
orb->setPosition(positionSetter);
attachChild(orb);
positionSetter = transform.transformPoint(positionSetter);
......@@ -75,3 +75,12 @@ void Player::keyBindings(){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) w_spell(1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) w_spell(0);
}
void Player::onCollideWith(SceneNode& collider){
switch(collider.identify()){
case Category::Collectible :
giveSword();
break;
}
Alive::onCollideWith(collider);
}
#include "../headers/room.hpp"
#include "../headers/door.hpp"
Room::Room(TextureHolder& textures, sf::IntRect& bounds)
......@@ -55,7 +54,7 @@ void Room::buildScene(std::string path){
break;
}
case Agent::SwordFloor :{
Agent* sword(new Agent(rType,mTextures));
Collectible* sword(new Collectible(mTextures,Collectible::typeIn(rType)));
sword->setPosition(alpha_x*x,alpha_y*y);
mSceneLayers[Air]->attachChild(sword);
break;
......
......@@ -11,7 +11,7 @@ SceneNode::~SceneNode(){
void SceneNode::reset(){
if(isMarkForReset){
//for(auto& child : mChildren) delete child;
for(auto& child : mChildren) delete child;
mChildren.clear();
}
else{
......@@ -106,3 +106,7 @@ void SceneNode::checkSceneCollision(SceneNode& root, std::set<Pair>& target){
for(auto &child : root.mChildren)
checkSceneCollision(*child,target);
}
void SceneNode::onCollideWith(SceneNode& collider){
return;
}
......@@ -36,6 +36,7 @@ void World::update(sf::Time dt){
mSceneGraph.update(dt);
mSceneGraph.removeWrecks(bounds);
checkCollision();
fillData();
}
void World::checkCollision(){
......@@ -43,25 +44,13 @@ void World::checkCollision(){
mSceneGraph.checkSceneCollision(mSceneGraph, stack);
for(auto &pair : stack){
if(pair.first->identify() == Category::Player && pair.second->identify() == Category::Orbital && !static_cast<Orbital&>(*pair.second).getOwner()){
static_cast<Player&>(*pair.first).hit();
pair.second->stageForRemoval();
fillData();
}
if(pair.first->identify() == Category::Agent && static_cast<Agent&>(*pair.first).getType() == Agent::SwordFloor && pair.second->identify() == Category::Player){
static_cast<Player&>(*pair.second).giveSword();
pair.first->stageForRemoval();
}
pair.first->onCollideWith(*pair.second);
pair.second->onCollideWith(*pair.first);
}
for(auto &pair : stack){
if(pair.first->identify() == Category::Ennemy && pair.second->identify() == Category::Sword){
static_cast<Player&>(*pair.first).hit();
}
if(pair.first->identify() == Category::Ennemy && pair.second->identify() == Category::Orbital && static_cast<Orbital&>(*pair.second).getOwner()){
static_cast<Player&>(*pair.first).hit();
}
if(pair.first->identify() == Category::Orbital && pair.second->identify() == Category::Orbital
&& static_cast<Orbital&>(*pair.first).getOwner() != static_cast<Orbital&>(*pair.second).getOwner()){
pair.first->stageForRemoval(); pair.second->stageForRemoval();
}
if(pair.first->identify() == Category::Player && pair.second->identify() == Category::Door){
mPlayer = static_cast<Player*>(mRoom->detachPlayer(*mPlayer));
Room* room = static_cast<Room*>(mSceneGraph.detachChild(*mRoom));
......@@ -73,5 +62,5 @@ void World::checkCollision(){
mRoom->attachPlayer(mPlayer);
mSceneGraph.attachChild(mRoom);
}
}
}
}
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