-
Notifications
You must be signed in to change notification settings - Fork 1
/
Missle.cpp
108 lines (96 loc) · 3.25 KB
/
Missle.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
#include "Missle.h"
#include "GameEngine.h"
#include "Sfx/SoundPlayer.h"
Missle::Missle(std::string fileName, std::string _missleColider, float Range ,float Velocity )
:range(Range), velocity(Velocity),missleColider(_missleColider)
{
myTexture = ImageManager::getInstance()->loadImage( "Data/Textures/Weapons/"+fileName );
myTexture.CreateMaskFromColor(sf::Color(255,0,255));
mySprite.SetImage(myTexture);
mySprite.SetPosition(0,0);
mySprite.SetCenter(mySprite.GetSize().x/2,mySprite.GetSize().y/2);
mySprite.setCircleMask((int)mySprite.GetCenter().x, (int)mySprite.GetCenter().y, 4);
mySprite.setType(fileName);
angle = 0;
inMove = false;
colisionWithiObiect = false;
}
Missle::~Missle(void)
{
}
void Missle::UpdateCollision()
{
GameEngine::getInstance()->AddToCollisionQuadtree(&mySprite);
}
void Missle::Logic()
{
inMove = false;
colisionWithiObiect = false;
currentPosition = mySprite.GetPosition();
currentDistance = sqrt((startPosition.x-currentPosition.x)*(startPosition.x-currentPosition.x)
+ (startPosition.y-currentPosition.y)*(startPosition.y-currentPosition.y));
if( GameEngine::getInstance()->DetectCollision(&mySprite,missleColider) )
{
colisionWithiObiect = true;
if ( missleColider == "enemy" )
SoundPlayer::getInstance()->Play(Snd::Shoot);
}else
if( GameEngine::getInstance()->DetectCollision(&mySprite,"wall") )
{
colisionWithiObiect = true;
SoundPlayer::getInstance()->Play(Snd::Shoot);
}
if(currentDistance < range)
{
if(targetPosition.x > 0)
angle = (float)( 90 + asin ( ( ( (-targetPosition.y)/distanceFromTarget))) * (180.0 / PI) );
else
angle = (float)( 90 + asin ( ( ( (+targetPosition.y)/distanceFromTarget))) * (180.0 / PI) );
mySprite.SetRotation(angle );
mySprite.Move(velocity * (targetPosition.x)/distanceFromTarget, velocity * (targetPosition.y ) / distanceFromTarget);
if ( !colisionWithiObiect ) inMove = true;
}
if( inMove == false )
{
currentPosition.x = 0.0;
currentPosition.y = 0.0;
mySprite.SetPosition( currentPosition );
}
}
void Missle::SetTarget(sf::Vector2i DesignatedPosition,float DistanceFromMouseClick )
{
targetPosition = DesignatedPosition;
distanceFromTarget = DistanceFromMouseClick;
}
void Missle::StartPosition(sf::Vector2f Position)
{
mySprite.SetPosition(Position.x,Position.y);
startPosition = Position;
}
void Missle::Display(sf::RenderWindow *window)
{
if(inMove != false) //ukrywanie po skonczonym biegu
window->Draw( mySprite );
if(GameEngine::getInstance()->devmode)
{
if ( GameEngine::getInstance()->DetectCollision(&mySprite) )
{
((CircleMask*)mySprite.getCollisionMask())->Display(window,
sf::Vector2f(mySprite.GetPosition().x-mySprite.GetCenter().x,
mySprite.GetPosition().y-mySprite.GetCenter().y),
sf::Color(255, 0, 0));
}
else
((CircleMask*)mySprite.getCollisionMask())->Display(window,
sf::Vector2f(mySprite.GetPosition().x-mySprite.GetCenter().x,
mySprite.GetPosition().y-mySprite.GetCenter().y));
}
}
void Missle::SetMissleColider( std::string _missleColider )
{
missleColider = _missleColider;
}
float Missle::ReturnAngle()
{
return angle;
}