-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
222 lines (180 loc) · 6.34 KB
/
Source.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "SDL.h"
#include "SDL_image.h"
#include<iostream>
#include <vector>
struct Button {
SDL_Rect rect;
bool hovering;
};
struct Bullet {
SDL_Rect rect;
bool active;
int speed;
int direction;
};
struct Enemy {
SDL_Rect rect; // enemy
SDL_Rect healthBarBox; // black border
SDL_Rect healthBar; // red health
int health;
int speed;
int direction;
bool active;
};
Bullet shoot(SDL_Renderer* renderer, SDL_Rect character, bool& canShoot) {
//canShoot = false;
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
double deltaX = mouseX - (character.x+character.w / 2);
double deltaY = mouseY - (character.y + character.h / 2);
double angle = atan2(deltaY, deltaX);
std::cout << "x: " << mouseX << " y: " << mouseY << " angle: " << angle << "\n";
std::cout << "x: " << character.x << " y: " << character.y << "\n";
// Create the bullet
Bullet bullet = {
{ character.x + character.w / 2 - 5, character.y + character.h / 2 - 5, 10, 10 },
true,
10,
angle
};
SDL_SetRenderDrawColor(renderer, 0, 100, 255, 255);
SDL_RenderFillRect(renderer, &character);
return bullet;
}
void collisionCheck(std::vector<Bullet>& bullets, std::vector<Enemy>& enemies) {
if (bullets.empty() || enemies.empty()) return;
for (auto& bullet : bullets) {
for (auto& enemy : enemies) {
if (SDL_HasIntersection(&bullet.rect, &enemy.rect)) {
bullet.active = false;
enemy.health -= 1;
enemy.healthBar.w -= 1;
std::cout<<enemy.health<<std::endl;
if (enemy.health <= 0) {
enemy.active = false;
enemies.erase(enemies.begin());
}
}
}
}
}
void enemySpawner(std::vector<Enemy>& enemies, int w, int h, int max) {
std::srand(static_cast<unsigned int>(time(nullptr)));
int count = std::rand() % max + 1;
for (int i = 0; i < count; ++i) {
int spawnX = std::rand() % w;
int spawnY = std::rand() % h;
Enemy enemy = { {spawnX, spawnY, 50, 50}, {spawnX, spawnY - 25, 50, 10}, {spawnX, spawnY - 25, 50, 10}, 10, 1, 0, true };
enemies.push_back(enemy);
}
}
int main(int argc, char* argv[])
{
int WIDTH = 1920;
int HEIGHT = 1080;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
bool done = false;
window = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
//interaction
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
SDL_Point mouseP = {mouseX, mouseY};
SDL_Rect character = { WIDTH / 2 - 250, HEIGHT / 2 - 250, 30, 30 };
std::vector<Bullet> bullets;
std::vector<Enemy> enemies;
enemySpawner(enemies, WIDTH, HEIGHT, 5);
//game logic
const int speed = 10;
int point = 0;
bool canShoot = true;
//time
Uint64 lastTime = SDL_GetTicks64();
const Uint64 interval = 1000; // 1 second
Bullet myBullet = { { 0, 0, 10, 10 }, true, 10, 0 };
Enemy myEnemy = { { 0, 0, 50, 50 }, { 0, 0, 50, 10 }, { 0, 0, 50, 10 }, 10, 5, 0, true };
SDL_Event event;
while (!done) {
while (SDL_PollEvent(&event)) {
switch (event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN: //input - character movement
if (event.key.keysym.sym == SDLK_a || event.key.keysym.sym == SDL_SCANCODE_LEFT) {
character.x -= speed;
}
else if (event.key.keysym.sym == SDLK_d || event.key.keysym.sym == SDL_SCANCODE_RIGHT) {
character.x += speed;
}
if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDL_SCANCODE_UP) {
character.y -= speed;
}
else if (event.key.keysym.sym == SDLK_s || event.key.keysym.sym == SDL_SCANCODE_DOWN) {
character.y += speed;
}
break;
//input - mouse
case SDL_MOUSEMOTION:
SDL_GetMouseState(&mouseX, &mouseY);
break;
case SDL_MOUSEBUTTONDOWN:
if (canShoot) {
myBullet = shoot(renderer, character, canShoot);
bullets.push_back(myBullet);
}
break;
}
}
for (auto& bullet : bullets) {
bullet.rect.x += cos(bullet.direction) * bullet.speed;
bullet.rect.y += sin(bullet.direction) * bullet.speed;
}
for (auto& enemy : enemies) {
enemy.rect.x += cos(enemy.direction) * enemy.speed;
enemy.healthBarBox.x += cos(enemy.direction) * enemy.speed;
enemy.healthBar.x += cos(enemy.direction) * enemy.speed;
enemy.rect.y += sin(enemy.direction) * enemy.speed;
enemy.healthBarBox.y += sin(enemy.direction) * enemy.speed;
enemy.healthBar.y += sin(enemy.direction) * enemy.speed;
}
if (SDL_GetTicks64() > lastTime + 1000) {
enemySpawner(enemies, WIDTH, HEIGHT, 5);
lastTime = SDL_GetTicks64();
}
if (SDL_GetTicks64() > lastTime + 40) {
collisionCheck(bullets, enemies);
lastTime = SDL_GetTicks64();
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
//draw
SDL_SetRenderDrawColor(renderer, 0, 100, 255, 255);
SDL_RenderFillRect(renderer, &character);
for (auto& bullet : bullets) {
if (bullet.active) {
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
SDL_RenderFillRect(renderer, &bullet.rect);
if (bullet.rect.x < 0 || bullet.rect.x > WIDTH || bullet.rect.y < 0 || bullet.rect.y > HEIGHT) {
bullet.active = false;
bullets.erase(bullets.begin());
}
}
}
for(auto& enemy : enemies){
if (enemy.active) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &enemy.rect);
SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255);
SDL_RenderFillRect(renderer, &enemy.healthBar);
}
}
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}