-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.cpp
More file actions
120 lines (105 loc) · 4.7 KB
/
View.cpp
File metadata and controls
120 lines (105 loc) · 4.7 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// Created by c on 4/24/24.
//
#include "View.h"
#include <iostream>
//Textures are taken from the website with free assets - https://opengameart.org/content/snake-game-assets
//Font is taken from the website with free assets - https://fontstruct.com/fontstructions/show/1501665/mine-sweeper
View::View(SnakeModel &snakeRef, Board & boardRef):snake(snakeRef), board(boardRef){
offsetX=20;
offsetY=20;
size=20;
if(!font.loadFromFile("assets/mine-sweeper.ttf"))
std::cout << "ERROR: Font didn't open." << std::endl;
loadTextures("grass.png"); //textures[0] - grass texture
loadTextures("wall.png"); //textures[1] - wall texture
loadTextures("apple.png"); //textures[2] - apple texture
loadTextures("head.png"); //textures[3] - snake head texture
loadTextures("head_dead.png"); //textures[4] - dead snake head (gamestate==FINISHED_LOSS) texture
loadTextures("body.png"); //textures[5] - snake body texture
loadTextures("body_turn.png"); //textures[6] - snake body turn (right) texture
loadTextures("tail.png"); //textures[7] - snake tail texture
}
void View::loadTextures(const std::string &picture) {
sf::Texture tmp;
if(!tmp.loadFromFile("assets/"+picture))
std::cout << "ERROR: " << picture << " didn't open." << std::endl;
else textures.push_back(tmp);
}
void View::drawBoard(sf::RenderWindow &window, int assetNr, char item) const {
sf::RectangleShape field(sf::Vector2f(size,size));
field.setTexture(&textures[assetNr]);
for(int row=-1; row<=board.getBoardHeight(); ++row)
for(int col=-1; col<=board.getBoardWidth(); ++col) {
if(board.getFieldInfo(row,col)==item) {
float Xo=offsetX+static_cast<float>(col)*size;
float Yo=offsetY+static_cast<float>(row)*size;
field.setPosition(Xo,Yo);
window.draw(field);
}
}
}
void View::drawScoreCounter(sf::RenderWindow &window) const {
sf::Text text;
text.setFont(font);
text.setPosition(615,250);
text.setCharacterSize(30);
text.setString("Score:");
window.draw(text);
text.move(65,50);
text.setString(std::to_string(snake.getEatenFood()));
window.draw(text);
}
void View::setSnakePartTexture(sf::RectangleShape &field,int row,int col) const {
if(snake.getSnakeInfo(row,col)=='H' or snake.getSnakeInfo(row,col)=='x') {
if(board.updateGameState()==FINISHED_LOSS) field.setTexture(&textures[4]);
else field.setTexture(&textures[3]);
}
if(snake.getSnakeInfo(row,col)=='B')
switch(snake.turnDirection(row,col)) {
case RIGHT: field.setTexture(&textures[6]); break;
case LEFT: field.setTexture(&textures[6]); mirrorField(field,row,col); break;
default: field.setTexture(&textures[5]); break;
}
if(snake.getSnakeInfo(row,col)=='T') field.setTexture(&textures[7]);
}
void View::mirrorField(sf::RectangleShape &field, int row, int col) const{
field.rotate(270);
switch(snake.getSnakePartDirection(row,col)) {
case RIGHT: field.move(0,size); break;
case DOWN: field.move(-size,0); break;
case LEFT: field.move(0,-size); break;
case UP: field.move(size,0); break;
}
}
void View::rotateSnake(int row, int col, sf::RectangleShape & field) const {
switch(snake.getSnakePartDirection(row,col)) {
case RIGHT: break;
case DOWN: field.rotate(90); field.move(size,0); break;
case LEFT: field.rotate(180); field.move(size,size); break;
case UP: field.rotate(270); field.move(0,size); break;
}
}
void View::drawSnake(sf::RenderWindow &window,int row,int col) const{
if(snake.getSnakeInfo(row,col)!=' ') {
sf::RectangleShape field(sf::Vector2f(size,size));
field.setPosition(offsetX+static_cast<float>(col)*size,offsetY+static_cast<float>(row)*size);
setSnakePartTexture(field,row,col);
rotateSnake(row,col,field);
window.draw(field);
}
}
void View::draw(sf::RenderWindow &window) const{
sf::RectangleShape background(sf::Vector2f(
size*static_cast<float>(board.getBoardWidth()),size*static_cast<float>(board.getBoardWidth())));
background.setPosition(offsetX,offsetY);
background.setTexture(&textures[0]);
window.draw(background);
drawBoard(window,1,'#');
drawBoard(window,1,'W');
drawBoard(window,2,'F');
drawScoreCounter(window);
for(int row=-1; row<=board.getBoardHeight(); ++row)
for(int col=-1; col<=board.getBoardWidth(); ++col)
drawSnake(window, row, col);
}