-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.cpp
186 lines (160 loc) · 5.2 KB
/
utils.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
183
184
185
186
#include "Wire.h"
#include "Lib/images.h"
#include "utils.h"
#include <TFT_eSPI.h> // Graphics and font library
/**********************⚡ GLOBAL Vars *******************************/
TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
extern sButton btnMove;
extern sButton btnSelect;
/*****************⚡ PRINT FUNCTIONS *********************/
#define lineChars 12 //Numero de caracteres por linea
void printTalkText(String txt)
{
int x=0;
tft.setCursor(23,25);
tft.setTextFont(2);
//tft.setFreeFont(FMB9);
tft.setTextColor(TFT_BLACK);
uint8_t line=0;
while(true){
delay(100);
tft.print(txt[x]);x++;
if(x%lineChars == 0){
line++;
tft.setCursor(23,25 + (line * 15));
}
if(x == txt.length()) break;
}
}
//Stops process until a key is pressed
void WaitAnyKey(){
while(true){
btnMove.check();
btnSelect.check();
if(btnMove.click()||btnSelect.click()) break;
}
}
/******************* BUTTON DETECTION ******************/
//sButton::sButton(byte bPin){ pin = bPin; pinMode(pin, INPUT); } //Constructor
void sButton::init(void){ pinMode(pin, INPUT); } // Init pushbutton pin
int sButton::click(void){ return clickState; }
void sButton::forceClick(void){ clickState = ForcedClick;} //Generates one click loop
void sButton::check(void)
{
const unsigned long ButTimeout = 250;
const unsigned long ButLongClick = 5000;
unsigned long msec = millis();
byte but = digitalRead (pin);
if (antState != but) {
antState = but;
delay (10); // **** debounce
if (LOW == but) { // press
if (msecLst) { // 2nd press
msecLst = 0;
clickState = DoubleClick;
Serial.println("DoubleClick --> ");
return;
}
else
msecLst = 0 == msec ? 1 : msec;
}
}
int elapsed = msec - msecLst;
if (msecLst && (elapsed > ButTimeout) && (elapsed < ButLongClick)) {
if(but != LOW) {
msecLst = 0;
clickState = SingleClick;
Serial.println("SingleClick --> ");
return;
}
}
//LongClick
if(msecLst && (but == antState) && (but == LOW)) {
if(elapsed > ButLongClick) {
msecLst = 0;
clickState = LongClick;
Serial.println("LongClick --> ");
return;
}
}
//ForcedClick
if(clickState == ForcedClick){
clickState = SingleClick;
return;
}
clickState = None;
}
/******************* ICON FUNCTIONS ******************/
sObject::sObject(uint8_t _xPos, uint8_t _yPos, uint8_t _iconType, bool randomX)// Constructor
{
xPos = _xPos; yPos = _yPos; iconType = _iconType;
if(randomX) xPos = random(0,7)*COLUMN_WIDHT;
isEnabled = true;
}
bool sObject::move(void)
{
if(!isEnabled) return false;
if(iconType != Bolt){
yPos=yPos+MOVEMENT_GAP;
if(yPos>FLOOR_POS) { isEnabled=false; return true; }
}
else{
yPos=yPos-MOVEMENT_GAP;
if(yPos<=CEELLING_POS) isEnabled=false;
}
return false;
}
// ------------- ARRAY of OBJECTS -----------/
int sGameObjects::add(sObject newObject){
//initialize objects all disabled
for(int x=0; x<MAX_OBJECTS; x++)
if(object[x].isEnabled == false){
object[x].isEnabled = true;
object[x].xPos = newObject.xPos;
object[x].yPos = newObject.yPos;
object[x].iconType = newObject.iconType;
return x;
}
return -1;
}
void sGameObjects::newObject(uint8_t iconType){
sObject obj(0,0,iconType,true);
add(obj);
}
TFT_eSprite objSprite = TFT_eSprite(&tft); // Invoke library sprite
void sGameObjects::pushToSprite(TFT_eSprite* background){
objSprite.createSprite(coinWidth,coinHeight); //Gem Sprite
for(int x=0; x<MAX_OBJECTS; x++)
if(object[x].isEnabled){
switch(object[x].iconType){
case BTCcoin: objSprite.pushImage(0, 0, coinWidth, coinHeight, coin); break;
case VioletGem: objSprite.pushImage(0, 0, gemaVioletaWidth, gemaVioletaHeight, gemaVioleta); break;
case GreenGem:
default: objSprite.pushImage(0, 0, gemaVerdeWidth, gemaVerdeHeight, gemaVerde); break;
}
objSprite.pushToSprite(background, object[x].xPos, object[x].yPos, TFT_BLACK);
}
}
bool sGameObjects::moveAndCheckColisions(int avatarPos, uint8_t &lives, uint16_t &points){
bool existColision = false;
for(int x=0; x<MAX_OBJECTS; x++)
if(object[x].isEnabled){
if(object[x].move()){ //If true means a gem reaches floor +1 point
points=points+1;
existColision = true;
}
if((object[x].yPos > (FLOOR_POS - avatarHeight)) && (object[x].yPos < FLOOR_POS) &&
//(object[x].xPos >= avatarPos) && (object[x].xPos <= (avatarPos + avatarWidth))){
(object[x].xPos == avatarPos)){
//Colision detected
existColision = true;
object[x].isEnabled = false; //Disappear object
switch(object[x].iconType){
case BTCcoin: points=points+10; break;
case VioletGem:
case GreenGem: if(lives>0) lives=lives -1;
}
}
}
return existColision;
}