Skip to content

Commit

Permalink
Add a concept of 'height' to projectiles
Browse files Browse the repository at this point in the history
- if given a projectile will be launched 'up' and later heads 'down' to
its target. Giving a nice 3d-ish effect. Shadow is just copy of the
image all in black
  • Loading branch information
stefanhendriks committed Jul 23, 2017
1 parent 057c06e commit 10ea20a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
Expand Up @@ -107,6 +107,10 @@ public void addProjectile(String id, IniDataWeapon iniDataWeapon) throws SlickEx
entityData.explosionId = iniDataWeapon.explosionId;
entityData.soundData = getAndEnsureSoundId(iniDataWeapon.soundId, id); // for launching projectile?

entityData.maxAscensionHeight = iniDataWeapon.ascendTo;
entityData.maxAscensionAtFlightPercentage = iniDataWeapon.ascendAt;
entityData.startToDescendPercentage = iniDataWeapon.descendAt;

entityData.setFacingsAndCalculateChops(iniDataWeapon.facings);
}

Expand Down
Expand Up @@ -40,6 +40,9 @@ public class EntitiesDataReader { // TODO: Rename to INIEntitiesDataReader? (all
public static final String INI_KEYWORD_BARREL = "Barrel";
public static final String INI_KEYWORD_FILE = "File";
public static final String INI_KEYWORD_SOUND = "Sound";
public static final String INI_KEYWORD_ASCEND_TO = "AscendTo";
public static final String INI_KEYWORD_ASCEND_AT = "AscendAt";
public static final String INI_KEYWORD_DESCEND_AT = "DescendAt";

public EntitiesData fromRulesIni() {
return fromResource(getClass().getResourceAsStream("/rules.ini"));
Expand Down
Expand Up @@ -18,6 +18,9 @@ public class IniDataWeapon {
public int facings;
public String image;
public String soundId;
public int ascendTo;
public float ascendAt;
public float descendAt;

public IniDataWeapon() {
}
Expand All @@ -31,5 +34,8 @@ public IniDataWeapon(Profile.Section struct) {
this.moveSpeed = struct.get(INI_KEYWORD_MOVE_SPEED, Float.class);
this.damage = struct.get(INI_KEYWORD_DAMAGE, Integer.class);
this.facings = struct.get(INI_KEYWORD_FACINGS, Integer.class);
this.ascendTo = struct.get(INI_KEYWORD_ASCEND_TO, Integer.class, 0);
this.ascendAt = struct.get(INI_KEYWORD_ASCEND_AT, Float.class, 0F);
this.descendAt = struct.get(INI_KEYWORD_DESCEND_AT, Float.class, 0F);
}
}
Expand Up @@ -17,6 +17,8 @@ public class Projectile extends Entity implements Moveable, Destructible {
// state
private Coordinate target;
private boolean destroyed;
private float height = 0F; // supposed 'height' of projectile
private float distanceCalculatedALaunch;

public Projectile(Coordinate mapCoordinates, SpriteSheet spriteSheet, Player player,
EntityData entityData, EntityRepository entityRepository) {
Expand All @@ -33,7 +35,12 @@ public EntityType getEntityType() {
public void render(Graphics graphics, int x, int y) {
if (graphics == null) throw new IllegalArgumentException("Graphics must be not-null");
Image sprite = getSprite();
graphics.drawImage(sprite, x, y);
sprite.setImageColor(0, 0, 0, 0.5f);
int shadowX = x + Math.round(height / 8);
graphics.drawImage(sprite, shadowX, y);

sprite.setImageColor(1, 1, 1, 1);
graphics.drawImage(sprite, x, (y - height));
}

public Image getSprite() {
Expand Down Expand Up @@ -61,9 +68,30 @@ public void update(float deltaInSeconds) {

Vector2D delta = normalised.scale(timeCorrectedSpeed);
coordinate = coordinate.add(delta);

if (entityData.maxAscensionHeight > 0) {
if (getFlightProgress() < entityData.maxAscensionAtFlightPercentage) {
if (height < entityData.maxAscensionHeight) {
height = (getFlightProgress() * entityData.maxAscensionHeight) * (1 / entityData.maxAscensionAtFlightPercentage);
if (height >= entityData.maxAscensionHeight) height = entityData.maxAscensionHeight;
}
} else {
if (getFlightProgress() > entityData.startToDescendPercentage) {
if (height > 0) {
float maxDescensionAtFlightPercentage = 1f - entityData.startToDescendPercentage;

float descendProgress = 1.0f - getFlightProgress();
float progress = descendProgress * (1 / maxDescensionAtFlightPercentage);

height = Math.min(entityData.maxAscensionHeight, progress * entityData.maxAscensionHeight);
if (height < 0) height = 0;
}
}
}
}
}

if (target.distance(coordinate) < 0.1F) {
if (getCurrentDistanceToTarget() < 0.1F) {
if (entityData.hasExplosionId()) {
entityRepository.explodeAt(getCenteredCoordinate(), entityData, player);
}
Expand All @@ -83,9 +111,19 @@ public void handle(Entity entity) {
}
}

public float getFlightProgress() {
return 1.0f - (getCurrentDistanceToTarget() / distanceCalculatedALaunch);
}

public float getCurrentDistanceToTarget() {
return target.distance(coordinate);
}

@Override
public void moveTo(Vector2D target) {
this.target = new Coordinate(target);
public void moveTo(Vector2D moveToTarget) {
Coordinate newTarget = new Coordinate(moveToTarget);
distanceCalculatedALaunch = target.distance(newTarget);
this.target = newTarget;
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/fundynamic/d2tm/game/types/EntityData.java
Expand Up @@ -67,6 +67,10 @@ public class EntityData {
private int widthInCells; // in cells, derived from pixels
private int heightInCells; // in cells, derived from pixels

public int maxAscensionHeight; // in pixels, how high a projectile can ascend when 'launched'
public float startToDescendPercentage; // normalised value (between 0 and 1.0), when should descend be initiated?
public float maxAscensionAtFlightPercentage; // normalised value (between 0 and 1.0), when should the projectile be at maxAscensionHeight during flight?

public int sight;

public float moveSpeed; // the speed a unit moves: value is pixels in seconds.
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/rules.ini
Expand Up @@ -118,6 +118,10 @@ Explosion=BOOM
MoveSpeed=160.0
Damage=200
Facings=16
# Large rocket really has height
AscendTo=48
AscendAt=0.30
DescendAt=0.65

[UNITS]

Expand Down
Expand Up @@ -155,6 +155,9 @@ public void readsWeaponsFromIniFile() {
assertThat(rifle.moveSpeed, is(160f));
assertThat(rifle.damage, is(28));
assertThat(rifle.hasSound(), is(true));
assertThat(rifle.maxAscensionHeight, is(83));
assertThat(rifle.maxAscensionAtFlightPercentage, is(0.23F));
assertThat(rifle.startToDescendPercentage, is(0.87F));
}

@Test
Expand Down
5 changes: 4 additions & 1 deletion src/test/resources/test-rules.ini
Expand Up @@ -22,8 +22,11 @@ Explosion=BOOM
MoveSpeed=160.0
Damage=28
Facings=0
# launch sound (for now)
# launch sound (for now) not used
Sound=SOUND1
AscendTo=83
AscendAt=0.23
DescendAt=0.87

[SUPERPOWERS]

Expand Down

0 comments on commit 10ea20a

Please sign in to comment.