-
Notifications
You must be signed in to change notification settings - Fork 1
/
Weapon.cpp
188 lines (174 loc) · 4.17 KB
/
Weapon.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "GameEngine.h"
#include "Weapon.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "Util.h"
Weapon::Weapon(WeaponType weaponType , int BulletFireLimit,float RepeatRate)
:bulletFireLimit(BulletFireLimit), repeatRate(RepeatRate)
{
directory = "Integral.PNG";
missleColider = "enemy";
damage = 1;
range = 1;
speed = 1;
SCREEN_WIDTH = 800;
SCREEN_HEIGHT = 600;
if(!SetWeapon(weaponType))
std::cout<<"Wrong weapon type \n";
missle = new Missle*[bulletFireLimit];
for(int i=0;i<bulletFireLimit;i++)
{
missle[i] = new Missle(directory,missleColider,(float)range,(float)speed);
//std::cout<<" Missle["<<i<<"] Loaded \n"; //dla celow testowych
}
fired = false;
active = false;
//std::cout<<"Konstruktor weapon online! \n"; //dla celow testowych
}
Weapon::~Weapon(void)
{
for ( int i = 0; i < bulletFireLimit; i++ )
delete missle[i];
delete[] missle;
}
void Weapon::UpdateCollision()
{
for(int i=0;i<bulletFireLimit;i++)
{
missle[i]->UpdateCollision();
}
}
void Weapon::Logic(bool FiringLocked, sf::Vector2i target)
{
//FiringLocked - if weapon is firing constantly
if( active == true )
{
if( FiringLocked && fired == true)
{
if( repetition.GetElapsedTime() > repeatRate )
{
repetition.Reset();
destenation.x = target.x - (int)fireFromPosition.x;
destenation.y = target.y - (int)fireFromPosition.y;
destenation.x += (-error/2) + rand()%error;
destenation.y += (-error/2) + rand()%error;
distanceFromMouse=sqrt((float)(destenation.x)*(destenation.x)+(float)(destenation.y)*(destenation.y));
if( missle[ReturnFirstAvailable(missle,bulletFireLimit)]->inMove == false )
{
missle[ReturnFirstAvailable(missle,bulletFireLimit)]->StartPosition(fireFromPosition);
missle[ReturnFirstAvailable(missle,bulletFireLimit)]->SetTarget(destenation,distanceFromMouse);
missle[ReturnFirstAvailable(missle,bulletFireLimit)]->inMove = true;
}
}
}
if( FiringLocked && fired == false) //First blood
{
if( repetition.GetElapsedTime() > repeatRate )
{
repetition.Reset();
destenation.x = target.x - (int)fireFromPosition.x;
destenation.y = target.y - (int)fireFromPosition.y;
distanceFromMouse=sqrt((float)(destenation.x)*(destenation.x)+(float)(destenation.y)*(destenation.y));
missle[0]->StartPosition(fireFromPosition);
missle[0]->SetTarget(destenation,distanceFromMouse);
missle[0]->inMove = true;
fired = true;
}
}
fired = false;
active = false;
}
for(int i = 0;i<bulletFireLimit;i++)
{
if(missle[i]->inMove == true)
{
missle[i]->Logic();
active = true;
fired = true;
}
}
}
void Weapon::Display(sf::RenderWindow *window)
{
for(int i =0; i < bulletFireLimit ; i++)
{
missle[i]->Display(window);
}
}
int Weapon::ReturnFirstAvailable(Missle **missle,int MissleAmount )
{
for(int i = 0 ; i < MissleAmount ; i++)
{
if(missle[i]->inMove == false)
{
return i;
}
}
return 0;
}
void Weapon::SetFiringPosition(sf::Vector2f Position)
{
fireFromPosition = Position;
}
void Weapon::PutScreenSize(int _SCREEN_WIDTH, int _SCREEN_HEIGHT)
{
SCREEN_WIDTH = _SCREEN_WIDTH;
SCREEN_HEIGHT = _SCREEN_HEIGHT;
}
int Weapon::SetWeapon(WeaponType Weapon)
{
switch(Weapon)
{
case Integral:
directory = "Integral.PNG";
damage = 10;
range = 300;
speed = 4.0;
repeatRate = 0.5;
bulletFireLimit = 5;
error = 1;
break;
case Derivative:
directory = "Derivative.PNG";
damage = 1;
range = 300;
speed = 8.0;
repeatRate = 0.2;
bulletFireLimit = 5;
error = 25;
break;
case Plus:
directory = "Plus.PNG";
damage = 50;
range = 300;
speed = 5.0;
repeatRate = 0.25;
bulletFireLimit = 5;
error = 5;
break;
case Minus:
directory = "Minus.PNG";
damage = 5;
range = 300;
speed = 5.0;
repeatRate = 0.25;
bulletFireLimit = 5;
error = 10;
break;
case YellowBall:
directory = "YellowDeathBall.PNG";
missleColider = "Hero";
damage = 5;
range = 300;
speed = 5.0;
repeatRate = 0.25;
bulletFireLimit = 3;
error = 50;
break;
default:
return 0;
break;
}
return 1;
}