69 lines
2.2 KiB
C++
69 lines
2.2 KiB
C++
#include "NoteTile.hpp"
|
|
#include "GameData.hpp"
|
|
#include "NoteSprite.hpp"
|
|
#include <SFML/Graphics/Texture.hpp>
|
|
#include <SFML/System/Vector2.hpp>
|
|
#include <SFML/Window/Window.hpp>
|
|
#include <memory>
|
|
|
|
std::vector<std::unique_ptr<NoteTile>> NoteTile::existing_tiles{};
|
|
// private
|
|
NoteTile::NoteTile(float play_time, float good_interval, NotePlaceEnum place,
|
|
float current_time)
|
|
: play_time(play_time), good_interval(good_interval), place(place),
|
|
note_sprite() {
|
|
note_sprite.fall_speed = 500;
|
|
note_sprite.sprite.setPosition(sf::Vector2f(
|
|
NOTE_PLACE_X_POS[place],
|
|
NOTE_PRESS_HEIGHT - note_sprite.fall_speed * (play_time - current_time)));
|
|
|
|
sf::Vector2u sprite_size = note_sprite.sprite.getTexture().getSize();
|
|
note_sprite.sprite.setOrigin(
|
|
sf::Vector2f(sprite_size.x / 2, sprite_size.y / 2));
|
|
|
|
float s = FLOWER_SIZE / sprite_size.x;
|
|
note_sprite.sprite.setScale(sf::Vector2f(s, s));
|
|
};
|
|
|
|
// public
|
|
void NoteTile::create(float play_time, float good_interval, NotePlaceEnum place,
|
|
float current_time) {
|
|
// Can't use make_unique because constructor is private
|
|
existing_tiles.push_back(std::unique_ptr<NoteTile>(
|
|
new NoteTile(play_time, good_interval, place, current_time)));
|
|
}
|
|
|
|
bool NoteTile::checkPress(float press_time, NotePlaceEnum key_pressed) {
|
|
auto &tiles = NoteTile::existing_tiles;
|
|
for (auto it = tiles.begin(); it != tiles.end(); ++it) {
|
|
const auto ¬e_tile = *it;
|
|
if (note_tile->play_time - note_tile->good_interval / 2 < press_time &&
|
|
note_tile->play_time + note_tile->good_interval / 2 > press_time &&
|
|
note_tile->place == key_pressed) {
|
|
/*printf("good touch \n");*/
|
|
tiles.erase(it);
|
|
return true;
|
|
}
|
|
}
|
|
/*printf("badd touch \n");*/
|
|
return false;
|
|
}
|
|
|
|
bool NoteTile::update(float dtime, sf::RenderWindow &window) {
|
|
bool no_miss = true;
|
|
auto &tiles = NoteTile::existing_tiles;
|
|
|
|
for (auto it = tiles.begin(); it != tiles.end();) {
|
|
auto ¬e_tile = *it;
|
|
|
|
if (note_tile->note_sprite.sprite.getPosition().y > SCREEN_HEIGHT) {
|
|
/*printf("missed tile \n");*/
|
|
no_miss = false;
|
|
it = tiles.erase(it);
|
|
} else {
|
|
note_tile->note_sprite.update(dtime, window);
|
|
++it;
|
|
}
|
|
}
|
|
return no_miss;
|
|
}
|