-
Notifications
You must be signed in to change notification settings - Fork 0
/
shots.cpp
136 lines (111 loc) · 2.87 KB
/
shots.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
#include <thread>
#include <mutex>
#include <condition_variable>
#include <cmath>
#include <chrono>
#include "shots.hpp"
#include "ship.hpp"
#include "asteroids.hpp"
#include "game.hpp"
const unsigned shoot_threshold_ms = 150;
const unsigned fastshot_detection_threshold = 100;
const unsigned fastshot_threshold_penalty = 400;
const unsigned fastshot_max_count = 5;
std::thread shots_thread;
std::mutex shots_mtx;
std::condition_variable shots_cv;
std::list <shot> shots;
std::chrono::time_point<std::chrono::system_clock> next_shot = std::chrono::system_clock::now();
unsigned cur_fastshot = 0;
static bool should_run = true;
locked_data<std::list<shot>> getShotsList()
{
locked_data<std::list<shot>> data(shots, std::unique_lock(shots_mtx));
return data;
}
static void shots_loop()
{
std::unique_lock<std::mutex> lock(shots_mtx);
while (should_run) {
auto&& asteroids_data = get_asteroids_list();
auto& asteroids = asteroids_data.get_data();
for (auto it = shots.begin(); it != shots.end(); ) {
it->y += 0.02;
if (it->y >= 1.0) {
it = shots.erase(it);
continue;
}
bool overlap = false;
for (auto asteroid_it = asteroids.begin(); asteroid_it != asteroids.end(); asteroid_it++) {
for (int i = 0; i < 6; i++) {
float asteroid_x = asteroid_it->vertices[i][0] + asteroid_it->x;
float asteroid_y = asteroid_it->vertices[i][1] + asteroid_it->y;
float distance = std::sqrt(std::pow(it->x - asteroid_x, 2) + std::pow(it->y - asteroid_y, 2));
if (distance <= 0.03) {
overlap = true;
break;
}
}
if (overlap) {
gameOnAsteroidShoot();
asteroid_it = asteroids.erase(asteroid_it);
it = shots.erase(it);
break;
}
}
if (!overlap) {
++it;
}
}
asteroids_data.release();
if (shots.empty()) {
shots_cv.wait(lock);
}
else {
shots_cv.wait_until(lock, std::chrono::system_clock::now() + std::chrono::milliseconds(10));
}
}
}
void createNewShot()
{
auto cur_time = std::chrono::system_clock::now();
if (next_shot > cur_time) {
return;
}
{
std::lock_guard<std::mutex> lock(shots_mtx);
shot newShot;
auto&& ship_pos_data = getShipPosX();
newShot.x = ship_pos_data.get_data();
ship_pos_data.release();
newShot.y = ship_y_pos;
if (shots.empty()) {
shots_cv.notify_one();
}
shots.push_back(newShot);
}
if (next_shot <= cur_time + std::chrono::milliseconds(fastshot_detection_threshold)) {
cur_fastshot++;
}
if (cur_fastshot == fastshot_max_count) {
cur_fastshot = 0;
next_shot = cur_time + std::chrono::milliseconds(fastshot_threshold_penalty);
}
else {
next_shot = cur_time + std::chrono::milliseconds(shoot_threshold_ms);
}
}
void startShotsThread()
{
should_run = true;
shots_thread = std::thread(shots_loop);
}
void stopShotsThread()
{
{
std::lock_guard<std::mutex> lock(shots_mtx);
should_run = false;
shots_cv.notify_one();
}
shots_thread.join();
}