Skip to content

Commit

Permalink
Purhasable upgrades.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kotáb Petr committed Jan 9, 2023
1 parent a3bfc1e commit 50ae768
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 11 deletions.
2 changes: 2 additions & 0 deletions TestGame/Components/Inventory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;

using TestGame.UI;

namespace TestGame.Components
{
Expand All @@ -10,6 +11,7 @@ internal class Inventory
public int Coins;
public float CurrentWater;
public float MaxWater;
public InventoryUI ui;

public Item SelectedItem => Selected == -1 ? null : Slots[Selected];

Expand Down
2 changes: 2 additions & 0 deletions TestGame/GlobalModifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
internal static class GlobalModifiers
{
public static float WaterDecreaseSpeed = 1.0f;
public static float RainChance = 0.3f;
public static float PlantOvergrownModifier = 1.0f;
}
}
5 changes: 3 additions & 2 deletions TestGame/Scenes/LevelScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace TestGame.Scenes
internal class LevelScene : Scene
{
private const int InitialInventorySize = 3;
private const int InitialFarmColumns = 10;
private const int InitialFarmColumns = 9;

private Vector2 windowSize;
private InventoryUI inventoryUI;
Expand Down Expand Up @@ -117,7 +117,7 @@ private void CreateInventory()

private void CreateFarm()
{
for (int i = 0; i < InitialFarmColumns; i++)
for (int i = 0; i < InitialFarmColumns + 2; i++)
PlantUtils.CreateFarmColumn(World);
}

Expand Down Expand Up @@ -204,6 +204,7 @@ private void CreateInventoryUI()
}
};

Inventory.ui = inventoryUI;
UILayer.AddElement(inventoryUI);
}

Expand Down
94 changes: 89 additions & 5 deletions TestGame/Scenes/ShopScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ namespace TestGame.Scenes
{
internal class ShopScene : Scene
{
private const float wateringUpgradeStep = 0.1f;
private const int wateringUpgradePrice = 100;

private const float plantOvergrownUpgradeStep = 0.1f;
private const int plantOvergrownUpragePrice = 100;

private const float rainChanceUpgradeStep = 0.1f;
private const float maxRainChangeUpgrade = 0.7f;
private const int rainChanceUpgradePrice = 100;

private const int waterCanUpgradePrice = 100;

private const int maxInventorySlots = 10;
private const int inventoryUpgradePrice = 100;

private const int rowSize = 7;
Expand Down Expand Up @@ -52,15 +64,16 @@ private void CreateUpgradeButtons()
Texture = Game.SpriteManager["scrollsandblocks"],
SourceRectange = new Rectangle(0, 64, 96, 32),
},
Scale = new Vector2(2.5f),
Scale = new Vector2(2.0f),
};
var font = Game.FontManager[new FontDescriptor()
{
Name = "calibri",
FontHeight = 32,
FontHeight = 24,
}];
float yPosition = windowSize.Y * 0.7f;

// water can upgrade
buttons.Add(new Button()
{
Apperance = apperance,
Expand All @@ -69,9 +82,14 @@ private void CreateUpgradeButtons()
});
buttons.Last().Clicked += (sender, e) =>
{
System.Console.WriteLine("water can upgrade");
if (inventory.Coins < waterCanUpgradePrice)
return;
inventory.Coins -= waterCanUpgradePrice;
inventory.MaxWater++;
};

// inventory uprade
buttons.Add(new Button()
{
Apperance = apperance.Clone(),
Expand All @@ -80,14 +98,80 @@ private void CreateUpgradeButtons()
});
buttons.Last().Clicked += (sender, e) =>
{
System.Console.WriteLine("inventory upgrade");
if (inventory.Coins < inventoryUpgradePrice)
return;
inventory.Coins -= inventoryUpgradePrice;
inventory.Slots.Add(null);
inventory.ui.Apperance.Position.X -= inventory.ui.Apperance.Size.X / 2;
if (inventory.Slots.Count >= maxInventorySlots)
{
(sender as Button).Enabled = false;
(sender as Button).Apperance.Sprite.Color = Color.Gray;
}
};

