diff --git a/scripts/LevelController.cs b/scripts/LevelController.cs index f62db81..d80d4b6 100644 --- a/scripts/LevelController.cs +++ b/scripts/LevelController.cs @@ -10,11 +10,6 @@ public partial class LevelController : Node private bool _isLevelRunning = false; - // Frequently accessed level elements - public RuntimeLevelData LevelData { get; set; } - public PoolContainer Pools { get; set; } - public PlantField PlantField { get; set; } - public override void _EnterTree() { Instance = this; @@ -48,10 +43,6 @@ public partial class LevelController : Node if (_isLevelRunning == false) return; - LevelData = null; - Pools = null; - PlantField = null; - _isLevelRunning = false; } } diff --git a/scripts/Sun.cs b/scripts/Sun.cs index bba8a44..5c966e8 100644 --- a/scripts/Sun.cs +++ b/scripts/Sun.cs @@ -16,7 +16,7 @@ public partial class Sun : Area2D { if (@event.IsActionPressed("primary_action")) { - LevelController.Instance.LevelData.AddSun(amount); + RuntimeLevelData.Instance.AddSun(amount); QueueFree(); } } diff --git a/scripts/components/gui/ShovelButton.cs b/scripts/components/gui/ShovelButton.cs index 6d1de2a..423a5ee 100644 --- a/scripts/components/gui/ShovelButton.cs +++ b/scripts/components/gui/ShovelButton.cs @@ -35,7 +35,7 @@ public partial class ShovelButton : TextureButton for (int i = Utility.LayersCount-1; i >= 0; i--) { - if (LevelController.Instance.Pools.EntityField[i].TryGetValue(checkedPosition, out var entity) && entity is RuntimePlantData plantData) + if (PoolContainer.Instance.EntityField[i].TryGetValue(checkedPosition, out var entity) && entity is RuntimePlantData plantData) { plantData.Kill(); break; diff --git a/scripts/components/gui/SunCounter.cs b/scripts/components/gui/SunCounter.cs index b2556da..f840303 100644 --- a/scripts/components/gui/SunCounter.cs +++ b/scripts/components/gui/SunCounter.cs @@ -5,6 +5,6 @@ public partial class SunCounter : Label { public override void _Process(double delta) { - Text = LevelController.Instance.LevelData.SunCount.ToString(); + Text = RuntimeLevelData.Instance.SunCount.ToString(); } } diff --git a/scripts/components/level/PlantField.cs b/scripts/components/level/PlantField.cs index a45f607..cefc3c7 100644 --- a/scripts/components/level/PlantField.cs +++ b/scripts/components/level/PlantField.cs @@ -6,20 +6,28 @@ public partial class PlantField : Node2D private readonly Vector2 tile = new Vector2(Utility.TileWidth, Utility.TileHeight); private PlantResource _resource; - private PlantSlot _slot; + private Seedpacket _slot; private bool _previousCanPlace; + + public static PlantField Instance {get; private set;} + public override void _Ready() { - LevelController.Instance.PlantField = this; + Instance = this; _plantSetter = GetChild(0); } - public void SetPlant(PlantSlot slot, PlantResource plant) + public void SetPlant(Seedpacket slot, PlantResource plant) { _resource = plant; _slot = slot; } + public void ResetPlant() + { + SetPlant(null,null); + } + public override void _Process(double delta) { // Getting and storing global mouse position, setting plant-poiner to it @@ -35,8 +43,8 @@ public partial class PlantField : Node2D bool canPlace = _resource != null && inBoundary - && LevelController.Instance.Pools.EntityField[_resource.Layer].ContainsKey(expected_pos) == false - && LevelController.Instance.LevelData.CheckSpendSun(_resource.Cost); + && PoolContainer.Instance.EntityField[_resource.Layer].ContainsKey(expected_pos) == false + && RuntimeLevelData.Instance.CheckSpendSun(_resource.Cost); // Setting visuals if (_previousCanPlace != canPlace) @@ -69,14 +77,14 @@ public partial class PlantField : Node2D if (@event.IsActionPressed("primary_action") && _previousCanPlace) { var plant = _resource.Scene.Instantiate(); - LevelController.Instance.Pools.Plants.AddChild(plant); + PoolContainer.Instance.Plants.AddChild(plant); plant.GlobalPosition = (_plantSetter.GlobalPosition / tile).Ceil() * tile - new Vector2(20, 14); plant.Resource = _resource; plant.Line = (int)plant.GlobalPosition.Y/Utility.TileHeight; - LevelController.Instance.Pools.EntityField[_resource.Layer].Add(plant.GlobalPosition, plant as IEntity); + PoolContainer.Instance.EntityField[_resource.Layer].Add(plant.GlobalPosition, plant as IEntity); - LevelController.Instance.LevelData.SpendSun(_resource.Cost); + RuntimeLevelData.Instance.SpendSun(_resource.Cost); // Unfocusing and recharging slot _slot.Recharge(); diff --git a/scripts/components/level/PoolContainer.cs b/scripts/components/level/PoolContainer.cs index 84503f5..8a15c3f 100644 --- a/scripts/components/level/PoolContainer.cs +++ b/scripts/components/level/PoolContainer.cs @@ -17,9 +17,11 @@ public partial class PoolContainer : Node [Export] public Node2D Structures { get; private set; } + public static PoolContainer Instance {get; private set;} + public Dictionary[] EntityField = { new Dictionary(), new Dictionary(), new Dictionary() }; public override void _Ready() { - LevelController.Instance.Pools = this; + Instance = this; } } diff --git a/scripts/components/level/RuntimeLevelData.cs b/scripts/components/level/RuntimeLevelData.cs index 54efc90..27894dd 100644 --- a/scripts/components/level/RuntimeLevelData.cs +++ b/scripts/components/level/RuntimeLevelData.cs @@ -1,17 +1,33 @@ +using System; using Godot; //using Godot.Collections; -using System; public partial class RuntimeLevelData : Node { + public enum LevelStates + { + ChooseYourSeeds, + Pregame, + Game, + Win, + Loose + } + [Export] public int SunCount { get; private set; } = 0; + public event Action OnLevelStateChanged; + + public static RuntimeLevelData Instance { get; private set; } + + private LevelStates _currentState = LevelStates.ChooseYourSeeds; public override void _Ready() { - LevelController.Instance.LevelData = this; + Instance = this; + GetTree().Paused = true; } + #region Sun public void AddSun(int amount) { SunCount += amount; @@ -27,6 +43,13 @@ public partial class RuntimeLevelData : Node return true; } + #endregion + + public void SetLevelState(LevelStates state) + { + OnLevelStateChanged(state); + _currentState = state; + } //private Array _selectedPlants; //private bool _selected; diff --git a/scripts/components/level/SunSpawner.cs b/scripts/components/level/SunSpawner.cs index 6b29156..5bd3ff9 100644 --- a/scripts/components/level/SunSpawner.cs +++ b/scripts/components/level/SunSpawner.cs @@ -17,7 +17,7 @@ public partial class SunSpawner : Node uint y = GD.Randi() % 5; var sun = SunScene.Instantiate(); - LevelController.Instance.Pools.Projectiles.AddChild(sun); + PoolContainer.Instance.Projectiles.AddChild(sun); sun.GlobalPosition = new Vector2(Utility.LeftFieldBoundary.X + x, -90); diff --git a/scripts/components/plants/PlantSunSpawner.cs b/scripts/components/plants/PlantSunSpawner.cs index 64973f3..5c7c231 100644 --- a/scripts/components/plants/PlantSunSpawner.cs +++ b/scripts/components/plants/PlantSunSpawner.cs @@ -13,7 +13,7 @@ public partial class PlantSunSpawner : Node2D var sun = _sunScene.Instantiate(); sun.amount = _amountPerSun; - LevelController.Instance.Pools.Projectiles.AddChild(sun); + PoolContainer.Instance.Projectiles.AddChild(sun); sun.GlobalPosition = GlobalPosition; } } diff --git a/scripts/components/plants/RuntimePlantData.cs b/scripts/components/plants/RuntimePlantData.cs index 2b2ccda..0b55a26 100644 --- a/scripts/components/plants/RuntimePlantData.cs +++ b/scripts/components/plants/RuntimePlantData.cs @@ -48,7 +48,7 @@ public partial class RuntimePlantData : Node2D, IEntity } public void Kill() { - LevelController.Instance.Pools.EntityField[Resource.Layer].Remove(GlobalPosition); + PoolContainer.Instance.EntityField[Resource.Layer].Remove(GlobalPosition); QueueFree(); } } diff --git a/scripts/components/plants/Shooter.cs b/scripts/components/plants/Shooter.cs index 9629a2e..8340823 100644 --- a/scripts/components/plants/Shooter.cs +++ b/scripts/components/plants/Shooter.cs @@ -20,7 +20,7 @@ public partial class Shooter : Node2D _timer.Start(); var instance = _projectile.Instantiate(); - LevelController.Instance.Pools.Projectiles.AddChild(instance); + PoolContainer.Instance.Projectiles.AddChild(instance); instance.GlobalTransform = GlobalTransform; if (instance is IProjectile projectile) diff --git a/scripts/debug/Cheats.cs b/scripts/debug/Cheats.cs index 963fe9f..fb277d2 100644 --- a/scripts/debug/Cheats.cs +++ b/scripts/debug/Cheats.cs @@ -7,7 +7,7 @@ public partial class Cheats : Node { if (@event.IsActionPressed("cheat_add_sun")) { - LevelController.Instance.LevelData.AddSun(50); + RuntimeLevelData.Instance.AddSun(50); } } }