-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
38 lines (34 loc) · 1.36 KB
/
Controller.cpp
File metadata and controls
38 lines (34 loc) · 1.36 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
//
// Created by c on 4/24/24.
//
#include "Controller.h"
Controller::Controller(SnakeModel &snakeRef, Board & boardRef) :snake(snakeRef), board(boardRef) {
delayTime=1.0f/snake.getSnakeVelocity();
}
void Controller::controllSnakeDirection(const sf::Event &event) const {
if(event.key.code==sf::Keyboard::D or event.key.code==sf::Keyboard::Right) {
if(snake.getSnakeDirection()==UP) snake.turn(RIGHT);
if(snake.getSnakeDirection()==DOWN) snake.turn(LEFT);
}
if(event.key.code==sf::Keyboard::A or event.key.code==sf::Keyboard::Left) {
if(snake.getSnakeDirection()==DOWN) snake.turn(RIGHT);
if(snake.getSnakeDirection()==UP) snake.turn(LEFT);
}
if(event.key.code==sf::Keyboard::W or event.key.code==sf::Keyboard::Up) {
if(snake.getSnakeDirection()==LEFT) snake.turn(RIGHT);
if(snake.getSnakeDirection()==RIGHT) snake.turn(LEFT);
}
if(event.key.code==sf::Keyboard::S or event.key.code==sf::Keyboard::Down) {
if(snake.getSnakeDirection()==RIGHT) snake.turn(RIGHT);
if(snake.getSnakeDirection()==LEFT) snake.turn(LEFT);
}
}
void Controller::play(const sf::Event & event, float &elapseTime) const{
if(board.updateGameState()==RUNNING) {
if(elapseTime>=delayTime) {
controllSnakeDirection(event);
board.update();
elapseTime=0.0f;
}
}
}