-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.cpp
More file actions
83 lines (77 loc) · 2.3 KB
/
Cell.cpp
File metadata and controls
83 lines (77 loc) · 2.3 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
#include "Cell.h"
#include<string>
#include <SFML/Graphics.hpp>
using namespace sf;
Texture Cell::cellBackground;
Texture Cell::revealedT;
std::vector<Texture> Cell::numbers;
bool Cell::loaded;
Cell::Cell(int i,int j,float size,Vector2f _offset,int value)
{
if(!loaded){
if(!cellBackground.loadFromFile("images/cell.png"))
throw std::runtime_error("could not load cell background image");
if(!revealedT.loadFromFile("images/revealed.png"))
throw std::runtime_error("could not load revealed background image");
for(int i=0;i<8;i++){
numbers.push_back(Texture());
}
for(int i=0;i<8;i++){
numbers[i]=Texture();
if(!numbers[i].loadFromFile("images/"+std::to_string(i+1)+".png"))
throw std::runtime_error("could not load a cell number image");
numbers[i].setSmooth(true);
}
cellBackground.setSmooth(true);
revealedT.setSmooth(true);
loaded=true;
}
this->value=value;
this->state=CellState::Hidden;
index.x=i;
index.y=j;
shape.setPosition(i*size+_offset.x,j*size+_offset.y);
shape.setSize(Vector2f(size,size));
background.setPosition(i*size+_offset.x,j*size+_offset.y);
background.setSize(Vector2f(size,size));
}
void Cell::reveal(){
if(state!=CellState::Flagged){
state=CellState::Revealed;
}
}
bool Cell::flag(){
if(state==CellState::Hidden){
state=CellState::Flagged;
return true;
}
else if(state == CellState::Flagged){
state=CellState::Hidden;
return true;
}
return false;
}
void Cell::update(){
if(state==CellState::Flagged)
shape.setTexture(sprites.getFlagTexture());
if(state==CellState::Revealed){
background.setTexture(&revealedT);
if(value==-1){
shape.setTexture(sprites.getBombTexture());
}
if(value>0){
shape.setTexture(&numbers[value-1]);
}
}
else
background.setTexture(&cellBackground);
}
void Cell::draw(RenderTarget & target,RenderStates states = RenderStates::Default) const{
target.draw(background);
if(state==CellState::Flagged)
target.draw(shape);
else if(state==CellState::Revealed){
if(value==-1 || value>0)
target.draw(shape);
}
}