/
Vampire.cpp
38 lines (33 loc) · 1.15 KB
/
Vampire.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* File: Vampire.cpp
* Author: iMer
*
* Created on 18. April 2015, 19:02
*/
#include "Vampire.hpp"
#include "Level.hpp"
#include "Slayer.hpp"
#include "Engine/util/Random.hpp"
#include <iostream>
Vampire::Vampire(engine::Scene* scene): Damagable(scene), m_targetPoint(0,0), m_targetTime(0) {
m_deathSound = engine::ResourceManager::instance()->MakeSound("assets/sounds/vampire_death.wav");
m_hurtSound = engine::ResourceManager::instance()->MakeSound("assets/sounds/vampire_hurt.wav");
}
Vampire::~Vampire() {
}
void Vampire::OnUpdate(sf::Time interval) {
Damagable::OnUpdate(interval);
Level* level = static_cast<Level*>(m_scene);
Slayer* slayer = level->GetSlayer();
m_targetTime-=interval.asSeconds();
engine::util::RandomFloat r(0, 1);
if (m_targetTime <= 0) {
m_targetTime = static_cast<float>(0.4f * (r()+1));
m_targetPoint = (slayer->GetGlobalPosition());
}
auto delta = m_targetPoint-GetGlobalPosition();
if (abs(delta.y) < 300 && abs(delta.x) < 500 && abs(delta.x) > 10) {
const float v = 2 * interval.asSeconds() ;
m_body->ApplyLinearImpulse(b2Vec2(v * (delta.x > 0?1:-1), r() < 0.005?-5:0), m_body->GetWorldCenter(), true);
}
}