Skip to content

Commit

Permalink
Move projectiles to a random coordinate within structure
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhendriks committed Oct 2, 2015
1 parent d4bec50 commit 5a8b21d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.fundynamic.d2tm.game.entities.*;
import com.fundynamic.d2tm.game.entities.projectiles.Projectile;
import com.fundynamic.d2tm.game.entities.structures.Structure;
import com.fundynamic.d2tm.game.map.Cell;
import com.fundynamic.d2tm.math.Vector2D;
import org.newdawn.slick.Color;
Expand Down Expand Up @@ -35,7 +36,7 @@ public void leftClicked() {
ArrayList<Entity> ents = new ArrayList(entities);
if (ents.size() > 0) {
Entity randomStructure = ents.get(0);
projectile.moveTo(randomStructure.getAbsoluteMapCoordinates());
projectile.moveTo(((Structure)randomStructure).getRandomPositionWithin());
} else {
projectile.moveTo(Vector2D.random(640, 640));
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/fundynamic/d2tm/game/entities/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public abstract class Entity implements Renderable, Updateable {

// Final properties of unit
protected EntityData entityData;
protected final SpriteSheet spriteSheet;
protected final int sight;
protected final Player player;
Expand All @@ -30,6 +31,14 @@ public Vector2D getAbsoluteMapCoordinates() {
return absoluteMapCoordinates;
}

public int getX() {
return absoluteMapCoordinates.getXAsInt();
}

public int getY() {
return absoluteMapCoordinates.getYAsInt();
}

public int getSight() {
return sight;
}
Expand Down Expand Up @@ -69,4 +78,5 @@ public void removeFromPlayerSet(Entity entity) {
player.removeEntity(entity);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class EntityData {

public Image image;

public int width;
public int height;
public int width; // in pixels
public int height; // in pixels

public int sight;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Structure(Vector2D absoluteMapCoordinates, Image imageOfStructure, Player

public Structure(Vector2D absoluteMapCoordinates, SpriteSheet spriteSheet, Player player, EntityData entityData, EntityRepository entityRepository) {
super(absoluteMapCoordinates, spriteSheet, entityData.sight, player, entityRepository);
this.entityData = entityData;
this.fadingSelection = new FadingSelection(entityData.width, entityData.height);
this.hitPointBasedDestructibility = new HitPointBasedDestructibility(entityData.hitPoints);
widthInCells = (int) Math.ceil(entityData.width / TILE_SIZE);
Expand Down Expand Up @@ -110,4 +111,10 @@ public void takeDamage(int hitPoints) {
public boolean isDestroyed() {
return hitPointBasedDestructibility.isDestroyed();
}

public Vector2D getRandomPositionWithin() {
int topX = getX() - 16;
int topY = getY() - 16;
return Vector2D.random(topX, topX + entityData.width, topY, topY + entityData.height);
}
}

0 comments on commit 5a8b21d

Please sign in to comment.