take care of other player

This commit is contained in:
Rémi Heredero 2023-12-28 18:15:53 +01:00
parent 633592a4ea
commit 53a2a09399
3 changed files with 28 additions and 17 deletions

View File

@ -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

27
app.cpp
View File

@ -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();

4
app.h
View File

@ -32,6 +32,8 @@ protected:
typedef struct {
Vector2D coordinate;
int relativeDistanceToMe;
int relativeDistanceToOtherPlayer;
int pts;
} Gem;
@ -43,9 +45,11 @@ protected:
QVector<Gem> gems_;
Vector2D myVehicle_;
QVector<Vector2D> otherVehicles_;
void addGem(int x, int y, int pts);
void fillField(QString body);
Vector2D computeRelativePts(Gem g);
void computeRelativeDistance(Gem* g);
void computeMove();
};