-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyObject.cpp
More file actions
85 lines (69 loc) · 2.24 KB
/
Copy pathEnemyObject.cpp
File metadata and controls
85 lines (69 loc) · 2.24 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "EnemyObject.h"
#include "Animation.h"
#include "GameEvent.h"
#include "GameObject.h"
#include "ProjectileObject.h"
#include "ProjectileFiredEvent.h"
#include "EventDispatcher.h"
#include "GameLevel.h"
#include <iostream>
EnemyObject::EnemyObject(Tmx::Object* description)
{
std::string name = description->GetType();
std::cout << "" << name << std::endl;
const Tmx::PropertySet& properties = description->GetProperties();
if( properties.HasProperty("anim_idle") )
{
registerAnimation("anim_idle", properties.GetLiteralProperty("anim_idle"));
}
else
{
std::cerr << "\t\t\tEnemy " << name << " lacks anim_idle" << std::endl;
}
m_Location.x = description->GetX();
m_Location.y = description->GetY();
m_XFlipped = false;
m_EnemyBehavior = [](float, GameLevel*){};
}
EnemyObject::~EnemyObject()
{}
void EnemyObject::update(float tick_ms, GameLevel* game_level)
{
getCurrentAnimation()->update(tick_ms);
m_EnemyBehavior(tick_ms, game_level);
// Just for demonstration, set up a behavior where the
// enemy fires a projectile at the player.
m_EnemyBehavior = [this](float tick_ms, GameLevel* level) {
sf::Clock& clock = this->m_Clock;
sf::Int32 delayBetweenBulletsInMs = 600;
if( clock.getElapsedTime().asMilliseconds() > delayBetweenBulletsInMs )
{
clock.restart();
}
else
{
return;
}
float firingDistance = 1000;
const sf::Vector2f& playerLocation = level->getPlayerLocation();
float xdiff = (this->getLocation().x - playerLocation.x);
float ydiff = (this->getLocation().y - playerLocation.y);
float dist = sqrt(xdiff*xdiff+ydiff*ydiff);
sf::Vector2f projectileHeading(xdiff/dist, ydiff/dist);
if(dist <= firingDistance)
{
ProjectileObject* projectile = new ProjectileObject( ProjectileType::PROJECTILE_BASIC, CollidesWith::PLAYERS, this );
projectile->init(getDispatcher(), m_GameLevel);
projectile->setLocation(getLocation().x, getLocation().y);
projectile->setVelocity( -projectileHeading.x * 300, -projectileHeading.y * 300);
ProjectileFiredEvent e(projectile);
this->getDispatcher()->dispatchEvent(&e);
}
};
}
void EnemyObject::init(EventDispatcher* dispatcher, GameLevel* game_level)
{
((GameObject*)(this))->init(dispatcher, game_level);
}
void EnemyObject::notify(GameEvent* event)
{}