Level Data decentralization

This commit is contained in:
Фёдор Веселов 2024-09-22 20:36:13 +05:00
commit e3613b132e
12 changed files with 52 additions and 28 deletions

View file

@ -10,11 +10,6 @@ public partial class LevelController : Node
private bool _isLevelRunning = false; 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() public override void _EnterTree()
{ {
Instance = this; Instance = this;
@ -48,10 +43,6 @@ public partial class LevelController : Node
if (_isLevelRunning == false) if (_isLevelRunning == false)
return; return;
LevelData = null;
Pools = null;
PlantField = null;
_isLevelRunning = false; _isLevelRunning = false;
} }
} }

View file

@ -16,7 +16,7 @@ public partial class Sun : Area2D
{ {
if (@event.IsActionPressed("primary_action")) if (@event.IsActionPressed("primary_action"))
{ {
LevelController.Instance.LevelData.AddSun(amount); RuntimeLevelData.Instance.AddSun(amount);
QueueFree(); QueueFree();
} }
} }

View file

@ -35,7 +35,7 @@ public partial class ShovelButton : TextureButton
for (int i = Utility.LayersCount-1; i >= 0; i--) 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(); plantData.Kill();
break; break;

View file

@ -5,6 +5,6 @@ public partial class SunCounter : Label
{ {
public override void _Process(double delta) public override void _Process(double delta)
{ {
Text = LevelController.Instance.LevelData.SunCount.ToString(); Text = RuntimeLevelData.Instance.SunCount.ToString();
} }
} }

View file

