-
Notifications
You must be signed in to change notification settings - Fork 0
/
astroidinator.ino
185 lines (143 loc) · 3.54 KB
/
astroidinator.ino
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
/**
* A template for using serial stuff with arduino
*
* @date 2019-02-11
* @author duncte123
* @version 1
*/
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <SimpleTimer.h>
#define SERIAL_RATE 115200
#define ENABLE_DEBUG true
#define JOY_MOVE A0
#define JOY_KEY 8
LiquidCrystal_I2C astroidinatorLcdDSte(0x3F, 20, 4); // 0x27 or 0x3F
SimpleTimer timerDSte;
// Globals
typedef struct {
int xPos = 0;
int yPos = 1;
String type = "}";
} PlayerDSte;
typedef struct {
int xPos = random(20, 80);
int yPos = random(0, 3);
String type;
} UFODSte;
UFODSte asteroidsDSte[10];
UFODSte spaceShipsDSte[10];
PlayerDSte playerDSte;
void printLn(const String& a_inputString) {
#if ENABLE_DEBUG
Serial.println(a_inputString);
#endif
}
void writeToLcdDSte(int a_x, int a_y, String a_text, bool a_clear) {
if (a_clear) {
astroidinatorLcdDSte.clear();
}
if (a_x > 20 || a_y > 4 || a_x < 0 || a_y < 0) {
return;
}
astroidinatorLcdDSte.setCursor(a_x, a_y);
int m_length = a_text.length();
for (int m_index = 0; m_index < m_length; m_index++) {
astroidinatorLcdDSte.print(a_text[m_index]);
}
}
void writeToLcdDSte(int a_x, int a_y, const String &a_text) {
writeToLcdDSte(a_x, a_y, a_text, false);
}
void initSerialDSte() {
#if ENABLE_DEBUG
Serial.begin(SERIAL_RATE);
printLn("READY: Serial initialized");
#endif
}
void showCreatorDSte() {
writeToLcdDSte(0, 0, "Created by:");
writeToLcdDSte(0, 1, "duncte123");
delay(2000);
}
void clearLcdDSte() {
writeToLcdDSte(0, 0, "", true);
}
void initPinsDSte() {
pinMode(JOY_KEY, INPUT_PULLUP);
}
void createObjectsDSte() {
for (auto &asteroid : asteroidsDSte) {
asteroid.type = "a";
}
for (auto &spaceShip : spaceShipsDSte) {
spaceShip.type = "s";
}
}
bool collisionDetectedDSte(const PlayerDSte &player, const UFODSte &item) {
if (player.xPos == item.xPos && player.yPos == item.yPos) {
printLn("Collision with " + item.type);
return true;
}
return false;
}
void checkControllerDSte() {
int joyDSte = analogRead(JOY_MOVE);
int mappedDSte = map(joyDSte, 0, 1023, 0, 255);
if (mappedDSte > 150) {
playerDSte.yPos++;
} else if (mappedDSte < 100) {
playerDSte.yPos--;
}
if (playerDSte.yPos > 3) {
playerDSte.yPos = 3;
}
if (playerDSte.yPos < 0) {
playerDSte.yPos = 0;
}
}
void moveObjectsDSte() {
clearLcdDSte();
for (auto &asteroid : asteroidsDSte) {
asteroid.xPos--;
writeToLcdDSte(asteroid.xPos, asteroid.yPos, asteroid.type);
if (collisionDetectedDSte(playerDSte, asteroid)) {
//
}
}
for (auto &spaceShip : spaceShipsDSte) {
spaceShip.xPos--;
writeToLcdDSte(spaceShip.xPos, spaceShip.yPos, spaceShip.type);
if (collisionDetectedDSte(playerDSte, spaceShip)) {
//
}
}
writeToLcdDSte(playerDSte.xPos, playerDSte.yPos, playerDSte.type);
}
void updateScoresDSte() {
//
}
void tickDSte() {
checkControllerDSte();
moveObjectsDSte();
}
void initTimerDSte() {
timerDSte.setInterval(250, tickDSte);
}
void setup() {
astroidinatorLcdDSte.init();
astroidinatorLcdDSte.setBacklight(1);
initPinsDSte();
initSerialDSte();
showCreatorDSte();
clearLcdDSte();
createObjectsDSte();
moveObjectsDSte();
initTimerDSte();
}
void loop() {
// int joy = analogRead(JOY_MOVE);
// int key = digitalRead(JOY_KEY);
// checkController();
timerDSte.run();
}