menu and scenes
This commit is contained in:
parent
3ba7354efe
commit
cebde72729
14 changed files with 144 additions and 26 deletions
|
@ -9,7 +9,9 @@ SOURCES += main.cpp \
|
|||
mygame.cpp \
|
||||
button.cpp \
|
||||
character.cpp \
|
||||
compas.cpp
|
||||
compas.cpp \
|
||||
scene.cpp \
|
||||
menu.cpp
|
||||
|
||||
HEADERS += \
|
||||
game.hpp \
|
||||
|
@ -17,4 +19,6 @@ HEADERS += \
|
|||
commons.hpp \
|
||||
button.hpp \
|
||||
character.hpp \
|
||||
compas.hpp
|
||||
compas.hpp \
|
||||
scene.hpp \
|
||||
menu.hpp
|
||||
|
|
|
@ -25,11 +25,11 @@ int Button::getClicks(){
|
|||
int c = _clicks;
|
||||
//cout << "popping clicks " << c << endl;
|
||||
_clicks = 0;
|
||||
//cout << "popclicks = " << _clicks << endl;
|
||||
if (_clicks > 0) std::cout << "popclicks = " << _clicks << std::endl;
|
||||
return c;
|
||||
}
|
||||
|
||||
void Button::update(sf::Vector2i mousePosition){
|
||||
void Button::update(sf::Vector2f mousePosition){
|
||||
//cout << "state = " << _state << endl;
|
||||
if(_state != ButtonState::off){
|
||||
if(!inside(mousePosition)) _state = 0;
|
||||
|
@ -39,7 +39,7 @@ void Button::update(sf::Vector2i mousePosition){
|
|||
setTextureRect(sf::IntRect(0,_ySize*_state,_xSize,_ySize));
|
||||
}
|
||||
|
||||
bool Button::inside(sf::Vector2i position){
|
||||
bool Button::inside(sf::Vector2f position){
|
||||
sf::FloatRect boundingBox = getGlobalBounds();
|
||||
if (boundingBox.contains(sf::Vector2f(position.x, position.y))) return true;
|
||||
//cout << "outside!" << endl;
|
||||
|
@ -51,13 +51,14 @@ void Button::handleMouseEvent(sf::Event& event){
|
|||
switch (event.type){
|
||||
case (sf::Event::MouseButtonPressed):
|
||||
if(_state == ButtonState::active) _state = ButtonState::pressed;
|
||||
std::cout << "pressed!" << _clicks << std::endl;
|
||||
break;
|
||||
|
||||
case (sf::Event::MouseButtonReleased):
|
||||
if(_state == ButtonState::pressed){
|
||||
_state = ButtonState::active;
|
||||
++_clicks;
|
||||
//cout << "Click!" << _clicks << endl;
|
||||
std::cout << "released!" << _clicks << std::endl;
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
|
@ -13,13 +13,13 @@ public:
|
|||
int getClicks();
|
||||
//buttons auto-detect if they are pressed, you just have to passa mouse-event to them
|
||||
void handleMouseEvent(sf::Event& event);
|
||||
void update(sf::Vector2i mousePosition);
|
||||
void update(sf::Vector2f mousePosition);
|
||||
|
||||
protected:
|
||||
int _xSize;
|
||||
int _ySize;
|
||||
sf::Texture _texture;
|
||||
bool inside(sf::Vector2i position);
|
||||
bool inside(sf::Vector2f position);
|
||||
|
||||
private:
|
||||
int _clicks;
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
const int SCENE_NUM = 4;
|
||||
|
||||
#define ASSERT(expression) do \
|
||||
{ \
|
||||
if(!(expression)) { \
|
||||
|
@ -26,8 +28,8 @@ namespace ButtonState {
|
|||
enum basicState {released, active, pressed, off};
|
||||
}
|
||||
|
||||
namespace GameState {
|
||||
enum gameState{menu,inGame,help,credits};
|
||||
namespace GameScene {
|
||||
enum gameScene{menu,inGame,help,credits};
|
||||
}
|
||||
|
||||
namespace PlayerState {
|
||||
|
|
|
@ -7,8 +7,7 @@
|
|||
|
||||
static const float margeErr = 0.80f;
|
||||
|
||||
class Compas
|
||||
{
|
||||
class Compas{
|
||||
|
||||
private:
|
||||
bool isPress;
|
||||
|
|
|
@ -13,6 +13,10 @@ Game::~Game() {
|
|||
Game::instance = nullptr;
|
||||
}
|
||||
|
||||
Game* Game::i() {
|
||||
return Game::instance;
|
||||
}
|
||||
|
||||
// Main game loop
|
||||
void Game::run() {
|
||||
sf::Clock c;
|
||||
|
|
|
@ -6,7 +6,7 @@ class Game {
|
|||
public:
|
||||
Game();
|
||||
virtual ~Game();
|
||||
static Game* i() { return Game::instance;}
|
||||
static Game* i();
|
||||
|
||||
virtual void run();
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include "mygame.hpp"
|
||||
#include "compas.hpp"
|
||||
int main() {
|
||||
MyGame game = MyGame();
|
||||
MyGame game;
|
||||
game.run();
|
||||
return 0;
|
||||
}
|
||||
|
|
40
VaporWaveWars/menu.cpp
Normal file
40
VaporWaveWars/menu.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include "menu.hpp"
|
||||
#include "mygame.hpp"
|
||||
|
||||
Menu::Menu() {
|
||||
_start.setPosition(5,5);
|
||||
_exit.setPosition(100,100);
|
||||
_start.turnOn();
|
||||
_exit.turnOn();
|
||||
_buttons.push_back(&_start);
|
||||
_buttons.push_back(&_exit);
|
||||
}
|
||||
|
||||
void Menu::update(float deltaTime, sf::RenderWindow*window){
|
||||
//float dx = InputManager::action(InputAction::moveX0);
|
||||
//_buttons[0]->move(dx,0);
|
||||
//std::cout << dx << std::endl;
|
||||
for(unsigned int i = 0; i < _buttons.size(); ++i){
|
||||
_buttons[i]->update(window->mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(*window))));
|
||||
}
|
||||
}
|
||||
|
||||
void Menu::draw(sf::RenderWindow* window){
|
||||
for(unsigned int i = 0; i < _buttons.size(); ++i){
|
||||
window->draw(*_buttons[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void Menu::updateButtons(sf::Event e){
|
||||
// std::cout << "update menu buttons" << std::endl;
|
||||
for(unsigned int i = 0; i < _buttons.size(); ++i){
|
||||
_buttons[i]->handleMouseEvent(e);
|
||||
}
|
||||
MyGame* g = static_cast<MyGame*>(Game::i());
|
||||
if (_start.getClicks() > 0){
|
||||
// g->changeScene(GameScene::inGame);
|
||||
std::cout << "Game not ready to be played. Please Wait." << std::endl;
|
||||
}
|
||||
else if(_exit.getClicks() > 0) g->isRunning = false;
|
||||
|
||||
}
|
22
VaporWaveWars/menu.hpp
Normal file
22
VaporWaveWars/menu.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef MENU_HPP
|
||||
#define MENU_HPP
|
||||
#include "commons.hpp"
|
||||
#include "scene.hpp"
|
||||
#include "button.hpp"
|
||||
class Menu : public Scene{
|
||||
public:
|
||||
Menu();
|
||||
Menu(const Menu& m) = delete;
|
||||
Menu(const Menu&& m) = delete;
|
||||
Menu& operator=(Menu& m) = delete;
|
||||
Menu& operator=(Menu&& m) = delete;
|
||||
void update(float deltaTime, sf::RenderWindow *window) final override;
|
||||
void draw(sf::RenderWindow *window) final override;
|
||||
void updateButtons(sf::Event e) final override;
|
||||
private:
|
||||
Button _start = Button("/home/raluca/Projects/WaveGGJ17/build-VaporWaveWars-Desktop-Debug/Resources/buttontejempl.png");
|
||||
Button _exit = Button("/home/raluca/Projects/WaveGGJ17/build-VaporWaveWars-Desktop-Debug/Resources/buttontejempl2.png");
|
||||
std::vector<Button*> _buttons;
|
||||
};
|
||||
|
||||
#endif // MENU_HPP
|
|
@ -1,13 +1,24 @@
|
|||
#include "mygame.hpp"
|
||||
|
||||
MyGame::MyGame() {
|
||||
_state = GameState::menu;
|
||||
_scene = GameScene::menu;
|
||||
_scenes = std::vector<Scene*>(SCENE_NUM);
|
||||
_scenes[GameScene::menu] = &_menu;
|
||||
std::cout << "in menu" << std::endl;
|
||||
|
||||
}
|
||||
|
||||
MyGame::~MyGame() {
|
||||
}
|
||||
|
||||
MyGame* i(){
|
||||
return static_cast<MyGame*>(Game::i());
|
||||
}
|
||||
|
||||
void MyGame::changeScene(GameScene::gameScene n){
|
||||
_scene = n;
|
||||
}
|
||||
|
||||
// Main game loop
|
||||
void MyGame::update(float deltaTime, sf::RenderWindow*window) {
|
||||
sf::Event event;
|
||||
|
@ -23,9 +34,12 @@ void MyGame::update(float deltaTime, sf::RenderWindow*window) {
|
|||
break;
|
||||
case (sf::Event::MouseMoved):
|
||||
case (sf::Event::MouseButtonPressed):
|
||||
switch(_state){
|
||||
case(GameState::menu):
|
||||
std::cout << "still in menu" << std::endl;
|
||||
case (sf::Event::MouseButtonReleased):
|
||||
|
||||
_scenes[_scene]->updateButtons(event);
|
||||
switch(_scene){
|
||||
case(GameScene::menu):
|
||||
//std::cout << "still in menu" << std::endl;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@ -38,11 +52,13 @@ void MyGame::update(float deltaTime, sf::RenderWindow*window) {
|
|||
}
|
||||
|
||||
// do shit
|
||||
_scenes[_scene]->update(deltaTime,window);
|
||||
}
|
||||
|
||||
void MyGame::draw(sf::RenderWindow*window) {
|
||||
//a e s t h e t i c s
|
||||
window->clear(sf::Color::Cyan);
|
||||
// draw shit
|
||||
_scenes[_scene]->draw(window);
|
||||
window->display();
|
||||
}
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
#ifndef MYGAME_HPP
|
||||
#define MYGAME_HPP
|
||||
#include "game.hpp"
|
||||
|
||||
#include "menu.hpp"
|
||||
|
||||
class MyGame : public Game{
|
||||
public:
|
||||
MyGame();
|
||||
virtual ~MyGame();
|
||||
static MyGame* i();
|
||||
void changeScene(GameScene::gameScene n);
|
||||
private:
|
||||
GameState::gameState _state;
|
||||
GameScene::gameScene _scene;
|
||||
std::vector<Scene*> _scenes;
|
||||
Menu _menu;
|
||||
virtual void update(float deltaTime, sf::RenderWindow *window) final override;
|
||||
virtual void draw(sf::RenderWindow *window) final override;
|
||||
};
|
||||
|
|
8
VaporWaveWars/scene.cpp
Normal file
8
VaporWaveWars/scene.cpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include "scene.hpp"
|
||||
|
||||
Scene::Scene(){
|
||||
}
|
||||
|
||||
Scene::~Scene(){
|
||||
|
||||
}
|
18
VaporWaveWars/scene.hpp
Normal file
18
VaporWaveWars/scene.hpp
Normal file
|
@ -0,0 +1,18 @@
|
|||
#ifndef SCENE_HPP
|
||||
#define SCENE_HPP
|
||||
|
||||
#include "commons.hpp"
|
||||
#include "game.hpp"
|
||||
class Scene
|
||||
{
|
||||
public:
|
||||
Scene();
|
||||
virtual ~Scene();
|
||||
virtual void update(float deltaTime, sf::RenderWindow *window) = 0;
|
||||
virtual void draw(sf::RenderWindow *window) = 0;
|
||||
virtual void updateButtons(sf::Event) = 0;
|
||||
protected:
|
||||
Game* parent;
|
||||
};
|
||||
|
||||
#endif // SCENE_HPP
|
Reference in a new issue