-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameController.cpp
More file actions
53 lines (46 loc) · 1.65 KB
/
GameController.cpp
File metadata and controls
53 lines (46 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Created by vb-jakub on 17.06.24.
//
#include "GameController.h"
#include <iostream>
#include <SFML/Audio/Sound.hpp>
GameController::GameController(Player &playerRef, Board &boardRef): player(playerRef), board(boardRef){
isStarted=false;
if(!chopBuffer.loadFromFile("sounds/chop.wav"))
std::cerr << "ERROR: Chop sound didn't open." << std::endl;
chop.setBuffer(chopBuffer);
if(!clickBuffer.loadFromFile("sounds/click.wav"))
std::cerr << "ERROR: Click sound didn't open." << std::endl;
click.setBuffer(clickBuffer);
}
void GameController::play(const sf::Event &event, float &elapseTime, FlyingLogs &logs){
if(board.getGameState()==RUNNING) {
if(isStarted) play_game(event,elapseTime,logs);
else play_startGame(event,elapseTime);
}
}
void GameController::play_game(const sf::Event &event, float &elapseTime, FlyingLogs &logs){
if(event.type==sf::Event::KeyPressed) {
if(event.key.code==sf::Keyboard::Right or event.key.code==sf::Keyboard::D) {
player.turn(RIGHT);
board.update();
if(board.getGameState()==RUNNING) logs.addLog(LEFT);
elapseTime=0;
chop.play();
}
if(event.key.code==sf::Keyboard::Left or event.key.code==sf::Keyboard::A) {
player.turn(LEFT);
board.update();
if(board.getGameState()==RUNNING) logs.addLog(RIGHT);
elapseTime=0;
chop.play();
}
}
}
void GameController::play_startGame(const sf::Event &event, float &elapseTime) {
if(event.key.code==sf::Keyboard::Enter) {
isStarted=true;
elapseTime=0;
click.play();
}
}