alive.cpp 2.81 KB
Newer Older
pa's avatar
pa committed
1
#include "../headers/alive.hpp"
2
#include "../headers/utilitary.hpp"
pa's avatar
pa committed
3

pa's avatar
pa committed
4
Alive::Alive(Agent::Type type, TextureHolder const& textures, int HP, Entity::Side side)
pa's avatar
pa committed
5
  :Entity(type,textures), mHP(HP), mState(Normal), mSide(side), mPoisoned(false){}
pa's avatar
pa committed
6

pa's avatar
pa committed
7 8

void Alive::updateCurrent(sf::Time dt){
pa's avatar
pa committed
9 10 11 12 13
  if(mPoisoned) mPoisonTime -= dt;
  if(mPoisoned && mPoisonTime.asSeconds() <= 0.f){
    hit();
    mPoisonTime = sf::seconds(1.f);
  }
pa's avatar
pa committed
14 15
  if(!isLocked()){
    act(dt);
16 17 18
    hitAnimation(dt);
    setType(typeOfState());
    Entity::updateCurrent(dt);
19 20
    setVelocity(0.f,0.f);
  }
pa's avatar
pa committed
21
  else{
22
    act(dt);
pa's avatar
pa committed
23 24 25
    mLockTime -= dt;
    if(mLockTime.asSeconds() <= 0.f)
      unlock();
26 27 28
    hitAnimation(dt);
    setType(typeOfState());
    Entity::updateCurrent(dt);
29
    setVelocity(0.f,0.f);
pa's avatar
pa committed
30 31 32
  }
}

33
void Alive::act(sf::Time dt){}
pa's avatar
pa committed
34 35 36 37 38 39 40 41 42 43 44

void Alive::lock(sf::Time dt){
  mLock = true;
  mLockTime = dt;
}

void Alive::unlock(){
  mLock = false;
}

Agent::Type Alive::typeOfState(){
45
  return Agent::Player;
pa's avatar
pa committed
46 47
}

pa's avatar
pa committed
48
void Alive::hit(bool poisonous, int dmg){
pa's avatar
pa committed
49 50
  if(poisonous && !mPoisoned) mPoisonTime = sf::seconds(1.f);
  mPoisoned = mPoisoned || poisonous;
51
  if(!mLock){
pa's avatar
pa committed
52
    mHP -= dmg;
53 54
    lock(sf::milliseconds(900.f));
    if(mHP <= 0){loot(); stageForRemoval();}
55
    mState = HitA;
56
    mHitCount = 6;
57 58
    mHitTime = sf::milliseconds(150.f);
  }
pa's avatar
pa committed
59 60 61 62
}

void Alive::hitAnimation(sf::Time dt){
  switch(mState){
63
  case Normal : break;
pa's avatar
pa committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

  case HitA : case HitB :
    mHitTime -= dt;
    if(mHitTime.asSeconds() <= 0.f){
      mHitCount--;
      mHitTime = sf::milliseconds(100.f);
      if(mHitCount == 0) mState = Normal;
      else{
        if(mState == HitA) mState = HitB;
        else mState = HitA;
      }
    }
    break;
  }
}

void Alive::moveUp(){
81
  if(getState() == Normal) getVelocity() += sf::Vector2f(0.f,-400.f);
pa's avatar
pa committed
82 83
}
void Alive::moveDown(){
84
  if(getState() == Normal) getVelocity() += sf::Vector2f(0.f,400.f);
pa's avatar
pa committed
85 86
}
void Alive::moveLeft(){
87
  if(getState() == Normal) getVelocity() += sf::Vector2f(-400.f,0.f);
pa's avatar
pa committed
88 89
}
void Alive::moveRight(){
90
  if(getState() == Normal) getVelocity() += sf::Vector2f(400.f,0.f);
pa's avatar
pa committed
91
}
pa's avatar
pa committed
92 93

void Alive::onCollideWith(SceneNode& collider){
pa's avatar
pa committed
94
  switch(collider.identify()){
pa's avatar
pa committed
95 96
    case Category::Orbital : case Category::Sword :
      if(static_cast<Damageable&>(collider).getSide() != mSide)
pa's avatar
pa committed
97 98
        hit(static_cast<Damageable&>(collider).isPoisonous(),
            static_cast<Damageable&>(collider).getDMG());
99 100 101 102
    break;
    case Category::Ennemy : case Category::Player :
      hit(false);
    break;
pa's avatar
pa committed
103
  }
pa's avatar
pa committed
104
}
105 106 107 108

void Alive::loot(){
  float rf = random_float();
  if(rf <= 0.1) drop(Collectible::Key);
pa's avatar
pa committed
109 110
  else if(rf <= 0.4) drop(Collectible::Dagger);
  else if(rf <= 0.7) drop(Collectible::Poison);
111 112 113 114 115 116 117 118
  else drop(Collectible::Coin);
}

void Alive::drop(Collectible::Type type){
  Collectible *coll = new Collectible(getTexHolder(),type);
  getParent()->attachChild(coll);
  coll->move(getWorldPosition());
}