-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First stab at reading a test-scenario.ini
- Loading branch information
1 parent
d59ea78
commit 90456df
Showing
9 changed files
with
216 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/com/fundynamic/d2tm/game/scenario/IniScenarioFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.fundynamic.d2tm.game.scenario; | ||
|
||
import com.fundynamic.d2tm.game.entities.EntityRepository; | ||
import com.fundynamic.d2tm.game.entities.Player; | ||
import com.fundynamic.d2tm.game.entities.entitiesdata.EntitiesData; | ||
import com.fundynamic.d2tm.game.map.Map; | ||
import com.fundynamic.d2tm.game.map.MapEditor; | ||
import com.fundynamic.d2tm.game.rendering.gui.battlefield.Recolorer; | ||
import com.fundynamic.d2tm.game.terrain.TerrainFactory; | ||
import com.fundynamic.d2tm.game.terrain.impl.DuneTerrain; | ||
import com.fundynamic.d2tm.game.terrain.impl.Sand; | ||
import com.fundynamic.d2tm.graphics.Shroud; | ||
import com.fundynamic.d2tm.math.MapCoordinate; | ||
import com.fundynamic.d2tm.utils.StringUtils; | ||
import org.ini4j.Ini; | ||
import org.ini4j.Profile; | ||
import org.newdawn.slick.SlickException; | ||
|
||
import java.io.InputStream; | ||
|
||
public class IniScenarioFactory extends ScenarioFactory { | ||
|
||
private String filename; | ||
|
||
public IniScenarioFactory(Shroud shroud, TerrainFactory terrainFactory, EntitiesData entitiesData, String filename) { | ||
super(shroud, terrainFactory, entitiesData); | ||
this.filename = filename; | ||
} | ||
|
||
@Override | ||
public Scenario create() { | ||
try { | ||
InputStream resourceAsStream = getClass().getResourceAsStream("/" + filename); | ||
if (resourceAsStream == null) throw new IllegalArgumentException("Unable to open file: " + filename); | ||
|
||
Ini ini = new Ini(resourceAsStream); | ||
|
||
Scenario.ScenarioBuilder builder = Scenario.builder(); | ||
|
||
Player human = readHumanPlayer(ini); | ||
builder.withHuman(human); | ||
|
||
Player cpu = getCpuPlayer(ini); | ||
builder.withCpuPlayer(cpu); | ||
|
||
Map map = readMap(); | ||
builder.withMap(map); | ||
|
||
EntityRepository entityRepository = getEntityRepository(map); | ||
builder.withEntityRepository(entityRepository); | ||
|
||
Profile.Section sounds = ini.get("UNITS"); | ||
String[] strings = sounds.childrenNames(); | ||
for (String id : strings) { | ||
Profile.Section struct = sounds.getChild(id); | ||
/** | ||
* MapCoordinate=1,1 | ||
Player=HUMAN | ||
Type=QUAD | ||
State=GUARD | ||
*/ | ||
String playerId = struct.get("Player", String.class, "HUMAN"); | ||
Player playerToUse = human; | ||
if ("CPU".equalsIgnoreCase(playerId)) { | ||
playerToUse = cpu; | ||
} | ||
|
||
String mapCoordinate = struct.get("MapCoordinate", String.class, ""); | ||
if (StringUtils.isEmpty(mapCoordinate)) { | ||
System.out.println("Unable to put unit " + struct + " on map, because it lacks a MapCoordinate"); | ||
continue; | ||
} | ||
|
||
String type = struct.get("Type", String.class, ""); | ||
if (StringUtils.isEmpty(type)) { | ||
System.out.println("Unable to put unit " + struct + " on map, because it lacks a Type"); | ||
continue; | ||
} | ||
|
||
entityRepository.placeUnitOnMap(MapCoordinate.fromString(mapCoordinate), type, playerToUse); | ||
} | ||
|
||
return builder.build(); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Unable to create Scenario from file: " + filename, e); | ||
} | ||
} | ||
|
||
public Map readMap() throws SlickException { | ||
Map map = new Map(shroud, 128, 128); | ||
|
||
MapEditor mapEditor = new MapEditor(terrainFactory); | ||
mapEditor.fillMapWithTerrain(map, DuneTerrain.TERRAIN_SAND); | ||
return map; | ||
} | ||
|
||
public Player getCpuPlayer(Ini ini) { | ||
Player cpu = new Player("CPU", Recolorer.FactionColor.RED); | ||
Profile.Section iniHuman = ini.get("CPU"); | ||
int startingCredits = iniHuman.get("Credits", Integer.class, 2000); | ||
cpu.setCredits(startingCredits); | ||
return cpu; | ||
} | ||
|
||
public Player readHumanPlayer(Ini ini) { | ||
Player human = new Player("Human", Recolorer.FactionColor.GREEN); | ||
Profile.Section iniHuman = ini.get("HUMAN"); | ||
int startingCredits = iniHuman.get("Credits", Integer.class, 2000); | ||
human.setCredits(startingCredits); | ||
return human; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
[HUMAN] | ||
Credits=5000 | ||
# focus at Constyard | ||
MapFocus=5,5 | ||
|
||
[CPU] | ||
Credits=4000 | ||
|
||
[UNITS] | ||
|
||
[UNITS/1] | ||
MapCoordinate=1,1 | ||
Player=HUMAN | ||
Type=QUAD | ||
State=GUARD | ||
|
||
[UNITS/2] | ||
MapCoordinate=1,2 | ||
Player=HUMAN | ||
Type=TANK | ||
State=GUARD | ||
|
||
[UNITS/3] | ||
MapCoordinate=1,5 | ||
Player=CPU | ||
Type=QUAD | ||
State=GUARD | ||
|
||
|
||
[STRUCTURES] | ||
|
||
[STRUCTURE/1] | ||
Type=CONSTYARD | ||
Player=HUMAN | ||
MapCoordinate=5,5 | ||
|
||
[MAP] | ||
Width=128 | ||
Height=128 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters