forked from VivianMochi/Crescendo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_state.cpp
130 lines (106 loc) · 3.2 KB
/
menu_state.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
#include "menu_state.hpp"
#include "level_state.hpp"
#include <cstdlib>
MenuState::MenuState() {
}
MenuState::~MenuState() {
for (Particle *particle : particles) {
delete particle;
}
}
void MenuState::init() {
sf::Texture &bg = loadTexture("img/menu.png");
bg.setRepeated(true);
background.setTexture(bg);
title.setTexture(loadTexture("img/title.png"));
title.setTextureRect(sf::IntRect(0, 0, 180, 40));
title.setPosition(120 - 90, 0);
text = BitmapText(loadTexture("img/font.png"), "Press Enter to begin");
text.setPosition(51, 80);
creditText = BitmapText(loadTexture("img/font.png"), "A Game by Nate Largo");
creditText.setPosition(50, 125);
loading = BitmapText(loadTexture("img/font.png"), "Loading...");
loading.setColor(sf::Color::White);
loading.setPosition(183, 125);
elevator.setTexture(loadTexture("img/elevator.png"));
elevator.setTextureRect(sf::IntRect(0, 0, 40, 135));
elevator.setPosition(-40, 0);
startSound.setBuffer(loadSoundBuffer("snd/start.wav"));
music.openFromFile("mus/menu.ogg");
music.setLoop(true);
music.play();
}
void MenuState::gotEvent(sf::Event event) {
if (event.type == sf::Event::KeyPressed) {
if (menuState == 0 && event.key.code == sf::Keyboard::Return) {
menuState = 1;
beatCounter = 2;
startSound.play();
}
}
}
void MenuState::update(sf::Time elapsed) {
if (menuState == 0) {
beatCounter += elapsed.asSeconds();
if (beatCounter >= 120.f / 130.f) {
beatCounter -= 120.f / 130.f;
}
backgroundOffset += elapsed.asSeconds() * (1 - beatCounter) * 25;
background.setTextureRect(sf::IntRect(0, backgroundOffset, 240, 135));
createParticle(sf::Vector2f(54 + std::rand() % 131, 10 + std::rand() % 20), sf::Vector2f(std::rand() % 5 - 2, 8 + std::rand() % 4), 1.6 + std::rand() % 10 * .1);
}
else {
beatCounter -= elapsed.asSeconds();
if (beatCounter <= -.1) {
game->changeState(new LevelState(1));
}
if (beatCounter > 0) {
background.setScale(beatCounter / 2, 1);
background.setPosition(120 * (1 - beatCounter / 2), 0);
elevator.setPosition(-40 * std::pow(0.09, 2 - beatCounter), 0);
music.setVolume(100 * beatCounter / 2);
}
else {
elevator.setPosition(0, 0);
music.stop();
}
}
// Update particles
for (int i = particles.size() - 1; i >= 0; i--) {
particles[i]->life -= elapsed.asSeconds();
if (particles[i]->life <= 0) {
delete particles[i];
particles.erase(particles.begin() + i);
}
else {
particles[i]->sprite.move(particles[i]->velocity * elapsed.asSeconds());
}
}
}
void MenuState::render(sf::RenderWindow &window) {
if (menuState == 0 || beatCounter > 0) {
window.draw(background);
}
window.draw(title);
if (menuState == 0 && beatCounter >= 60.f / 130.f) {
window.draw(text);
}
window.draw(creditText);
for (Particle *particle : particles) {
window.draw(particle->sprite);
}
if (menuState == 1) {
window.draw(elevator);
if (beatCounter <= 0) {
window.draw(loading);
}
}
}
void MenuState::createParticle(sf::Vector2f position, sf::Vector2f velocity, float lifespan) {
Particle *temp = new Particle();
temp->sprite.setTexture(loadTexture("img/particle.png"));
temp->sprite.setPosition(position);
temp->velocity = velocity;
temp->life = lifespan;
particles.push_back(temp);
}