-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
68 lines (60 loc) · 2.35 KB
/
Player.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
#include "Player.h"
#include "Mildred.h"
#include "Calc.h"
#include <cmath>
Player::Player(double positionX, double positionY, double size, double viewAngle, double speed)
{
this->positionX = positionX;
this->positionY = positionY;
this->size = size;
this->viewAngle = Calc::ToRadians(viewAngle);
this->speed = speed;
this->posXCentered = positionX + size / 2;
this->posYCentered = positionY + size / 2;
}
void Player::MoveForward() {
this->positionX = this->positionX + cos(this->viewAngle) * this->speed;
this->positionY = this->positionY + sin(this->viewAngle) * this->speed;
this->UpdateCenteredPosition();
}
void Player::MoveBackward()
{
this->positionX = this->positionX + cos(this->viewAngle + Calc::ToRadians(180)) * this->speed;
this->positionY = this->positionY + sin(this->viewAngle + Calc::ToRadians(180)) * this->speed;
this->UpdateCenteredPosition();
}
void Player::MoveLeft()
{
this->positionX = this->positionX + cos(this->viewAngle - Calc::ToRadians(90)) * this->speed;
this->positionY = this->positionY + sin(this->viewAngle - Calc::ToRadians(90)) * this->speed;
this->UpdateCenteredPosition();
}
void Player::MoveRight()
{
this->positionX = this->positionX + cos(this->viewAngle + Calc::ToRadians(90)) * this->speed;
this->positionY = this->positionY + sin(this->viewAngle + Calc::ToRadians(90)) * this->speed;
this->UpdateCenteredPosition();
}
void Player::DrawAngleLine() {
Mildred::SetRenderDrawColor(128, 0, 155, 255);
int x2 = this->positionX + this->size / 2 + cos(this->viewAngle) * 150;
int y2 = this->positionY + this->size / 2 + sin(this->viewAngle) * 150;
SDL_RenderDrawLine(Mildred::GetRenderer(), this->posXCentered, this->posYCentered,
x2, y2);
}
void Player::AdjustAngle(int* xrel, int* yrel)
{
Player::viewAngle += Calc::ToRadians(*xrel / 3);
//cout << "Viewangle: " << Player::viewAngle << endl;
//cout << "Normalized angle in radians: " << Calc::NormalizeAngleRad(Player::viewAngle) << endl;
//viewAngle += yrel;
}
void Player::DrawPlayerOnMinimap() {
Mildred::SetRenderDrawColor(0, 255, 0, 255);
Mildred::DrawRect(this->size, this->size, this->positionX, this->positionY);
DrawAngleLine();
}
void Player::UpdateCenteredPosition() {
this->posXCentered = positionX + size / 2;
this->posYCentered = positionY + size / 2;
}