-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlinkBehavior.cpp
68 lines (55 loc) · 1.03 KB
/
BlinkBehavior.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
#include "BlinkBehavior.h"
#include "Color.h"
#include "Pixel.h"
#include "Sprite.h"
#include "SpriteManager.h"
BlinkBehavior::BlinkBehavior(Behaves* subject, long duration, int repeat) : Behavior(subject, duration, repeat)
{
entity = dynamic_cast<Entity*>(subject);
// creates a copy of entity's sprite to make all pixels transparent
spriteOff = SpriteManager::getInstance().transparent(entity->sprite);
spriteOn = entity->sprite;
// startinf state is "on"
on();
}
BlinkBehavior::~BlinkBehavior()
{
}
void BlinkBehavior::execute(long delta)
{
if (state == State::On) {
if (timeOn < duration) {
timeOn += delta;
}
else {
off();
}
}
else {
if (timeOff < duration) {
timeOff += delta;
}
else {
on();
}
}
}
void BlinkBehavior::on()
{
entity->sprite = spriteOn;
state = State::On;
timeOn = 0;
if (finished()) {
notify();
}
}
void BlinkBehavior::off()
{
entity->sprite = spriteOff;
state = State::Off;
timeOff = 0;
}
bool BlinkBehavior::finished()
{
return (repeat > 0 && ++counter > repeat);
}