initial commit

This commit is contained in:
ben committed 2026-05-17 12:19:19 -07:00
commit fbd12d4a8e
320 files changed
+124298

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
For this discussion I looked at [Mindustry](https://github.com/Anuken/Mindustry). Mindustry is a tower defense factory building game that is open source and written in java. I mostly found examples of interfaces for parts of the code where it is helpful to have multiple different implementations of one thing for flexibility or future changes. This included world generation, goals and weapon logic.
One of the first examples I found was the interface [Objective](https://github.com/Anuken/Mindustry/blob/2ad41a904753a47f6fb1a7b64dbea46204ce207e/core/src/mindustry/game/Objectives.java#L141).
```
/** Defines a specific objective for a game. */
public interface Objective{
/** @return whether this objective is met. */
boolean complete();
/** @return the string displayed when this objective is completed, in imperative form.
* e.g. when the objective is 'complete 10 waves', this would display "complete 10 waves". */
String display();
/** Build a display for this zone requirement.*/
default void build(Table table){
}
}
```
This interface represents a generic objective in the game to ensure that they all have certain methods such as "complete" and "display." Different objectives can implement this interface to allow a variety of different objectives to be interchangeable since they are required to have the same base methods.
One example of a class that implements the Objective interface is the Research class.
```
public static class Research implements Objective{
public UnlockableContent content;
public Research(UnlockableContent content){
this.content = content;
}
protected Research(){}
@Override
public boolean complete(){
return content.unlockedHost();
}
@Override
public String display(){
return Core.bundle.format("requirement.research",
//TODO broken for multi tech nodes.
(content.techNode == null || content.techNode.parent == null || content.techNode.parent.content.unlockedHost()) ?
(content.emoji() + " " + content.localizedName) : "???");
}
@Override
public String toString(){
return "research: " + content;
}
}
```
This implements its own logic for checking the progress and displaying the task in the game. This interface is beneficial since there are many different possible objectives and using an interface like this ensures that they all implement required methods to check their status in game and make sure that they all work together well.
A second example that I found was the [WorldGenerator](https://github.com/Anuken/Mindustry/blob/2ad41a904753a47f6fb1a7b64dbea46204ce207e/core/src/mindustry/maps/generators/WorldGenerator.java#L5) interface.
```
public interface WorldGenerator{
void generate(Tiles tiles, WorldParams params);
/** Do not modify tiles here. This is only for specialized configuration. */
default void postGenerate(Tiles tiles){}
}
```
While this isn't a very complex interface, it is still important since it allows for different world generators to easily be added and interchanged. It defines a generate method with specific inputs. The [BasicGenerator](https://github.com/Anuken/Mindustry/blob/2ad41a904753a47f6fb1a7b64dbea46204ce207e/core/src/mindustry/maps/generators/BasicGenerator.java#L18) is an example of a class that implements WorldGenerator. It has the generate method as well as other methods that assist in the world generation. An interface is helpful here because a different generator could be written that implements WorldGenerator and it would be easy to interchange the BasicGenerator with the new one.
https://github.com/Anuken/Mindustry/blob/2ad41a904753a47f6fb1a7b64dbea46204ce207e/core/src/mindustry/type/AmmoType.java#L7
https://github.com/Anuken/Mindustry/blob/2ad41a904753a47f6fb1a7b64dbea46204ce207e/core/src/mindustry/maps/generators/WorldGenerator.java#L5