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

@ -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<Sprite2D>(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<RuntimePlantData>();
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();

View file

@ -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<Vector2, IEntity>[] EntityField = { new Dictionary<Vector2, IEntity>(), new Dictionary<Vector2, IEntity>(), new Dictionary<Vector2, IEntity>() };
public override void _Ready()
{
LevelController.Instance.Pools = this;
Instance = this;
}
}

View file

@ -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<LevelStates> 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<PlantResource> _selectedPlants;
//private bool _selected;

View file

@ -17,7 +17,7 @@ public partial class SunSpawner : Node
uint y = GD.Randi() % 5;
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);