-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollisionObjects.cpp
120 lines (106 loc) · 3.79 KB
/
CollisionObjects.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
#include "CollisionObjects.hpp"
CollisionObject::CollisionObject(float x, float y, float w, float h, const Texture& texture)
{
position = Vector2f(x,y);
width = w;
height = h;
shape.setPosition(position);
shape.setTexture(texture);
}
CollisionObject::CollisionObject(float x, float y, float w, float h)
{
width = w;
height = h;
position = Vector2f(x,y);
}
Elevator::Elevator(float x, float y, float w, float h, const Texture& texture) : CollisionObject(x, y, w, h, texture) {
direction = rand() % 2 * 2 - 1;
waitTimer = 0;
}
void Elevator::move(float numLoops) {
if(waitTimer == 0) {
position.x += ((float)direction * 1.5 / numLoops);
shape.setPosition(position);
}
if(waitTimer != 0) {
waitTimer += 1.f / numLoops;
}
if(waitTimer >= 100) {
waitTimer = 0;
}
}
void Elevator::collide(Wall& wall) {
if(position.x + width >= wall.position.x && position.x <= wall.position.x + wall.width && position.y + height >= wall.position.y && position.y <= wall.position.y + wall.height)
{
if(position.x + width >= wall.position.x && position.x <= wall.position.x + wall.width - 3 && position.y + height >= wall.position.y + 3 && position.y <= wall.position.y + wall.height - 3)
{
position.x = wall.position.x - width - 1.5;
direction = -1;
waitTimer++;
}
if(position.x + width >= wall.position.x + 3 && position.x <= wall.position.x + wall.width && position.y + height >= wall.position.y + 3 && position.y <= wall.position.y + wall.height - 3)
{
position.x = wall.position.x + wall.width + 1.5;
direction = 1;
waitTimer++;
}
}
}
Wall::Wall(float x, float y, float w, float h, const Texture& texture) : CollisionObject(x, y, w, h, texture) {}
Wall::Wall(float x, float y, float w, float h) : CollisionObject(x, y, w, h) {}
Board::Board(float x, float y, float w, float h, const Texture& texture) : CollisionObject(x, y, w, h, texture) {
timerThing = 255;
startTimer = false;
}
void Board::runTimer() {
if(startTimer) {
shape.setColor(Color(255,255,255,timerThing-=5));
}
if(timerThing <= 0) {
startTimer = false;
timerThing--;
shape.setColor(Color(255,255,255,0));
}
if(timerThing <= -400) {
timerThing = 255;
shape.setColor(Color(255,255,255,255));
}
}
Portal::Portal(float x, float y, float w, float h, const Texture& texture) : CollisionObject(x, y, w, h, texture) {}
Sign::Sign(float x, float y, float w, float h, const Texture& texture) : CollisionObject(x, y, w, h, texture) {}
Coin::Coin(float x, float y, float w, float h, const Texture& texture) : CollisionObject(x, y, w, h, texture) {
rotation = (float)(rand() % 11 - 5) / 5;
rotateDirection = true;
position2 = Vector2f(x + 13, y);
}
Coin::Coin(float x, float y, float w, float h, const Texture& texture, const Texture& texture2) : CollisionObject(x, y, w, h, texture) {
rotation = (float)(rand() % 11 - 5) / 5;
rotateDirection = true;
shape2.setTexture(texture2);
position2 = Vector2f(x + 13, y);
}
void Coin::rotate() {
if(rotation <= 0) {
shape.setPosition(position2.x + shape.getGlobalBounds().width / 2, shape.getPosition().y);
} else {
shape.setPosition(position2.x - shape.getGlobalBounds().width / 2, shape.getPosition().y);
}
if(rotateDirection) {
rotation -= 0.05;
} else {
rotation += 0.05;
}
if(rotation <= -1) {
rotateDirection = false;
}
if(rotation >= 1) {
rotateDirection = true;
}
shape.setScale(sin(M_PI * rotation * 0.5), 1);
}
Jump::Jump(float x, float y, float w, float h, const Texture& texture, const Texture& texture2) : CollisionObject(x,y,w,h,texture) {
firstTexture = &texture;
secondTexture = &texture2;
originalPos = Vector2f(x,y);
springTimer = 0;
}