-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCShip.cpp
143 lines (112 loc) · 4.35 KB
/
CShip.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
// CGameObject.cpp
#define _USE_MATH_DEFINES
#include <algorithm>
#include <chrono>
#include <thread>
#include <cmath>
#include "stdafx.h"
#include "Constants.h"
#include "CGameObject.h"
#include "CShip.h"
#include "CMissile.h"
// constructor
CShip::CShip(cv::Point position, std::string shape, std::string color, std::vector<CGameObject*>& missileObjects, std::vector<CGameObject*>& allObjects)
: CGameObject(position, shape, color), gameobjects(gameobjects), missileobjects(missileObjects), allobjects(allObjects) {
m_lives = 10;
m_maxHealth = 100;
m_health = m_maxHealth;
m_angle = 90;
m_angleVelocity = 0;
m_acceleration = 0;
m_missileCount = 0;
m_missileStack = 20;
m_objectType = "ship";
}
// destructor
CShip::~CShip() {
// destructor
}
void CShip::rotate_left() {
m_angleVelocity = -700;
}
void CShip::rotate_right() {
m_angleVelocity = 700;
}
void CShip::stop_rotation() {
m_angleVelocity = 0;
}
void CShip::accelerate() {
m_acceleration = 500;
}
void CShip::deccelerate() {
m_acceleration = 0;
}
void CShip::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
// change direction
m_angle += m_angleVelocity * deltaTime;
//std::cout << "Inside class: angle " << m_angle << "\t";
// Check for border collision before updating the position
if (!is_border_collision(canvasSize)) {
// separate x and y components of velocity
m_position.x += m_velocity.x * deltaTime + 0.5 * cos(m_angle * PI / 180) * m_acceleration * pow(deltaTime, 2);
m_position.y += m_velocity.y * deltaTime + 0.5 * sin(m_angle * PI / 180) * m_acceleration * pow(deltaTime, 2);
// Update velocity based on acceleration
m_velocity.x += cos(m_angle * PI / 180) * m_acceleration * deltaTime;
m_velocity.y += sin(m_angle * PI / 180) * m_acceleration * deltaTime;
}
border_bounce(canvasSize);
// Update last update time for the next iteration
lastUpdateTime = currentTime;
}
}
void CShip::draw(cv::Mat& canvas) {
// Calculate the points of the triangle
double angleRad = m_angle * PI / 180;
double arrowLength = m_radius; // Set arrow length to match the radius of the circle
double arrowWidth = m_radius * 0.4; // Adjust arrow width as desired
cv::Point tip(
m_position.x + arrowLength * cos(angleRad),
m_position.y + arrowLength * sin(angleRad)
);
cv::Point leftWing(
m_position.x + arrowWidth * cos(angleRad + PI / 2),
m_position.y + arrowWidth * sin(angleRad + PI / 2)
);
cv::Point rightWing(
m_position.x + arrowWidth * cos(angleRad - PI / 2),
m_position.y + arrowWidth * sin(angleRad - PI / 2)
);
// Draw the arrowhead in white color
cv::line(canvas, tip, leftWing, cv::Scalar(255, 255, 255), 2);
cv::line(canvas, leftWing, rightWing, cv::Scalar(255, 255, 255), 2);
cv::line(canvas, rightWing, tip, cv::Scalar(255, 255, 255), 2);
// Draw the health bar
drawHealthBar(canvas, m_position);
// Draw ejecting lines if acceleration is not zero
if (m_acceleration != 0) {
// Calculate the position of the back of the ship
cv::Point back(
m_position.x - arrowLength * cos(angleRad),
m_position.y - arrowLength * sin(angleRad)
);
// Draw red and yellow lines ejecting out from the back
cv::line(canvas, back, leftWing, cv::Scalar(0, 0, 255), 2); // Red line
cv::line(canvas, back, rightWing, cv::Scalar(0, 255, 255), 2); // Yellow line
}
}
void CShip::fire() {
for (auto i: {-10, 0, 10}) {
CMissile* newMissile = new CMissile(m_position, "shape", "color");
// Set the missile's initial position, angle, velocity, and canvas size (if needed)
// Fire the missile
newMissile->fire(m_position, m_angle + i, m_velocity, cv::Rect(0, 0, 1200, 900));
// Append the new missile object to the missile object vector
missileobjects.push_back(newMissile);
}
}