-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollisionable.cpp
More file actions
138 lines (121 loc) · 3.99 KB
/
Collisionable.cpp
File metadata and controls
138 lines (121 loc) · 3.99 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
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "Collisionable.hpp"
#include "GameManager.hpp"
#include "utils.hpp"
Collisionable::Collisionable() {
}
Collisionable::Collisionable(GameManager* gm, sf::Texture* text, int spriteWidth, int spriteHeight, int nSpriteX, int nSpriteY) {
this->gm = gm;
sprite.setTexture(*text);
this->spriteWidth = spriteWidth;
this->spriteHeight = spriteHeight;
this->nSpriteX = nSpriteX;
this->nSpriteY = nSpriteY;
speed = sf::Vector2f(0,0);
}
int Collisionable::getWidth() {
return spriteWidth;
}
int Collisionable::getHeight() {
return spriteHeight;
}
int Collisionable::getNSpriteX() {
return nSpriteX;
}
int Collisionable::getNSpriteY() {
return nSpriteY;
}
sf::Vector2f Collisionable::getSpeed() {
return speed;
}
sf::Vector2f Collisionable::getPosition() {
return sprite.getPosition();
}
void Collisionable::setPosition(float x,float y) {
sprite.setPosition(sf::Vector2f(x,y));
}
void Collisionable::modTexture(sf::Texture *text, int spriteWidth, int spriteHeight, int nSpriteX, int nSpriteY){
sprite.setTexture(*text);
this->spriteWidth = spriteWidth;
this->spriteHeight = spriteHeight;
this->nSpriteX = nSpriteX;
this->nSpriteY = nSpriteY;
}
bool Collisionable::areCollisioning(Collisionable *a, Collisionable *b) {
int n = 30;
if(a->getPosition().x +n > b->getPosition().x + b->getWidth()) return false;
if(a->getPosition().x + a->getWidth() - n < b->getPosition().x) return false;
if(a->getPosition().y > b->getPosition().y + b->getHeight()) return false;
if(a->getPosition().y + a->getHeight() < b->getPosition().y) return false;
return true;
}
int Collisionable::collisionVertical(float x, float y) {
int m = 30;
int n = 20;
int maxX = spriteWidth-2*m;
std::string color;
for (int i = 0; i < maxX; i += maxX/n) {
color = gm->getBoard()->getPixelColor(x+m+i,y);
if (color == "Red") return 2;
if (color == "Black") return 1;
color = (gm->getBoard()->getPixelColor(x+m+i,y+spriteHeight));
if (color == "Red") return 2;
if (color == "Black") return 1;
}
return 0;
}
int Collisionable::collisionHorizontal(float x, float y) {
int n = 20;
int maxY = spriteHeight;
std::string color;
for (int j = 0; j < maxY; j += maxY/n) {
color = gm->getBoard()->getPixelColor(x + 30,y+j);
if (color == "Red") return 2;
if (color == "Black") return 1;
color = gm->getBoard()->getPixelColor(x+spriteWidth - 30,y+j);
if (color == "Red") return 2;
if (color == "Black") return 1;
}
return 0;
}
int Collisionable::collisionVertical(float x, float y, int lvl) {
int m = 30+(lvl == PState::wings?50:0);
int n = 20;
int maxX = spriteWidth-2*m;
int maxY = spriteHeight;
int elValor=0;
if(lvl==1) elValor=80;
else if(lvl==2) elValor=80;
else if(lvl==3 || lvl==4 ) elValor=100;
std::string color;
for (int i = 0; i < maxX; i += maxX/n) {
color = gm->getBoard()->getPixelColor(x+m+i,y+elValor);
if (color == "Red") return 2;
if (color == "Black") return 1;
color = (gm->getBoard()->getPixelColor(x+m+i,y+maxY));
if (color == "Red") return 2;
if (color == "Black") return 1;
}
return 0;
}
int Collisionable::collisionHorizontal(float x, float y, int lvl) {
int m = 30+(lvl == PState::wings?50:0);
int n = 20;
int maxY = spriteHeight;
int elValor=0;
if(lvl==1) elValor=80;
else if(lvl==2) elValor=80;
else if(lvl==3 || lvl==4) elValor=100;
std::string color;
for (int j = 0; j < maxY-elValor; j += (maxY-elValor)/n) {
color = gm->getBoard()->getPixelColor(x + m, y + j + elValor);
if (color == "Red") return 2;
if (color == "Black") return 1;
color = gm->getBoard()->getPixelColor(x+spriteWidth - m,y+elValor+j);
if (color == "Red") return 2;
if (color == "Black") return 1;
}
return 0;
}
void Collisionable::move(Dir::Direction dir) {
direction = dir;
}