Content mapping, part 1

This commit is contained in:
Anuken
2018-09-05 16:02:30 -04:00
parent be3147465d
commit eb3e507a11
57 changed files with 248 additions and 137 deletions

View File

@@ -1,6 +1,7 @@
package io.anuke.mindustry.game;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.type.ContentType;
/**
* Base interface for a content type that is loaded in {@link io.anuke.mindustry.core.ContentLoader}.
@@ -11,7 +12,7 @@ public interface Content{
* Returns the type name of this piece of content.
* This should return the same value for all instances of this content type.
*/
String getContentTypeName();
ContentType getContentType();
/**
* Returns a list of all instances of this content.

View File

@@ -5,6 +5,7 @@ import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.ObjectMap.Entry;
import com.badlogic.gdx.utils.ObjectSet;
import io.anuke.mindustry.game.EventType.UnlockEvent;
import io.anuke.mindustry.type.ContentType;
import io.anuke.ucore.core.Events;
import io.anuke.ucore.core.Settings;
@@ -12,19 +13,19 @@ import static io.anuke.mindustry.Vars.debug;
public class ContentDatabase{
/** Maps unlockable type names to a set of unlocked content.*/
private ObjectMap<String, ObjectSet<String>> unlocked = new ObjectMap<>();
private ObjectMap<ContentType, ObjectSet<String>> unlocked = new ObjectMap<>();
/** Whether unlockables have changed since the last save.*/
private boolean dirty;
/** Returns whether or not this piece of content is unlocked yet.*/
public boolean isUnlocked(UnlockableContent content){
if(debug) return true;
if(!unlocked.containsKey(content.getContentTypeName())){
unlocked.put(content.getContentTypeName(), new ObjectSet<>());
if(!unlocked.containsKey(content.getContentType())){
unlocked.put(content.getContentType(), new ObjectSet<>());
}
ObjectSet<String> set = unlocked.get(content.getContentTypeName());
ObjectSet<String> set = unlocked.get(content.getContentType());
return set.contains(content.getContentName());
}
@@ -39,11 +40,11 @@ public class ContentDatabase{
public boolean unlockContent(UnlockableContent content){
if(!content.canBeUnlocked()) return false;
if(!unlocked.containsKey(content.getContentTypeName())){
unlocked.put(content.getContentTypeName(), new ObjectSet<>());
if(!unlocked.containsKey(content.getContentType())){
unlocked.put(content.getContentType(), new ObjectSet<>());
}
boolean ret = unlocked.get(content.getContentTypeName()).add(content.getContentName());
boolean ret = unlocked.get(content.getContentType()).add(content.getContentName());
//fire unlock event so other classes can use it
if(ret){
@@ -67,9 +68,9 @@ public class ContentDatabase{
}
public void load(){
ObjectMap<String, Array<String>> result = Settings.getJson("content-database", ObjectMap.class);
ObjectMap<ContentType, Array<String>> result = Settings.getJson("content-database", ObjectMap.class);
for(Entry<String, Array<String>> entry : result.entries()){
for(Entry<ContentType, Array<String>> entry : result.entries()){
ObjectSet<String> set = new ObjectSet<>();
set.addAll(entry.value);
unlocked.put(entry.key, set);
@@ -80,9 +81,9 @@ public class ContentDatabase{
public void save(){
ObjectMap<String, Array<String>> write = new ObjectMap<>();
ObjectMap<ContentType, Array<String>> write = new ObjectMap<>();
for(Entry<String, ObjectSet<String>> entry : unlocked.entries()){
for(Entry<ContentType, ObjectSet<String>> entry : unlocked.entries()){
write.put(entry.key, entry.value.iterator().toArray());
}

View File

@@ -0,0 +1,12 @@
package io.anuke.mindustry.game;
import com.badlogic.gdx.utils.Array;
/**Interface for a list of content to be loaded in {@link io.anuke.mindustry.core.ContentLoader}.*/
public interface ContentList{
/**This method should create all the content.*/
void load();
/**This method should return the list of the content of this type, for further loading.*/
Array<? extends Content> getAll();
}

View File

@@ -0,0 +1,13 @@
package io.anuke.mindustry.game;
public interface MappableContent extends Content {
/**
* Returns the unqiue name of this piece of content.
* The name only needs to be unique for all content of this type.
* Do not use IDs for names! Make sure this string stays constant with each update unless removed.
* (e.g. having a recipe and a block, both with name "wall" is fine, as they are different types).
*/
String getContentName();
int getID();
}

View File

@@ -5,54 +5,31 @@ import io.anuke.ucore.scene.ui.layout.Table;
import static io.anuke.mindustry.Vars.control;
/**
* Base interface for an unlockable content type.
*/
public interface UnlockableContent extends Content{
/**
* Returns the unqiue name of this piece of content.
* The name only needs to be unique for all content of this type.
* Do not use IDs for names! Make sure this string stays constant with each update unless removed.
* (e.g. having a recipe and a block, both with name "wall" is fine, as they are different types).
*/
String getContentName();
/**
* Returns the localized name of this content.
*/
/**Base interface for an unlockable content type.*/
public interface UnlockableContent extends MappableContent{
/**Returns the localized name of this content.*/
String localizedName();
TextureRegion getContentIcon();
/**
* This should show all necessary info about this content in the specified table.
*/
/**This should show all necessary info about this content in the specified table.*/
void displayInfo(Table table);
/**
* Called when this content is unlocked. Use this to unlock other related content.
*/
/**Called when this content is unlocked. Use this to unlock other related content.*/
default void onUnlock(){
}
/**
* Whether this content is always hidden in the content info dialog.
*/
/**Whether this content is always hidden in the content info dialog.*/
default boolean isHidden(){
return false;
}
/**
* Lists the content that must be unlocked in order for this specific content to become unlocked. May return null.
*/
/**Lists the content that must be unlocked in order for this specific content to become unlocked. May return null.*/
default UnlockableContent[] getDependencies(){
return null;
}
/**
* Returns whether dependencies are satisfied for unlocking this content.
*/
/**Returns whether dependencies are satisfied for unlocking this content.*/
default boolean canBeUnlocked(){
UnlockableContent[] depend = getDependencies();
if(depend == null){