-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectMovement.cpp
More file actions
166 lines (144 loc) · 6.02 KB
/
objectMovement.cpp
File metadata and controls
166 lines (144 loc) · 6.02 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
#include "declarations.h"
#include "prototype.h"
extern Application app;
/// This function allows the bitmaps to move
/// Need to pass the movable object and keyboard structs
/// No return
void spriteMovement(Character &cMovement, Keyboard &kMovement, Rocks rMovement[], PowerUp pMovement[]) {
// Take the dx and dy values and add them to the characters coordinates
if (cMovement.characterPositionX >= 0 && cMovement.characterPositionX <= 570) {
cMovement.characterPositionX += kMovement.xCoordinateMovement;
} else if(cMovement.characterPositionX <= 0 && kMovement.xCoordinateMovement >= 1) {
cMovement.characterPositionX += kMovement.xCoordinateMovement;
} else if (cMovement.characterPositionX >= 570 && kMovement.xCoordinateMovement <= -1) {
cMovement.characterPositionX += kMovement.xCoordinateMovement;
}
if (cMovement.characterPositionY >= 0 && cMovement.characterPositionY <= 705) {
cMovement.characterPositionY += kMovement.yCoordinateMovement;
} else if(cMovement.characterPositionY <= 0 && kMovement.yCoordinateMovement >= 1) {
cMovement.characterPositionY += kMovement.yCoordinateMovement;
} else if (cMovement.characterPositionY >= 705 && kMovement.yCoordinateMovement <= -1) {
cMovement.characterPositionY += kMovement.yCoordinateMovement;
}
// Movement of the rocks and power ups based on if the slow motion power up is enabled
if (!al_get_timer_started(app.slowmoTimer)) {
for (int i = 0; i < NUMBER_OF_ROCKS; i++) {
rMovement[i].rockPositionY += 1 + al_get_timer_count(app.speedIncreaser);
}
for (int i = 0; i < NUMBER_OF_POWERUPS; i++) {
pMovement[i].powerupYCoordinate += 2 * (1 + al_get_timer_count(app.speedIncreaser));
}
} else {
for (int i = 0; i < NUMBER_OF_ROCKS; i++) {
rMovement[i].rockPositionY += 1;
}
for (int i = 0; i < NUMBER_OF_POWERUPS; i++) {
pMovement[i].powerupYCoordinate += 2;
}
}
}
/// This function translates the keyboard booleans to a dx and dy which will move the character
/// Need to pass the keyboard struct
/// No return type
void keyboardMovement(Keyboard &movement) {
// Vertical movement
if (movement.keyDown && movement.keyUp){
movement.yCoordinateMovement = 0;
} else if (movement.keyUp) {
movement.yCoordinateMovement = -1 - al_get_timer_count(app.speedIncreaser);
} else if (movement.keyDown) {
movement.yCoordinateMovement = 1 + al_get_timer_count(app.speedIncreaser);
} else {
movement.yCoordinateMovement = 0;
}
// Horizontal movement
if (movement.keyRight && movement.keyLeft){
movement.xCoordinateMovement = 0;
} else if (movement.keyLeft) {
movement.xCoordinateMovement = -1 - al_get_timer_count(app.speedIncreaser);
} else if (movement.keyRight) {
movement.xCoordinateMovement = 1 + al_get_timer_count(app.speedIncreaser);
} else {
movement.xCoordinateMovement = 0;
}
// Double speed
if (movement.keySpace) {
movement.xCoordinateMovement *= 2;
movement.yCoordinateMovement *= 2;
}
}
/// This function makes the bool turn true or false based of if the key is pressed or released
/// Essentially translating the key codes to a bool
/// Need to pass the key code the keyboard struct and flag if it was a key down or key up event allowing the function to be reused
/// For multiple events
/// No return type
void keyboardEvent(int keycode, Keyboard &event, bool keyDown) {
// Key code to bool translation
switch(keycode) {
case ALLEGRO_KEY_UP:
event.keyUp = !event.keyUp;
break;
case ALLEGRO_KEY_DOWN:
event.keyDown = !event.keyDown;
break;
case ALLEGRO_KEY_LEFT:
event.keyLeft = !event.keyLeft;
break;
case ALLEGRO_KEY_RIGHT:
event.keyRight = !event.keyRight;
break;
case ALLEGRO_KEY_SPACE:
event.keySpace = !event.keySpace;
break;
case ALLEGRO_KEY_R:
// Only allows the key to become true when key down
if (keyDown) {
event.keyR = true;
}
break;
case ALLEGRO_KEY_P:
// Only allows the bool to change value when it is key down
if (keyDown && !event.keyP) {
event.keyP = true;
} else if (keyDown && event.keyP) {
event.keyP = false;
}
break;
case ALLEGRO_KEY_B:
if (keyDown && !event.keyB) {
event.keyB = true;
} else if (keyDown && event.keyB) {
event.keyB = false;
}
break;
default:
break;
}
}
/// The function takes the objects x and y coordinates and draws the image at those coordinates when the timer event occurs
/// Need to pass the movable object structs
/// No return type
void drawSprites(Character &cDraw, Rocks rDraw[], Lives lDraw[], PowerUp pDraw[]) {
// Draw all the objects in the game
al_draw_bitmap(cDraw.bitmap, cDraw.characterPositionX, cDraw.characterPositionY, 0);
for (int i=0; i<NUMBER_OF_LIVES; i++) {
if (lDraw[i].usable) {
al_draw_bitmap(lDraw[i].bitmap, lDraw[i].xCoordinate, lDraw[i].yCoordinate, 0);
}
}
for (int i = 0; i < NUMBER_OF_ROCKS; i++) {
al_draw_bitmap(rDraw[i].bitmap, rDraw[i].rockPositionX, rDraw[i].rockPositionY, 0);
}
for (int i = 0; i < NUMBER_OF_POWERUPS; i++) {
al_draw_bitmap(pDraw[i].bitmap, pDraw[i].powerupXCoordinate, pDraw[i].powerupYCoordinate, 0);
}
}
/// This function prints out the level the user is on, on the bottom left corner
/// No return type
void userLevel() {
// Declare variables
int obtainUserLevel = 0;
// Obtain timer count and add one as the count starts at 0
obtainUserLevel = al_get_timer_count(app.speedIncreaser) + 1;
al_draw_textf(app.arial, RED, 0, 775, ALLEGRO_ALIGN_LEFT, "Level %d", obtainUserLevel);
}