-
Notifications
You must be signed in to change notification settings - Fork 1
/
Zombie.cpp
125 lines (109 loc) · 2.61 KB
/
Zombie.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
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
#include "Include/Zombie.h"
zombie::zombie()
{
vx = 0;
vy = 0;
size = SCREEN_HEIGHT / 7;
type = 0;
attackTimer = 0;
}
void zombie::init()
{
//set random position
bool ok = false;
int randomX = 0;
int randomY = 0;
while (!ok)
{
randomX = GetRandomInt(size, LEVEL_WIDTH - size, 1);
randomY = GetRandomInt(size, LEVEL_HEIGHT - size, 1);
setPosition(randomX, randomY);
if (calDistance(myPlayer) > SCREEN_WIDTH) //to avoid zombie spawning within the camera view
{
ok = true;
}
}
setRenderPosition(px, py);
//set random type
int randomType = GetRandomInt(0, 8, 1);
//randomType = 0; //temp
if (randomType <= NORMAL_ZOMBIE_CHANCE) //normal zombie
{
type = 0;
health = NORMAL_ZOMBIE_HEALTH;
speed = NORMAL_ZOMBIE_SPEED;
damage = NORMAL_ZOMBIE_DAMAGE;
attackRange = NORMAL_ZOMBIE_ATTACK_RANGE;
attackSpeed = NORMAL_ZOMBIE_ATTACK_SPEED;
}
else if (randomType <= NORMAL_ZOMBIE_CHANCE + FAST_ZOMBIE_CHANCE) //fast zombie
{
type = 1;
health = FAST_ZOMBIE_HEALTH;
speed = FAST_ZOMBIE_SPEED;
damage = FAST_ZOMBIE_DAMAGE;
attackRange = FAST_ZOMBIE_ATTACK_RANGE;
attackSpeed = FAST_ZOMBIE_ATTACK_SPEED;
}
else if (randomType <= NORMAL_ZOMBIE_CHANCE + FAST_ZOMBIE_CHANCE + TANK_ZOMBIE_CHANCE) //tank zombie
{
type = 2;
health = TANK_ZOMBIE_HEALTH;
speed = TANK_ZOMBIE_SPEED;
damage = TANK_ZOMBIE_DAMAGE;
attackRange = TANK_ZOMBIE_ATTACK_RANGE;
attackSpeed = TANK_ZOMBIE_ATTACK_SPEED;
}
}
void zombie::move(gameObject target)
{
currentState = zombieState::WALK;
calRotation(target.px, target.py);
float dirX = 0;
float dirY = 0;
dirX = -cos(rotation * M_PI / 180.0);
dirY = -sin(rotation * M_PI / 180.0);
vx = dirX * speed * deltaTimer.getDeltaTime();
vy = dirY * speed * deltaTimer.getDeltaTime();
px += vx;
py += vy;
setRenderPosition(px, py);
}
bool zombie::attack(player& target)
{
currentState = zombieState::ATTACK;
attackTimer += deltaTimer.getDeltaTime();
if (attackTimer > attackSpeed)
{
attackTimer = 0;
target.health -= damage;
return true;
}
return false;
}
void zombie::hurt()
{
health -= myPlayer.myWeapon[myPlayer.currentWeapon].getDamage();
}
void zombie::setAnimation(LTexture& targetTexture, SDL_Rect& targetClip)
{
currentTexture = &targetTexture;
currentClip = &targetClip;
}
void zombie::render(SDL_Rect& camera)
{
//change color based on the type
switch (type)
{
case 0:
currentTexture->setColor(255, 255, 255);
break;
case 1:
currentTexture->setColor(100, 255, 100);
break;
case 2:
currentTexture->setColor(100, 100, 255);
break;
}
currentTexture->render(rx - camera.x, ry - camera.y, size, size, currentClip, 180 + rotation);
}