From 53a2a09399325c678dc442777bc30bde0fdb435c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Thu, 28 Dec 2023 18:15:53 +0100 Subject: [PATCH] take care of other player --- STOMPClient.pro | 14 -------------- app.cpp | 27 ++++++++++++++++++++++++--- app.h | 4 ++++ 3 files changed, 28 insertions(+), 17 deletions(-) delete mode 100644 STOMPClient.pro diff --git a/STOMPClient.pro b/STOMPClient.pro deleted file mode 100644 index 804c68a..0000000 --- a/STOMPClient.pro +++ /dev/null @@ -1,14 +0,0 @@ -QT += network gui widgets -CONFIG += c++14 console -CONFIG -= app_bundle -HEADERS += \ - app.h \ - interface/iStompObserver.h \ - interface/iStompSubject.h \ - stomp.h \ - stompframe.h -SOURCES += \ - app.cpp \ - main.cpp \ - stomp.cpp \ - stompframe.cpp diff --git a/app.cpp b/app.cpp index b55728b..6c7bea6 100644 --- a/app.cpp +++ b/app.cpp @@ -17,7 +17,6 @@ void App::sendConfirmation(bool success) { } void App::subscribeConfirmation(bool success) { - st_->sendRequest("/topic/sdi10.gem.command", "up"); } void App::receiveIndication(int id, QString destination, QString body) { @@ -52,6 +51,9 @@ void App::fillField(QString body) { if(c == 'Y') { myVehicle_ = Vector2D(Vector2D::CreatePoint(x, GRID_SIZE-y)); } + if(c == 'h') { + otherVehicles_.append(Vector2D(Vector2D::CreatePoint(x, GRID_SIZE-y))); + } if(c == 'g') addGem(x, y, 100); if(c == 'G') addGem(x, y, 250); if(c == 'D') addGem(x, y, 500); @@ -69,14 +71,33 @@ Vector2D App::computeRelativePts(Gem g) { v.reverse(); v.normalize(MAX_LENGHT); - return Vector2D(v.lenght()*g.pts*v.lenght(), v.angle()); + return Vector2D(v.lenght()*g.pts, v.angle()); +} + +void App::computeRelativeDistance(Gem* g) { + int deltaX = abs(g->coordinate.x() - myVehicle_.x()); + int deltaY = abs(g->coordinate.y() - myVehicle_.y()); + g->relativeDistanceToMe = deltaX+deltaY; + g->relativeDistanceToOtherPlayer = MAX_LENGHT*2; + for(Vector2D v : otherVehicles_) { + deltaX = abs(g->coordinate.x() - v.x()); + deltaY = abs(g->coordinate.y() - v.y()); + if(deltaX+deltaY < g->relativeDistanceToOtherPlayer) { + g->relativeDistanceToOtherPlayer = deltaX+deltaY; + } + } } void App::computeMove() { static const double PI = 3.14159265358979323846; Vector2D myRelativePts; for(Gem g : gems_) { - myRelativePts = myRelativePts+computeRelativePts(g); + computeRelativeDistance(&g); + Vector2D v = computeRelativePts(g); + myRelativePts = myRelativePts+v; + if(g.relativeDistanceToMe <= g.relativeDistanceToOtherPlayer) { + myRelativePts = myRelativePts+v; + } } double angle = myRelativePts.angle(); diff --git a/app.h b/app.h index dab66aa..52e558d 100644 --- a/app.h +++ b/app.h @@ -32,6 +32,8 @@ protected: typedef struct { Vector2D coordinate; + int relativeDistanceToMe; + int relativeDistanceToOtherPlayer; int pts; } Gem; @@ -43,9 +45,11 @@ protected: QVector gems_; Vector2D myVehicle_; + QVector otherVehicles_; void addGem(int x, int y, int pts); void fillField(QString body); Vector2D computeRelativePts(Gem g); + void computeRelativeDistance(Gem* g); void computeMove(); };