Initial support for arbitrary types of content in tech tree

This commit is contained in:
Anuken
2020-05-18 09:40:38 -04:00
parent f290f688d0
commit 3784251108
5 changed files with 35 additions and 26 deletions
+18 -10
View File
@@ -328,21 +328,29 @@ public class TechTree implements ContentList{
}); });
} }
private static TechNode node(Block block, Runnable children){ private static TechNode node(UnlockableContent content, Runnable children){
ItemStack[] requirements = new ItemStack[block.requirements.length]; ItemStack[] requirements;
if(content instanceof Block){
Block block = (Block)content;
requirements = new ItemStack[block.requirements.length];
for(int i = 0; i < requirements.length; i++){ for(int i = 0; i < requirements.length; i++){
requirements[i] = new ItemStack(block.requirements[i].item, 40 + Mathf.round(Mathf.pow(block.requirements[i].amount, 1.25f) * 6, 10)); requirements[i] = new ItemStack(block.requirements[i].item, 40 + Mathf.round(Mathf.pow(block.requirements[i].amount, 1.25f) * 6, 10));
} }
}else{
requirements = ItemStack.empty;
}
return new TechNode(block, requirements, children); return new TechNode(content, requirements, children);
} }
private static TechNode node(Block block){ private static TechNode node(Block block){
return node(block, () -> {}); return node(block, () -> {});
} }
public static TechNode create(Block parent, Block block){ public static TechNode create(UnlockableContent parent, UnlockableContent block){
TechNode.context = all.find(t -> t.block == parent); TechNode.context = all.find(t -> t.content == parent);
return node(block, () -> {}); return node(block, () -> {});
} }
@@ -350,17 +358,17 @@ public class TechTree implements ContentList{
static TechNode context; static TechNode context;
public TechNode parent; public TechNode parent;
public final Block block; public final UnlockableContent content;
public final ItemStack[] requirements; public final ItemStack[] requirements;
public final Array<TechNode> children = new Array<>(); public final Array<TechNode> children = new Array<>();
TechNode(TechNode ccontext, Block block, ItemStack[] requirements, Runnable children){ TechNode(TechNode ccontext, UnlockableContent content, ItemStack[] requirements, Runnable children){
if(ccontext != null){ if(ccontext != null){
ccontext.children.add(this); ccontext.children.add(this);
} }
this.parent = ccontext; this.parent = ccontext;
this.block = block; this.content = content;
this.requirements = requirements; this.requirements = requirements;
context = this; context = this;
@@ -369,8 +377,8 @@ public class TechTree implements ContentList{
all.add(this); all.add(this);
} }
TechNode(Block block, ItemStack[] requirements, Runnable children){ TechNode(UnlockableContent content, ItemStack[] requirements, Runnable children){
this(context, block, requirements, children); this(context, content, requirements, children);
} }
} }
} }
+3 -2
View File
@@ -242,8 +242,9 @@ public class ContentParser{
//add research tech node //add research tech node
if(research[0] != null){ if(research[0] != null){
//TODO only works with blocks
Block parent = find(ContentType.block, research[0]); Block parent = find(ContentType.block, research[0]);
TechNode baseNode = exists && TechTree.all.contains(t -> t.block == block) ? TechTree.all.find(t -> t.block == block) : TechTree.create(parent, block); TechNode baseNode = exists && TechTree.all.contains(t -> t.content == block) ? TechTree.all.find(t -> t.content == block) : TechTree.create(parent, block);
LoadedMod cur = currentMod; LoadedMod cur = currentMod;
postreads.add(() -> { postreads.add(() -> {
@@ -254,7 +255,7 @@ public class ContentParser{
baseNode.parent.children.remove(baseNode); baseNode.parent.children.remove(baseNode);
} }
TechNode parnode = TechTree.all.find(t -> t.block == parent); TechNode parnode = TechTree.all.find(t -> t.content == parent);
if(parnode == null){ if(parnode == null){
throw new IllegalArgumentException("Block '" + parent.name + "' isn't in the tech tree, but '" + block.name + "' requires it to be researched."); throw new IllegalArgumentException("Block '" + parent.name + "' isn't in the tech tree, but '" + block.name + "' requires it to be researched.");
} }
@@ -411,7 +411,7 @@ public class PlanetDialog extends FloatingDialog{
Tmp.v31.set(selected.tile.v).rotate(Vec3.Y, -planet.getRotation()).scl(-1f).nor(); Tmp.v31.set(selected.tile.v).rotate(Vec3.Y, -planet.getRotation()).scl(-1f).nor();
float dot = cam.direction.dot(Tmp.v31); float dot = cam.direction.dot(Tmp.v31);
stable.getColor().a = Math.max(dot, 0f)*2f; stable.getColor().a = Math.max(dot, 0f)*2f;
if(stable.getColor().a <= 0.001f){ if(dot*2f <= -0.1f){
stable.remove(); stable.remove();
selected = null; selected = null;
} }
@@ -180,7 +180,7 @@ public class TechTreeDialog extends FloatingDialog{
} }
boolean locked(TechNode node){ boolean locked(TechNode node){
return node.block.locked(); return node.content.locked();
} }
class LayoutNode extends TreeNode<LayoutNode>{ class LayoutNode extends TreeNode<LayoutNode>{
@@ -224,7 +224,7 @@ public class TechTreeDialog extends FloatingDialog{
infoTable.touchable(Touchable.enabled); infoTable.touchable(Touchable.enabled);
for(TechTreeNode node : nodes){ for(TechTreeNode node : nodes){
ImageButton button = new ImageButton(node.node.block.icon(Cicon.medium), Styles.nodei); ImageButton button = new ImageButton(node.node.content.icon(Cicon.medium), Styles.nodei);
button.visible(() -> node.visible); button.visible(() -> node.visible);
button.clicked(() -> { button.clicked(() -> {
if(moved) return; if(moved) return;
@@ -271,7 +271,7 @@ public class TechTreeDialog extends FloatingDialog{
button.setPosition(node.x + panX + width / 2f, node.y + panY + height / 2f + offset, Align.center); button.setPosition(node.x + panX + width / 2f, node.y + panY + height / 2f + offset, Align.center);
button.getStyle().up = !locked(node.node) ? Tex.buttonOver : !data.hasItems(node.node.requirements) ? Tex.buttonRed : Tex.button; button.getStyle().up = !locked(node.node) ? Tex.buttonOver : !data.hasItems(node.node.requirements) ? Tex.buttonRed : Tex.button;
((TextureRegionDrawable)button.getStyle().imageUp) ((TextureRegionDrawable)button.getStyle().imageUp)
.setRegion(node.visible ? node.node.block.icon(Cicon.medium) : Icon.lock.getRegion()); .setRegion(node.visible ? node.node.content.icon(Cicon.medium) : Icon.lock.getRegion());
button.getImage().setColor(!locked(node.node) ? Color.white : Color.gray); button.getImage().setColor(!locked(node.node) ? Color.white : Color.gray);
}); });
addChild(button); addChild(button);
@@ -305,16 +305,16 @@ public class TechTreeDialog extends FloatingDialog{
} }
void unlock(TechNode node){ void unlock(TechNode node){
data.unlockContent(node.block); data.unlockContent(node.content);
data.removeItems(node.requirements); data.removeItems(node.requirements);
showToast(Core.bundle.format("researched", node.block.localizedName)); showToast(Core.bundle.format("researched", node.content.localizedName));
checkNodes(root); checkNodes(root);
hoverNode = null; hoverNode = null;
treeLayout(); treeLayout();
rebuild(); rebuild();
Core.scene.act(); Core.scene.act();
Sounds.unlock.play(); Sounds.unlock.play();
Events.fire(new ResearchEvent(node.block)); Events.fire(new ResearchEvent(node.content));
} }
void rebuild(){ void rebuild(){
@@ -343,11 +343,11 @@ public class TechTreeDialog extends FloatingDialog{
infoTable.table(b -> { infoTable.table(b -> {
b.margin(0).left().defaults().left(); b.margin(0).left().defaults().left();
b.button(Icon.info, Styles.cleari, () -> ui.content.show(node.block)).growY().width(50f); b.button(Icon.info, Styles.cleari, () -> ui.content.show(node.content)).growY().width(50f);
b.add().grow(); b.add().grow();
b.table(desc -> { b.table(desc -> {
desc.left().defaults().left(); desc.left().defaults().left();
desc.add(node.block.localizedName); desc.add(node.content.localizedName);
desc.row(); desc.row();
if(locked(node)){ if(locked(node)){
desc.table(t -> { desc.table(t -> {
@@ -376,8 +376,8 @@ public class TechTreeDialog extends FloatingDialog{
}); });
infoTable.row(); infoTable.row();
if(node.block.description != null){ if(node.content.description != null){
infoTable.table(t -> t.margin(3f).left().labelWrap(node.block.displayDescription()).color(Color.lightGray).growX()).fillX(); infoTable.table(t -> t.margin(3f).left().labelWrap(node.content.displayDescription()).color(Color.lightGray).growX()).fillX();
} }
addChild(infoTable); addChild(infoTable);
@@ -226,7 +226,7 @@ public class SStats implements SteamUserStatsCallback{
Events.on(ResearchEvent.class, e -> { Events.on(ResearchEvent.class, e -> {
if(e.content == Blocks.router) researchRouter.complete(); if(e.content == Blocks.router) researchRouter.complete();
if(!TechTree.all.contains(t -> t.block.locked())){ if(!TechTree.all.contains(t -> t.content.locked())){
researchAll.complete(); researchAll.complete();
} }
}); });