-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLauncherSystem.cpp
More file actions
161 lines (143 loc) · 7.46 KB
/
Copy pathLauncherSystem.cpp
File metadata and controls
161 lines (143 loc) · 7.46 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
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
#include "LauncherSystem.h"
#include <iostream>
#include <sstream>
void LauncherSystem::Update(){
EntityStream es = engine_->GetEntityStream();
std::set<Entity*> entities;
entities = es.WithTag(Component::MISSILEQUEUE);
if(engine_->GetContext().LoadNextMissile){
UpdateQueue(entities);
}
Point mouseInput = engine_->mouseinput;
Engine::KEY_PRESSED keyInput = engine_->keyInput;
for(Entity* entity:entities){
Component* component = entity->GetComponent(Component::MISSILEQUEUE); //selects all the entities who are missiles in the queue
MissileQueueComponent* mqc = dynamic_cast<MissileQueueComponent*>(component);
if(mqc->queuenumber==0){ //selects the missile who is curently in the launcher
Component* component = entity->GetComponent(Component::POSITION);
PositionComponent* pc= dynamic_cast<PositionComponent*>(component);
//NON REPLAY CASE
if(engine_->GetContext().replay == false){
engine_->GetContext().missiles.push_back(std::to_string(mouseInput.x_) += std::string(" ") += std::to_string(mouseInput.y_)); //puts the mousinput in the vector missile for replay purposes later on
if(MissileSelected(entity,mouseInput,keyInput)){ //checking if the missile in the launcher is selected
engine_->GetContext().missiles.push_back(std::string("START"));
mqc->selected = true;
}
else if(mqc->selected == true && keyInput == Engine::KEY_MOUSE_UP){ //checking if the missile was selected and now released so we can launch him
mqc->selected = false;
engine_->GetContext().missiles.push_back(std::string("END"));
engine_->GetContext().missiles.push_back(std::to_string(mouseInput.x_) += std::string(" ") += std::to_string(mouseInput.y_));
LaunchMissile(entity,mqc,mouseInput);
}
else if(mqc->selected == true){
pc->position = (ConvertMouse(mouseInput)+Point(120,230))/2;
}
}
//REPLAY CASE, the same as the non replay case but,
//we read from a vector missiles as replacement for the mouseinput
else{
if(engine_->GetContext().missiles.size() == 0){
std::cerr << "Replay failed: likely because of a desync between the replay and the original game. The highscorefile might be invalid" << std::endl;
exit(1);
}
std::string s = engine_->GetContext().missiles[0];
engine_->GetContext().missiles.erase(engine_->GetContext().missiles.begin());
if(s == std::string("START")){
mqc->selected = true;
}
else if(s == std::string("END")){
std::string s = engine_->GetContext().missiles[0];
engine_->GetContext().missiles.erase(engine_->GetContext().missiles.begin());
mqc->selected = false;
std::stringstream ss;;
ss << s;
double x;
double y;
ss >> x;
ss >> y;
mouseInput.x_ = x;
mouseInput.y_ = y;
LaunchMissile(entity,mqc,mouseInput);
}
else if(mqc->selected == true){
std::stringstream ss;;
ss << s;
double x;
double y;
ss >> x;
ss >> y;
mouseInput.x_ = x;
mouseInput.y_ = y;
//std::cout << x << " " << y << std::endl;
pc->position = (ConvertMouse(mouseInput)+Point(120,230))/2;
}
}
}
}
}
Point LauncherSystem::ConvertMouse(Point p){
//Mouseinput naar standaard coord + spriteoffset
return Point(p.x_-(MISSILE_DST_HEIGHT/2),SCREEN_HEIGHT-(p.y_-(MISSILE_DST_HEIGHT/2)));
}
bool LauncherSystem::MissileSelected(Entity* entity,Point mouseInput,Engine::KEY_PRESSED keyInput){
PositionComponent* pc= dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
//Cirkel hitbox, checking if there is a mouseclick in the hitbox of missile 1
if((ConvertMouse(mouseInput))*(pc->position)<=(MISSILE_DST_HEIGHT/2) && keyInput == Engine::KEY_MOUSE_DOWN && entity->HasComponent(Component::MISSILE1)){
return true;
}
//rectangle(no square) hitbox, checking if there is a mouseclick in the hitbox of missile 2
else if(abs((ConvertMouse(mouseInput)).x_-(pc->position).x_)<=(MISSILE_DST_WIDTH/2) && (((0<=((ConvertMouse(mouseInput)).y_)-((pc->position).y_)) && (((ConvertMouse(mouseInput)).y_)-((pc->position).y_)<=MISSILE_DST_HEIGHT/2-8))||(((-MISSILE_DST_HEIGHT/2+4)<=((ConvertMouse(mouseInput).y_)-(pc->position).y_))&&(((ConvertMouse(mouseInput).y_)-(pc->position).y_)<=0))) && keyInput == Engine::KEY_MOUSE_DOWN && entity->HasComponent(Component::MISSILE2)){
return true;
}
//triangle hitbox, checking if there is a mouseclick in the hitbox of missile 3
else if(abs((ConvertMouse(mouseInput)).x_-(pc->position).x_)<=abs((ConvertMouse(mouseInput)).y_-(pc->position).y_-MISSILE_DST_HEIGHT/2)/2 && abs((ConvertMouse(mouseInput)).y_-(pc->position).y_)<=(MISSILE_DST_HEIGHT/2) && keyInput == Engine::KEY_MOUSE_DOWN && entity->HasComponent(Component::MISSILE3) ){
return true;
}
else{
return false;
}
}
void LauncherSystem::LaunchMissile(Entity* entity,MissileQueueComponent* mqc,Point mousepos){
//launches the missile and plays a sound when launched
//deletes the MissileQueueComponent and adds a CurrentMissileComponent to the entity
//it gives the entity also a given based on displacement of the elastic of the launcher
ak_->PlayLaunchSound();
engine_->RemoveEntity(entity);
entity->Remove(mqc);
double vx = std::min(LAUNCH_STRENGTH*(140 - ConvertMouse(mousepos).x_),1200.0);
double vy = std::min(LAUNCH_STRENGTH*(220 - ConvertMouse(mousepos).y_),1200.0);
std::cout << vx <<" "<< vy <<std::endl;
entity->Add(new CurrentMissileComponent(vx,vy));
engine_->AddEntity(entity);
}
void LauncherSystem::UpdateQueue(std::set<Entity*> entities){
//updates the queue when a missile was launched and then hit targets or went out of the boundaries
//adds a new missile entity to the queue
engine_->GetContext().missileCounter += 1;
for(Entity* entity:entities){
MissileQueueComponent* mqc = dynamic_cast<MissileQueueComponent*>(entity->GetComponent(Component::MISSILEQUEUE));
PositionComponent* mc = dynamic_cast<PositionComponent*>(entity->GetComponent(Component::POSITION));
mqc->queuenumber -= 1;
if(mqc->queuenumber==0){
mc->position = Point(140,220);
}
else{
mc->position = Point(80-40*(mqc->queuenumber-1),125);
}
}
Entity* me = new Entity;
me->Add(new PositionComponent(Point(0,125)));
int r = rand()%3;
if(r == 0){
me->Add(new Missile1Component());
}
else if(r == 1){
me->Add(new Missile2Component());
}
else if(r == 2){
me->Add(new Missile3Component());
}
me->Add(new MissileQueueComponent());
engine_->AddEntity(me);
engine_->GetContext().LoadNextMissile = false;
}