-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameView.cpp
More file actions
124 lines (109 loc) · 4.94 KB
/
GameView.cpp
File metadata and controls
124 lines (109 loc) · 4.94 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
121
122
123
124
//
// Created by vb-jakub on 16.06.24.
//
#include "GameView.h"
#include <iostream>
//Font is taken from the website with free assets - https://www.fontsquirrel.com/fonts/lato?q%5Bterm%5D=Lato&q%5Bsearch_check%5D=Y
//Textures is taken from the website with free assets - https://gamecodeschool.com/gamemaker-studio/buiding-a-timberman-game-in-gamemaker-studio-2/
GameView::GameView(sf::RenderWindow &windowRef, float &elapseTimeRef, Player &playerRef, Board &boardRef, const sf::Texture &playerTexture):
window(windowRef),elapseTime(elapseTimeRef),player(playerRef),board(boardRef){
logHeight=75;
logWidth=120;
offsetY=120;
if(!font.loadFromFile("assets/Lato_Font.ttf"))
std::cerr << "ERROR: Font didn't open." << std::endl;
textures.push_back(playerTexture); //textures[0] - player texture
loadTexture("axe.png"); //textures[1] - axe texture
loadTexture("rip.png"); //textures[2] - rip texture
loadTexture("background.png"); //textures[3] - background texture
loadTexture("tree.png"); //textures[4] - tree texture
loadTexture("branch.png"); //textures[5] - branch texture
}
void GameView::loadTexture(const std::string &textureName) {
sf::Texture tmp;
if(!tmp.loadFromFile("assets/"+textureName))
std::cerr << "ERROR: " << textureName << " didn't open." << std::endl;
else textures.push_back(tmp);
}
void GameView::draw() const{
drawBackground();
drawTree();
if(board.getGameState()==RUNNING) drawPlayer();
else drawGrave();
}
void GameView::drawBackground() const {
sf::RectangleShape background(static_cast<sf::Vector2f>(window.getSize()));
background.setTexture(&textures[3]);
window.draw(background);
}
void GameView::drawTree() const{
sf::RectangleShape tree(sf::Vector2f(logWidth,static_cast<float>(window.getSize().y)-offsetY));
tree.setTexture(&textures[4]);
tree.setOrigin(tree.getLocalBounds().width/2.0f,0);
tree.setPosition(static_cast<float>(window.getSize().x)/2.0f,0);
window.draw(tree);
drawBranches();
}
void GameView::drawBranches() const {
for(int treeSegment=0; treeSegment<board.getTreeHeight(); ++treeSegment) {
if(board.getBranchSide(treeSegment)!=NONE) {
sf::RectangleShape branch(sf::Vector2f(logWidth*1.2f,logHeight));
branch.setTexture(&textures[5]);
branch.setPosition
((static_cast<float>(window.getSize().x)+logWidth)/2.0f,
static_cast<float>(window.getSize().y)-logHeight*static_cast<float>(treeSegment+1)-offsetY);
if(board.getBranchSide(treeSegment)==LEFT) {
branch.setScale(-1.0f,1.0f);
branch.move(-logWidth,0);
}
window.draw(branch);
}
}
}
void GameView::drawPlayer() const {
sf::RectangleShape playerModel(sf::Vector2f(logHeight,logHeight*1.5f));
playerModel.setTexture(&textures[0]);
playerModel.setPosition
((static_cast<float>(window.getSize().x)+logWidth)/2.0f+10,
static_cast<float>(window.getSize().y)-logHeight-offsetY);
if(player.getPlayerSide()==LEFT) {
playerModel.setScale(-1.0f,1.0f);
playerModel.move(-logWidth-20,0);
}
window.draw(playerModel);
drawAxe();
}
void GameView::drawAxe() const {
sf::RectangleShape axe(sf::Vector2f(logWidth*0.7f,logHeight*0.4f));
axe.setTexture(&textures[1]);
axe.setOrigin(axe.getLocalBounds().width,axe.getLocalBounds().height/2.0f);
axe.setPosition((static_cast<float>(window.getSize().x)+logWidth)/2.0f+20,static_cast<float>(window.getSize().y)-offsetY-10);
if(elapseTime>0.2f) {
if(player.getPlayerSide()==RIGHT)
axe.rotate(90);
else axe.rotate(-90);
axe.move(0,10.0f);
}
if(player.getPlayerSide()==LEFT) {
axe.setScale(-1.0f,1.0f);
axe.move(-logWidth-40,0);
}
window.draw(axe);
}
void GameView::drawGrave() const {
sf::RectangleShape grave(sf::Vector2f(logHeight,logHeight));
grave.setTexture(&textures[2]);
grave.setPosition((static_cast<float>(window.getSize().x)+logWidth)/2.0f+10,static_cast<float>(window.getSize().y)-logHeight-offsetY);
if(player.getPlayerSide()==LEFT) grave.move(-logWidth-logHeight-20,0);
window.draw(grave);
}
void GameView::drawStartCaption() const {
sf::Text text;
text.setFont(font);
text.setCharacterSize(60);
text.setString("PRESS ENTER TO START");
sf::FloatRect textBounds=text.getLocalBounds();
text.setOrigin((textBounds.left+textBounds.width)/2.0f,(textBounds.top+textBounds.height)/2.0f);
text.setPosition(static_cast<float>(window.getSize().x)/2.0f,static_cast<float>(window.getSize().y)/2.0f);
window.draw(text);
}