-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.cpp
More file actions
90 lines (81 loc) · 2.45 KB
/
Character.cpp
File metadata and controls
90 lines (81 loc) · 2.45 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
#include "Character.h"
#include "raymath.h"
Character::Character(int WindowWidth, int WindowHeight) :
WindowWidth(WindowWidth),
WindowHeight(WindowHeight)
{
Width = Texture.width / MaxFrame;
Height = Texture.height;
}
Vector2 Character::GetScreenPos()
{
return Vector2{
static_cast<float>(WindowWidth)/2.0f - (0.5f * Width) * Scale,
static_cast<float>(WindowHeight)/2.0f - (0.5f * Height) * Scale
};
}
void Character::Tick(float DeltaTime)
{
if (!GetAlive()) return;
// Define the movement of the map
if (IsKeyDown(KEY_A)) {Velocity.x -= 1.0;}
if (IsKeyDown(KEY_D)) Velocity.x += 1.0; // if the "if" statement only have one line we can omit the curly braces
if (IsKeyDown(KEY_W)) {Velocity.y -= 1.0;}
if (IsKeyDown(KEY_S)) {Velocity.y += 1.0;}
// update velocity than tick
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
};
rotation = IsMouseButtonDown(MOUSE_LEFT_BUTTON) ? 35.f : 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
};
rotation = IsMouseButtonDown(MOUSE_LEFT_BUTTON) ? -35.f : 0.f;
}
// draw the sword
Rectangle Source{
0.f,
0.f,
static_cast<float>(Weapon.width) * RightLeft,
static_cast<float>(Weapon.height)};
Rectangle Destination{
GetScreenPos().x + offset.x,
GetScreenPos().y + offset.y,
Weapon.width * Scale,
Weapon.height * Scale};
DrawTexturePro(Weapon, Source, Destination, origin, rotation, WHITE);
// DrawRectangleLines(
// WeaponCollisionRec.x,
// WeaponCollisionRec.y,
// WeaponCollisionRec.width,
// WeaponCollisionRec.height,
// RED
// );
}
void Character::TakeDamage(float Damage)
{
Health -= Damage;
if (Health <= 0.f)
{
SetAlive(false);
}
}