-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicUnit.cpp
More file actions
105 lines (85 loc) · 2.61 KB
/
Copy pathDynamicUnit.cpp
File metadata and controls
105 lines (85 loc) · 2.61 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
#define _WINDOWS
#include <cmath>
#include <algorithm>
#include <numeric>
#include "DynamicUnit.h"
DynamicUnit::DynamicUnit(Sprite* sprite, double x, double y, double width, double height, double velocity, double minVelocity, double maxVelocity) : Unit(sprite, x, y, width, height) {
this->_maxVelocity = maxVelocity;
this->_minVelocity = minVelocity;
this->velocity(velocity);
this->_dx = 0;
this->_dy = 0;
this->_directionX = 0;
this->_directionY = 0;
}
void DynamicUnit::velocity(double velocity) {
this->_velocity = velocity;
this->_dx = directionX() * this->velocity();
this->_dy = directionY() * this->velocity();
}
void DynamicUnit::resetVelocity() {
this->_velocity = 0;
this->_dx = 0;
this->_dy = 0;
}
double DynamicUnit::velocity() {
return std::clamp(_velocity, this->minVelocity(), this->maxVelocity());
}
void DynamicUnit::maxVelocity(double maxVelocity) {
this->_maxVelocity = maxVelocity;
}
double DynamicUnit::maxVelocity() {
return this->_maxVelocity >= 0 ? this->_maxVelocity : HUGE_VAL;
}
void DynamicUnit::minVelocity(double minVelocity) {
this->_minVelocity = minVelocity;
}
double DynamicUnit::minVelocity() {
return this->_minVelocity >= 0 ? this->_minVelocity : -HUGE_VAL;
}
void DynamicUnit::direction(std::pair<double, double> direction) {
this->direction(direction.first, direction.second);
}
void DynamicUnit::direction(double x, double y) {
this->_directionX = x;
this->_directionY = y;
this->_dx = this->directionX() * this->velocity();
this->_dy = this->directionY() * this->velocity();
}
double DynamicUnit::directionX() {
return this->_directionX;
}
double DynamicUnit::directionY() {
return this->_directionY;
}
void DynamicUnit::update() {
if (this->velocity() != 0) {
this->moveRelative(_dx, _dy);
}
}
Side DynamicUnit::reflect(Unit* other) {
switch (other->collides(this)) {
case Side::TOP:
this->move(this->x(), other->y() - this->height() - 1);
this->direction(this->directionX(), -this->directionY());
return Side::TOP;
case Side::BOTTOM:
this->move(this->x(), other->y() + other->height() + 1);
this->direction(this->directionX(), -this->directionY());
return Side::BOTTOM;
case Side::RIGHT:
this->move(other->x() + other->width() + 1, this->y());
this->direction(-this->directionX(), this->directionY());
return Side::RIGHT;
case Side::LEFT:
this->move(other->x() - this->width() - 1, this->y());
this->direction(-this->directionX(), this->directionY());
return Side::LEFT;
};
}
void DynamicUnit::accelerate(double coef) {
this->velocity(_velocity * (1 + coef));
}
void DynamicUnit::decelerate(double coef) {
this->velocity(_velocity / (1 + coef));
}