-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.h
155 lines (122 loc) · 5.65 KB
/
car.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <cmath>
#include <iostream>
#include "vehicle.h"
#include "waypoint.h"
#include "trafficlight.h"
#ifndef CAR_H
#define CAR_H
class Car : public Vehicle { //Car class is inherited from Vehicle class {
public:
//Constructor for the Car class
Car(tVehicleType t, float x, float y, float dir) {
//Initialization of the variables
this->x = x;
this->y = y;
this->dir = dir;
//Loading the textures according to the vehicle type
switch (t) {
case CAR1:
texture.loadFromFile("images/vehicles/car1.png");
break;
case CAR2:
texture.loadFromFile("images/vehicles/car2.png");
break;
case CAR3:
texture.loadFromFile("images/vehicles/car3.png");
break;
case CAR4:
texture.loadFromFile("images/vehicles/car4.png");
break;
case CAR5:
texture.loadFromFile("images/vehicles/car5.png");
break;
case CAR6:
texture.loadFromFile("images/vehicles/car6.png");
break;
default:
break;
}
sprite.setTexture(texture);
//Sets sprite's origin
sprite.setOrigin(sf::Vector2f(sprite.getGlobalBounds().width / 2, sprite.getGlobalBounds().height / 2));
}
//This function detects traffic lights at specific position and stop the car at red lights
void redLight(TrafficLight light[], float &x, float &y, float &dir) {
float tx, ty, tdir;
light->getPosition(tx, ty, tdir);
if(light->getState() == RED) {
if(x == tx) {
x = tx;
}
if(y == ty) {
y = ty;
}
}
sprite.setPosition(x, y);
}
void move() {
//This function includes the first alternative path for cars
TrafficLight tlights[] = {
{2*239 + 70, 250, 180, GREEN}, {2*239 + 240, 170, 90, RED},
{4*239 + 70, 2*239 + 70, 180, RED}, {4*239 + 50, 2*239 + 170, 0, GREEN},
{2*239 + 40, 2*239 + 170, 90, GREEN}, {2*239 + 70, 2*239 + 70, 180, RED},
{2*239 + 200, 2*239 + 70, 270, RED}
};
if(dir == 1) y += 3; //if direction is down, increase y
if(dir == 2) x -= 3; //if direction is left, decrease x
if(dir == 3) y -= 3; //if direction is up, decrease y
if(dir == 4) x += 3; //if direction is right, increase x
//If x is about at the end of the screen and direction is right, change its direction to down (CTR)
if(x < 1080 && x >= 1075 && dir == 4) dir = 1;
//If x is about at the end of the screen, y is at the middle of the screen and direction is down:
//Change the direction to left (TRIGHT)
if(x < 1080 && x >= 1075 && y < 605 && y >= 595 && dir == 1) dir = 2;
//If x and y are at the middle of the screen and direction is left, change the direction to down (CROSS)
if(x < 605 && x > 595 && y < 605 && y >= 595 && dir == 2) dir = 1;
//If x is at the middle of the screen, y is nearly at the end of the screen and direction is down:
//Change the direction to left (TBOT)
if(x < 605 && x > 595 && y >= 1075 && y < 1080 && dir == 1) dir = 2;
//If y is nearly at the end of the screen, x is at zeroth column and the direction is left:
//Change the direction to up (CBL)
if(y >= 1075 && y < 1080 && x < 130 && x >= 107 && dir == 2) dir = 3;
//If x and y are at the zeroth column and the direction is up:
//Change its direction to right (CTL)
if(x < 130 && x >= 107 && y < 130 && y >= 107 && dir == 3) dir = 4;
sprite.setPosition(sf::Vector2f(x, y)); //Setting the position of the sprite
if(dir != 4)
sprite.setRotation(dir * 90.0f); //Setting the rotation
else if(dir == 4)
sprite.setRotation(0.0f); //If direction is right, instead of 360.0f, changed it to 0.0f
}
void move2() {
//This function includes the second alternative path for cars
//This part is the same as in the first option
if(dir == 1) y += 3;
if(dir == 2) x -= 3;
if(dir == 3) y -= 3;
if(dir == 4) x += 3;
//If x is at the middle of the screen, y is at the beginning of the screen, and the direction is right:
//Change the direction to down (TTOP)
if(x < 605 && x > 595 && y >= 107 && y < 130 && dir == 4) dir = 1;
//If x is at the middle of the screen, y is nearly at the end of the screen and the direction is down:
//Change the direction to left (TBOT)
if(x < 605 && x > 595 && y >= 1075 && y < 1080 && dir == 1) dir = 2;
//If y is nearly at the end of the screen, x is at the beginning of the screen and the direction is left:
//Change the direction to up (CBL)
if(y >= 1075 && y < 1080 && x < 130 && x >= 107 && dir == 2) dir = 3;
//If x and y are at the beginning og the screen, and the direction is up
//Change the direction to right (CTL)
if(x < 130 && x >= 107 && y < 130 && y >= 107 && dir == 3) dir = 4;
sprite.setPosition(sf::Vector2f(x, y)); //Set sprite's position
if(dir != 4)
sprite.setRotation(dir * 90.0f); //Setting the rotation
else if(dir == 4)
sprite.setRotation(0.0f); //If direction is right, changed it to 0.0f instead of 4 * 90.0f = 360.0f
}
void draw(sf::RenderWindow *window) {
window->draw(this->sprite); //Drawing the car sprite to the window.
}
};
#endif // CAR_H