Plants flash and shovel alt
This commit is contained in:
parent
b4bf3ab8bc
commit
e4c25a1ca5
23 changed files with 451 additions and 195 deletions
|
|
@ -1,16 +1,26 @@
|
|||
using Godot;
|
||||
using Newlon.Components.Plants;
|
||||
using Newlon.Components.Level;
|
||||
using Newlon.Components.Zombies;
|
||||
|
||||
namespace Newlon.Components.GUI;
|
||||
|
||||
public partial class ShovelButton : TextureButton
|
||||
{
|
||||
[Export] private PackedScene particles;
|
||||
private Entity hoveredEntity;
|
||||
private bool Selected => hoveredEntity != null;
|
||||
[Export]
|
||||
private RayCast2D raycast;
|
||||
private void OnFocusExited()
|
||||
{
|
||||
ButtonPressed = false;
|
||||
}
|
||||
public override void _Ready()
|
||||
{
|
||||
raycast.Reparent(PoolContainer.Instance);
|
||||
}
|
||||
|
||||
public override void _Toggled(bool toggledOn)
|
||||
{
|
||||
Cursor.Instance.shovel = toggledOn;
|
||||
|
|
@ -22,24 +32,118 @@ public partial class ShovelButton : TextureButton
|
|||
|
||||
if (@event.IsActionPressed("primary_action") && ButtonPressed)
|
||||
{
|
||||
var checkedPosition = (PoolContainer.Instance.Plants.GetGlobalMousePosition() / FieldParams.Tile).Ceil() * FieldParams.Tile - new Vector2(20, 14);
|
||||
|
||||
for (int i = FieldParams.LayersCount - 1; i >= 0; i--)
|
||||
if (hoveredEntity != null)
|
||||
{
|
||||
if (PoolContainer.Instance.EntityField[i].TryGetValue(checkedPosition, out var entity) && entity is RuntimePlantData plantData)
|
||||
if (hoveredEntity is RuntimePlantData hoveredPlant)
|
||||
{
|
||||
plantData.Kill();
|
||||
|
||||
PoolContainer.Instance.SpawnParticles(particles, plantData.GlobalPosition + Vector2.Down * FieldParams.TileHeight / 2.0f);
|
||||
|
||||
break;
|
||||
hoveredPlant.Kill();
|
||||
PoolContainer.Instance.SpawnParticles(particles, hoveredPlant.GlobalPosition + Vector2.Down * FieldParams.TileHeight / 2.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
hoveredEntity.TakeDamage(205, null);
|
||||
}
|
||||
}
|
||||
hoveredEntity?.GetNode<FlashShaderController>("FlashController").Deselect();
|
||||
hoveredEntity = null;
|
||||
ButtonPressed = false;
|
||||
}
|
||||
if (@event.IsActionPressed("cancel_plant") && ButtonPressed)
|
||||
{
|
||||
hoveredEntity?.GetNode<FlashShaderController>("FlashController").Deselect();
|
||||
hoveredEntity = null;
|
||||
ButtonPressed = false;
|
||||
}
|
||||
}
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
|
||||
if (ButtonPressed)
|
||||
{
|
||||
var gridEntity = GetTile(PoolContainer.Instance.Plants.GetGlobalMousePosition());
|
||||
if (TrySetGridEntity(gridEntity)) return;
|
||||
if (TrySetDynEntity()) return;
|
||||
|
||||
else
|
||||
{
|
||||
if (Selected)
|
||||
{
|
||||
hoveredEntity.GetNode<FlashShaderController>("FlashController").Deselect();
|
||||
hoveredEntity = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Selected)
|
||||
{
|
||||
hoveredEntity.GetNode<FlashShaderController>("FlashController").Deselect();
|
||||
hoveredEntity = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
private Entity GetTile(Vector2 position)
|
||||
{
|
||||
var checkedPosition = (position / FieldParams.Tile).Ceil() * FieldParams.Tile - new Vector2(20, 14);
|
||||
for (int i = FieldParams.LayersCount - 1; i >= 0; i--)
|
||||
{
|
||||
if (PoolContainer.Instance.EntityField[i].TryGetValue(checkedPosition, out var entity) && entity is RuntimePlantData plantData)
|
||||
{
|
||||
return plantData;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private bool TrySetGridEntity(Entity chosenEntity)
|
||||
{
|
||||
if (chosenEntity != null && Selected == false)
|
||||
{
|
||||
hoveredEntity = chosenEntity;
|
||||
hoveredEntity.GetNode<FlashShaderController>("FlashController").Select();
|
||||
return true;
|
||||
}
|
||||
if (chosenEntity == null && Selected && hoveredEntity is RuntimePlantData)
|
||||
{
|
||||
hoveredEntity.GetNode<FlashShaderController>("FlashController").Deselect();
|
||||
hoveredEntity = null;
|
||||
return true;
|
||||
}
|
||||
if (hoveredEntity == null || chosenEntity == null) return false;
|
||||
if (chosenEntity != hoveredEntity && Selected)
|
||||
{
|
||||
hoveredEntity.GetNode<FlashShaderController>("FlashController").Deselect();
|
||||
hoveredEntity = chosenEntity;
|
||||
hoveredEntity.GetNode<FlashShaderController>("FlashController").Select();
|
||||
return true;
|
||||
}
|
||||
return hoveredEntity != null && hoveredEntity is RuntimeZombieData == false;
|
||||
}
|
||||
private bool TrySetDynEntity()
|
||||
{
|
||||
raycast.GlobalPosition = PoolContainer.Instance.GetGlobalMousePosition();
|
||||
|
||||
if (raycast.IsColliding())
|
||||
{
|
||||
var entity = ((Area2D)raycast.GetCollider()).GetParent() as Entity;
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
if (Selected == false)
|
||||
{
|
||||
hoveredEntity = entity;
|
||||
hoveredEntity.GetNode<FlashShaderController>("FlashController").Select();
|
||||
return true;
|
||||
}
|
||||
if (hoveredEntity == null) return false;
|
||||
if (Selected && hoveredEntity != entity)
|
||||
{
|
||||
hoveredEntity.GetNode<FlashShaderController>("FlashController").Deselect();
|
||||
entity.GetNode<FlashShaderController>("FlashController").Select();
|
||||
hoveredEntity = entity;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return hoveredEntity != null && hoveredEntity is RuntimeZombieData;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue