Can now pick free techs

This commit is contained in:
Yair Morgenstern 2017-12-09 20:36:51 +02:00
parent b6f31af5a4
commit 13974b635b
2 changed files with 105 additions and 76 deletions

View File

@ -94,6 +94,8 @@ public class WorldScreen extends CameraStageBaseScreen {
}
public void update(){
if(game.civInfo.tech.freeTechs!=0)
game.setScreen(new TechPickerScreen(game,true));
updateTechButton();
updateTileTable();
updateTiles();

View File

@ -5,6 +5,7 @@ import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.unciv.civinfo.CivilizationTech;
import com.unciv.game.UnCivGame;
import com.unciv.models.gamebasics.GameBasics;
import com.unciv.models.gamebasics.Technology;
@ -17,84 +18,20 @@ import java.util.Stack;
public class TechPickerScreen extends PickerScreen {
HashMap<String, TextButton> techNameToButton = new HashMap<String, TextButton>();
Technology SelectedTech;
com.unciv.civinfo.CivilizationTech civTech = game.civInfo.tech;
ArrayList<String> TechsToResearch = new ArrayList<String>(civTech.techsToResearch);
boolean isFreeTechPick;
Technology selectedTech;
CivilizationTech civTech = game.civInfo.tech;
ArrayList<String> techsToResearch = new ArrayList<String>(civTech.techsToResearch);
public void SetButtonsInfo() {
for (String techName : techNameToButton.keySet()) {
TextButton TB = techNameToButton.get(techName);
TB.getStyle().checkedFontColor = Color.BLACK;
if (civTech.isResearched(techName)) TB.setColor(Color.GREEN);
else if (TechsToResearch.contains(techName)) TB.setColor(Color.BLUE);
else if (civTech.canBeResearched(techName)) TB.setColor(Color.WHITE);
else TB.setColor(Color.GRAY);
TB.setChecked(false);
TB.setText(techName);
if (SelectedTech != null) {
Technology thisTech = GameBasics.Technologies.get(techName);
if (techName.equals(SelectedTech.name)) {
TB.setChecked(true);
TB.setColor(TB.getColor().lerp(Color.LIGHT_GRAY, 0.5f));
}
if (thisTech.prerequisites.contains(SelectedTech.name)) TB.setText("*" + techName);
else if (SelectedTech.prerequisites.contains(techName)) TB.setText(techName + "*");
}
if (TechsToResearch.contains(techName)) {
TB.setText(TB.getText() + " (" + TechsToResearch.indexOf(techName) + ")");
}
if(!civTech.isResearched(techName)) TB.setText(TB.getText() + "\r\n" + game.civInfo.turnsToTech(techName) + " turns");
}
}
public void selectTechnology(Technology tech) {
SelectedTech = tech;
descriptionLabel.setText(tech.description);
if (civTech.isResearched(tech.name)) {
rightSideButton.setText("Research");
rightSideButton.setTouchable(Touchable.disabled);
rightSideButton.setColor(Color.GRAY);
SetButtonsInfo();
return;
}
rightSideButton.setTouchable(Touchable.enabled);
rightSideButton.setColor(Color.WHITE);
if (civTech.canBeResearched(tech.name)) {
TechsToResearch.clear();
TechsToResearch.add(tech.name);
} else {
Stack<String> Prerequisites = new Stack<String>();
ArrayDeque<String> CheckPrerequisites = new ArrayDeque<String>();
CheckPrerequisites.add(tech.name);
while (!CheckPrerequisites.isEmpty()) {
String techNameToCheck = CheckPrerequisites.pop();
if (civTech.isResearched(techNameToCheck))
continue; //no need to add or check prerequisites
Technology techToCheck = GameBasics.Technologies.get(techNameToCheck);
for (String str : techToCheck.prerequisites)
if (!CheckPrerequisites.contains(str)) CheckPrerequisites.add(str);
Prerequisites.add(techNameToCheck);
}
TechsToResearch.clear();
while (!Prerequisites.isEmpty()) TechsToResearch.add(Prerequisites.pop());
}
rightSideButton.setText("Research \r\n" + TechsToResearch.get(0));
SetButtonsInfo();
public TechPickerScreen(final UnCivGame game, boolean freeTechPick){
this(game);
isFreeTechPick=true;
}
public TechPickerScreen(final UnCivGame game) {
super(game);
Technology[][] techMatrix = new Technology[10][5]; // Divided into columns, then rows
Technology[][] techMatrix = new Technology[10][10]; // Divided into columns, then rows
for (int i = 0; i < techMatrix.length; i++) {
techMatrix[i] = new Technology[10];
}
@ -103,11 +40,10 @@ public class TechPickerScreen extends PickerScreen {
techMatrix[technology.column.columnNumber-1][technology.row - 1] = technology;
}
// Table topTable = new Table();
for (int i = 0; i < 10; i++) {
topTable.row().pad(5);
for (int j = 0; j < 9; j++) {
for (int j = 0; j < 10; j++) {
final Technology tech = techMatrix[j][i];
if (tech == null) topTable.add(); // empty cell
else {
@ -125,16 +61,107 @@ public class TechPickerScreen extends PickerScreen {
SetButtonsInfo();
}
rightSideButton.setText("Research");
rightSideButton.setText("Pick a tech");
rightSideButton.setTouchable(Touchable.disabled);
rightSideButton.setColor(Color.GRAY);
rightSideButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
civTech.techsToResearch = TechsToResearch;
if (isFreeTechPick) {
civTech.techsResearched.add(selectedTech.name);
civTech.freeTechs-=1;
game.civInfo.notifications.add("We have stumbled upon the discovery of "+selectedTech.name+"!");
if(selectedTech.name.equals(civTech.currentTechnology()))
civTech.techsToResearch.remove(selectedTech.name);
}
else civTech.techsToResearch = techsToResearch;
game.setWorldScreen();
dispose();
}
});
}
public void SetButtonsInfo() {
for (String techName : techNameToButton.keySet()) {
TextButton TB = techNameToButton.get(techName);
TB.getStyle().checkedFontColor = Color.BLACK;
if (civTech.isResearched(techName)) TB.setColor(Color.GREEN);
else if (techsToResearch.contains(techName)) TB.setColor(Color.BLUE);
else if (civTech.canBeResearched(techName)) TB.setColor(Color.WHITE);
else TB.setColor(Color.GRAY);
TB.setChecked(false);
TB.setText(techName);
if (selectedTech != null) {
Technology thisTech = GameBasics.Technologies.get(techName);
if (techName.equals(selectedTech.name)) {
TB.setChecked(true);
TB.setColor(TB.getColor().lerp(Color.LIGHT_GRAY, 0.5f));
}
if (thisTech.prerequisites.contains(selectedTech.name)) TB.setText("*" + techName);
else if (selectedTech.prerequisites.contains(techName)) TB.setText(techName + "*");
}
if (techsToResearch.contains(techName)) {
TB.setText(TB.getText() + " (" + techsToResearch.indexOf(techName) + ")");
}
if(!civTech.isResearched(techName)) TB.setText(TB.getText() + "\r\n" + game.civInfo.turnsToTech(techName) + " turns");
}
}
public void selectTechnology(Technology tech) {
selectedTech = tech;
descriptionLabel.setText(tech.description);
if(isFreeTechPick) {selectTechnologyForFreeTech(tech); return;}
if (civTech.isResearched(tech.name)) {
rightSideButton.setText("Research");
rightSideButton.setTouchable(Touchable.disabled);
rightSideButton.setColor(Color.GRAY);
SetButtonsInfo();
return;
}
rightSideButton.setTouchable(Touchable.enabled);
rightSideButton.setColor(Color.WHITE);
if (civTech.canBeResearched(tech.name)) {
techsToResearch.clear();
techsToResearch.add(tech.name);
} else {
Stack<String> Prerequisites = new Stack<String>();
ArrayDeque<String> CheckPrerequisites = new ArrayDeque<String>();
CheckPrerequisites.add(tech.name);
while (!CheckPrerequisites.isEmpty()) {
String techNameToCheck = CheckPrerequisites.pop();
if (civTech.isResearched(techNameToCheck))
continue; //no need to add or check prerequisites
Technology techToCheck = GameBasics.Technologies.get(techNameToCheck);
for (String str : techToCheck.prerequisites)
if (!CheckPrerequisites.contains(str)) CheckPrerequisites.add(str);
Prerequisites.add(techNameToCheck);
}
techsToResearch.clear();
while (!Prerequisites.isEmpty()) techsToResearch.add(Prerequisites.pop());
}
rightSideButton.setText("Research \r\n" + techsToResearch.get(0));
SetButtonsInfo();
}
private void selectTechnologyForFreeTech(Technology tech){
if (civTech.canBeResearched(tech.name)) {
rightSideButton.setText("Pick " + selectedTech.name+"\r\n as free tech!");
rightSideButton.setTouchable(Touchable.enabled);
rightSideButton.setColor(Color.WHITE);
}
else {
rightSideButton.setText("Pick a free tech");
rightSideButton.setTouchable(Touchable.disabled);
rightSideButton.setColor(Color.GRAY);
}
}
}