Skip to content

Commit

Permalink
More Abstraction and a few changes to existing API
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolykt committed Mar 6, 2021
1 parent e7cc80b commit 74c7065
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 51 deletions.
12 changes: 12 additions & 0 deletions src/main/java/de/geolykt/starloader/api/Identifiable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.geolykt.starloader.api;

public interface Identifiable {

/**
* Obtains the usually unique (numeric) identifier of the object. There should be no duplicates.
* However due to the way the game operates, there might be duplicates, which can have unintended consquences.
* @return The UID of the empire
*/
public int getUID();

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import de.geolykt.starloader.api.Metadatable;
import snoddasmannen.galimulator.Fleet;
import snoddasmannen.galimulator.Religion;
import snoddasmannen.galimulator.actors.Flagship;
Expand All @@ -14,7 +15,7 @@
/**
* Interface for any empire that is still in the game
*/
public interface ActiveEmpire extends Empire {
public interface ActiveEmpire extends Empire, Metadatable {

/**
* Assigns a {@link StateActor} to the empire.
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/de/geolykt/starloader/api/empire/Empire.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import org.jetbrains.annotations.NotNull;

import de.geolykt.starloader.api.Galimulator;
import de.geolykt.starloader.api.Metadatable;
import de.geolykt.starloader.api.Identifiable;
import snoddasmannen.galimulator.GalColor;

/**
* Base interface for any empires that exist in the game, whether collapsed or not
*/
public interface Empire extends Dateable, Metadatable {
public interface Empire extends Dateable, Identifiable {

/**
* The age of an empire is counted in years and ceases to stop increasing once the empire has collapsed.
Expand Down Expand Up @@ -45,12 +45,6 @@ public default int getAge() {
*/
public int getStarCount();

/**
* Obtains the unique (numeric) identifier of the empire. There should be no duplicates.
* @return The UID of the empire
*/
public int getUID();

/**
* Empires are usually not in the collapse state, however sometimes this is true, albeit rare.
* @return true if the empire ceased to exist
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/de/geolykt/starloader/api/empire/Star.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import com.badlogic.gdx.math.Vector2;

import de.geolykt.starloader.api.Galimulator;
import de.geolykt.starloader.api.Identifiable;
import de.geolykt.starloader.api.Metadatable;
import snoddasmannen.galimulator.Religion;

/**
* Wrapper interface for Stars
*/
public interface Star extends Metadatable {
public interface Star extends Identifiable, Metadatable {

/**
* Adds a star to the neighbour lists. Please note that you likely do not want to call this method yourself.
Expand Down Expand Up @@ -97,16 +98,6 @@ public interface Star extends Metadatable {
*/
public @NotNull Vector<Star> getNeighboursRecursive(int recurseDepth);

/**
* The unique identifier of the star should not change throughout it's lifecycle,
* however nothing forbids it from actually doing that.
* However it should not be done by any extension as this likely breaks something.
* Additionally nothing verifies it's uniqueness, however if it is not unique most lookup techniques will fail
* or return unexpected results.
* @return The unique identifier that represents the star
*/
public int getUniqueId();

/**
* Obtains the wealth of the star.
* Wealthier stars are harder to take and have more additional extras for them and the empire owning the star
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.geolykt.starloader.api.gui;

public interface AutocloseableDialog {

/**
* Obtains the time at which the dialog will be closed automatically, or -1 if automatic closing is not done.
* The time is relative to the starting point of {@link System#currentTimeMillis()} and is in milliseconds.
*/
public long getAutocloseTime();

}
28 changes: 2 additions & 26 deletions src/main/java/de/geolykt/starloader/api/gui/BasicDialog.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
package de.geolykt.starloader.api.gui;

import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import de.geolykt.starloader.impl.DialogCloseListenerWrapper;
import snoddasmannen.galimulator.Space;
import snoddasmannen.galimulator.ui.bh;

/**
* A simple wrapper around the dialog, a graphical component of Galimulator
* A basic dialog.
*/
public class BasicDialog {
public interface BasicDialog extends AutocloseableDialog, ButtonDialog {

/**
* Creates and displays a dialog
* @param title The title of the dialog
* @param description The description (content/body) of the dialog
* @param choices The buttons of the dialog
* @param listeners The listeners that are applied to the dialog
* @param duration The duration that the dialog should stay opened in seconds
* @param playSFX True if the close sound should be used.
*/
public BasicDialog(@NotNull String title, @NotNull String description, @Nullable List<String> choices,
@NotNull ArrayList<BasicDialogCloseListener> listeners, int duration, boolean playSFX) {
bh dialog = Space.a(title, description, choices, duration, null, true);
dialog.a(new DialogCloseListenerWrapper(listeners, playSFX));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ public BasicDialogBuilder supressCloseSound() {
* @return The dialog that was built via the operation.
*/
public BasicDialog buildAndShow() {
return new BasicDialog(title, description, choices, listeners, duration, playSFX);
return new de.geolykt.starloader.impl.BasicDialog(title, description, choices, listeners, duration, playSFX);
}
}
12 changes: 12 additions & 0 deletions src/main/java/de/geolykt/starloader/api/gui/ButtonDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.geolykt.starloader.api.gui;

import org.jetbrains.annotations.Nullable;

public interface ButtonDialog {

/**
* This call simulates a button click. However this method can also be called if the dialog was closed automatically.
* @param buttonPressed Some magic value that I don't fully understand or null, if the dialog was closed automatically
*/
public void close(@Nullable Object buttonPressed); // TODO understand the parameter
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package de.geolykt.starloader.apimixins;

import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import de.geolykt.starloader.api.Galimulator;
import de.geolykt.starloader.api.empire.Empire;
import snoddasmannen.galimulator.GalColor;

@Mixin(snoddasmannen.galimulator.EmpireAnnals.class)
public class EmpireAnnalsMixins implements Empire {

@Shadow
public int empireId;

@Shadow
public int birthYear;

@Shadow
public int deathYear;

@Shadow
public String name;

@Shadow
public String nameIdentifier;

@Shadow
public GalColor color;

@Override
public int getFoundationYear() {
return birthYear;
}

@Override
public int getUID() {
return empireId;
}

@Override
public int getCollapseYear() {
return deathYear;
}

@Override
public @NotNull GalColor getColor() {
return color;
}

@Override
public @NotNull String getEmpireName() {
return name;
}

@Override
public int getStarCount() {
if (deathYear != -1) {
return 0;
} else {
return Galimulator.getEmpirePerUID(getUID()).getStarCount();
}
}

@Override
public boolean hasCollapsed() {
return deathYear != -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public Vector<Integer> getNeighbourIDs() {
}

@Override
public int getUniqueId() {
public int getUID() {
return id;
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/de/geolykt/starloader/impl/BasicDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package de.geolykt.starloader.impl;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import de.geolykt.starloader.api.gui.BasicDialogCloseListener;
import snoddasmannen.galimulator.Space;
import snoddasmannen.galimulator.ui.bh;

/**
* A simple wrapper around the dialog, a graphical component of Galimulator
*/
public class BasicDialog implements de.geolykt.starloader.api.gui.BasicDialog {

protected final bh dialog;
protected final long closeTime;

/**
* Creates and displays a dialog
* @param title The title of the dialog
* @param description The description (content/body) of the dialog
* @param choices The buttons of the dialog
* @param listeners The listeners that are applied to the dialog
* @param duration The duration that the dialog should stay opened in seconds
* @param playSFX True if the close sound should be used.
*/
public BasicDialog(@NotNull String title, @NotNull String description, @Nullable List<String> choices,
@NotNull ArrayList<BasicDialogCloseListener> listeners, int duration, boolean playSFX) {
dialog = Space.a(title, description, choices, duration, null, true);
dialog.a(new DialogCloseListenerWrapper(listeners, playSFX));
// Luckily the close time is final, so we only have to get it once
try {
Field field = this.dialog.getClass().getField("d");
field.setAccessible(true);
closeTime = field.getLong(this.dialog);
field.setAccessible(false);
} catch (ReflectiveOperationException | SecurityException e) {
throw new RuntimeException("Something went wrong while performing the reflections for Audiosample wrapping", e);
}
}

/**
* Obtains the time at which the dialog will be closed automatically, or -1 if automatic closing is not done.
* The time is relative to the starting point of {@link System#currentTimeMillis()} and is in milliseconds.
*/
public long getAutocloseTime() {
return closeTime;
}

@Override
public void close(@Nullable Object buttonPressed) {
dialog.a(buttonPressed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import de.geolykt.starloader.api.gui.DialogCloseCause;
import de.geolykt.starloader.api.resource.AudioSampleWrapper;
import de.geolykt.starloader.api.gui.BasicDialogCloseListener;
import snoddasmannen.galimulator.AudioManager$AudioSample;
import snoddasmannen.galimulator.ui.bk;

public class DialogCloseListenerWrapper implements bk {
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/api-mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"target": "@env(DEFAULT)",
"compatibilityLevel": "JAVA_8",
"mixins": [
"InstanceMixins",
"EmpireMixins",
"AllianceMixins",
"EmpireAnnalsMixins",
"EmpireMixins",
"InstanceMixins",
"StarMixins"
]
}

0 comments on commit 74c7065

Please sign in to comment.