Skip to content

Commit

Permalink
Re-use facing calculation logic between projectile and unit
Browse files Browse the repository at this point in the history
- For #63
  • Loading branch information
stefanhendriks committed Oct 21, 2015
1 parent c48fd6a commit eb945cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fundynamic.d2tm.game.behaviors.Destructible;
import com.fundynamic.d2tm.game.behaviors.Moveable;
import com.fundynamic.d2tm.game.entities.*;
import com.fundynamic.d2tm.game.entities.units.UnitFacings;
import com.fundynamic.d2tm.math.Vector2D;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
Expand Down Expand Up @@ -38,18 +39,10 @@ public Image getSprite() {
return spriteSheet.getSprite(getFacing(absoluteCoordinates, target), 0);
}

/**
* Calculates facing index.
*
* @return
*/
public int getFacing(Vector2D from, Vector2D to) {
int facing = 0;
if (entityData.hasFacings() && !from.equals(to)) {
double angle = from.angleTo(to);
float chop = entityData.getChop();
angle += (chop / 2);
facing = (int) (angle / chop) % entityData.getFacings();
facing = UnitFacings.calculateFacingSpriteIndex(from, to, entityData.getFacings(), entityData.getChop());
}
return facing;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,17 @@ public static int getCounterClockwiseSteps(int currentId, int desiredId) {

// TODO: this looks very similar to the Projectile determine facing logic, move to somewhere else!?
public static UnitFacings getFacing(Vector2D from, Vector2D to) {
double angle = from.angleTo(to);
int facings = 8; // 8 facings in total for a unit
float chop = 360F / facings;
return byId(calculateFacingSpriteIndex(from, to, facings));
}

public static int calculateFacingSpriteIndex(Vector2D from, Vector2D to, int facings) {
return calculateFacingSpriteIndex(from, to, facings, 360F / facings);
}

public static int calculateFacingSpriteIndex(Vector2D from, Vector2D to, int facings, float chop) {
double angle = from.angleTo(to);
angle += (chop / 2);
int facing = (int) (angle / chop) % facings;
return byId(facing);
return (int) (angle / chop) % facings;
}
}

0 comments on commit eb945cc

Please sign in to comment.