// rain chance upgrade
buttons.Add(new Button()
{
Apperance = apperance.Clone(),
Font = font,
Text = $"Rain Chance - {rainChanceUpgradePrice}",
});
buttons.Last().Clicked += (sender, e) =>
{
if (inventory.Coins < rainChanceUpgradePrice)
return;
inventory.Coins -= rainChanceUpgradePrice;
GlobalModifiers.RainChance += rainChanceUpgradeStep;
if (GlobalModifiers.RainChance >= maxRainChangeUpgrade)
{
(sender as Button).Enabled = false;
(sender as Button).Apperance.Sprite.Color = Color.Gray;
}
};

// plant overgrown upgrade
buttons.Add(new Button()
{
Apperance = apperance.Clone(),
Font = font,
Text = $"Overgrown - {plantOvergrownUpragePrice}",
});
buttons.Last().Clicked += (sender, e) =>
{
if (inventory.Coins < plantOvergrownUpragePrice)
return;
inventory.Coins -= plantOvergrownUpragePrice;
GlobalModifiers.PlantOvergrownModifier += plantOvergrownUpgradeStep;
};

// watering upgrade
buttons.Add(new Button()
{
Apperance = apperance.Clone(),
Font = font,
Text = $"Watering - {wateringUpgradePrice}",
});
buttons.Last().Clicked += (sender, e) =>
{
if (inventory.Coins < wateringUpgradePrice)
return;
inventory.Coins -= wateringUpgradePrice;
GlobalModifiers.WaterDecreaseSpeed += wateringUpgradeStep;
};

for (int i = 0; i < buttons.Count; i++)
{
buttons[i].Apperance.Position = new Vector2()
{
X = (windowSize.X / (buttons.Count + 1)) * (i + 1) - buttons[i].Apperance.Size.X / 2,
X = (windowSize.X / (buttons.Count + 1)) * (i + 1)
- buttons[i].Apperance.Size.X / 2,
Y = yPosition,
};
UILayer.AddElement(buttons[i]);
Expand Down
3 changes: 2 additions & 1 deletion TestGame/Systems/PlantSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public override void Update(Plant plant, Apperance apperance, ProgressBar progre
}

// decay
if (plant.CurrentGrow >= plant.GrowDuration + plant.Type.MaxOvergrow)
if (plant.CurrentGrow >= plant.GrowDuration
+ (plant.Type.MaxOvergrow * GlobalModifiers.PlantOvergrownModifier))
{
if (PlantDecay)
{
Expand Down
5 changes: 4 additions & 1 deletion TestGame/Systems/WeatherSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public override void Draw(GameTime gameTime)
ellapsed -= nextWeatherEvent;

var events = Enum.GetValues<WeatherEvent>();
activeEvent = events[random.Next(1, events.Length)];
if (random.NextSingle() <= GlobalModifiers.RainChance)
activeEvent = WeatherEvent.Rain;
else
activeEvent = events[random.Next(2, events.Length)];
InvokeEvent();
}
else if (activeEvent != WeatherEvent.None && ellapsed >= weatherEventDuration)
Expand Down
3 changes: 3 additions & 0 deletions TestGame/UI/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ internal abstract class UIElement
{
public Apperance Apperance;

public bool Enabled = true;
public bool Visible = true;

public virtual void Update() { }
public virtual void Draw(SpriteBatch spriteBatch)
=> Apperance.Draw(spriteBatch);
Expand Down
6 changes: 4 additions & 2 deletions TestGame/UI/UILayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public void Update()
{
foreach (var control in controls)
{
control.Update();
if (control.Enabled)
control.Update();
}
}

Expand All @@ -27,7 +28,8 @@ public void Draw(SpriteBatch spriteBatch)
spriteBatch.Begin();
foreach (var control in controls)
{
control.Draw(spriteBatch);
if (control.Visible)
control.Draw(spriteBatch);
}
spriteBatch.End();
}
Expand Down

0 comments on commit 50ae768

Please sign in to comment.