You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
project-kyoku/src/classicgame/classicgame.cpp

143 lines
3.8 KiB
C++

#include "classicgame.h"
#include "classicinputtype.h"
#include "classictimeline.h"
#include "classicviewmanager.h"
#include "classicnote.h"
ClassicGame::ClassicGame() :
_timeline(std::make_unique<ClassicTimeline>()),
_view_manager(std::make_unique<ClassicViewManager>())
{
_font.loadFromFile("VeraMono.ttf");
_keys_to_buttons =
{
{sf::Keyboard::Up, Button::UP}, // Load from settings
{sf::Keyboard::Right, Button::RIGHT},
{sf::Keyboard::Down, Button::DOWN},
{sf::Keyboard::Left, Button::LEFT},
{sf::Keyboard::W, Button::UP},
{sf::Keyboard::D, Button::RIGHT},
{sf::Keyboard::S, Button::DOWN},
{sf::Keyboard::A, Button::LEFT},
{sf::Keyboard::E, Button::SLIDER_RIGHT},
{sf::Keyboard::Q, Button::SLIDER_LEFT}
};
_buttons_to_pressed_actions=
{
{Button::UP, Action::PRESS_UP},
{Button::RIGHT, Action::PRESS_RIGHT},
{Button::DOWN, Action::PRESS_DOWN},
{Button::LEFT, Action::PRESS_LEFT},
{Button::SLIDER_RIGHT, Action::PRESS_SLIDER_RIGHT},
{Button::SLIDER_LEFT, Action::PRESS_SLIDER_LEFT}
};
_buttons_to_released_actions=
{
{Button::UP, Action::RELEASE_UP},
{Button::RIGHT, Action::RELEASE_RIGHT},
{Button::DOWN, Action::RELEASE_DOWN},
{Button::LEFT, Action::RELEASE_LEFT},
{Button::SLIDER_RIGHT, Action::RELEASE_SLIDER_RIGHT},
{Button::SLIDER_LEFT, Action::RELEASE_SLIDER_LEFT}
};
}
ClassicGame::~ClassicGame()
{}
void ClassicGame::run()
{
_timeline->fetchVisibleNotes(_view_manager);
_timeline->run();
}
void ClassicGame::input(const sf::Event& event)
{
Action new_action = Action::NONE;
microsec timestamp = _timeline->currentMusicOffset();
switch (event.type)
{
default:
return;
break;
case sf::Event::KeyPressed:
{
if (_keys_to_buttons.find(event.key.code) != _keys_to_buttons.end())
new_action = getActionKeyPressed(_keys_to_buttons[event.key.code]);
}
break;
case sf::Event::KeyReleased:
{
if (_keys_to_buttons.find(event.key.code) != _keys_to_buttons.end())
new_action = getActionKeyReleased(_keys_to_buttons[event.key.code]);
}
break;
}
auto note = _timeline->getActiveNote();
if (!_timeline->isExpired(note) || (*note)->state() != ClassicNote::State::DEAD)
{
auto grade = (*note)->input(ClassicInputType(timestamp, new_action));
sf::Text new_grade;
new_grade.setFillColor(sf::Color::White);
new_grade.setPosition((*note)->getCoordinates().x, (*note)->getCoordinates().y - 40);
switch (grade)
{
case ClassicNote::Grade::PERFECT:
new_grade.setString("PERFECT"); break;
case ClassicNote::Grade::GOOD:
new_grade.setString("GOOD"); break;
case ClassicNote::Grade::BAD:
new_grade.setString("BAD"); break;
}
new_grade.setFont(_font);
_grades.emplace_back(new_grade);
}
}
Action ClassicGame::getActionKeyPressed(Button button) const
{
return _buttons_to_pressed_actions.at(button);
}
Action ClassicGame::getActionKeyReleased(Button button) const
{
return _buttons_to_released_actions.at(button);
}
void ClassicGame::update()
{
_timeline->update();
_timeline->fetchVisibleNotes(_view_manager);
for (auto& grade : _grades)
{
if (grade.getFillColor().a > 20)
{
grade.setFillColor(sf::Color(255, 255, 255, grade.getFillColor().a - 20));
}
}
_grades.remove_if([](const auto& grade) { return grade.getFillColor().a <= 20; });
}
void ClassicGame::draw(sf::RenderWindow& window) const
{
_timeline->drawVisibleNotes(window);
for (auto& grade : _grades)
{
window.draw(grade);
}
}