-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
158 lines (120 loc) · 3.72 KB
/
Copy pathgame.cpp
File metadata and controls
158 lines (120 loc) · 3.72 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
#include "game.h"
#include <iostream>
Game::Game(){}
Asteroid Game::spawnAsteroids(Settings s, Ship ship){
Asteroid a;
float xSide, ySide;
xSide = rand() % (s.hypotenuse - s.WIDTH);
ySide = rand() % (s.hypotenuse - s.HEIGHT);
a.x = rand() % 2 == 0 ? xSide + s.WIDTH : -xSide;
a.y = rand() % 2 == 0 ? ySide + s.HEIGHT : -ySide;
a.size = rand() % (100 - 50) + 50;
a.angle = a.angle * M_PI / 180;
int randValue = rand() % (6 - 3) + 3;
for (int i = 0; i < a.pointSize; i++)
{
a.pointArray.push_back((rand() % (100 - 60) + 60) / 100);
}
a.vx = -cos(acos((a.x - ship.x) / sqrt(pow(a.y - ship.y, 2) + pow(a.x - ship.x, 2)))) * randValue;
a.vy = -sin(asin((a.y - ship.y) / sqrt(pow(a.y - ship.y, 2) + pow(a.x - ship.x, 2)))) * randValue;
a.vr *= rand() % 2 == 0 ? -1 : 1;
return a;
}
void Game::launchAsteroid(Asteroid &a, Settings s){
a.x += a.vx;
a.y += a.vy;
a.angle += a.vr;
bool top = a.y + a.size > s.HEIGHT;
bool bot = a.y - a.size < 0;
bool left = a.x - a.size < 0;
bool right = a.x + a.size > s.WIDTH;
if (a.insideArena)
{
if (top || bot)
{
a.vy *= -1;
}
if (left || right)
{
a.vx *= -1;
}
}
else if(!top && !bot && !left && !right){
a.insideArena = true;
}
}
bool Game::checkCollision(Ship ship, Asteroid &asteroid){
int hypotenuse;
int a,b;
asteroid.radius = asteroid.size;
ship.radius = 5;
a = asteroid.x - ship.x;
b = asteroid.y - ship.y;
hypotenuse = sqrt(pow(a,2) + pow(b,2));
return abs(hypotenuse) < (asteroid.radius + ship.radius);
}
bool Game::checkAsteroidCollision(std::vector<Asteroid> &asteroids){
for (int i = 0; i < asteroids.size(); i++)
{
for (int j = i + 1; j < asteroids.size(); j++)
{
Asteroid &a = asteroids.at(i);
Asteroid &b = asteroids.at(j);
if (circleCollision(a.x, b.x, a.y, b.y, a.radius, b.radius))
{
float axOld = a.vx;
float bxOld = b.vx;
float ayOld = a.vy;
float byOld = b.vy;
a.vx = bxOld;
a.vy = byOld;
b.vx = axOld;
b.vy = ayOld;
}
}
}
}
bool Game::circleCollision(float x1, float x2, float y1, float y2, float r1, float r2){
int hypotenuse;
int a,b;
a = x1 - x2;
b = y1 - y2;
hypotenuse = sqrt(pow(a,2) + pow(b,2));
return abs(hypotenuse) < (r1 + r2);
}
void Game::updateBulletMovement(std::vector<Bullet> &bullet, Settings settings){
for (int i = 0; i < bullet.size(); i++)
{
Bullet &b = bullet.at(i);
b.x += b.vx;
b.y += b.vy;
if (b.x > settings.WIDTH || b.x < 0)
{
bullet.erase(bullet.begin() + i);
}
else if (b.y > settings.HEIGHT || b.y < 0)
{
bullet.erase(bullet.begin() + i);
}
}
}
void Game::bulletAsteroidCollision(std::vector<Bullet> &bullets, std::vector<Asteroid> &asteroids, Settings settings){
for (int i = 0; i < bullets.size(); i++)
{
Bullet &a = bullets.at(i);
for (int j = 0; j < asteroids.size(); j++)
{
Asteroid &b = asteroids.at(j);
if (circleCollision(a.x, b.x, a.y, b.y, 0, b.size))
{
b.health -= a.damage;
if (b.health <= 0)
{
settings.playerPoints += b.points;
asteroids.erase(asteroids.begin() + j);
bullets.erase(bullets.begin() + i);
}
}
}
}
}