Commit cb5b7538 authored by Emery Tanghanwaye's avatar Emery Tanghanwaye
Browse files

waterGame: add scrolling support

parent 28cb810e
......@@ -8,17 +8,28 @@ public class WButton : Button
// static properties of each button
private readonly float BasePrice;
private readonly float CashPerSecond;
private readonly string UnitName;
private readonly string _UnitName;
private readonly bool IsBad;
private Game Game;
// more dynamic properties of each button
private int NumUnits = 0;
private int _NumUnits = 0;
private float CurrentPrice = 0f;
public int GetNumUnits()
public int NumUnits
{
return NumUnits;
get
{
return _NumUnits;
}
}
public string UnitName
{
get
{
return _UnitName;
}
}
public float Purchase(float money)
......@@ -28,7 +39,7 @@ public class WButton : Button
if (money >= CurrentPrice)
{
moneyLeft = (money - CurrentPrice);
NumUnits += 1;
_NumUnits += 1;
CurrentPrice += (CurrentPrice * 0.5f);
}
......@@ -49,7 +60,7 @@ public class WButton : Button
public override void _PhysicsProcess(float delta)
{
Text = $"Buy a {UnitName} (Cost: {CurrentPrice:C2}, makes {CashPerSecond:C2}/s)";
Text = $"{UnitName} (Cost: {CurrentPrice:C2}, makes {CashPerSecond:C2}/s)";
}
public void OnButtonRelease()
......@@ -72,7 +83,7 @@ public class WButton : Button
public WButton(string unitName, float basePrice, float cashPerSecond, bool isBad) : base()
{
this.UnitName = unitName;
this._UnitName = unitName;
this.BasePrice = basePrice;
this.CashPerSecond = cashPerSecond;
this.IsBad = isBad;
......
using Godot;
using System;
public class Camera : Control
{
private Camera2D Cam;
private HScrollBar ScrlBar;
private Vector2 OriginalPos;
public override void _Ready()
{
Cam = GetNode<Camera2D>("Camera2D");
ScrlBar = GetNode<HScrollBar>("CanvasLayer/HScrollBar");
OriginalPos = Cam.Offset;
}
public override void _PhysicsProcess(float delta)
{
Vector2 nextPos = OriginalPos;
nextPos.x = OriginalPos.x + (ScrlBar.Value);
Cam.Offset = nextPos;
}
}
\ No newline at end of file
......@@ -10,7 +10,6 @@ public class Game : Node2D
// Labels for different text labels in the game
private Label MoneyLabel;
private Label WellLabel;
private Label ProfitLabel;
// List of all the buttons in the game
......@@ -51,38 +50,46 @@ public class Game : Node2D
public void IncreaseBadPoints() { BadPoints++; }
private void CreateButtons()
private void CreateFirstStageButtons()
{
WButton wellButton = new WButton("Well", 10, 1, false);
WButton wellButton = new WButton("Wells", 10, 1, false);
GUI.AddChild(wellButton);
Buttons.Add(wellButton);
wellButton.SetPosition(new Vector2(90f, 180f));
wellButton.SetSize(new Vector2(220f, 100f));
WLabel wellLabel = new WLabel(wellButton);
GUI.AddChild(wellLabel);
WButton ecoFiltButton = new WButton("Eco-Friendly Filtration", 25, 2, false);
GUI.AddChild(ecoFiltButton);
Buttons.Add(ecoFiltButton);
ecoFiltButton.SetPosition(new Vector2(90f, 370f));
ecoFiltButton.SetSize(new Vector2(220f, 100f));
WLabel ecoFiltLabel = new WLabel(ecoFiltButton);
GUI.AddChild(ecoFiltLabel);
WButton cheapFiltButton = new WButton("Cheap Filtration", 15, 5, true);
GUI.AddChild(cheapFiltButton);
Buttons.Add(cheapFiltButton);
cheapFiltButton.SetPosition(new Vector2(500f, 370f));
cheapFiltButton.SetSize(new Vector2(220f, 100f));
WLabel cheapFiltLabel = new WLabel(cheapFiltButton);
GUI.AddChild(cheapFiltLabel);
}
public override void _Ready()
{
// Pull in our required labels
MoneyLabel = GetNode<Label>("GUI/MoneyLabel");
WellLabel = GetNode<Label>("GUI/WellLabel");
ProfitLabel = GetNode<Label>("GUI/ProfitLabel");
MoneyLabel = GetNode<Label>("GUI/CanvasLayer/MoneyLabel");
ProfitLabel = GetNode<Label>("GUI/CanvasLayer/ProfitLabel");
// Pull in GUI reference
GUI = GetNode<Control>("GUI");
CreateButtons();
CreateFirstStageButtons();
}
public override void _PhysicsProcess(float delta)
......@@ -90,7 +97,6 @@ public class Game : Node2D
Money += ProfitRate * delta;
MoneyLabel.Text = $"{Money:C2}";
ProfitLabel.Text = $"{ProfitRate:C2} per second";
WellLabel.Text = $"Wells: {Buttons.ToArray()[0].GetNumUnits()}";
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
......
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=3 format=2]
[ext_resource path="res://Game.cs" type="Script" id=1]
[ext_resource path="res://Camera.cs" type="Script" id=2]
[node name="Game" type="Node2D"]
script = ExtResource( 1 )
[node name="Camera" type="Control" parent="."]
margin_left = 60.0
margin_top = 660.0
margin_right = 100.0
margin_bottom = 700.0
script = ExtResource( 2 )
[node name="Camera2D" type="Camera2D" parent="Camera"]
offset = Vector2( 580, -300 )
current = true
[node name="CanvasLayer" type="CanvasLayer" parent="Camera"]
[node name="HScrollBar" type="HScrollBar" parent="Camera/CanvasLayer"]
margin_left = 60.0
margin_top = 660.0
margin_right = 1220.0
margin_bottom = 690.0
max_value = 1000.0
[node name="GUI" type="Control" parent="."]
margin_right = 40.0
margin_bottom = 40.0
[node name="MoneyLabel" type="Label" parent="GUI"]
[node name="CanvasLayer" type="CanvasLayer" parent="GUI"]
[node name="ProfitLabel" type="Label" parent="GUI/CanvasLayer"]
margin_left = 25.9466
margin_top = 82.4186
margin_right = 65.9466
margin_bottom = 96.4186
[node name="MoneyLabel" type="Label" parent="GUI/CanvasLayer"]
margin_left = 19.2844
margin_top = 44.0787
margin_right = 59.2844
margin_bottom = 58.0787
text = "$0"
[node name="WellLabel" type="Label" parent="GUI"]
margin_left = 22.2763
margin_top = 64.8037
margin_right = 62.2763
margin_bottom = 78.8037
text = "Wells: 0"
[node name="ProfitLabel" type="Label" parent="GUI"]
margin_left = 25.9466
margin_top = 82.4185
margin_right = 65.9466
margin_bottom = 96.4185
using Godot;
using System;
public class WLabel : Label
{
private readonly WButton WButton;
public override void _PhysicsProcess(float delta)
{
Text = $"{WButton.UnitName}: {WButton.NumUnits}";
}
public WLabel() : this(null) { }
public WLabel(WButton wbutton) : base()
{
this.WButton = wbutton;
Vector2 LabelPos = new Vector2(WButton.GetPosition().x, (WButton.GetPosition().y + WButton.GetSize().y + 2f));
this.SetPosition(LabelPos);
}
}
\ No newline at end of file
......@@ -19,6 +19,12 @@ config/name="WaterGame"
run/main_scene="res://Game.tscn"
config/icon="res://icon.png"
[display]
window/size/width=1280
window/size/height=720
window/stretch/mode="viewport"
[mono]
project/auto_update_project=false
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment