-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
385 lines (320 loc) · 13.2 KB
/
main.cpp
File metadata and controls
385 lines (320 loc) · 13.2 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <cmath>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <stdio.h>
#include "moon.h"
#include "star.h"
bool isWhite(sf::Image& image, float px, float py){
return image.getPixel(px, py) == sf::Color::White;
}
float getAngle(sf::Vector2f &orig, sf::Vector2i &des) {
return std::atan2(des.y - orig.y, des.x - orig.x)*180/(M_PI);
}
namespace State{
enum state {
Idle, Speaking, Searching, GroupChat, fadeOut
};
}
float distance(const sf::Vector2f &orig, const sf::Vector2f &des) {
return std::sqrt(std::pow(std::abs(des.x-orig.x), 2) + std::pow(std::abs(des.y-orig.y), 2));
}
float getAngle(const sf::Vector2f &orig,const sf::Vector2f &des) {
return std::atan2(des.y - orig.y, des.x - orig.x)*180/(M_PI);
}
int main(){
/* initialize random seed: */
srand (time(NULL));
//SFML OBJECTS
sf::View view;
sf::Event event;
sf::Clock deltaClock;
sf::CircleShape guia;
guia.setRadius(10);
guia.setFillColor(sf::Color(200,200,200,50));
guia.setOrigin(guia.getLocalBounds().width/2, guia.getLocalBounds().height/2);
float deltatime = 0.0;
sf::Font font;
font.loadFromFile("res/font.otf");
int moonSentenceIndex = 0;
std::vector <std::string> moonSentences;
int groupIndex = 0;
std::vector< std::vector <std::pair <float, std::string > > > groups =
{
{
std::pair <float, std::string >(0, "Hi, been a long time!"),
std::pair <float, std::string >(5, "I've missed you so much!")
}
,
{
std::pair <float, std::string >(0, "Glad you are here!"),
std::pair <float, std::string >(6, "Oh, I love you! ^^ ")
}
,
{
}
,
{
std::pair <float, std::string >(0, "Hey boys!"),
std::pair <float, std::string >(6, "You are awesome!"),
std::pair <float, std::string >(10, "So good to see you all!!!")
}
,
{
std::pair <float, std::string >(0, "Loneliness fades away with you"),
std::pair <float, std::string >(6, "We are all good together!")
}
,
{}
,
{}
};
moonSentences.push_back("I'm so alone in this universe... \n Is hard to live this way...");
moonSentences.push_back("I wish someone missed me too...");
moonSentences.push_back("Has to be so nice to be loved...");
moonSentences.push_back("Wish someone would care about me... \n I'm sure is a warm feeling...");
moonSentences.push_back("Friendship... It hurts so much not having it...");
moonSentences.push_back("Are we? I'm invisible to them...");
moonSentences.push_back("But wait...");
moonSentences.push_back("What if everyone was talking about me this whole time?");
moonSentences.push_back("Game made with love \n by Genis Bayona \n at Oxfordhacks 2016");
moonSentences.push_back("Music: Sad Day from \n Royalty Free Music \n Bensound. ");
Moon moon("The life of the moon is so lonely", font);
moon.setOrigin(moon.getLocalBounds().width/2, moon.getLocalBounds().height/2);
std::vector < Star > stars;
State::state currentState = State::Idle;
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), L"The Moon", sf::Style::Resize|sf::Style::Close);
view.reset(sf::FloatRect(0,0,
window.getSize().x, window.getSize().y));
window.setFramerateLimit(30);
std::vector < sf::CircleShape > ministars;
float fadeOutValue = 0;
sf::Music m;
m.openFromFile("res/music.ogg");
m.setLoop(true);
m.play();
//GAME LOOP
while(window.isOpen()){
//Deltatime
deltatime = deltaClock.restart().asSeconds();
//Loop for handling events
while(window.pollEvent(event)){
switch (event.type){
//Close event
case sf::Event::Closed:
window.close();
break;
//KeyPressed event
case sf::Event::KeyPressed:
//Close key
if (event.key.code == sf::Keyboard::Escape) {
window.close();
}
//Default
default:
//Do nothing
break;
}
}
sf::Vector2f groupPosition;
switch(currentState){
case State::Idle:
//std::cout << "idle" << std::endl;
if(moon.textBox.finished()){
//std::cout << "boobs" << std::endl;
if(moonSentenceIndex >= moonSentences.size()) moon.setSentence("Thank you : )");
else moon.setSentence(moonSentences[moonSentenceIndex]);
++moonSentenceIndex;
currentState = State::Speaking;
}
break;
case State::Speaking:
//std::cout << "speaking" << std::endl;
if(moon.textBox.finished()){
currentState = State::Searching;
if(groupIndex < groups.size()){
auto group = groups[groupIndex];
++groupIndex;
int sign1 = -1;
if( rand()%2 == 0) sign1 = 1;
int sign2 = -1;
if( rand()%2 == 0) sign2 = 1;
int sign3 = -1;
if( rand()%2 == 0) sign3 = 1;
int sign4 = -1;
if( rand()%2 == 0) sign4 = 1;
int w_size = static_cast<int>(window.getSize().y);
groupPosition = moon.getPosition() + sf::Vector2f((w_size/5 + rand()%w_size) * sign1,
(w_size/5 + rand()%w_size) * sign2);
for(auto &pair: group){
stars.emplace_back(Star(font, pair.first));
stars[stars.size()-1].setPosition(groupPosition+sf::Vector2f((20 + rand()%150) * sign3,
(20 + rand()%150) * sign4));
stars[stars.size()-1].setSentence(pair.second);
}
}
else {
int size = 20;
stars.emplace_back(Star(font, 2));
stars[stars.size()-1].setPosition(sf::Vector2f(0.0,0.0));
stars[stars.size()-1].setSentence(": )");
stars.emplace_back(Star(font, 3));
stars[stars.size()-1].setPosition(sf::Vector2f(1*size,-1.42*size));
stars[stars.size()-1].setSentence("<3");
stars.emplace_back(Star(font, 3));
stars[stars.size()-1].setPosition(sf::Vector2f(-1*size,-1.42*size));
stars[stars.size()-1].setSentence("<3");
stars.emplace_back(Star(font, 4));
stars[stars.size()-1].setPosition(sf::Vector2f(2*size,-2.84*size));
stars[stars.size()-1].setSentence(": )");
stars.emplace_back(Star(font, 4));
stars[stars.size()-1].setPosition(sf::Vector2f(-2*size,-2.84*size));
stars[stars.size()-1].setSentence("<3");
stars.emplace_back(Star(font, 5));
stars[stars.size()-1].setPosition(sf::Vector2f(1.6*size,-3.9*size));
stars[stars.size()-1].setSentence(": )");
stars.emplace_back(Star(font, 3));
stars[stars.size()-1].setPosition(sf::Vector2f(-1.4*size,-3.9*size));
stars[stars.size()-1].setSentence("( :");
stars.emplace_back(Star(font, 6));
stars[stars.size()-1].setPosition(sf::Vector2f(0*size,-3.1*size));
stars[stars.size()-1].setSentence("<3");
}
}
break;
case State::Searching:
{
////std::cout << "seraching the meaning of life" << moon.getPosition().x << "," <<moon.getPosition().y << std::endl;
bool goToNextState = true;
for(Star &s : stars){
//// std::cout << "star -> " << s.getPosition().x << ";" << s.getPosition().y << std::endl;
if(distance(moon.getPosition(), s.getPosition()) > window.getSize().y/2) goToNextState = false;
}
for(Star &s : stars){
s.update(deltatime);
}
if(goToNextState){
for(Star &s : stars){
s.setCanSpeak(true);
}
currentState = State::GroupChat;
}
break;
}
case State::GroupChat:
{
//std::cout << "groupchating (. Y .)" << std::endl;
for(Star &s : stars){
s.update(deltatime);
}
bool finished = true;
for(Star &s : stars){
if(!s.textBox.finished()) finished = false;
}
if(finished) {
fadeOutValue = 0.0;
currentState = State::fadeOut;
}
break;
}
case State::fadeOut:
if(fadeOutValue <= 1){
fadeOutValue += deltatime* 0.5;
for(Star &s : stars){
s.setColor(sf::Color(255,255,255, 255-255*fadeOutValue));
s.sglow.setColor(sf::Color(255,255,255, 255-255*fadeOutValue));
}
}
else {
stars.clear();
currentState = State::Idle;
}
break;
default:
break;
}
moon.update(deltatime, window);
//Set view values
sf::Vector2f viewPosition = view.getCenter();
view.reset(sf::FloatRect(viewPosition.x, viewPosition.y,
window.getSize().x, window.getSize().y));
sf::Vector2f step;
step.x = (viewPosition.x - moon.getPosition().x+10) *-0.1* deltatime;
step.y = (viewPosition.y - moon.getPosition().y+10) *-0.1* deltatime;
float maxDist = 20;
float dist = distance(viewPosition, moon.getPosition());
sf::Vector2f extra(0.f,0.f);
if( dist > maxDist){
extra.x = (viewPosition.x - moon.getPosition().x+10) *-1* deltatime;
extra.y = (viewPosition.y - moon.getPosition().y+10) *-1* deltatime;
}
view.setCenter(viewPosition + step + extra);
view.setViewport(sf::FloatRect(0,0,1.0f,1.0f));
/*
view.reset(sf::FloatRect(moon.getPosition().x, moon.getPosition().y,
window.getSize().x, window.getSize().y));
viewPosition.y = moon.getPosition().y+10;
viewPosition.x = moon.getPosition().x+10;
view.setCenter(viewPosition);
view.setViewport(sf::FloatRect(0,0,1.0f,1.0f));
*/
//Set window view, draw and display
window.setView(view);
sf::Color c = sf::Color(0,0,0,0.1);
window.clear(c);
for(auto it = ministars.begin(); it != ministars.end();){
if(std::abs(it->getPosition().x - moon.getPosition().x) > window.getSize().x
|| std::abs(it->getPosition().y - moon.getPosition().y) > window.getSize().y
) it = ministars.erase(it);
else ++it;
}
const int maxStars = 50;
if(ministars.size() < maxStars){
for(int i = ministars.size(); i < maxStars; ++i){
sf::Vector2f pos;
pos.x = rand()%window.getSize().x;
pos.x -= window.getSize().x/2;
pos.y = rand()%window.getSize().y;
pos.y -= window.getSize().y/2;
sf::CircleShape c(1,4);
c.setPosition(moon.getPosition() + pos);
c.setFillColor(sf::Color(250,250,250,100));
ministars.push_back(c);
}
}
for(int i = 0; i < maxStars; ++i){
window.draw(ministars[i]);
}
if(stars.size() > 0){
float angle = getAngle(moon.getPosition(), stars[0].getPosition());
//std::cout << "the angle is "<< angle << std::endl;
sf::Vector2f guiaPos;
guia.setFillColor(sf::Color(200,200,200,50));
guiaPos.x = moon.getPosition().x + std::cos(angle*M_PI/180) * (moon.getRadius()*3 + guia.getRadius());
guiaPos.y = moon.getPosition().y + std::sin(angle*M_PI/180) * (moon.getRadius()*3 + guia.getRadius());
guia.setPosition(guiaPos);
window.draw(guia);
guia.setFillColor(sf::Color(0,0,0,255));
guiaPos.x = moon.getPosition().x + std::cos(angle*M_PI/180) * (moon.getRadius()*3 );//+ guia.getRadius());
guiaPos.y = moon.getPosition().y + std::sin(angle*M_PI/180) * (moon.getRadius()*3 );//+ guia.getRadius());
guia.setPosition(guiaPos);
window.draw(guia);
}
for(Star &s : stars){
/*
float dist = std::abs(distance(moon.getPosition(), s.getPosition()));
if(dist < 2*moon.getGlobalBounds().width){
float factor = dist/2.f*moon.getGlobalBounds().width;
s.setColor(sf::Color(255,255,255,int(255*factor)));
}else s.setColor(sf::Color(255,255,255,255));
*/
s.render(window);
//window.draw(s);
}
moon.render(window);
window.display();
}
}