-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
160 lines (103 loc) · 2.54 KB
/
Copy pathEnemy.cpp
File metadata and controls
160 lines (103 loc) · 2.54 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "Enemy.h"
Enemy::Enemy() {
prev_pos = prev_vel = acc = vel = pos = { 0.0f, 0.0f, 0.0f };
col = { 0.0f, 0.0f, 0.0f, 0.0f };
UpdateCollision();
ev = { 0 };
m_basicType = GD_BT_ENEMY;
}
void Enemy::Create(UINT tex, UINT vShader, UINT pShader) {
sprite.AssignResources(tex, vShader, pShader);
sprite.SetSourceRect(2);
sprite.SetDimensions(0.5f, 0.5f);
sprite.Create();
// dot stuff
float xW = 0.5f;
float yH = 0.5f;
float pOff = 0.002f;
ps1.m_numPoints = 5;
ps1.m_points = new XMFLOAT3[ps1.m_numPoints];
ps1.m_points[0] = { -xW - pOff,0.0f,0.0f };
ps1.m_points[1] = { 0.0f,yH + pOff,0.0f };
ps1.m_points[2] = { xW + pOff,0.0f,0.0f };
ps1.m_points[3] = { 0.0f, -yH - pOff,0.0f };
ps1.m_points[4] = { 0.0f, 0.0f,0.0f };
ps1.Create(ps1.m_points, 5);
}
void Enemy::Update(double deltaTime) {
if (!m_Alive)return;
prev_animState = animState;
if (ev.collidingBelow)vel.y = 0.0f;
if (ev.collidingAbove)vel.y = 0.0f;
if (ev.collidingRight)xdir = -1;
if (ev.collidingLeft)xdir = 1;
vel.x = (deltaTime / 1000) * 3.0f * xdir;
//Animation
if (vel.x > 0)animState = AS_PL_WALKLEFT;
else if (vel.x < 0)animState = AS_PL_WALKRIGHT;
vel.y -= (deltaTime / 1000);
//Final Calcs
Animate(deltaTime);
vel.x *= 0.8;
if (vel.y < -0.5f)vel.y = -0.5f;
MoveBy(vel);
prev_pos = pos;
}
void Enemy::Draw() {
if (m_Alive)sprite.Draw(pos);
ps1.Draw(pos);
}
void Enemy::CollidedWith(int type, int value){
switch (type) {
case GD_BT_MELEE_ATTACK:
m_Alive = false;
break;
case GD_BT_PLAYER:
//m_Alive = false;
break;
}
}
int Enemy::GetType()
{
return 0;
}
void Enemy::Animate(double deltaTime) {
if (animState != prev_animState) { // Instantly play if new animation
curAnimFrame = 0;
sprite.SetSourceRect(AS_PL_FRAMES[animState].frameLoc[curAnimFrame]);
return;
}
elapsedTime += deltaTime;
if (elapsedTime > 300) { //play frame
elapsedTime -= 300;
curAnimFrame++;
if (curAnimFrame >= AS_PL_FRAMES[animState].numFrames)curAnimFrame = 0;
sprite.SetSourceRect(AS_PL_FRAMES[animState].frameLoc[curAnimFrame]);
}
}
void Enemy::UpdateCollision() {
XMFLOAT2 temp = sprite.GetSprWH();
col.x = pos.x - temp.x;
col.y = pos.y + temp.y;
col.z = pos.x + temp.x;
col.w = pos.y - temp.y;
}
void Enemy::SetCollision(XMFLOAT4 c) {
col = c;
}
XMFLOAT4 Enemy::GetCollision() {
UpdateCollision();
return col;
}
void Enemy::MoveBy(XMFLOAT3 p) {
pos.x += p.x;
pos.y += p.y;
pos.z += p.z;
UpdateCollision();
}
void Enemy::MoveTo(XMFLOAT3 p) {
pos.x = p.x;
pos.y = p.y;
pos.z = p.z;
UpdateCollision();
}