-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcollisionAble.cpp
More file actions
91 lines (78 loc) · 2.67 KB
/
collisionAble.cpp
File metadata and controls
91 lines (78 loc) · 2.67 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
#include "collisionAble.h"
#include "sprite.h"
#include "map.h"
#include <iostream>
collisionAble::collisionAble(Sprite & spr, Map & map, bool player, int ID, Move move)
:exist{true}, sprite{ spr }, map{ map }, player{ player }, objID{ ID }, move{ move }{}
void collisionAble::checkCollision(std::list<TankPtr> & tanks, std::list<Bullet> & bullets){
// check the motion clock, we do not want the tank too fast: P
if (timer.getElapsedTime().asSeconds() >= speed){
if (move != Move::none){
// pull out the box that surrounds the vehicle to be tested,
//will it not hit the wrong block
sf::FloatRect box = spr.getGlobalBounds(); // the tank's box
// Check the position of the tank on the map
posMap.x = int((spr.getPosition().x - map.pr.sides) / map.pr.blockSize);
posMap.y = int((spr.getPosition().y - map.pr.topBot) / map.pr.blockSize);
float speed{ map.pr.blockSize / 4.f };
// we change the picture
updateImage();
bool COL{ false };
// Collision with blocks relative to the direction of movement
switch (move){
case collisionAble::Move::left:{
box.left -= speed;
posMap.x--;
if (collision(posMap, box)) COL = { true };
posMap.y++;
if (collision(posMap, box)) COL = { true };
posMap.y++;
if (collision(posMap, box)) COL = { true };
if (COL) return;
} break;
case collisionAble::Move::right:{
box.left += speed;
posMap.x += 2;
if (collision(posMap, box)) COL = { true };
posMap.y++;
if (collision(posMap, box)) COL = { true };
posMap.y++;
if (collision(posMap, box)) COL = { true };
if (COL) return;
} break;
case collisionAble::Move::up:{
box.top -= speed;
posMap.y--;
if (collision(posMap, box)) COL = { true };
posMap.x++;
if (collision(posMap, box)) COL = { true };
posMap.x++;
if (collision(posMap, box)) COL = { true };
if (COL) return;
} break;
case collisionAble::Move::down:{
box.top += speed;
posMap.y += 2;
if (collision(posMap, box)) COL = { true };
posMap.x++;
if (collision(posMap, box)) COL = { true };
posMap.x++;
if (collision(posMap, box)) COL = { true };
if (COL) return;
} break;
}
// check if there is a collision with the rest of the tanks
if (colWithTanks(tanks, bullets, speed)) return;
// If the collision did not appear, then we move the proper
//spite of the vehicle to the position of the box and overwrite
//the main position
pos.x = box.left;
pos.y = box.top;
spr.setPosition(box.left, box.top);
// we reset the timer
timer.restart();
}
}
// If any "if" has knocked us out, set the object to the default position
spr.setPosition(pos); // the old position
}