working game (but bad)
This commit is contained in:
parent
dbb296ff5d
commit
5c65b99604
6 changed files with 89 additions and 12 deletions
|
@ -18,7 +18,8 @@ else()
|
|||
endif()
|
||||
|
||||
add_executable(simpleGame
|
||||
Source/Game.cpp Include/Game.hpp Source/Main.cpp
|
||||
Source/Game.cpp Include/Game.hpp Include/GameData.hpp
|
||||
Source/Main.cpp
|
||||
Source/AudioEmitter.cpp Include/AudioEmitter.hpp
|
||||
Source/NoteSprite.cpp Include/NoteSprite.hpp
|
||||
Source/NoteTile.cpp Include/NoteTile.hpp Include/NotePlaceEnum.hpp
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/Graphics/Sprite.hpp>
|
||||
#ifndef BOOK_GAME_HPP
|
||||
#define BOOK_GAME_HPP
|
||||
|
||||
#include "AudioEmitter.hpp"
|
||||
#include <GameData.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
class Game {
|
||||
|
@ -15,6 +18,7 @@ public:
|
|||
|
||||
private:
|
||||
void processEvents(AudioEmitter &audioEmitter);
|
||||
void start();
|
||||
void update(sf::Time elapsedTime);
|
||||
void render();
|
||||
|
||||
|
@ -22,12 +26,17 @@ private:
|
|||
|
||||
static const sf::Time TimePerFrame;
|
||||
|
||||
sf::RenderWindow mWindow{sf::VideoMode({640, 480}), "SFML Application"};
|
||||
sf::RenderWindow mWindow{sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}),
|
||||
"SFML Application"};
|
||||
sf::Texture mTexture;
|
||||
sf::Font mFont;
|
||||
sf::Text mStatisticsText{mFont};
|
||||
sf::Time mStatisticsUpdateTime;
|
||||
|
||||
sf::RectangleShape leftPressZone;
|
||||
sf::RectangleShape middlePressZone;
|
||||
sf::RectangleShape rightPressZone;
|
||||
|
||||
std::size_t mStatisticsNumFrames{0};
|
||||
};
|
||||
|
||||
|
|
12
SimpleGame/src/Include/GameData.hpp
Normal file
12
SimpleGame/src/Include/GameData.hpp
Normal file
|
@ -0,0 +1,12 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
static constexpr unsigned int SCREEN_WIDTH = 1080;
|
||||
static constexpr unsigned int SCREEN_HEIGHT = 720;
|
||||
static constexpr unsigned int NOTE_PRESS_HEIGHT = SCREEN_HEIGHT * 0.9;
|
||||
|
||||
static constexpr unsigned int MIDDLE_WIDTH = SCREEN_WIDTH / 2;
|
||||
static constexpr unsigned int MIDDLE_HEIGHT = SCREEN_HEIGHT / 2;
|
||||
|
||||
static constexpr unsigned int NOTE_PLACE_X_POS[3] = {
|
||||
SCREEN_WIDTH * 3 / 10, SCREEN_WIDTH * 5 / 10, SCREEN_WIDTH * 7 / 10};
|
|
@ -1,9 +1,13 @@
|
|||
#include "Game.hpp"
|
||||
#include "AudioEmitter.hpp"
|
||||
#include "GameData.hpp"
|
||||
#include "NoteSprite.hpp"
|
||||
#include "NoteTile.hpp"
|
||||
#include "TilePattern.hpp"
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
|
@ -14,6 +18,21 @@ Game::Game() {
|
|||
assert(mFont.openFromFile("media/Sansation.ttf"));
|
||||
mStatisticsText.setPosition({5.f, 5.f});
|
||||
mStatisticsText.setCharacterSize(10);
|
||||
|
||||
leftPressZone = sf::RectangleShape(sf::Vector2f(50, 10));
|
||||
middlePressZone = sf::RectangleShape(sf::Vector2f(50, 10));
|
||||
rightPressZone = sf::RectangleShape(sf::Vector2f(50, 10));
|
||||
|
||||
leftPressZone.setPosition(
|
||||
sf::Vector2f(NOTE_PLACE_X_POS[0], NOTE_PRESS_HEIGHT));
|
||||
middlePressZone.setPosition(
|
||||
sf::Vector2f(NOTE_PLACE_X_POS[1], NOTE_PRESS_HEIGHT));
|
||||
rightPressZone.setPosition(
|
||||
sf::Vector2f(NOTE_PLACE_X_POS[2], NOTE_PRESS_HEIGHT));
|
||||
|
||||
leftPressZone.setFillColor(sf::Color::Black);
|
||||
middlePressZone.setFillColor(sf::Color::Black);
|
||||
rightPressZone.setFillColor(sf::Color::Black);
|
||||
}
|
||||
|
||||
void generateTilePatternAndMusic(AudioEmitter &audio_emitter) {
|
||||
|
@ -38,6 +57,7 @@ void Game::run() {
|
|||
AudioEmitter audioEmitter = AudioEmitter();
|
||||
generateTilePatternAndMusic(audioEmitter);
|
||||
bool generated = false;
|
||||
|
||||
mWindow.setFramerateLimit(60);
|
||||
while (mWindow.isOpen()) {
|
||||
|
||||
|
@ -55,6 +75,21 @@ void Game::processEvents(AudioEmitter &audioEmitter) {
|
|||
if (event->is<sf::Event::Closed>()) {
|
||||
audioEmitter.audioEnd();
|
||||
mWindow.close();
|
||||
break;
|
||||
}
|
||||
if (const auto *keyPressed = event->getIf<sf::Event::KeyPressed>()) {
|
||||
sf::Keyboard::Scancode key_scan_code = keyPressed->scancode;
|
||||
switch (key_scan_code) {
|
||||
case sf::Keyboard::Scancode::Left:
|
||||
NoteTile::checkPress(audioEmitter.getTime(), NotePlaceEnum::Left);
|
||||
break;
|
||||
case sf::Keyboard::Scancode::Up:
|
||||
NoteTile::checkPress(audioEmitter.getTime(), NotePlaceEnum::Middle);
|
||||
break;
|
||||
case sf::Keyboard::Scancode::Right:
|
||||
NoteTile::checkPress(audioEmitter.getTime(), NotePlaceEnum::Right);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,6 +98,9 @@ void Game::update(const sf::Time elapsedTime) {}
|
|||
|
||||
void Game::render() {
|
||||
mWindow.clear(sf::Color::Yellow);
|
||||
mWindow.draw(leftPressZone);
|
||||
mWindow.draw(middlePressZone);
|
||||
mWindow.draw(rightPressZone);
|
||||
NoteTile::update(1.0 / 60, mWindow);
|
||||
mWindow.draw(mStatisticsText);
|
||||
mWindow.display();
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
#include "NoteTile.hpp"
|
||||
#include "GameData.hpp"
|
||||
#include "NoteSprite.hpp"
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Window/Window.hpp>
|
||||
#include <memory>
|
||||
|
||||
const int NotePlaceXPos[3] = {200, 400, 600};
|
||||
std::vector<std::unique_ptr<NoteTile>> NoteTile::existing_tiles{};
|
||||
// private
|
||||
NoteTile::NoteTile(float play_time, float good_interval, NotePlaceEnum place,
|
||||
|
@ -13,9 +13,9 @@ NoteTile::NoteTile(float play_time, float good_interval, NotePlaceEnum place,
|
|||
note_sprite() {
|
||||
// TODO do real note_sprite init, make it fall at good speed far enough...
|
||||
note_sprite.fall_speed = 100;
|
||||
note_sprite.sprite.setPosition(
|
||||
sf::Vector2f(NotePlaceXPos[place],
|
||||
480 - note_sprite.fall_speed * (play_time - current_time)));
|
||||
note_sprite.sprite.setPosition(sf::Vector2f(
|
||||
NOTE_PLACE_X_POS[place],
|
||||
NOTE_PRESS_HEIGHT - note_sprite.fall_speed * (play_time - current_time)));
|
||||
}
|
||||
|
||||
// public
|
||||
|
@ -27,18 +27,33 @@ void NoteTile::create(float play_time, float good_interval, NotePlaceEnum place,
|
|||
}
|
||||
|
||||
bool NoteTile::checkPress(float press_time, NotePlaceEnum key_pressed) {
|
||||
for (const auto ¬e_tile : NoteTile::existing_tiles) {
|
||||
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;
|
||||
}
|
||||
|
||||
void NoteTile::update(float dtime, sf::RenderWindow &window) {
|
||||
for (const auto ¬e_tile : NoteTile::existing_tiles) {
|
||||
note_tile->note_sprite.update(dtime, window);
|
||||
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");
|
||||
it = tiles.erase(it);
|
||||
} else {
|
||||
note_tile->note_sprite.update(dtime, window);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,11 +7,13 @@
|
|||
|
||||
void generateTilePattern(std::vector<std::pair<float, int>> new_notes,
|
||||
const AudioEmitter &audio_emitter) {
|
||||
int i = 0;
|
||||
int i = -1;
|
||||
for (auto note : new_notes) {
|
||||
i++;
|
||||
if (i % 4 != 0) // skip les 3/4 des notes (je suis trop nul sinon)
|
||||
continue;
|
||||
float start_time = note.first;
|
||||
NotePlaceEnum notePlace = static_cast<NotePlaceEnum>(i % 3);
|
||||
NoteTile::create(start_time, 1.0, notePlace, audio_emitter.getTime());
|
||||
i++;
|
||||
NoteTile::create(start_time, 0.5, notePlace, audio_emitter.getTime());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue