-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
180 lines (150 loc) · 7.73 KB
/
Copy pathmain.cpp
File metadata and controls
180 lines (150 loc) · 7.73 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
#include "raylib.h"
#include "cat.h"
#include "crosshair.h"
#include <vector>
#include "powerUp.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
typedef enum GameScreen { TITLE = 0, GAMEPLAY, ENDING } GameScreen;
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "Cat Shooter");
InitAudioDevice();
Music title_music = LoadMusicStream("assets/music/title.mp3");
Music game_music = LoadMusicStream("assets/music/place_holder.mp3");
Music gameover_music = LoadMusicStream("assets/music/gameover.mp3");
SetMusicVolume(gameover_music, 0.5f);
// Backgrounds
Image image = LoadImage("assets/backgrounds/title_background.png"); // Loaded in CPU memory (RAM)
Texture2D title_background = LoadTextureFromImage(image); // Image converted to texture, GPU memory (VRAM)
image = LoadImage("assets/backgrounds/gameplay_background.png");
Texture2D gameplay_background = LoadTextureFromImage(image);
image = LoadImage("assets/backgrounds/gameover_background.png");
Texture2D gameover_background = LoadTextureFromImage(image);
UnloadImage(image);
// Cats!!
std::vector<Cat> cats{};
// crosshair
Crosshair crosshair{};
PowerUp powerUp {};
GameState gameState;
GameScreen currentScreen = TITLE;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()){ // Detect window close button or ESC key
float deltaTime = GetFrameTime();
BeginDrawing();
HideCursor();
// Switch to handle different screens
switch (currentScreen) {
case TITLE:
// Music stuff
if (!IsMusicStreamPlaying(title_music)) {
PlayMusicStream(title_music);
}
UpdateMusicStream(title_music);
// We set (or reset) game data
if (gameState.resetFlag){
cats.clear();
cats.push_back({});
cats.push_back({});
gameState.health = 3;
gameState.score = 0;
gameState.resetFlag = false;
}
DrawTexture(title_background, 0, 0, WHITE);
DrawText("Cat Shooter", 20, 20, 40, BLACK);
DrawText("PRESS ENTER TO START", 120, 220, 20, BLACK);
if (IsKeyPressed(KEY_ENTER)){
StopMusicStream(title_music);
currentScreen = GAMEPLAY;
gameState.resetFlag = true;
}
break;
case GAMEPLAY:
if (!IsMusicStreamPlaying(game_music)) {
PlayMusicStream(game_music);
}
UpdateMusicStream(game_music);
DrawTexture(gameplay_background, 0, 0, WHITE);
// im sure there's a better way to center this but this is what i could find in the cheatsheet
// DrawText(TextFormat("Cursor position is:\nX:%i Y:%i", GetMouseX(), GetMouseY()), screenWidth/2 - MeasureText(TextFormat("Ball position is:\nX:%i Y:%i", GetMouseX(), GetMouseY()),20)/2, screenHeight/2, 20, BLACK);
DrawText(TextFormat("Score: %i", gameState.score), 700 - MeasureText(TextFormat("Ball position is:\nX:%i Y:%i", GetMouseX(), GetMouseY()),20)/2, 25, 20, BLACK);
DrawText(TextFormat("Health: %i", gameState.health), 100 - MeasureText(TextFormat("Ball position is:\nX:%i Y:%i", GetMouseX(), GetMouseY()),20)/2, 25, 20, BLACK);
powerUp.draw(deltaTime, gameState, GetMousePosition());
// Shield visuals
if (gameState.shield == 3) {
DrawLineEx({1,1}, {799, 1}, 3.0f, GREEN);
DrawLineEx({1,1}, {1, 449}, 3.0f, GREEN);
DrawLineEx({799,449}, {799, 1}, 3.0f, GREEN);
DrawLineEx({799,449}, {1, 449}, 3.0f, GREEN);
}
if (gameState.shield == 2) {
DrawLineEx({1,1}, {799, 1}, 3.0f, YELLOW);
DrawLineEx({1,1}, {1, 449}, 3.0f, YELLOW);
DrawLineEx({799,449}, {799, 1}, 3.0f, YELLOW);
DrawLineEx({799,449}, {1, 449}, 3.0f, YELLOW);
}
if (gameState.shield == 1) {
DrawLineEx({1,1}, {799, 1}, 3.0f, RED);
DrawLineEx({1,1}, {1, 449}, 3.0f, RED);
DrawLineEx({799,449}, {799, 1}, 3.0f, RED);
DrawLineEx({799,449}, {1, 449}, 3.0f, RED);
}
for (Cat& cat : cats) {
cat.draw(deltaTime, gameState);
cat.checkForClick(GetMousePosition());
cat.move(gameState);
}
if (gameState.flagDespawnCat) {
if (!cats.empty() && cats.size() != 1) {
cats.pop_back();
}
gameState.flagDespawnCat = false;
}
if(gameState.flagSpawnNewCat && gameState.score % 1000 == 0 && gameState.score !=0){
gameState.flagSpawnNewCat = false;
cats.push_back({});
}
if (gameState.score % 1000) {
gameState.flagSpawnNewCat = true;
}
crosshair.drawCrosshair();
if(gameState.health <= 0){
currentScreen = ENDING;
}
break;
case ENDING:
if (!IsMusicStreamPlaying(gameover_music)) {
PlayMusicStream(gameover_music);
}
if (gameState.highScore < gameState.score){
gameState.highScore = gameState.score;
}
UpdateMusicStream(gameover_music);
DrawTexture(gameover_background, 0, 0, WHITE);
DrawText("Game Over!", 300, 150, 40, BLACK);
DrawText(TextFormat("PRESS ENTER TO TRY AGAIN\n\tHigh Score: %i", gameState.highScore), 250, 220, 20, BLACK);
if (IsKeyPressed(KEY_ENTER)){
StopMusicStream(gameover_music);
currentScreen = TITLE;
}
break;
}
EndDrawing();
//----------------------------------------------------------------------------------
}
UnloadMusicStream(title_music);
CloseAudioDevice();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}