From b5783f74c7c70f6d44815939b37d399fa5897d42 Mon Sep 17 00:00:00 2001 From: Stefan Hendriks Date: Wed, 5 Oct 2016 21:54:16 +0200 Subject: [PATCH] read entity data for structures from the struct class to reduce long param list --- .../entities/entitiesdata/EntitiesData.java | 6 ++-- .../entitiesdata/EntitiesDataReader.java | 15 ++------ .../entitiesdata/ini/IniDataStructure.java | 32 +++++++++-------- .../entitiesdata/ini/IniDataUnit.java | 1 - .../entitiesdata/EntitiesDataTest.java | 35 +++++++++++-------- 5 files changed, 43 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesData.java b/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesData.java index 82e4580a..901e634d 100644 --- a/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesData.java +++ b/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesData.java @@ -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, @@ -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; } diff --git a/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesDataReader.java b/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesDataReader.java index e4f2253b..50828a7c 100644 --- a/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesDataReader.java +++ b/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesDataReader.java @@ -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) ); } } diff --git a/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/ini/IniDataStructure.java b/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/ini/IniDataStructure.java index 975ad3f2..5e6f374a 100644 --- a/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/ini/IniDataStructure.java +++ b/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/ini/IniDataStructure.java @@ -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; @@ -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() { diff --git a/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/ini/IniDataUnit.java b/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/ini/IniDataUnit.java index e5c27eb2..6dfdbb20 100644 --- a/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/ini/IniDataUnit.java +++ b/src/main/java/com/fundynamic/d2tm/game/entities/entitiesdata/ini/IniDataUnit.java @@ -39,7 +39,6 @@ public class IniDataUnit { public float buildTimeInSeconds; public IniDataUnit() { - } public IniDataUnit(Profile.Section struct) { diff --git a/src/test/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesDataTest.java b/src/test/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesDataTest.java index c48b5f61..d7d5510f 100644 --- a/src/test/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesDataTest.java +++ b/src/test/java/com/fundynamic/d2tm/game/entities/entitiesdata/EntitiesDataTest.java @@ -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 @@ -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! }