-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacter.cpp
91 lines (78 loc) · 2.3 KB
/
Character.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
#include "Character.h"
#include "raymath.h"
#define DEBUG
Character::Character(int winWidth, int winHeight) :
windowWidth(winWidth),
windowHeight(winHeight)
{
width = texture.width / maxFrames;
height = texture.height;
}
Vector2 Character::getScreenPos()
{
return Vector2{
static_cast<float>(windowWidth) / 2.0f - scale * (0.5f * width),
static_cast<float>(windowHeight) / 2.0f - scale * (0.5f * height)
};
}
void Character::tick(float deltaTime)
{
if (!getAlive()) return;
if (IsKeyDown(KEY_A))
velocity.x -= 1.0;
if (IsKeyDown(KEY_D))
velocity.x += 1.0;
if (IsKeyDown(KEY_W))
velocity.y -= 1.0;
if (IsKeyDown(KEY_S))
velocity.y += 1.0;
BaseCharacter::tick(deltaTime);
Vector2 origin{};
Vector2 offset{};
float rotation{};
if (rightLeft > 0.f)
{
origin = {0.f, weapon.height * scale};
offset = {35.f, 55.f};
weaponCollisionRec = {
getScreenPos().x + offset.x,
getScreenPos().y + offset.y - weapon.height * scale,
weapon.width * scale,
weapon.height * scale
};
IsMouseButtonDown(MOUSE_LEFT_BUTTON) ? rotation = 35.f : rotation = 0.f;
}
else
{
origin = {weapon.width * scale, weapon.height * scale};
offset = {25.f, 55.f};
weaponCollisionRec = {
getScreenPos().x + offset.x - weapon.width * scale,
getScreenPos().y + offset.y - weapon.height * scale,
weapon.width * scale,
weapon.height * scale
};
IsMouseButtonDown(MOUSE_LEFT_BUTTON) ? rotation = -35.f : rotation = 0.f;
}
// draw the sword
Rectangle source{0.f, 0.f, static_cast<float>(weapon.width) * rightLeft, static_cast<float>(weapon.height)};
Rectangle dest{getScreenPos().x + offset.x, getScreenPos().y + offset.y, weapon.width * scale, weapon.height * scale};
DrawTexturePro(weapon, source, dest, origin, rotation, WHITE);
#ifdef DEBUG
DrawRectangleLines(
weaponCollisionRec.x,
weaponCollisionRec.y,
weaponCollisionRec.width,
weaponCollisionRec.height,
RED
);
#endif
}
void Character::takeDamage(float damage)
{
health -= damage;
if(health <= 0.f)
{
setAlive(false);
}
}