-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cpp
95 lines (81 loc) · 1.61 KB
/
Enemy.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
#include "Enemy.h"
#include "Bullet.h"
#include <iostream>
Enemy::Enemy() : GameObject()
{
tag = Tag::EnemyTag;
}
Enemy::Enemy(glm::vec2 pos, Texture2D sprite, glm::vec3 color) :
GameObject(pos, glm::vec2(50.0f, 50.0f), sprite, color), timeMove(0.5f), stepLength(10.0f)
{
tag = EnemyTag;
}
void Enemy::Init()
{
timeCount = timeMove;
stepCount = 0;
moveState = ENEMY_MOVE_RIGHT;
AddObserver(world);
}
void Enemy::Fire()
{
Bullet* bullet = new Bullet(Position + glm::vec2(0.0f, 20.0f), Sprite, glm::vec2(0.0f, 1.0f), BulletFromEnemyTag);
//NOT GOOD
bullet->AddWorld(world);
bullet->Init();
bullet->Spawn();
world->AddGameObject(bullet);
}
void Enemy::AddWorld(nohaGame* world)
{
this->world = world;
}
void Enemy::Update(float dt) {
if (timeCount > 0.0f)
{
timeCount -= dt;
}
else {
timeCount = timeMove;
stepCount += 1;
if (stepCount < 4) {
switch (moveState)
{
case ENEMY_MOVE_RIGHT:
Position.x += stepLength*1.5;
break;
case ENEMY_MOVE_RIGHT_DOWN:
Position.y += stepLength;
break;
case ENEMY_MOVE_LEFT:
Position.x -= stepLength*1.5;
break;
case ENEMY_MOVE_LEFT_DOWN:
Position.y += stepLength;
break;
}
}
else
{
stepCount = 0;
switch (moveState)
{
case ENEMY_MOVE_RIGHT:
moveState = ENEMY_MOVE_RIGHT_DOWN;
break;
case ENEMY_MOVE_RIGHT_DOWN:
moveState = ENEMY_MOVE_LEFT;
break;
case ENEMY_MOVE_LEFT:
moveState = ENEMY_MOVE_LEFT_DOWN;
break;
case ENEMY_MOVE_LEFT_DOWN:
moveState = ENEMY_MOVE_RIGHT;
break;
}
}
}
if (Position.y > SCREEN_HEIGHT - 200.0f) {
notify(this, PLAYERLOSE);
}
}