Removed ContentUnlockSet

This commit is contained in:
Anuken
2018-11-19 14:38:46 -05:00
parent 8c437eb815
commit a932111040
3 changed files with 32 additions and 76 deletions

View File

@@ -25,7 +25,7 @@ allprojects {
appName = 'Mindustry' appName = 'Mindustry'
gdxVersion = '1.9.9' gdxVersion = '1.9.9'
roboVMVersion = '2.3.0' roboVMVersion = '2.3.0'
uCoreVersion = 'c93c55179ec05b44926d59c5878534a3177d804f' uCoreVersion = 'c9aadd4d0b5848dbc4dbbd0fcd701b11c30c02bb'
getVersionString = { getVersionString = {
String buildVersion = getBuildVersion() String buildVersion = getBuildVersion()

View File

@@ -1,51 +0,0 @@
package io.anuke.mindustry.game;
import com.badlogic.gdx.utils.ObjectMap;
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;
public class ContentUnlockSet {
private ObjectMap<ContentType, ObjectSet<String>> unlocked = new ObjectMap<>();
private boolean dirty;
public boolean isUnlocked(UnlockableContent content){
if(content.alwaysUnlocked()) return true;
if(!unlocked.containsKey(content.getContentType())){
unlocked.put(content.getContentType(), new ObjectSet<>());
}
ObjectSet<String> set = unlocked.get(content.getContentType());
return set.contains(content.getContentName());
}
public boolean unlockContent(UnlockableContent content){
if(!content.canBeUnlocked() || content.alwaysUnlocked()) return false;
if(!unlocked.containsKey(content.getContentType())){
unlocked.put(content.getContentType(), new ObjectSet<>());
}
boolean ret = unlocked.get(content.getContentType()).add(content.getContentName());
//fire unlock event so other classes can use it
if(ret){
content.onUnlock();
Events.fire(new UnlockEvent(content));
dirty = true;
}
return ret;
}
public boolean isDirty() {
return dirty;
}
public ObjectMap<ContentType, ObjectSet<String>> getUnlocked() {
return unlocked;
}
}

View File

@@ -1,15 +1,16 @@
package io.anuke.mindustry.game; package io.anuke.mindustry.game;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap; import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.ObjectMap.Entry;
import com.badlogic.gdx.utils.ObjectSet; import com.badlogic.gdx.utils.ObjectSet;
import io.anuke.mindustry.game.EventType.UnlockEvent;
import io.anuke.mindustry.type.ContentType; import io.anuke.mindustry.type.ContentType;
import io.anuke.ucore.core.Events;
import io.anuke.ucore.core.Settings; import io.anuke.ucore.core.Settings;
/**Stores player unlocks. Clientside only.*/ /**Stores player unlocks. Clientside only.*/
public class Unlocks{ public class Unlocks{
ContentUnlockSet set = new ContentUnlockSet(); private ObjectMap<ContentType, ObjectSet<String>> unlocked = new ObjectMap<>();
private boolean dirty;
static{ static{
Settings.setSerializer(ContentType.class, (stream, t) -> stream.writeInt(t.ordinal()), stream -> ContentType.values()[stream.readInt()]); Settings.setSerializer(ContentType.class, (stream, t) -> stream.writeInt(t.ordinal()), stream -> ContentType.values()[stream.readInt()]);
@@ -17,7 +18,15 @@ public class Unlocks{
/** Returns whether or not this piece of content is unlocked yet.*/ /** Returns whether or not this piece of content is unlocked yet.*/
public boolean isUnlocked(UnlockableContent content){ public boolean isUnlocked(UnlockableContent content){
return set.isUnlocked(content); if(content.alwaysUnlocked()) return true;
if(!unlocked.containsKey(content.getContentType())){
unlocked.put(content.getContentType(), new ObjectSet<>());
}
ObjectSet<String> set = unlocked.get(content.getContentType());
return set.contains(content.getContentName());
} }
/** /**
@@ -28,16 +37,27 @@ public class Unlocks{
* @return whether or not this content was newly unlocked. * @return whether or not this content was newly unlocked.
*/ */
public boolean unlockContent(UnlockableContent content){ public boolean unlockContent(UnlockableContent content){
return !set.isUnlocked(content) && currentSet().unlockContent(content); if(!content.canBeUnlocked() || content.alwaysUnlocked()) return false;
}
private ContentUnlockSet currentSet(){ if(!unlocked.containsKey(content.getContentType())){
return set; unlocked.put(content.getContentType(), new ObjectSet<>());
}
boolean ret = unlocked.get(content.getContentType()).add(content.getContentName());
//fire unlock event so other classes can use it
if(ret){
content.onUnlock();
Events.fire(new UnlockEvent(content));
dirty = true;
}
return ret;
} }
/** Returns whether unlockables have changed since the last save.*/ /** Returns whether unlockables have changed since the last save.*/
public boolean isDirty(){ public boolean isDirty(){
return set.isDirty(); return dirty;
} }
/** Clears all unlocked content. Automatically saves.*/ /** Clears all unlocked content. Automatically saves.*/
@@ -46,24 +66,11 @@ public class Unlocks{
} }
public void load(){ public void load(){
ObjectMap<ContentType, Array<String>> outer = Settings.getObject("unlocks", ObjectMap.class, ObjectMap::new); unlocked = Settings.getObject("unlockset", ObjectMap.class, ObjectMap::new);
ContentUnlockSet cset = new ContentUnlockSet();
for (Entry<ContentType, Array<String>> entry : outer.entries()){
ObjectSet<String> set = new ObjectSet<>();
set.addAll(entry.value);
cset.getUnlocked().put(entry.key, set);
}
} }
public void save(){ public void save(){
ObjectMap<ContentType, Array<String>> write = new ObjectMap<>(); Settings.putObject("unlockset", unlocked);
for(Entry<ContentType, ObjectSet<String>> entry : set.getUnlocked().entries()){
write.put(entry.key, entry.value.iterator().toArray());
}
Settings.putObject("unlocks", write);
Settings.save(); Settings.save();
} }