take care of other player

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

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