-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLogic.cpp
More file actions
208 lines (161 loc) · 4.81 KB
/
GameLogic.cpp
File metadata and controls
208 lines (161 loc) · 4.81 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <cstdint>
#include <stdio.h>
#include <stdint.h>
#include "Tank.h"
#include "Object.h"
#include "GameLogic.h"
#define TERRAINOFFSET 5
//THIS SHOULD BE CHANGED IF WE ARE CHANGING IMAGE OR MAKING MAP PROGRAMATICALLY
void updateTerrainHeights(int16_t terrainHeights[], int length, const uint16_t *image){
const uint8_t terrainWidth = 160;
const uint8_t terrainImageHeight = 128;
// //iterate and populate, after its done, fill in the rest with same as before
// for(int i = 0; i < terrainWidth; i++){
// for(int j = 0; j < terrainImageHeight; j++){
// if(image[terrainWidth * j + i] == 0){
// while(image[terrainWidth * j + i] == 0){j++;}
// terrainHeights[i] = terrainWidth - j;
// break;
// }
// }
// }
// //Filling in the rest of the terrainheights array, assinging all elements to the last value (we repeated the colums)
// for(int i = 127; i < length; i++){
// terrainHeights[i] = terrainHeights[terrainWidth - 1];
// }
//iterate and populate, after its done, fill in the rest with same as before
for(int i = 0; i < terrainWidth; i++){
for(int j = 0; j < terrainImageHeight; j++){
if(image[terrainWidth * j + i] == 0){
terrainHeights[i] = terrainImageHeight - j;
terrainHeights[i] = terrainHeights[i] + TERRAINOFFSET;
}
}
}
}
void updateSlopes(int16_t terrainHeights[], int length, int16_t slopes[], int lengthSlope, bool playerOne){
//Slopes are 1 to 4, have an array that is 1 to 5 with indexes
if(length != lengthSlope){
while(1){}
}
//Filling with initial slopes
for(int i = 0; i < length - 3; i++){
int sum = (int16_t)terrainHeights[i] - (int16_t)terrainHeights[i + 1];
// slopes[i] = sum >> 2; //sum / 4
slopes[i] = (int16_t)sum;
}
for(int j = 0; j < 3; j++){
slopes[lengthSlope - 1 - j] = slopes[lengthSlope - 4];
}
//Done with filling with initial slopes
//Cascading the slopes
for(int i = 0; i < lengthSlope; i++){
if(slopes[i] != 0 && i < lengthSlope - 15){
int j = i;
while(j < lengthSlope - 1){
if(j >= i + 3 || slopes[j + 1] != 0){
break;
}
slopes[j + 1] = slopes[j];
}
}
}
// //Offsetting
// for(int i = 0; i < length; i++){
// int sum = terrainHeights[i] - terrainHeights[i + 1];
// if(i >= 12){
// slopes[i - 12] = sum; // Shift slope to align with tank center
// }
// }
// //Creating the offsets in the slopes
// if(playerOne){
// for(int i = 24; i < lengthSlope; i++){
// if(slopes[i] < 0){
// slopes[i - 24] = slopes[i];
// }
// }
// }
//else{
// for(int i = 0; i < lengthSlope - 24; i++){
// if(slopes[i] < 0){
// slopes[i + 24] = slopes[i];
// }
// }
// }
}
void offsetHeights(int16_t terrainHeights[], int length, int16_t slopes[], int lengthSlope, bool playerOne){
for(int i = 0; i < length; i++){
if(slopes[i] > 0){
terrainHeights[i] -= 15;
}
}
}
// class Object {
// public:
// Object(int xPos, int yPos, const uint16_t *imageData);
// protected:
// int x;
// int y;
// const uint16_t *image;
// };
// Constructor
Object::Object(int xPos, int yPos, const uint16_t *imageData) {
x = xPos;
y = yPos;
image = imageData;
}
// Method definitions
int Object::getX() {
return x;
}
int Object::getY() {
return y;
}
const uint16_t*Object::getImage(){
return image;
}
void Object::setX(int newX) {
if(newX >= 0 && newX < 160){
x = newX;
}
}
void Object::setY(int newY) {
if(newY >= 0 && newY < 128){
y = newY;
}
}
void Object::setImage(const uint16_t *newImage) {
image = newImage; // just repointing, not modifying
}
// class Tank : public Object {
// public:
// Tank(int xPos, int yPos, const uint16_t *imageData, int hp, int spd, int fire);
// private:
// int health;
// int speed;
// int firePower;
// };
// Tank constructor (uses base class constructor too)
Tank::Tank(int xPos, int yPos, const uint16_t *imageData, int hp, int spd, int fire)
: Object(xPos, yPos, imageData), health(hp), speed(spd), firePower(fire) {
}
// Tank method implementations
int Tank::getHealth() {
return health;
}
int Tank::getSpeed() {
return speed;
}
int Tank::getFirePower() {
return firePower;
}
uint8_t Tank::getAngleDeg() const {
return angleDeg;
}
void Tank::setAngleDeg(uint8_t angle) {
angleDeg = angle;
}
void Tank::takeDamage(int dmg) {
health -= dmg;
if (health < 0) health = 0;
}