Level Data decentralization
This commit is contained in:
parent
76b33db91f
commit
e3613b132e
12 changed files with 52 additions and 28 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ public partial class Sun : Area2D
|
|||
{
|
||||
if (@event.IsActionPressed("primary_action"))
|
||||
{
|
||||
LevelController.Instance.LevelData.AddSun(amount);
|
||||
RuntimeLevelData.Instance.AddSun(amount);
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ public partial class PlantSunSpawner : Node2D
|
|||
var sun = _sunScene.Instantiate<Sun>();
|
||||
sun.amount = _amountPerSun;
|
||||
|
||||
LevelController.Instance.Pools.Projectiles.AddChild(sun);
|
||||
PoolContainer.Instance.Projectiles.AddChild(sun);
|
||||
sun.GlobalPosition = GlobalPosition;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ public partial class Shooter : Node2D
|
|||
|
||||
_timer.Start();
|
||||
var instance = _projectile.Instantiate<Node2D>();
|
||||
LevelController.Instance.Pools.Projectiles.AddChild(instance);
|
||||
PoolContainer.Instance.Projectiles.AddChild(instance);
|
||||
instance.GlobalTransform = GlobalTransform;
|
||||
|
||||
if (instance is IProjectile projectile)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ public partial class Cheats : Node
|
|||
{
|
||||
if (@event.IsActionPressed("cheat_add_sun"))
|
||||
{
|
||||
LevelController.Instance.LevelData.AddSun(50);
|
||||
RuntimeLevelData.Instance.AddSun(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue