-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCEnemyMissile.cpp
92 lines (71 loc) · 2.58 KB
/
CEnemyMissile.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
// CGameObject.cpp
#define _USE_MATH_DEFINES
#include <algorithm>
#include <chrono>
#include <thread>
#include <cmath>
#include "stdafx.h"
#include "Constants.h"
#include "CGameObject.h"
#include "CEnemyMissile.h"
CEnemyMissile::CEnemyMissile(cv::Point2f position, std::string shape, std::string color)
: CGameObject(position, shape, color) {
m_state = "active";
m_objectType = "enemyMissile";
m_speed = 200;
m_maxFuel = 2000000;
m_fuel = m_maxFuel;
m_thrust = 0;
m_lives = 1;
m_maxHealth = 10;
m_health = m_maxHealth;
m_radius = 5;
}
CEnemyMissile::~CEnemyMissile() {
}
void CEnemyMissile::fire(cv::Point2f player_position, double player_angle, cv::Point player_velocity, cv::Rect& canvasSize) {
if (m_state == "active") {
// Get the current time
auto currentTime = std::chrono::high_resolution_clock::now();
// Calculate the time difference since the last update
std::chrono::duration<double> timeDiff = currentTime - lastUpdateTime;
double deltaTime = timeDiff.count(); // Delta time in seconds
// Set the missile's position to the player's position
m_position = player_position;
// Calculate the missile's velocity based on the player's angle and the missile's speed
m_velocity.x = player_velocity.x + cos(player_angle * PI / 180) * m_speed;
m_velocity.y = player_velocity.y + sin(player_angle * PI / 180) * m_speed;
// Update last update time for the next iteration
lastUpdateTime = currentTime;
}
}
void CEnemyMissile::update(cv::Rect& canvasSize) {
if (m_state == "active")
{
// Get the current time
auto currentTime = std::chrono::high_resolution_clock::now();
// Calculate the time difference since the last update
std::chrono::duration<double> timeDiff = currentTime - lastUpdateTime;
double deltaTime = timeDiff.count(); // Delta time in seconds
if (!is_border_collision(canvasSize)) {
// separate x and y components of velocity
m_position.x += m_velocity.x * deltaTime;
m_position.y += m_velocity.y * deltaTime;
}
else {
m_lives = 0;
m_state = "inactive";
}
m_fuel -= 10;
if (m_fuel == 0) {
m_state = "inactive";
}
// Update last update time for the next iteration
lastUpdateTime = currentTime;
}
}
void CEnemyMissile::draw(cv::Mat& canvas) {
if (m_state == "active") {
cv::circle(canvas, m_position, m_radius, cv::Scalar(0, 255, 255), cv::FILLED);
}
}