-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.cpp
120 lines (99 loc) · 2.36 KB
/
Cell.cpp
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
//
// Cell.cpp
// battleeee
//
// Created by Ingy on 12/29/14.
// Copyright (c) 2014 Ingy. All rights reserved.
//
#include "Cell.h"
#include "Globals.h"
#include "ResourcePath.hpp"
#include <iostream>
using namespace std;
Cell :: Cell()
{
hit = false; xpos = 0; ypos = 0;
miss = false; s = NULL;
cellSprite.setTexture(blank);
string textureSource= resourcePath()+ "ship1.png";
this->shipTexture.loadFromFile(textureSource);
string textureSource1= resourcePath()+ "cross1.png";
this->shipHitTexture.loadFromFile(textureSource1);
string textureSource2=resourcePath()+ "cross.png";
this-> cellHitTexture.loadFromFile(textureSource2);
}
Cell :: ~Cell()
{
}
void Cell :: drawC (RenderWindow * n)
{
n->draw(cellSprite);
}
bool Cell :: isHit()
{
return hit;
}
bool Cell :: isMiss() // Check if a cell that is hit is a miss
{
return miss;
}
void Cell :: hitCell() // attack the ship and update the boolean "hit" update miss as well
{
hit = true;
if(!this->hasShip())
{
miss=true;
cellSprite.setTexture(cellHitTexture);
cellSprite.setTextureRect(IntRect(0,0,cellHitTexture.getSize().x, cellHitTexture.getSize().y));
}
else
{
miss = false;
cellSprite.setTexture(shipHitTexture);
s->hitShip();
}
}
bool Cell :: hasShip()
{
return (s!=NULL);
}
void Cell :: setPosition (int x, int y, bool comp)
{
xpos = x;
ypos = y;
if(comp)
cellSprite.setPosition(oppGridXOffset+xpos*gridCellSize, oppGridYOffset+ypos*gridCellSize);
else cellSprite.setPosition(plGridXOffset+xpos*gridCellSize, plGridYOffset+ypos*gridCellSize);
}
pair<int, int> Cell :: getPosition ()
{
return pair <int, int> (xpos,ypos);
}
bool Cell :: shipSunk () // return true if the ship the cell contains is sunk
{
if(s != NULL)
return(s->shipSunk());
else{
//cout << "error s is null!!!" << endl;
return false;
}
}
void Cell::placeShip(Ship * p)
{
if(s==NULL)
{
this->s=p;
cellSprite.setTexture(shipTexture);
cellSprite.setTextureRect(IntRect(0,0,shipTexture.getSize().x, shipTexture.getSize().y));
}
else cout<< "ALREADY HAS SHIP" <<endl;
}
Ship* Cell::getShip()
{
return s;
}
void Cell:: removeShip()
{
s=NULL;
cellSprite.setTexture(blank);
}