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

146 lines
3.8 KiB
C++

#include "classicgame.h"
#include "classictimeline.h"
#include "classicnote.h"
#include "classicmapcreator.h"
#include "classicnotemanager.h"
#include "tools/music.h"
#include <iostream>
ClassicGame::ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager, std::unique_ptr<Music>&& music) :
_timeline(std::make_unique<ClassicTimeline>()),
_graphics_manager(std::move(manager)),
_note_manager(std::make_unique<ClassicNoteManager>(_graphics_manager)),
_music(std::move(music)),
_is_paused(false)
{
_slap_buffer.loadFromFile("very-final-slap.wav");
_slap.setBuffer(_slap_buffer);
_slap.setVolume(50);
_keys_to_buttons =
{
{sf::Keyboard::Up, Type::UP}, // Load from settings
{sf::Keyboard::Right, Type::RIGHT},
{sf::Keyboard::Down, Type::DOWN},
{sf::Keyboard::Left, Type::LEFT},
{sf::Keyboard::W, Type::UP},
{sf::Keyboard::D, Type::RIGHT},
{sf::Keyboard::S, Type::DOWN},
{sf::Keyboard::A, Type::LEFT},
{sf::Keyboard::E, Type::SLIDER_RIGHT},
{sf::Keyboard::Q, Type::SLIDER_LEFT}
};
_buttons_to_pressed_actions=
{
{Type::UP, Action::PRESS_UP},
{Type::RIGHT, Action::PRESS_RIGHT},
{Type::DOWN, Action::PRESS_DOWN},
{Type::LEFT, Action::PRESS_LEFT},
{Type::SLIDER_RIGHT, Action::PRESS_SLIDER_RIGHT},
{Type::SLIDER_LEFT, Action::PRESS_SLIDER_LEFT}
};
_buttons_to_released_actions=
{
{Type::UP, Action::RELEASE_UP},
{Type::RIGHT, Action::RELEASE_RIGHT},
{Type::DOWN, Action::RELEASE_DOWN},
{Type::LEFT, Action::RELEASE_LEFT},
{Type::SLIDER_RIGHT, Action::RELEASE_SLIDER_RIGHT},
{Type::SLIDER_LEFT, Action::RELEASE_SLIDER_LEFT}
};
}
ClassicGame::~ClassicGame()
{}
void ClassicGame::run()
{
ClassicMapCreator creator(_graphics_manager);
auto beatmap = creator.createBeatmap("aa");
_music->openFromFile("METEOR.flac");
_music->setVolume(10);
_music->play();
_timeline->run(std::move(beatmap.notes), beatmap.visibility_offset);
}
void ClassicGame::input(PlayerInput&& inputdata)
{
inputdata.timestamp = _music->fetchOffset();
switch (inputdata.event.type)
{
default:
return;
break;
case sf::Event::KeyPressed:
{
if (inputdata.event.key.code == sf::Keyboard::Space)
{
if (!_is_paused)
{
_is_paused = true;
_music->pause();
return;
}
else
{
_is_paused = false;
_music->play();
return;
}
}
auto note_it = _timeline->getActiveNote();
if (!_timeline->isExpired(note_it))
{
auto note = (*note_it);
_note_manager->input(note, std::move(inputdata));
_slap.play();
if (note->hold && _note_manager->allElementsPressed(note)) // also check for Type
{
_notes_on_hold.emplace_back(note);
std::cout << "HOLD initited by " << inputdata.event.key.code << '\n';
}
}
}
break;
case sf::Event::KeyReleased:
{
bool key_match = std::any_of(_notes_on_hold.begin(), _notes_on_hold.end(),
[key=inputdata.event.key.code](const auto& note)
{
return note->isPressedAs(key);
});
if (key_match)
{
_notes_on_hold.clear();
std::cout << "HOLD released by " << inputdata.event.key.code << '\n';
}
}
break;
}
}
void ClassicGame::update()
{
_timeline->update(_music->fetchOffset());
_timeline->fetchVisibleNotes();
}
void ClassicGame::draw() const
{
_timeline->drawVisibleNotes();
}