-
Notifications
You must be signed in to change notification settings - Fork 0
/
catris.cpp
182 lines (152 loc) · 6.17 KB
/
catris.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
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
#include "catris.h"
Catris::Catris(fontDataReader fontDataReader, spriteDataReader spriteDataReader):
_fontDataReader(fontDataReader), _spriteDataReader(spriteDataReader), _buffer(NULL) {
_rainbowTimer = new Timer(8000);
_scrollTimer = new Timer(60);
_animTimer = new Timer(0);
_scrollText = new ScrollText(0, 14, 10, fontDataReader, (uint8_t*) font4x5);
unsigned long delay = 150;
_happyAnimation = new Animation(12);
_happyAnimation->setFrame(0, delay, (uint8_t*) catrisHappy1LeftSprite);
_happyAnimation->setFrame(1, delay, (uint8_t*) catrisHappy2LeftSprite);
_happyAnimation->setFrame(2, delay, (uint8_t*) catrisHappy3LeftSprite);
_happyAnimation->setFrame(3, delay, (uint8_t*) catrisHappy1LeftSprite);
_happyAnimation->setFrame(4, delay, (uint8_t*) catrisHappy2LeftSprite);
_happyAnimation->setFrame(5, delay * 5, (uint8_t*) catrisHappy3LeftSprite);
_happyAnimation->setFrame(6, delay, (uint8_t*) catrisHappy3RightSprite);
_happyAnimation->setFrame(7, delay, (uint8_t*) catrisHappy2RightSprite);
_happyAnimation->setFrame(8, delay, (uint8_t*) catrisHappy1RightSprite);
_happyAnimation->setFrame(9, delay, (uint8_t*) catrisHappy3RightSprite);
_happyAnimation->setFrame(10, delay, (uint8_t*) catrisHappy2RightSprite);
_happyAnimation->setFrame(11, delay * 5, (uint8_t*) catrisHappy1RightSprite);
_shockedAnimation = new Animation(11);
_shockedAnimation->setFrame(0, 1000, (uint8_t*) catrisShocked1Sprite);
_shockedAnimation->setFrame(1, 50, (uint8_t*) catrisShocked2Sprite);
_shockedAnimation->setFrame(2, 100, (uint8_t*) catrisShocked3Sprite);
_shockedAnimation->setFrame(3, 50, (uint8_t*) catrisShocked2Sprite);
_shockedAnimation->setFrame(4, 2000, (uint8_t*) catrisShocked1Sprite);
_shockedAnimation->setFrame(5, 50, (uint8_t*) catrisShocked2Sprite);
_shockedAnimation->setFrame(6, 100, (uint8_t*) catrisShocked3Sprite);
_shockedAnimation->setFrame(7, 50, (uint8_t*) catrisShocked2Sprite);
_shockedAnimation->setFrame(8, 50, (uint8_t*) catrisShocked1Sprite);
_shockedAnimation->setFrame(9, 100, (uint8_t*) catrisShocked3Sprite);
_shockedAnimation->setFrame(10, 50, (uint8_t*) catrisShocked2Sprite);
_worriedAnimation = new Animation(11);
_worriedAnimation->setFrame(0, delay, (uint8_t*) catrisWorried1Sprite);
_worriedAnimation->setFrame(1, delay, (uint8_t*) catrisWorried2Sprite);
_worriedAnimation->setFrame(2, delay, (uint8_t*) catrisWorried3Sprite);
_worriedAnimation->setFrame(3, delay, (uint8_t*) catrisWorried4Sprite);
_worriedAnimation->setFrame(4, delay, (uint8_t*) catrisWorried3Sprite);
_worriedAnimation->setFrame(5, delay, (uint8_t*) catrisWorried2Sprite);
_worriedAnimation->setFrame(6, delay, (uint8_t*) catrisWorried1Sprite);
_worriedAnimation->setFrame(7, delay, (uint8_t*) catrisWorried4Sprite);
_worriedAnimation->setFrame(8, delay, (uint8_t*) catrisWorried1Sprite);
_worriedAnimation->setFrame(9, delay, (uint8_t*) catrisWorried2Sprite);
_worriedAnimation->setFrame(10, delay, (uint8_t*) catrisWorried3Sprite);
delay = 100;
_inLoveAnimation = new Animation(7);
_inLoveAnimation->setFrame(0, delay, (uint8_t*) catrisInLove1Sprite);
_inLoveAnimation->setFrame(1, delay, (uint8_t*) catrisInLove2Sprite);
_inLoveAnimation->setFrame(2, delay, (uint8_t*) catrisInLove3Sprite);
_inLoveAnimation->setFrame(3, delay, (uint8_t*) catrisInLove4Sprite);
_inLoveAnimation->setFrame(4, delay, (uint8_t*) catrisInLove3Sprite);
_inLoveAnimation->setFrame(5, delay, (uint8_t*) catrisInLove2Sprite);
_inLoveAnimation->setFrame(6, delay, (uint8_t*) catrisInLove1Sprite);
delay = 200;
_lowBatteryAnimation = new Animation(3);
_lowBatteryAnimation->setFrame(0, delay, (uint8_t*) lowBattery1Sprite);
_lowBatteryAnimation->setFrame(1, delay, (uint8_t*) lowBattery2Sprite);
_lowBatteryAnimation->setFrame(2, 2000, (uint8_t*) lowBattery3Sprite);
delay = 100;
_highScoreAnimation = new Animation(4);
_highScoreAnimation->setFrame(0, 2000, (uint8_t*) highScore1Sprite);
_highScoreAnimation->setFrame(1, delay, (uint8_t*) highScore2Sprite);
_highScoreAnimation->setFrame(2, delay, (uint8_t*) highScore3Sprite);
_highScoreAnimation->setFrame(3, delay, (uint8_t*) highScore2Sprite);
setAnimation(Anim::Happy);
}
Catris::~Catris() {
delete _rainbowTimer;
delete _scrollTimer;
delete _animTimer;
delete _scrollText;
delete _happyAnimation;
delete _shockedAnimation;
delete _worriedAnimation;
delete _inLoveAnimation;
delete _lowBatteryAnimation;
delete _highScoreAnimation;
}
Catris::Anim Catris::getAnimation() {
return _currentAnim;
}
void Catris::setAnimation(Anim animation) {
_currentAnim = animation;
switch (animation) {
case Anim::Happy:
_loadAnimation(_happyAnimation);
break;
case Anim::Shocked:
_loadAnimation(_shockedAnimation);
break;
case Anim::Worried:
_loadAnimation(_worriedAnimation);
break;
case Anim::InLove:
_loadAnimation(_inLoveAnimation);
break;
case Anim::LowBattery:
_loadAnimation(_lowBatteryAnimation);
break;
case Anim::HighScore:
_loadAnimation(_highScoreAnimation);
break;
}
}
void Catris::setText(const char* text) {
_scrollText->setText(text);
}
void Catris::setFormattedText(const char* format, ...) {
if (_buffer != NULL) {
free(_buffer);
}
va_list args;
va_start(args, format);
int len = vsnprintf(NULL, 0, format, args) + 1;
va_end(args);
_buffer = (char*) malloc(len);
va_start(args, format);
vsprintf(_buffer, format, args);
va_end(args);
setText(_buffer);
}
bool Catris::update() {
if (_scrollTimer->fire()) {
return _scrollText->scroll();
} else {
return false;
}
}
void Catris::draw(canvas canvas) {
_playAnimation(canvas);
uint8_t color[3];
hsv2rgb(360 * _rainbowTimer->progress(true), 1, 1, color);
_scrollText->draw(canvas, color);
}
void Catris::_loadAnimation(Animation* animation) {
_currentAnimation = animation;
_animFrame = 0;
_animTimer->reset(animation->getDuration(0));
}
void Catris::_playAnimation(canvas canvas) {
if (_animTimer->fire()) {
_animFrame = _animFrame == _currentAnimation->frameCount - 1 ? 0 : _animFrame + 1;
_animTimer->reset(_currentAnimation->getDuration(_animFrame));
}
for (uint8_t x = 0; x < 10; ++x) {
for (uint8_t y = 1; y < 14; ++y) {
canvas(x, y, 0, 0, 0);
}
}
drawSprite(canvas, spritePalette, _spriteDataReader, _currentAnimation->getSprite(_animFrame), 0, 1);
}