started collissions

This commit is contained in:
thatscringebro
2023-10-10 17:01:55 -04:00
parent e3741047bc
commit 111446f8ac
5 changed files with 32 additions and 6 deletions

View File

@@ -3,7 +3,7 @@
#define DELAY 30000 #define DELAY 30000
Game::Game() Game::Game()
:max_x(1000), max_y(800), direction(0), window(sf::VideoMode(max_x, max_y), "SFML Window"), :max_x(1000), max_y(800), direction(0), window(sf::VideoMode(max_x, max_y), "ASCII RACER"),
racecar(400.0f, 30.0f, 1.7f) { // maxSpeed, acceleration, and steerSpeed values racecar(400.0f, 30.0f, 1.7f) { // maxSpeed, acceleration, and steerSpeed values
if (!font.loadFromFile("cascaydia.otf")) { if (!font.loadFromFile("cascaydia.otf")) {
std::cerr << "Failed to load font" << std::endl; std::cerr << "Failed to load font" << std::endl;
@@ -19,7 +19,8 @@ Game::Game()
mapText.setFillColor(sf::Color::White); mapText.setFillColor(sf::Color::White);
mapText.setPosition(10, 10); mapText.setPosition(10, 10);
racecar.setPosition(max_x / 2, max_y / 2); // Set initial position for the racecar racecar.setPosition(1000, 80); // Set initial position for the racecar
racecar.setRotation(180);
} }
void Game::run() { void Game::run() {
@@ -44,13 +45,23 @@ void Game::run() {
if (racecar.getSpeed() < 0) { if (racecar.getSpeed() < 0) {
racecar.brake(dt); // Apply brakes racecar.brake(dt); // Apply brakes
} else { } else {
racecar.accelerate(dt); // Reverse the car if(!isTrackCollision(racecar.getX(), racecar.getY())) {
racecar.accelerate(dt); // accelerate the car
}
else {
racecar.stop();
}
} }
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
if (racecar.getSpeed() > 0) { if (racecar.getSpeed() > 0) {
racecar.brake(dt); // Apply brakes racecar.brake(dt); // Apply brakes
} else { } else {
racecar.reverse(dt); // Reverse the car if(!isTrackCollision(racecar.getX(), racecar.getY())) {
racecar.reverse(dt); // Reverse the car
}
else {
racecar.stop();
}
} }
} else { } else {
racecar.decelerate(dt); // No key pressed, decelerate as usual racecar.decelerate(dt); // No key pressed, decelerate as usual
@@ -81,7 +92,6 @@ void Game::run() {
speedText.setFillColor(sf::Color::Red); speedText.setFillColor(sf::Color::Red);
} }
// Update the view to follow the racecar // Update the view to follow the racecar
view.setCenter(racecar.getX(), racecar.getY()); view.setCenter(racecar.getX(), racecar.getY());
window.setView(view); window.setView(view);
@@ -97,3 +107,8 @@ void Game::run() {
} }
} }
bool Game::isTrackCollision(float x, float y) {
// Check if the racecar's position collides with the track
char trackChar = asciiMap[x];
return (trackChar == '/' || trackChar == '\\' || trackChar == '|' || trackChar == '_' || trackChar == '-');
}

1
game.h
View File

@@ -9,6 +9,7 @@ class Game {
public: public:
Game(); Game();
void run(); void run();
bool isTrackCollision(float x, float y);
private: private:
int max_x; int max_x;

View File

@@ -1,7 +1,7 @@
#include "racecar.h" #include "racecar.h"
Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed) Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed)
: maxSpeed(maxSpeed), acceleration(acceleration), steerSpeed(steerSpeed), dx(0), dy(0), steeringAngle(0) { : maxSpeed(maxSpeed), acceleration(acceleration), steerSpeed(steerSpeed), dx(0), dy(0), steeringAngle(3.13) {
if (!font.loadFromFile("cascaydia.otf")) { if (!font.loadFromFile("cascaydia.otf")) {
std::cerr << "Failed to load font" << std::endl; std::cerr << "Failed to load font" << std::endl;
exit(1); exit(1);
@@ -52,6 +52,10 @@ void Racecar::brake(float dt) {
} }
} }
void Racecar::stop() {
speed = 0;
}
void Racecar::reverse(float dt) { void Racecar::reverse(float dt) {
if (speed > -maxSpeed / 2) { if (speed > -maxSpeed / 2) {
speed -= 1.5 * acceleration * dt; speed -= 1.5 * acceleration * dt;
@@ -66,6 +70,10 @@ void Racecar::setPosition(float x, float y) {
carText.setPosition(x, y); carText.setPosition(x, y);
} }
void Racecar::setRotation(float value) {
carText.setRotation(value);
}
float Racecar::getSpeed() const { float Racecar::getSpeed() const {
return speed; return speed;
} }

View File

@@ -12,10 +12,12 @@ public:
void accelerate(float dt); void accelerate(float dt);
void decelerate(float dt); void decelerate(float dt);
void brake(float dt); // Brake the car void brake(float dt); // Brake the car
void stop();
void reverse(float dt); // Reverse the car void reverse(float dt); // Reverse the car
void steer(float dt, float direction); // -1 for left, 1 for right void steer(float dt, float direction); // -1 for left, 1 for right
sf::Text getDrawable(); sf::Text getDrawable();
void setPosition(float x, float y); void setPosition(float x, float y);
void setRotation(float value);
float getSpeed() const; // Get the current speed of the car float getSpeed() const; // Get the current speed of the car
float getX() const; float getX() const;
float getY() const; float getY() const;

Binary file not shown.