Skip to content

Commit

Permalink
read entity data for structures from the struct class to reduce long …
Browse files Browse the repository at this point in the history
…param list
  • Loading branch information
stefanhendriks committed Oct 5, 2016
1 parent b28b151 commit b5783f7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public void addParticle(String id, String pathToImage, int widthInPixels, int he
*
* @throws SlickException
*/
public EntityData addStructure(IniDataStructure iniDataStructure) throws SlickException {
public EntityData addStructure(String id, IniDataStructure iniDataStructure) throws SlickException {
EntityData entityData = createEntity(
iniDataStructure.id,
id,
iniDataStructure.image,
null,
iniDataStructure.width,
Expand All @@ -106,7 +106,7 @@ public EntityData addStructure(IniDataStructure iniDataStructure) throws SlickEx

if (!idProvided(iniDataStructure.explosion)) {
if (!tryGetEntityData(EntityType.PARTICLE, iniDataStructure.explosion)) {
throw new IllegalArgumentException("structure " + iniDataStructure.id + " [explosion] refers to non-existing [EXPLOSIONS/" + iniDataStructure.explosion + "]");
throw new IllegalArgumentException("structure " + id + " [explosion] refers to non-existing [EXPLOSIONS/" + iniDataStructure.explosion + "]");
}
entityData.explosionId = iniDataStructure.explosion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,8 @@ public void readStructures(EntitiesData entitiesData, Ini ini) throws SlickExcep
for (String id : strings) {
Profile.Section struct = structures.getChild(id);
entitiesData.addStructure(
new IniDataStructure(
id,
struct.get(INI_KEYWORD_IMAGE, String.class, null),
struct.get(INI_KEYWORD_WIDTH, Integer.class),
struct.get(INI_KEYWORD_HEIGHT, Integer.class),
struct.get(INI_KEYWORD_SIGHT, Integer.class),
struct.get(INI_KEYWORD_HIT_POINTS, Integer.class),
struct.get(INI_KEYWORD_EXPLOSION, String.class, EntitiesData.UNKNOWN),
struct.get(INI_KEYWORD_BUILD_ICON, String.class, null),
struct.get(INI_KEYWORD_BUILDS, String.class, ""),
struct.get(INI_KEYWORD_BUILD_TIME, Float.class, 0F),
struct.get(INI_KEYWORD_BUILD_LIST, String.class, "")
)
id,
new IniDataStructure(struct)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.fundynamic.d2tm.game.entities.entitiesdata.ini;

import com.fundynamic.d2tm.game.entities.entitiesdata.EntitiesData;
import com.fundynamic.d2tm.game.entities.entitybuilders.EntityBuilderType;
import com.fundynamic.d2tm.utils.StringUtils;
import org.ini4j.Profile;

import static com.fundynamic.d2tm.game.entities.entitiesdata.EntitiesDataReader.*;

/**
* Object representation of a STRUCTURE entry in the INI file.
*/
public class IniDataStructure {


public String id;
public String image;
public int width;
public int height;
Expand All @@ -21,18 +23,20 @@ public class IniDataStructure {
public float buildTimeInSeconds;
public String buildList;

public IniDataStructure(String id, String image, int width, int height, int sight, int hitpoints, String explosion, String buildIcon, String entityBuilderType, float buildTimeInSeconds, String buildList) {
this.id = id;
this.image = image;
this.width = width;
this.height = height;
this.sight = sight;
this.hitpoints = hitpoints;
this.explosion = explosion;
this.buildIcon = buildIcon;
this.entityBuilderType = entityBuilderType;
this.buildTimeInSeconds = buildTimeInSeconds;
this.buildList = buildList;
public IniDataStructure() {
}

public IniDataStructure(Profile.Section struct) {
this.image = struct.get(INI_KEYWORD_IMAGE, String.class, null);
this.width = struct.get(INI_KEYWORD_WIDTH, Integer.class);
this.height = struct.get(INI_KEYWORD_HEIGHT, Integer.class);
this.sight = struct.get(INI_KEYWORD_SIGHT, Integer.class);
this.hitpoints = struct.get(INI_KEYWORD_HIT_POINTS, Integer.class);
this.explosion = struct.get(INI_KEYWORD_EXPLOSION, String.class, EntitiesData.UNKNOWN);
this.buildIcon = struct.get(INI_KEYWORD_BUILD_ICON, String.class, null);
this.entityBuilderType = struct.get(INI_KEYWORD_BUILDS, String.class, "");
this.buildTimeInSeconds = struct.get(INI_KEYWORD_BUILD_TIME, Float.class, 0F);
this.buildList = struct.get(INI_KEYWORD_BUILD_LIST, String.class, "");
}

public EntityBuilderType getEntityBuilderType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class IniDataUnit {
public float buildTimeInSeconds;

public IniDataUnit() {

}

public IniDataUnit(Profile.Section struct) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,30 @@ public void createStructureCreatesStructureData() throws SlickException {
int widthInCells = widthInPixels / Game.TILE_SIZE;
int heightInCells = heightInPixels / Game.TILE_SIZE;
int hitPoints = 1000;
String entityBuilderType = "";
String idOfEntity = "1";
int sight = 3;
String explosionId = "UNKNOWN";



IniDataStructure iniDataStructure = new IniDataStructure();

iniDataStructure.image = "constyard.png";
iniDataStructure.width = widthInPixels;
iniDataStructure.height = heightInPixels;
iniDataStructure.sight = sight;
iniDataStructure.hitpoints = 1000;
iniDataStructure.explosion = explosionId;
iniDataStructure.buildIcon = "icon_constyard.bmp";
iniDataStructure.entityBuilderType = entityBuilderType;
iniDataStructure.buildTimeInSeconds = 1.0F;
iniDataStructure.buildList = "WINDTRAP,REFINERY";

// add
entitiesData.addStructure(
new IniDataStructure(
idOfEntity,
"constyard.png",
widthInPixels,
heightInPixels,
sight,
1000,
explosionId,
"icon_constyard.bmp",
"",
1.0F,
"WINDTRAP,REFINERY"
)
idOfEntity,
iniDataStructure
);

// get & assert
Expand All @@ -145,8 +150,8 @@ public void createStructureCreatesStructureData() throws SlickException {
@Test (expected = IllegalArgumentException.class)
public void createStructureWithDuplicateIdThrowsIllegalArgumentException() throws SlickException {
String idOfEntity = "1";
entitiesData.addStructure(new IniDataStructure(idOfEntity, "constyard.png", 32, 32, 2, 1000, "1", "icon_constyard.bmp", "", 1.0F, "")); // success!
entitiesData.addStructure(new IniDataStructure(idOfEntity, "this is irrelevant", 32, 32, 3, 1000, "1", "icon_constyard.bmp", "", 1.0F, "")); // boom!
entitiesData.addStructure(idOfEntity, new IniDataStructure()); // success!
entitiesData.addStructure(idOfEntity, new IniDataStructure()); // boom!
}


Expand Down

0 comments on commit b5783f7

Please sign in to comment.