Skip to content

Commit

Permalink
Fix facing indexing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhendriks committed Oct 4, 2015
1 parent b8b7343 commit 5864175
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public int getFacing(Vector2D from, Vector2D to) {
if (entityData.hasFacings()) {
if (from != to) {
double angle = from.angleTo(to);
float chop = 360 / entityData.facings; // degrees equals .. facings over 360 degrees
facing = (int) (angle / chop);
float chop = 360F / entityData.facings;
angle += (chop / 2);
facing = (int) (angle / chop) % entityData.facings;
}
}
return facing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,22 @@ public class ProjectileTest {
public static final Vector2D DOWN_LEFT = DOWN.min(create(32, 0));
public static final Vector2D DOWN_RIGHT = DOWN.add(create(32, 0));

public static final Vector2D UP_RIGHT_RIGHTX5 = UP_RIGHT.add(create(128, 0));
public static final Vector2D UP_RIGHT_RIGHTX5_WITH_JUST_ENOUGH_RIGHT_JUST_BELOW_HALF_CHOP = UP_RIGHT.add(create(129, 0));

public static final Vector2D UP_RIGHT_RIGHT = UP_RIGHT.add(create(32, 0)); // a bit more to the right (tilts rocket a bit downwards)
public static final Vector2D UP_RIGHT_UP = UP_RIGHT.min(create(0, 32)); // a bit more to the up (tilts rocket a bit more upwards)

public static final Vector2D UP_LEFT_LEFT = UP_LEFT.min(create(32, 0)); // a bit more to the left (tilts rocket a bit downwards)
public static final Vector2D UP_LEFT_UP = UP_LEFT.min(create(0, 32)); // a bit more to the up (tilts rocket a bit more upwards)

public static final Vector2D DOWN_LEFT_LEFT = DOWN_LEFT.min(create(32, 0));
public static final Vector2D DOWN_LEFT_DOWN = DOWN_LEFT.add(create(0, 32));

public static final Vector2D DOWN_RIGHT_DOWN = DOWN_RIGHT.add(create(0, 32));
public static final Vector2D DOWN_RIGHT_RIGHT = DOWN_RIGHT.add(create(32, 0));
public static final Vector2D DOWN_RIGHT_RIGHTX5 = DOWN_RIGHT.add(create(129, 0));

private Projectile projectile;

// this is the order as in LargeRocket.png
Expand All @@ -40,6 +54,16 @@ public void fromCenterToRight() throws SlickException {
assertFacingIs(CENTER_FROM, RIGHT, 0);
}

@Test
public void fromCenterToUpRightRightX5() throws SlickException {
assertFacingIs(CENTER_FROM, UP_RIGHT_RIGHTX5, 1);
}

@Test
public void fromCenterToUpRightRightJustBelowHalfChopThreshold() throws SlickException {
assertFacingIs(CENTER_FROM, UP_RIGHT_RIGHTX5_WITH_JUST_ENOUGH_RIGHT_JUST_BELOW_HALF_CHOP, 0);
}

@Test
public void fromCenterToUpRightRight() throws SlickException {
assertFacingIs(CENTER_FROM, UP_RIGHT_RIGHT, 1);
Expand All @@ -60,31 +84,65 @@ public void fromCenterToUp() throws SlickException {
assertFacingIs(CENTER_FROM, UP, 4);
}

@Test
public void fromCenterToUpLeftUp() throws SlickException {
assertFacingIs(CENTER_FROM, UP_LEFT_UP, 5);
}

@Test
public void fromCenterToUpLeft() throws SlickException {
assertFacingIs(CENTER_FROM, UP_LEFT, 6);
}

@Test
public void fromCenterToUpLeftLeft() throws SlickException {
assertFacingIs(CENTER_FROM, UP_LEFT_LEFT, 7);
}

@Test
public void fromCenterToLeft() throws SlickException {
assertFacingIs(CENTER_FROM, LEFT, 8);
}

@Test
public void fromCenterToUpDownLeft() throws SlickException {
public void fromCenterToDownLeftLeft() throws SlickException {
assertFacingIs(CENTER_FROM, DOWN_LEFT_LEFT, 9);
}

@Test
public void fromCenterToDownLeft() throws SlickException {
assertFacingIs(CENTER_FROM, DOWN_LEFT, 10);
}

@Test
public void fromCenterToUpDown() throws SlickException {
public void fromCenterToDownLeftDown() throws SlickException {
assertFacingIs(CENTER_FROM, DOWN_LEFT_DOWN, 11);
}

@Test
public void fromCenterToDown() throws SlickException {
assertFacingIs(CENTER_FROM, DOWN, 12);
}

@Test
public void fromCenterToUpDownRight() throws SlickException {
public void fromCenterToDownRightDown() throws SlickException {
assertFacingIs(CENTER_FROM, DOWN_RIGHT_DOWN, 13);
}

@Test
public void fromCenterToDownRight() throws SlickException {
assertFacingIs(CENTER_FROM, DOWN_RIGHT, 14);
}

@Test
public void fromCenterToDownRightRight() throws SlickException {
assertFacingIs(CENTER_FROM, DOWN_RIGHT_RIGHT, 15);
}

@Test
public void fromCenterToDownRightX5SoItWrapsAroundBackToFacingZero() throws SlickException {
assertFacingIs(CENTER_FROM, DOWN_RIGHT_RIGHTX5, 0); // wraps from 16 to 0
}

public void assertFacingIs(Vector2D from, Vector2D to, int expectedFacing) {
assertThat(projectile.getFacing(from, to), is(expectedFacing));
Expand Down

0 comments on commit 5864175

Please sign in to comment.