@ -6,20 +6,28 @@ public partial class PlantField : Node2D
private readonly Vector2 tile = new Vector2(Utility.TileWidth, Utility.TileHeight); private readonly Vector2 tile = new Vector2(Utility.TileWidth, Utility.TileHeight);
private PlantResource _resource; private PlantResource _resource;
private PlantSlot _slot; private Seedpacket _slot;
private bool _previousCanPlace; private bool _previousCanPlace;
public static PlantField Instance {get; private set;}
public override void _Ready() public override void _Ready()
{ {
LevelController.Instance.PlantField = this; Instance = this;
_plantSetter = GetChild<Sprite2D>(0); _plantSetter = GetChild<Sprite2D>(0);
} }
public void SetPlant(PlantSlot slot, PlantResource plant) public void SetPlant(Seedpacket slot, PlantResource plant)
{ {
_resource = plant; _resource = plant;
_slot = slot; _slot = slot;
} }
public void ResetPlant()
{
SetPlant(null,null);
}
public override void _Process(double delta) public override void _Process(double delta)
{ {
// Getting and storing global mouse position, setting plant-poiner to it // Getting and storing global mouse position, setting plant-poiner to it
@ -35,8 +43,8 @@ public partial class PlantField : Node2D
bool canPlace = _resource != null bool canPlace = _resource != null
&& inBoundary && inBoundary
&& LevelController.Instance.Pools.EntityField[_resource.Layer].ContainsKey(expected_pos) == false && PoolContainer.Instance.EntityField[_resource.Layer].ContainsKey(expected_pos) == false
&& LevelController.Instance.LevelData.CheckSpendSun(_resource.Cost); && RuntimeLevelData.Instance.CheckSpendSun(_resource.Cost);
// Setting visuals // Setting visuals
if (_previousCanPlace != canPlace) if (_previousCanPlace != canPlace)
@ -69,14 +77,14 @@ public partial class PlantField : Node2D
if (@event.IsActionPressed("primary_action") && _previousCanPlace) if (@event.IsActionPressed("primary_action") && _previousCanPlace)
{ {
var plant = _resource.Scene.Instantiate<RuntimePlantData>(); var plant = _resource.Scene.Instantiate<RuntimePlantData>();
LevelController.Instance.Pools.Plants.AddChild(plant); PoolContainer.Instance.Plants.AddChild(plant);
plant.GlobalPosition = (_plantSetter.GlobalPosition / tile).Ceil() * tile - new Vector2(20, 14); plant.GlobalPosition = (_plantSetter.GlobalPosition / tile).Ceil() * tile - new Vector2(20, 14);
plant.Resource = _resource; plant.Resource = _resource;
plant.Line = (int)plant.GlobalPosition.Y/Utility.TileHeight; 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 // Unfocusing and recharging slot
_slot.Recharge(); _slot.Recharge();

View file

@ -17,9 +17,11 @@ public partial class PoolContainer : Node
[Export] [Export]
public Node2D Structures { get; private set; } public Node2D Structures { get; private set; }
public static PoolContainer Instance {get; private set;}
public Dictionary<Vector2, IEntity>[] EntityField = { new Dictionary<Vector2, IEntity>(), new Dictionary<Vector2, IEntity>(), new Dictionary<Vector2, IEntity>() }; public Dictionary<Vector2, IEntity>[] EntityField = { new Dictionary<Vector2, IEntity>(), new Dictionary<Vector2, IEntity>(), new Dictionary<Vector2, IEntity>() };
public override void _Ready() public override void _Ready()
{ {
LevelController.Instance.Pools = this; Instance = this;
} }
} }

View file

@ -1,17 +1,33 @@
using System;
using Godot; using Godot;
//using Godot.Collections; //using Godot.Collections;
using System;
public partial class RuntimeLevelData : Node public partial class RuntimeLevelData : Node
{ {
public enum LevelStates
{
ChooseYourSeeds,
Pregame,
Game,
Win,
Loose
}
[Export] [Export]
public int SunCount { get; private set; } = 0; public int SunCount { get; private set; } = 0;
public event Action<LevelStates> OnLevelStateChanged;
public static RuntimeLevelData Instance { get; private set; }
private LevelStates _currentState = LevelStates.ChooseYourSeeds;
public override void _Ready() public override void _Ready()
{ {
LevelController.Instance.LevelData = this; Instance = this;
GetTree().Paused = true;
} }
#region Sun
public void AddSun(int amount) public void AddSun(int amount)
{ {
SunCount += amount; SunCount += amount;
@ -27,6 +43,13 @@ public partial class RuntimeLevelData : Node
return true; return true;
} }
#endregion
public void SetLevelState(LevelStates state)
{
OnLevelStateChanged(state);
_currentState = state;
}
//private Array<PlantResource> _selectedPlants; //private Array<PlantResource> _selectedPlants;
//private bool _selected; //private bool _selected;

View file

@ -17,7 +17,7 @@ public partial class SunSpawner : Node
uint y = GD.Randi() % 5; uint y = GD.Randi() % 5;
var sun = SunScene.Instantiate<Sun>(); var sun = SunScene.Instantiate<Sun>();
LevelController.Instance.Pools.Projectiles.AddChild(sun); PoolContainer.Instance.Projectiles.AddChild(sun);
sun.GlobalPosition = new Vector2(Utility.LeftFieldBoundary.X + x, -90); sun.GlobalPosition = new Vector2(Utility.LeftFieldBoundary.X + x, -90);

View file

@ -13,7 +13,7 @@ public partial class PlantSunSpawner : Node2D
var sun = _sunScene.Instantiate<Sun>(); var sun = _sunScene.Instantiate<Sun>();
sun.amount = _amountPerSun; sun.amount = _amountPerSun;
LevelController.Instance.Pools.Projectiles.AddChild(sun); PoolContainer.Instance.Projectiles.AddChild(sun);
sun.GlobalPosition = GlobalPosition; sun.GlobalPosition = GlobalPosition;
} }
} }

View file

@ -48,7 +48,7 @@ public partial class RuntimePlantData : Node2D, IEntity
} }
public void Kill() public void Kill()
{ {
LevelController.Instance.Pools.EntityField[Resource.Layer].Remove(GlobalPosition); PoolContainer.Instance.EntityField[Resource.Layer].Remove(GlobalPosition);
QueueFree(); QueueFree();
} }
} }

View file

@ -20,7 +20,7 @@ public partial class Shooter : Node2D
_timer.Start(); _timer.Start();
var instance = _projectile.Instantiate<Node2D>(); var instance = _projectile.Instantiate<Node2D>();
LevelController.Instance.Pools.Projectiles.AddChild(instance); PoolContainer.Instance.Projectiles.AddChild(instance);
instance.GlobalTransform = GlobalTransform; instance.GlobalTransform = GlobalTransform;
if (instance is IProjectile projectile) if (instance is IProjectile projectile)

View file

@ -7,7 +7,7 @@ public partial class Cheats : Node
{ {
if (@event.IsActionPressed("cheat_add_sun")) if (@event.IsActionPressed("cheat_add_sun"))
{ {
LevelController.Instance.LevelData.AddSun(50); RuntimeLevelData.Instance.AddSun(50);
} }
} }
} }