Repository navigation
Expand file tree
/
Copy pathenemyBase.cpp
More file actions
131 lines (124 loc) · 2.82 KB
/
Copy pathenemyBase.cpp
File metadata and controls
131 lines (124 loc) · 2.82 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
#include "enemyBase.hpp"
#include "player.hpp"
#include <raymath.h>
enemyBase::enemyBase(Texture2D tex, int frameCount, float animTime, Vector2 pos, float speed,float hp,Player& playerptr): playerptr(playerptr),
texture(tex),
frameCount(frameCount),
animTime(animTime),
animPlayer(tex, frameCount, animTime),
Cboxes(pos, dir, animPlayer.getWidth(),
animPlayer.getTexture().height, {30,30})
{
this->texture = tex;
this->hp = hp;
this->frameCount = frameCount;
this->animTime = animTime;
this->state = EnemyState::IDLE;
this->pos = pos;
this->speed = speed;
animPlayer.setTexture(tex,frameCount,animTime);
SetRandomSeed(int(GetTime()));
}
void enemyBase::update()
{
if(hp<0)
{
alive =false;
pos={100000,100000};
}
if(alive)
{
if(state==EnemyState::ATTACKING)
{
dir={0,0};
}
else
lockon();
base::update();
// Update player position, velocity, and state
//HURT
if(getHit)
{
if(hurtcooldown<=GetTime()-lasthurttime)
{
takedmg(playerptr.attackStrength);
hitTaken=true;
lasthurttime=GetTime();
}
hitTaken=false;
}
if (.2f>=GetTime() - lasthurttime)
{
tint = RED;
}
else
{
tint = WHITE;
}
updateState();
Cboxes.Update(pos,dir,animPlayer.getWidth(),animPlayer.getTexture().height,animPlayer.Hdir);
}
}
void enemyBase::updateState()
{
if(this->hp<0)
{
state = EnemyState::DEAD;
}
switch (state)
{
case EnemyState::DEAD:
break;
case EnemyState::IDLE:
animPlayer.setTexture(texture, frameCount, animTime);
break;
case EnemyState::RUNNING:
animPlayer.setTexture(runningTexture, 6, 1);
break;
case EnemyState::ATTACKING:
animPlayer.setTexture(attackTexture,4,0.75);
if(0.75<=GetTime()-lastattacktime)
{
state = EnemyState::IDLE;
}
break;
}
}
void enemyBase::draw()
{
if(alive)
{
base::draw();
DrawTextureRec(animPlayer.getTexture(), animPlayer.getAnimatedframe(), pos, tint);
//DrawText(TextFormat("Dir: %.2f, %.2f", dir.x,dir.y), playerptr.pos.x,playerptr.pos.y,2, BLACK);
//DrawRectangleLinesEx(Cboxes.hurtbox,2,BLUE);
//DrawRectangleLinesEx(Cboxes.nxtFrameBox,2,GREEN);
//DrawRectangleLinesEx(Cboxes.hitbox,2,RED);
DrawText(TextFormat("HP: %.0f /100", hp), pos.x,pos.y, 20, RED);
}
// Draw the player on the screen
}
void enemyBase::lockon()
{
Vector2 dir = Vector2Subtract(playerptr.pos, this->pos);
float distance = Vector2Length(dir);
if (distance > 0.0f && distance < 350.0f)
{
if(isColliding)
{
this->dir={0,0};
this->state=EnemyState::IDLE;
}
else
{
this->dir=Vector2Normalize(dir);
this->state = EnemyState::RUNNING;
this->animPlayer.Hdir=((dir.x <= 0.0f) ? 1.0f : -1.0f)*((dir.y <= 0.0f) ? 1.0f : -1.0f)*((dir.x!=0.0f&&dir.y>0) ? 1.0f :-1.0f);
}
}
else
{
this->dir={0,0};
this->state = EnemyState::IDLE;
}
}