Skip to content

Commit

Permalink
Display the science/luxury rates and allow the player to change them
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWerner committed Feb 7, 2025
1 parent 9327d27 commit 9bcbeee
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 9 deletions.
5 changes: 5 additions & 0 deletions C7/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ public void processEngineMessages(GameData gameData) {
// TODO: calculate research speed.
EmitSignal(SignalName.UpdateTechProgress, tech.Name, -1);
break;
case MsgUpdateUiAfterSliderChange mUUASC:
// F1 is the science advisor.
// TODO: Move the F* key strings to a set of constants/enum.
EmitSignal(SignalName.ShowSpecificAdvisor, "F1");
break;
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions C7/UIElements/Advisors/Advisors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ private void OnShowSpecificAdvisor(string advisorType) {
}

if (advisorType.Equals("F1")) {
if (domesticAdvisor == null) {
domesticAdvisor = new DomesticAdvisor();
advisors.Add(domesticAdvisor);
AddChild(domesticAdvisor);
} else {
domesticAdvisor.Show();
if (domesticAdvisor != null) {
RemoveChild(domesticAdvisor);
domesticAdvisor = null;
}

domesticAdvisor = new DomesticAdvisor();
advisors.Add(domesticAdvisor);
AddChild(domesticAdvisor);
this.Show();
}
if (advisorType.Equals("F6")) {
Expand Down
74 changes: 71 additions & 3 deletions C7/UIElements/Advisors/DomesticAdvisor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
using C7Engine;
using C7GameData;
using Godot;
using System;

public partial class DomesticAdvisor : TextureRect {
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
TextureRect scienceSliderIcon = new();
Label scienceSliderLabel = new();
TextureRect luxurySliderIcon = new();
Label luxurySliderLabel = new();

// The Y position of the two sliders.
private int scienceSliderY = 84;
private int luxurySliderY = 130;

// Called when the node enters the scene tree for the first time.
public override void _Ready() {
Expand Down Expand Up @@ -45,9 +52,70 @@ private void CreateUI() {
GoBackButton.SetPosition(new Vector2(952, 720));
AddChild(GoBackButton);
GoBackButton.Pressed += ReturnToMenu;

ImageTexture scienceSliderTexture = Util.LoadTextureFromPCX("Art/city screen/CityIcons.pcx", 34, 2, 30, 30);
ImageTexture luxurySliderTexture = Util.LoadTextureFromPCX("Art/city screen/CityIcons.pcx", 376, 2, 30, 30);

int scienceRate;
int luxuryRate;
using (UIGameDataAccess gameDataAccess = new()) {
Player player = gameDataAccess.gameData.GetHumanPlayers()[0];
scienceRate = player.scienceRate;
luxuryRate = player.luxuryRate;
}

scienceSliderIcon.Texture = scienceSliderTexture;
scienceSliderIcon.SetPosition(new Vector2(CalculateSliderXPos(scienceRate), scienceSliderY));
AddChild(scienceSliderIcon);

luxurySliderIcon.Texture = luxurySliderTexture;
luxurySliderIcon.SetPosition(new Vector2(CalculateSliderXPos(luxuryRate), luxurySliderY));
AddChild(luxurySliderIcon);

scienceSliderLabel.Text = $"{scienceRate * 10}%";
scienceSliderLabel.SetPosition(new Vector2(760, scienceSliderY + 6));
AddChild(scienceSliderLabel);

luxurySliderLabel.Text = $"{luxuryRate * 10}%";
luxurySliderLabel.SetPosition(new Vector2(760, luxurySliderY + 4));
AddChild(luxurySliderLabel);

ImageTexture plusTexture = Util.LoadTextureFromPCX("Art/Advisors/domestic_icons_aux.pcx", 75, 1, 22, 22);
ImageTexture minusTexture = Util.LoadTextureFromPCX("Art/Advisors/domestic_icons_aux.pcx", 51, 1, 22, 22);

TextureButton moreScience = new();
moreScience.TextureNormal = plusTexture;
moreScience.SetPosition(new Vector2(725, scienceSliderY + 25));
moreScience.Pressed += () => { new MsgChangeSliders(true, false, false, false).send(); };
AddChild(moreScience);

TextureButton lessScience = new();
lessScience.TextureNormal = minusTexture;
lessScience.SetPosition(new Vector2(575, scienceSliderY + 25));
lessScience.Pressed += () => { new MsgChangeSliders(false, true, false, false).send(); };
AddChild(lessScience);

TextureButton moreLuxury = new();
moreLuxury.TextureNormal = plusTexture;
moreLuxury.SetPosition(new Vector2(725, luxurySliderY - 5));
moreLuxury.Pressed += () => { new MsgChangeSliders(false, false, true, false).send(); };
AddChild(moreLuxury);

TextureButton lessLuxury = new();
lessLuxury.TextureNormal = minusTexture;
lessLuxury.SetPosition(new Vector2(575, luxurySliderY - 5));
lessLuxury.Pressed += () => { new MsgChangeSliders(false, false, false, true).send(); };
AddChild(lessLuxury);
}

private void ReturnToMenu() {
GetParent<Advisors>().Hide();
}

private int CalculateSliderXPos(int sliderRate) {
int minX = 570;
int maxX = 725;

return minX + (int)((maxX - minX) * (sliderRate / 10.0));
}
}
61 changes: 61 additions & 0 deletions C7Engine/EntryPoints/MessageToEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,67 @@ public override void process() {
}
}

public class MsgChangeSliders : MessageToEngine {
private bool moreScience;
private bool lessScience;
private bool moreLuxury;
private bool lessLuxury;

public MsgChangeSliders(bool moreScience, bool lessScience, bool moreLuxury, bool lessLuxury) {
this.moreScience = moreScience;
this.lessScience = lessScience;
this.moreLuxury = moreLuxury;
this.lessLuxury = lessLuxury;
}

public override void process() {
Player player = EngineStorage.gameData.GetHumanPlayers()[0];

if (moreScience && player.scienceRate == 10 || lessScience && player.scienceRate == 0) {
return;
}
if (moreLuxury && player.luxuryRate == 10 || lessLuxury && player.luxuryRate == 0) {
return;
}

// Increase our science rate, taking away from tax rate if we can,
// otherwise decrease the luxury rate.
if (moreScience) {
player.scienceRate++;
if (player.taxRate > 0) {
player.taxRate--;
} else {
player.luxuryRate--;
}
}

// Ditto for luxury.
if (moreLuxury) {
player.luxuryRate++;
if (player.taxRate > 0) {
player.taxRate--;
} else {
player.scienceRate--;
}
}

// Decreasing is easier, we decrease the requested slider and bump
// up the tax rate.
if (lessScience) {
player.scienceRate--;
player.taxRate++;
}

if (lessLuxury) {
player.luxuryRate--;
player.taxRate++;
}

// Update the ui to reflect our changes.
new MsgUpdateUiAfterSliderChange().send();
}
}

public class MsgEndTurn : MessageToEngine {

private ILogger log = Log.ForContext<MsgEndTurn>();
Expand Down
2 changes: 2 additions & 0 deletions C7Engine/EntryPoints/MessageToUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class MsgUpdateUiAfterMove : MessageToUI { }

public class MsgUpdateUiAfterTechSelection : MessageToUI { }

public class MsgUpdateUiAfterSliderChange : MessageToUI { }

public class MsgCityDestroyed : MessageToUI {
public City city;

Expand Down

0 comments on commit 9bcbeee

Please sign in to comment.