-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCharacter.cpp
More file actions
132 lines (86 loc) · 3.18 KB
/
Character.cpp
File metadata and controls
132 lines (86 loc) · 3.18 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
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
#include "GameController.h"
#include "Character.h"
using namespace Utils;
Character::Character(LPCTSTR bitmapPath, Graphics* graphics, float x, float y, float xSpeed,
float ySpeed, float jumpHeight, float gravity, int acceleration)
: Animation(bitmapPath, graphics, new GameVector(x, y), new GameVector(xSpeed, 0), new GameVector(0, gravity), false)
{
World::offset = 0;
position->x = static_cast<float>(World::offset + SCREEN_WIDTH / 2);
feet = new GameVector(position->x + DEFAULT_BLOCK_SIZE / 2, position->y + DEFAULT_BLOCK_SIZE);
head = new GameVector(position->x + DEFAULT_BLOCK_SIZE / 2, position->y);
middle = new GameVector(position->x + DEFAULT_BLOCK_SIZE / 2, position->y + DEFAULT_BLOCK_SIZE / 2);
jumping = false;
this->acceleration = acceleration;
}
Character::~Character() {
Animation::~Animation();
delete position;
delete feet;
delete head;
delete middle;
}
void Character::Update()
{
Animation::Update();
UpdateCoordinates();
if (GetAsyncKeyState(static_cast<int>(Direction::Right)) & 0x8000 && isNextToWall() != Direction::Right) MoveRight();
else if (GetAsyncKeyState(static_cast<int>(Direction::Left)) & 0x8000 && isNextToWall() != Direction::Left) MoveLeft();
else index = 0;
// if character would get stuck
if (GetAsyncKeyState(VK_HOME) & 0x8000) {
position->y = SCREEN_HEIGHT / 4;
position->x = SCREEN_WIDTH / 2;
}
if (isOnLand()) {
if (!jumping) velocity->y = 0;
if (GetAsyncKeyState(static_cast<int>(Direction::Up)) & 0x8000) {
Jump();
jumping = true;
}
else jumping = false;
}
else if (World::isCollisionEnabled(convertToBlockCoord(head->x), convertToBlockCoord(head->y - 10))) {
velocity->y = static_cast<float>(acceleration);
}
GravityEvent();
}
void Character::Render()
{
Animation::Render();
}
void Character::Jump() {
if (position->y >= SCREEN_HEIGHT / 4 && position->y < SCREEN_HEIGHT) {
velocity->y = static_cast<float>(-acceleration);
}
else jumping = false;
}
bool Character::isOnLand() {
if (World::isCollisionEnabled(convertToBlockCoord(feet->x), convertToBlockCoord(feet->y)))
return true;
return false;
}
Character::Direction Character::isNextToWall() {
if (World::isCollisionEnabled(convertToBlockCoord(middle->x + 8), convertToBlockCoord(middle->y)))
return Direction::Right;
else if (World::isCollisionEnabled(convertToBlockCoord(middle->x - 8), convertToBlockCoord(middle->y)))
return Direction::Left;
return Direction::null;
}
void Character::UpdateCoordinates() {
feet->x = static_cast<float>(World::offset + SCREEN_WIDTH / 2 + DEFAULT_BLOCK_SIZE / 2);
feet->y = position->y + DEFAULT_BLOCK_SIZE;
head->x = static_cast<float>(World::offset + SCREEN_WIDTH / 2 + DEFAULT_BLOCK_SIZE / 2);
head->y = position->y + DEFAULT_BLOCK_SIZE / 2;
middle->x = static_cast<float>(World::offset + SCREEN_WIDTH / 2 + DEFAULT_BLOCK_SIZE / 2);
middle->y = position->y + DEFAULT_BLOCK_SIZE / 2;
}
void Character::MoveRight()
{
index = 1;
if (World::offset < WORLD_BORDER) World::offset += static_cast<int>(velocity->x);
}
void Character::MoveLeft() {
index = 2;
if (convertToBlockCoord(static_cast<float>(World::offset)) > World::chunks.front()->GetStartPoint()) World::offset -= static_cast<int>(velocity->x);
}