From 38fdc11917ea322268121d9b79fd9a485d9c7c43 Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Thu, 8 Nov 2018 00:14:01 +0100 Subject: [PATCH] Adjustable core position (#307) * Made starting core position configurable and fixed tutorial sector core position * Refactored classes to use a public int interface and a GridPoint2 as internal storage * Battle missions now spawn the core at 50, 50 again * Default player cores are now created at the first spawnpoint * Manually fixed formatting which was not caught by auto formatter --- .../anuke/mindustry/maps/TutorialSector.java | 3 +- .../maps/missions/BattleMission.java | 20 +++++- .../mindustry/maps/missions/Mission.java | 6 -- .../missions/MissionWithStartingCore.java | 63 +++++++++++++++++++ .../mindustry/maps/missions/WaveMission.java | 27 +++++--- 5 files changed, 102 insertions(+), 17 deletions(-) create mode 100644 core/src/io/anuke/mindustry/maps/missions/MissionWithStartingCore.java diff --git a/core/src/io/anuke/mindustry/maps/TutorialSector.java b/core/src/io/anuke/mindustry/maps/TutorialSector.java index 3d6a486e72..81937c9973 100644 --- a/core/src/io/anuke/mindustry/maps/TutorialSector.java +++ b/core/src/io/anuke/mindustry/maps/TutorialSector.java @@ -30,7 +30,8 @@ public class TutorialSector{ //TODO fill turret with items mission //new BlockMission(ProductionBlocks.mechanicalDrill).setMessage("$tutorial.drillturret"), - new WaveMission(2).setMessage("$tutorial.waves"), + // Create a wave mission which spawns the core at 60, 60 rather than in the center of the map + new WaveMission(2, 60, 60).setMessage("$tutorial.waves"), new ItemMission(Items.lead, 150).setMessage("$tutorial.lead"), new ItemMission(Items.copper, 250).setMessage("$tutorial.morecopper"), diff --git a/core/src/io/anuke/mindustry/maps/missions/BattleMission.java b/core/src/io/anuke/mindustry/maps/missions/BattleMission.java index 4ea34022c6..bd2e8e26a9 100644 --- a/core/src/io/anuke/mindustry/maps/missions/BattleMission.java +++ b/core/src/io/anuke/mindustry/maps/missions/BattleMission.java @@ -12,8 +12,24 @@ import io.anuke.ucore.util.Bundles; import static io.anuke.mindustry.Vars.*; -public class BattleMission extends Mission{ +public class BattleMission extends MissionWithStartingCore{ final int spacing = 30; + public static final int defaultXCorePos = 50; + public static final int defaultYCorePos = 50; + + /** Creates a battle mission with the player core being at (@defaultXCorePos, @defaultYCorePos) */ + public BattleMission(){ + this(defaultXCorePos, defaultYCorePos); + } + + /** + * Creates a wave survival with the player core being at a custom location. + * @param xCorePos The X coordinate of the custom core position. + * @param yCorePos The Y coordinate of the custom core position. + */ + public BattleMission(int xCorePos, int yCorePos){ + super(xCorePos, yCorePos); + } @Override public String getIcon(){ @@ -37,7 +53,7 @@ public class BattleMission extends Mission{ @Override public void generate(Generation gen){ - generateCoreAt(gen, 50, 50, defaultTeam); + generateCoreAtFirstSpawnPoint(gen, defaultTeam); if(state.teams.get(defaultTeam).cores.size == 0){ return; diff --git a/core/src/io/anuke/mindustry/maps/missions/Mission.java b/core/src/io/anuke/mindustry/maps/missions/Mission.java index 7544ee5861..25c9c5574e 100644 --- a/core/src/io/anuke/mindustry/maps/missions/Mission.java +++ b/core/src/io/anuke/mindustry/maps/missions/Mission.java @@ -100,10 +100,4 @@ public abstract class Mission{ } public void generate(Generation gen){} - - public void generateCoreAt(Generation gen, int coreX, int coreY, Team team){ - gen.tiles[coreX][coreY].setBlock(StorageBlocks.core); - gen.tiles[coreX][coreY].setTeam(team); - state.teams.get(team).cores.add(gen.tiles[coreX][coreY]); - } } diff --git a/core/src/io/anuke/mindustry/maps/missions/MissionWithStartingCore.java b/core/src/io/anuke/mindustry/maps/missions/MissionWithStartingCore.java new file mode 100644 index 0000000000..7b42129055 --- /dev/null +++ b/core/src/io/anuke/mindustry/maps/missions/MissionWithStartingCore.java @@ -0,0 +1,63 @@ +package io.anuke.mindustry.maps.missions; + +import com.badlogic.gdx.math.GridPoint2; +import com.badlogic.gdx.utils.Array; +import io.anuke.mindustry.content.blocks.StorageBlocks; +import io.anuke.mindustry.game.Team; +import io.anuke.mindustry.maps.generation.Generation; +import io.anuke.mindustry.world.Tile; + +import static io.anuke.mindustry.Vars.state; + +public abstract class MissionWithStartingCore extends Mission{ + + + /** Stores a custom starting location for the core, or null if the default calculation (map center) shall be used. */ + private final GridPoint2 customStartingPoint; + + /** Default constructor. Missions created this way will have a player starting core in the center of the map. */ + MissionWithStartingCore(){ + this.customStartingPoint = null; + } + + /** + * Creates a mission with a core on a non-default location. + * @param xCorePos The x coordinate of the custom core position. + * @param yCorePos The y coordinate of the custom core position. + */ + MissionWithStartingCore(int xCorePos, int yCorePos){ + this.customStartingPoint = new GridPoint2(xCorePos, yCorePos); + } + + /** + * Generates a player core based on generation parameters. + * @param gen The generation parameters which provide the map size. + * @param team The team to generate the core for. + */ + public void generateCoreAtFirstSpawnPoint(Generation gen, Team team){ + Array spawnPoints = getSpawnPoints(gen); + if(spawnPoints == null || spawnPoints.size == 0){ + throw new IllegalArgumentException("A MissionWithStartingCore subclass did not provide a spawn point in getSpawnPoints(). However, at least one point must always be provided."); + } + + Tile startingCoreTile = gen.tiles[spawnPoints.first().x][spawnPoints.first().y]; + startingCoreTile.setBlock(StorageBlocks.core); + startingCoreTile.setTeam(team); + state.teams.get(team).cores.add(startingCoreTile); + } + + /** + * Retrieves the spawn point in the center of the map or at a custom location which was provided through the constructor. + * @param gen The generation parameters which provide the map size. + * @return The center of the map or a custom location. + * @implNote Must return an array with at least one entry. + */ + @Override + public Array getSpawnPoints(Generation gen){ + if(this.customStartingPoint == null){ + return Array.with(new GridPoint2(gen.width / 2, gen.height / 2)); + }else{ + return Array.with(this.customStartingPoint); + } + } +} diff --git a/core/src/io/anuke/mindustry/maps/missions/WaveMission.java b/core/src/io/anuke/mindustry/maps/missions/WaveMission.java index 7a002d85b7..7ddaa5e0d0 100644 --- a/core/src/io/anuke/mindustry/maps/missions/WaveMission.java +++ b/core/src/io/anuke/mindustry/maps/missions/WaveMission.java @@ -15,13 +15,30 @@ import static io.anuke.mindustry.Vars.state; import static io.anuke.mindustry.Vars.waveTeam; import static io.anuke.mindustry.Vars.world; -public class WaveMission extends Mission{ +public class WaveMission extends MissionWithStartingCore{ private final int target; + /** + * Creates a wave survival mission with the player core being in the center of the map. + * @param target The number of waves to be survived. + */ public WaveMission(int target){ + super(); this.target = target; } + /** + * Creates a wave survival with the player core being at a custom location. + * @param target The number of waves to be survived. + * @param xCorePos The X coordinate of the custom core position. + * @param yCorePos The Y coordinate of the custom core position. + */ + public WaveMission(int target, int xCorePos, int yCorePos){ + super(xCorePos, yCorePos); + this.target = target; + } + + @Override public Array getWaves(Sector sector){ return Waves.getSpawns(); @@ -29,8 +46,7 @@ public class WaveMission extends Mission{ @Override public void generate(Generation gen){ - int coreX = gen.width/2, coreY = gen.height/2; - generateCoreAt(gen, coreX, coreY, Team.blue); + generateCoreAtFirstSpawnPoint(gen, Team.blue); } @Override @@ -71,9 +87,4 @@ public class WaveMission extends Mission{ public boolean isComplete(){ return state.wave > target && Vars.unitGroups[Vars.waveTeam.ordinal()].size() == 0; } - - @Override - public Array getSpawnPoints(Generation gen){ - return Array.with(new GridPoint2(gen.width/2, gen.height/2)); - } }