-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.cpp
106 lines (86 loc) · 2.22 KB
/
Bullet.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
#include "Bullet.h"
#include "Collision.h"
#include "nohaGame.h"
#include "Wall.h"
//Debug
#include <iostream>
Bullet::Bullet(): GameObject(), speed(200.0f), direction(glm::vec2(0.0f, 1.0f)), start(false)
{
}
Bullet::Bullet(glm::vec2 pos, Texture2D sprite, glm::vec2 direction, Tag tag, glm::vec3 color):
GameObject(pos, glm::vec2(20.0f, 20.0f), sprite, color), direction(direction), speed(200.0f), start(false)
{
this->tag = tag;
}
void Bullet::Update(float dt)
{
if (!start) return;
float velocity = speed * dt;
Position += velocity * direction;
GameObject* other = new GameObject();
if (CheckCollisionWithWorld(*other))
{
if (other->tag == EnemyTag && tag == BulletFromPlayerTag)
{
Destroyed = true;
notify(other, Event::ENEMYHIT);
notify(other, Event::DESTROYSGAMEOBJECT);
notify(this, Event::DESTROYSGAMEOBJECT);
}
if (other->tag == PlayerTag && tag == BulletFromEnemyTag)
{
Destroyed = true;
notify(other, Event::DESTROYSGAMEOBJECT);
notify(this, Event::DESTROYSGAMEOBJECT);
}
if (other->tag == BulletFromEnemyTag && tag == BulletFromPlayerTag ||
other->tag == BulletFromPlayerTag && tag == BulletFromEnemyTag
)
{
Destroyed = true;
notify(other, Event::DESTROYSGAMEOBJECT);
notify(this, Event::DESTROYSGAMEOBJECT);
}
if (other->tag == WALL) {
Destroyed = true;
notify(other, Event::WALLHIT);
notify(this, Event::DESTROYSGAMEOBJECT);
}
if (other->tag == BossTag) {
Destroyed = true;
notify(other, Event::BOSSHIT);
notify(this, Event::DESTROYSGAMEOBJECT);
}
}
if (Position.x < 0.0f || Position.x > SCREEN_WIDTH || Position.y < 100.0f || Position.y > SCREEN_HEIGHT) {
Destroyed = true;
notify(this, Event::DESTROYSGAMEOBJECT);
}
}
void Bullet::Init()
{
AddObserver(world);
}
void Bullet::Spawn()
{
start = true;
}
bool Bullet::CheckCollisionWithWorld(GameObject& other_)
{
for (int i = 0; i < world->NumberOfGameObjects; i++) {
if (world->gameObjects[i]) {
GameObject* other = world->gameObjects[i];
if (other->ID != this->ID && !other->Destroyed) {
if (CheckCollision(*this, *other)) {
other_ = *world->gameObjects[i];
return true;
}
}
}
}
return false;
}
void Bullet::AddWorld(nohaGame* world)
{
this->world = world;
}