-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEnemy.cpp
More file actions
198 lines (155 loc) · 5.3 KB
/
Enemy.cpp
File metadata and controls
198 lines (155 loc) · 5.3 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "Utility/package.hpp"
#include "package.h"
//---------------------------------------------------------------------------------
// Written by Terence J. Grant - tjgrant [at] tatewake [dot] com
// Find the full tutorial at: http://gamedev.tutsplus.com/series/
//----------------------------------------------------------------------------------
void Enemy::AddBehaviour(Behavior b)
{
mBehaviors.push_back(b);
}
void Enemy::ApplyBehaviours()
{
std::list<Behavior>::iterator iter, iterNext;
iter = mBehaviors.begin();
iterNext = iter;
while (iter != mBehaviors.end())
{
iterNext++;
bool result = false;
switch (*iter)
{
case kFollow: result = followPlayer(0.9f); break;
case kMoveRandom: result = moveRandomly(); break;
}
if (!result)
{
mBehaviors.erase(iter);
}
iter = iterNext;
}
}
Enemy::Enemy(tTexture* image, const tVector2f& position)
: mPointValue(1),
mTimeUntilStart(60)
{
mImage = image;
mPosition = position;
mRadius = image->getSurfaceSize().width / 2.0f;
mColor = tColor4f(0,0,0,0);
mKind = kEnemy;
}
void Enemy::update()
{
if (mTimeUntilStart <= 0)
{
ApplyBehaviours();
}
else
{
mTimeUntilStart--;
mColor = tColor4f(1,1,1,1) * (1.0f - (float)mTimeUntilStart / 60.0f);
}
mPosition += mVelocity;
mPosition = tVector2f(tMath::clamp(mPosition.x, getSize().width / 2.0f, GameRoot::getInstance()->getViewportSize().width - getSize().width / 2.0f),
tMath::clamp(mPosition.y, getSize().height / 2.0f, GameRoot::getInstance()->getViewportSize().height - getSize().height / 2.0f));
mVelocity *= 0.8f;
}
void Enemy::draw(tSpriteBatch* spriteBatch)
{
if (mTimeUntilStart > 0)
{
float factor = mTimeUntilStart / 60.0f;
spriteBatch->draw(1, mImage, tPoint2f((int32_t)mPosition.x, (int32_t)mPosition.y), tOptional<tRectf>(), tColor4f(factor),
mOrientation, getSize() / 2.0f, tVector2f(2.0f - factor));
}
Entity::draw(spriteBatch);
}
bool Enemy::getIsActive()
{
return mTimeUntilStart <= 0;
}
int Enemy::getPointValue()
{
return mPointValue;
}
Enemy* Enemy::createSeeker(const tVector2f& position)
{
Enemy* enemy = new Enemy(Art::getInstance()->getSeeker(), position);
enemy->AddBehaviour(kFollow);
enemy->mPointValue = 2;
return enemy;
}
Enemy* Enemy::createWanderer(const tVector2f& position)
{
Enemy* enemy = new Enemy(Art::getInstance()->getWanderer(), position);
enemy->mRandomDirection = tMath::random() * tMath::PI * 2.0f;
enemy->mRandomState = 0;
enemy->AddBehaviour(kMoveRandom);
return enemy;
}
void Enemy::handleCollision(Enemy* other)
{
tVector2f d = mPosition - other->mPosition;
mVelocity += 10.0f * d / (d.lengthSquared() + 1.0f);
}
void Enemy::wasShot()
{
mIsExpired = true;
PlayerStatus::getInstance()->addPoints(mPointValue);
PlayerStatus::getInstance()->increaseMultiplier();
//TODO: ShapeBlaster fmod
float hue1 = Extensions::nextFloat(0, 6);
float hue2 = fmodf(hue1 + Extensions::nextFloat(0, 2), 6.0f);
tColor4f color1 = ColorUtil::HSVToColor(hue1, 0.5f, 1);
tColor4f color2 = ColorUtil::HSVToColor(hue2, 0.5f, 1);
for (int i = 0; i < 120; i++)
{
float speed = 18.0f * (1.0f - 1 / Extensions::nextFloat(1, 10));
ParticleState state(Extensions::nextVector2(speed, speed), ParticleState::kEnemy, 1);
tColor4f color = Extensions::colorLerp(color1, color2, Extensions::nextFloat(0, 1));
GameRoot::getInstance()->getParticleManager()->createParticle(Art::getInstance()->getLineParticle(), mPosition, color, 190, 1.5f, state);
}
// TODO: RR: Implement this
/*tSound* temp = Sound::getInstance()->getExplosion();
if (!temp->isPlaying())
{
temp->play(0, 1);
}*/
}
bool Enemy::followPlayer(float acceleration)
{
if (!PlayerShip::getInstance()->getIsDead())
{
tVector2f temp = (PlayerShip::getInstance()->getPosition() - mPosition);
temp = temp * (acceleration / temp.length());
mVelocity += temp;
}
if (mVelocity != tVector2f(0,0))
{
mOrientation = atan2f(mVelocity.y, mVelocity.x);
}
return true;
}
bool Enemy::moveRandomly()
{
if (mRandomState == 0)
{
mRandomDirection += tMath::random() * 0.2f - 0.1f;
}
mVelocity += 0.4f * tVector2f(cosf(mRandomDirection), sinf(mRandomDirection));
mOrientation -= 0.05f;
tRectf bounds = tRectf(0,0, GameRoot::getInstance()->getViewportSize());
bounds.location.x -= -mImage->getSurfaceSize().width / 2.0f - 1.0f;
bounds.location.y -= -mImage->getSurfaceSize().height / 2.0f - 1.0f;
bounds.size.width += 2.0f * (-mImage->getSurfaceSize().width / 2.0f - 1.0f);
bounds.size.height += 2.0f * (-mImage->getSurfaceSize().height / 2.0f - 1.0f);
if (!bounds.contains(tPoint2f((int32_t)mPosition.x, (int32_t)mPosition.y)))
{
tVector2f temp = tVector2f(GameRoot::getInstance()->getViewportSize().x, GameRoot::getInstance()->getViewportSize().y) / 2.0f;
temp -= mPosition;
mRandomDirection = atan2f(temp.y, temp.x) + tMath::random() * tMath::PI - tMath::PI / 2.0f;
}
mRandomState = (mRandomState + 1) % 6;
return true;
}