Skip to content

Commit

Permalink
Merge pull request #427 from agateau/improve-helicopter-start-stop
Browse files Browse the repository at this point in the history
improve helicopter start stop
  • Loading branch information
agateau authored Jan 25, 2024
2 parents 85bb6c2 + 405826b commit 9caa4c6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .changes/unreleased/Fixed-20240124-223814.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: Fixed
body: 'Helicopter behavior has been improved: there should no longer be stuck helicopters,
or continuously playing helicopter sounds.'
time: 2024-01-24T22:38:14.903171376+01:00
36 changes: 31 additions & 5 deletions core/src/com/agateau/pixelwheels/racescreen/Helicopter.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,39 @@ private enum State {

private static void decideSpawnPosition(
Assets assets, Track track, Helicopter helicopter, Vector2 vehiclePos) {
float width = Constants.UNIT_FOR_PIXEL * assets.helicopterBody.getRegionWidth();
float height = Constants.UNIT_FOR_PIXEL * assets.helicopterBody.getRegionHeight();
float halfWidth = Constants.UNIT_FOR_PIXEL * assets.helicopterBody.getRegionWidth() / 2;
float halfHeight = Constants.UNIT_FOR_PIXEL * assets.helicopterBody.getRegionHeight() / 2;
float mapWidth = track.getMapWidth();
float mapHeight = track.getMapHeight();

float startX = (vehiclePos.x > mapWidth / 2) ? mapWidth : 0;
float startY = (vehiclePos.y > mapHeight / 2) ? mapHeight : 0;
float left = vehiclePos.x;
float bottom = vehiclePos.y;
float right = mapWidth - vehiclePos.x;
float top = mapHeight - vehiclePos.y;

// Start from left edge
float startX = -halfWidth;
float startY = vehiclePos.y;
float distance = left;

// Is right edge closer?
if (distance > right) {
startX = mapWidth + halfWidth;
distance = right;
}

// Is bottom edge closer?
if (distance > bottom) {
startX = vehiclePos.x;
startY = -halfHeight;
distance = bottom;
}

// Is top edge closer?
if (distance > top) {
startX = vehiclePos.x;
startY = mapHeight + halfHeight;
}

helicopter.mSpawnPosition.set(startX, startY);

Expand Down Expand Up @@ -224,7 +250,7 @@ public void leave() {
}

private void actLeaving(float delta) {
if (mSoundPlayer.getVolume() == 0) {
if (isAtDestination()) {
setFinished(true);
mSoundPlayer.stop();
return;
Expand Down

0 comments on commit 9caa4c6

Please sign in to comment.