/
CLaser.cpp
102 lines (84 loc) · 2.74 KB
/
CLaser.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
#include "CLaser.h"
#include "CProjectile.h"
#include "CMatrix4x4.h"
CLaser::CLaser(IEntity* parent, int damage, float fireRate) : CWeapon(parent)
{
SetDamage(damage);
SetFireRate(fireRate);
SetProjSpeed(1000000.0f); //This is so that the projectile are instantly off screen the frame after being fired
mpBeamMesh = gEngine->LoadMesh(PARTICLE_MODEL);
mpLaserModel = 0;
}
void CLaser::Fire()
{
//Get matrix for current player location and forward vector
tlx::CMatrix4x4 matrix;
GetParent()->GetMatrix(matrix.m);
//Create forward vector (z axis)
CVector3 direction(matrix.m[8], matrix.m[9], matrix.m[10]);
//Get position from matrix
//Since we've already got the matrix it's slightly more efficient than calling GetParent()->GetCenterPoint()
CVector3 pos(matrix.m[12], OFF_SCREEN_Y, matrix.m[14]);
//Calculate the rotation of the parent entity, just so it only needs to be calculated once
float rotation = GetParent()->GetRotation();
//Makes the space between invisible bullets 2.0f instead of 1.0f
direction *= 2.0f;
for (int i = 0; i < 70; i++)
{
CProjectile* bullet = new CProjectile();
bullet->SetPosition(pos);
bullet->SetRotation(rotation);
bullet->SetDamage(GetDamage());
bullet->SetSpeed(GetProjSpeed());
bullet->SetParent(GetParent());
bullet->SetExplodeable(false);
GetBulletList()->push_back(unique_ptr<CProjectile>(bullet));
//Increment position of next bullet
pos += direction;
}
}
void CLaser::SetFiring(bool isFiring)
{
if (isFiring != IsFiring())
{
//State has changed
if (isFiring)
{
//Now firing, so add laser beam
mpLaserModel = gEngine->GetModel(mpBeamMesh, LASER_BEAM_TEX);
mpLaserModel->SetPosition(0.5f, 0.0f, 50.0f);
mpLaserModel->AttachToParent(GetParent()->GetModel());
//Attaching to parent can screw over the scaling, so reset scale
mpLaserModel->ResetScale();
mpLaserModel->ScaleZ(100.0f);
mpLaserModel->ScaleX(3.0f);
}
else if(mpLaserModel)
{
//No longer firing, remove laser beam
mpLaserModel->DetachFromParent();
gEngine->CacheModel(mpLaserModel, LASER_BEAM_TEX);
mpLaserModel = 0;
}
}
else if (isFiring) //Still firing, ensure laser is attached to parent's current model
{
mpLaserModel->DetachFromParent();
mpLaserModel->AttachToParent(GetParent()->GetModel());
//Attaching to parent can screw over the scaling, so reset scale
mpLaserModel->ResetScale();
mpLaserModel->ScaleZ(100.0f);
mpLaserModel->ScaleX(3.0f);
}
CWeapon::SetFiring(isFiring);
}
CLaser::~CLaser()
{
if (mpLaserModel)
{
//Remove the laser model and cache it
mpLaserModel->DetachFromParent();
gEngine->CacheModel(mpLaserModel, LASER_BEAM_TEX);
mpLaserModel = 0;
}
}