-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.cpp
More file actions
86 lines (70 loc) 路 2.08 KB
/
Copy pathBullet.cpp
File metadata and controls
86 lines (70 loc) 路 2.08 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
#include <headers/BoxCollider.h>
#include <headers/Bullet.h>
#include <headers/PhysicManager.h>
Bullet::Bullet(bool isFriendly) : PhysicEntity() {
mTimer = Timer::Instance();
mTexture = new AnimatedTexture("Bullet/bullet.png", 11 * 32, 0, 32, 32, 4,
0.2f, AnimatedTexture::horizontal);
mTexture->Parent(this);
mTexture->Scale(Vector2(0.25f, 0.25f));
mTexture->Pos(VEC2_ZERO);
mTexture->WrapMode(AnimatedTexture::loop);
mSpeed = 250.0f;
mDirection = up;
Reload();
AddCollider(new BoxCollider(mTexture->ScaledDimensions() - VEC2_ONE * 10));
if (isFriendly) {
mId = PhysicManager::Instance()->RegisterEntity(
this, PhysicManager::CollisionLayers::FriendlyProjectiles);
} else {
mId = PhysicManager::Instance()->RegisterEntity(
this, PhysicManager::CollisionLayers::HostileProjectiles);
}
}
Bullet::~Bullet() {
mTimer = NULL;
delete mTexture;
mTexture = NULL;
}
void Bullet::Fire(Vector2 pos, DIRECTION dir) {
Pos(pos);
Active(true);
mDirection = dir;
}
void Bullet::Hit(PhysicEntity* other) { Reload(); }
void Bullet::Reload() { Active(false); }
void Bullet::Update() {
if (Active()) {
switch (mDirection) {
case up:
AbsoluteRotate(90.0f);
Translate(VEC2_UP * mSpeed * mTimer->DeltaTime(), world);
break;
case down:
AbsoluteRotate(-90.0f);
Translate(-VEC2_UP * mSpeed * mTimer->DeltaTime(), world);
break;
case left:
AbsoluteRotate(0.0f);
Translate(-VEC2_RIGHT * mSpeed * mTimer->DeltaTime(), world);
break;
case right:
AbsoluteRotate(180.0f);
Translate(VEC2_RIGHT * mSpeed * mTimer->DeltaTime(), world);
break;
}
if (Pos().x < -OFFSCREEN_BUFFER ||
Pos().x > Graphics::Instance()->SCREEN_WIDTH + OFFSCREEN_BUFFER ||
Pos().y < -OFFSCREEN_BUFFER ||
Pos().y > Graphics::Instance()->SCREEN_HEIGHT + OFFSCREEN_BUFFER) {
Reload();
}
mTexture->Update();
}
}
void Bullet::Render() {
if (Active()) {
mTexture->Render();
PhysicEntity::Render();
}
}