-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectile.cpp
91 lines (71 loc) · 1.69 KB
/
Projectile.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
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
86
87
88
89
#include "Projectile.hpp"
#include <iostream>
namespace
{
const std::vector<ProjectileData> Table = initializeProjectileData();
}
int Projectile::Count = 0;
Projectile::Projectile(Type type, const TextureHolder &textures)
: Entity(1)
, mType(type)
, mSprite(textures.get(Table[type].texture))
, mTargetDirection()
{
centerOrigin(mSprite);
if(getCategory() == Category::EnemyProjectile)
mSprite.rotate(180);
++Count;
}
Projectile::~Projectile()
{
--Count;
}
void Projectile::guideTowards(sf::Vector2f position)
{
assert(isGuided());
mTargetDirection = unitVector(position - getWorldPosition());
}
bool Projectile::isGuided() const
{
return mType == Missile;
}
// virtual
unsigned int Projectile::getCategory() const
{
if(mType == EnemyBullet)
return Category::EnemyProjectile;
else
return Category::AlliedProjectile;
}
// virtual
sf::FloatRect Projectile::getBoundingRect() const
{
return getWorldTransform().transformRect(mSprite.getGlobalBounds());
}
float Projectile::getMaxSpeed() const
{
return Table[mType].speed;
}
int Projectile::getDamage() const
{
return Table[mType].damage;
}
// virtual
void Projectile::updateCurrent(sf::Time dt, CommandQueue &commands)
{
if(isGuided())
{
const float approachRate = 200.f; // 转角速度
sf::Vector2f newVelocity = unitVector(approachRate * dt.asSeconds() * mTargetDirection + getVelocity());
newVelocity *= getMaxSpeed();
float angle = std::atan2(newVelocity.y, newVelocity.x);
setRotation(radianToDegree(angle) + 90.f);
setVelocity(newVelocity);
}
Entity::updateCurrent(dt, commands);
}
// virtual
void Projectile::drawCurrent(sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(mSprite, states);
}