-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameLogic.c
190 lines (155 loc) · 5.05 KB
/
gameLogic.c
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
/*
* gameLogic.c
*
* Created on: 26.06.2017
* Author: Student225988
*/
#include "gameLogic.h"
#include "avr/io.h"
#include <stdlib.h>
void gameStart(GameState* gs){
gs->barrelsSize = 2;
int i;
for(i=0;i<gs->barrelsSize;i++){
gs->barrels[i].barrelPosition = -30;
gs->barrels[i].barrelDirection = 0;
gs->barrels[i].anBarrelFrame = 0;
gs->barrels[i].inGame = 0;
}
gs->playerInAirFlag = 0; // on ground
gs->playerPosition = 7; // left side of range in the middle 7-8
gs->playerJumptimeout = 0;
gs->gameOverFlag = 0;
gs->playerHealth = 3;
gs->anGlobalCounter = 0;
gs->anBarrelFreq = 2;
srand(1234);
}
void gameInitGraphics(GameState* gs, GraphicsEngine* gEngine){
gs->doPlayerStanding = 0;
Asset assPlayerStanding;
assPlayerStanding.pixelLines[0] = 0b00000;
assPlayerStanding.pixelLines[1] = 0b00100;
assPlayerStanding.pixelLines[2] = 0b00000;
assPlayerStanding.pixelLines[3] = 0b11111;
assPlayerStanding.pixelLines[4] = 0b11011;
assPlayerStanding.pixelLines[5] = 0b10101;
assPlayerStanding.pixelLines[6] = 0b01010;
assPlayerStanding.pixelLines[7] = 0b01010;
loadAsset(gEngine, assPlayerStanding, gs->doPlayerStanding);
gs->doPlayerJumping = 1;
Asset assPlayerJumping;
assPlayerJumping.pixelLines[0] = 0b00100;
assPlayerJumping.pixelLines[1] = 0b00000;
assPlayerJumping.pixelLines[2] = 0b11111;
assPlayerJumping.pixelLines[3] = 0b11011;
assPlayerJumping.pixelLines[4] = 0b10101;
assPlayerJumping.pixelLines[5] = 0b01010;
assPlayerJumping.pixelLines[6] = 0b00000;
assPlayerJumping.pixelLines[7] = 0b00000;
loadAsset(gEngine, assPlayerJumping, gs->doPlayerJumping);
gs->doBarrelAnim0 = 2;
Asset assBarrelAnim0;
assBarrelAnim0.pixelLines[0] = 0b00000;
assBarrelAnim0.pixelLines[1] = 0b00000;
assBarrelAnim0.pixelLines[2] = 0b00000;
assBarrelAnim0.pixelLines[3] = 0b00000;
assBarrelAnim0.pixelLines[4] = 0b00110;
assBarrelAnim0.pixelLines[5] = 0b01111;
assBarrelAnim0.pixelLines[6] = 0b01111;
assBarrelAnim0.pixelLines[7] = 0b00110;
loadAsset(gEngine, assBarrelAnim0, gs->doBarrelAnim0);
gs->doBarrelAnim1 = 3;
Asset doBarrelAnim1;
doBarrelAnim1.pixelLines[0] = 0b00000;
doBarrelAnim1.pixelLines[1] = 0b00000;
doBarrelAnim1.pixelLines[2] = 0b00000;
doBarrelAnim1.pixelLines[3] = 0b00110;
doBarrelAnim1.pixelLines[4] = 0b01111;
doBarrelAnim1.pixelLines[5] = 0b01111;
doBarrelAnim1.pixelLines[6] = 0b00110;
doBarrelAnim1.pixelLines[7] = 0b00000;
loadAsset(gEngine, doBarrelAnim1, gs->doBarrelAnim1);
}
void gameLoopIteration(GameState* gs){
// game over logic
if(gs->gameOverFlag) return;
// player movement (jump logic)
if(gs->playerJumptimeout > 0){
gs->playerJumptimeout--;
if(gs->playerJumptimeout == 0){
gs->playerInAirFlag = 0;
}
}
// barrels routines
int i;
for(i=0;i<gs->barrelsSize && gs->barrels[i].inGame;i++){
if(gs->barrels[i].inGame == 0) continue;
// barrel movement
gs->barrels[i].barrelPosition += gs->barrels[i].barrelDirection;
// player hit
if(gs->playerInAirFlag == 0 && gs->playerPosition == gs->barrels[i].barrelPosition){
gs->playerHealth--;
if(gs->playerHealth == 0){
gs->gameOverFlag = 1;
}
}
// out of map check
if(gs->barrels[i].barrelPosition < 0 || gs->barrels[i].barrelPosition > 15){
gs->barrels[i].inGame = 0;
}
}
// barrel spawning
for(i=0;i<gs->barrelsSize;i++){
if(gs->barrels[i].inGame == 1) continue;
if(rand() % 100 < BARREL_SPAWN_PROB){
gs->barrels[i].barrelDirection = rand() % 2 ? 1 : -1;
gs->barrels[i].barrelPosition = gs->barrels[i].barrelDirection == 1 ? 0 : 15;
gs->barrels[i].inGame = 1;
}
}
}
void onLeftButtonPress(GameState* gs){
if(gs->playerJumptimeout == 0){
gs->playerJumptimeout = JUMP_TIMEOUT;
gs->playerInAirFlag = 1;
}
}
void onRightButtonPress(GameState* gs){
gs->playerPosition = gs->playerPosition == 7 ? 8 : 7;
}
void onRedraw(GraphicsEngine* gEngine, GameState* gs){
if(gs->gameOverFlag){
char* gameOverString = "Game over";
char* gameOverString1 = "You noob...";
prepareDrawString(gEngine, gameOverString, 9, 0,0);
prepareDrawString(gEngine, gameOverString1, 11, 3,1);
} else{
gs->anGlobalCounter++;
// draw player
if(gs->playerInAirFlag == 0)
prepareDraw(gEngine,gs->doPlayerStanding,gs->playerPosition,1);
else
prepareDraw(gEngine,gs->doPlayerJumping,gs->playerPosition,0);
// draw barrel
int i;
for(i=0;i<gs->barrelsSize && gs->barrels[i].inGame;i++){
if(gs->anGlobalCounter % gs->anBarrelFreq == 0){
gs->barrels[i].anBarrelFrame = (gs->barrels[i].anBarrelFrame ? 0 : 1);
}
if(gs->barrels[i].anBarrelFrame == 0){
prepareDraw(gEngine,gs->doBarrelAnim0,gs->barrels[i].barrelPosition,1);
} else{
prepareDraw(gEngine,gs->doBarrelAnim1,gs->barrels[i].barrelPosition,1);
}
}
}
executeDraw(gEngine);
// draw health
int healthIterator = 0;
while(healthIterator < gs->playerHealth){
prepareLEDDraw(gEngine, healthIterator);
healthIterator++;
}
executeLEDDraw(gEngine);
}