Sun, Shovel, Fastforward, GUI Improvements

This commit is contained in:
Фёдор Веселов 2024-09-16 09:57:11 +05:00
commit 63935d5978
28 changed files with 546 additions and 45 deletions

View file

@ -4,15 +4,36 @@ using System;
public partial class Cursor : Node
{
public static Cursor Instance { get; private set; }
public bool shovel = false;
public bool plant = false;
public override void _Ready()
{
Instance = this;
SetDefaultCursor();
}
public void UpdateCursor()
{
if (shovel)
{
SetShovelCursor();
return;
}
if (plant)
{
SetPlantCursor();
return;
}
SetDefaultCursor() ;
}
public void SetDefaultCursor()
{
Input.SetCustomMouseCursor(ResourceLoader.Load<Texture2D>("res://assets/sprites/atlases/cursor/default_arrow.png"));
Input.SetCustomMouseCursor(ResourceLoader.Load<Texture2D>("res://assets/sprites/atlases/cursor/default_arrow.png"),shape:Input.CursorShape.Arrow);
Input.SetCustomMouseCursor(ResourceLoader.Load<Texture2D>("res://assets/sprites/atlases/cursor/default_point.png"),shape:Input.CursorShape.PointingHand);
}
@ -20,4 +41,9 @@ public partial class Cursor : Node
{
Input.SetCustomMouseCursor(ResourceLoader.Load<Texture2D>("res://assets/sprites/atlases/cursor/plant_arrow.png"));
}
public void SetShovelCursor()
{
Input.SetCustomMouseCursor(ResourceLoader.Load<Texture2D>("res://assets/sprites/atlases/cursor/shovel_arrow.png"));
}
}

31
scripts/Sun.cs Normal file
View file

@ -0,0 +1,31 @@
using Godot;
using System;
public partial class Sun : Area2D
{
[Export] public int amount = 25;
[Export] private Timer _deathTimer;
[Export] private AnimationPlayer _rotation;
[Export] private AnimationPlayer _fade;
public override void _Ready()
{
_rotation.SpeedScale = 1.0f + GD.Randf() / 2.0f;
}
public override void _InputEvent(Viewport viewport, InputEvent @event, int shapeIdx)
{
if (@event.IsActionPressed("primary_action"))
{
LevelController.Instance.LevelData.AddSun(amount);
QueueFree();
}
}
public override void _Process(double delta)
{
if (_deathTimer.TimeLeft/_deathTimer.WaitTime <= 0.25)
{
_fade.Play("main");
}
}
}

View file

@ -0,0 +1,31 @@
using Godot;
using System;
public partial class FastForwardButton : TextureButton
{
[Export] private Texture2D firstSpeed;
[Export] private Texture2D secondSpeed;
[Export] private Texture2D thirdSpeed;
private int speed = 1;
public void OnPressed()
{
speed = Mathf.Wrap(speed+1, 1, 4);
switch (speed)
{
case 1:
TextureNormal = firstSpeed;
break;
case 2:
TextureNormal = secondSpeed;
break;
case 3:
TextureNormal = thirdSpeed;
break;
}
Engine.TimeScale = speed;
}
}

View file

@ -8,11 +8,21 @@ public partial class PlantSlot : TextureButton
[Export] private TextureRect _icon;
[Export] private Timer _timer;
private bool _recharging;
public override void _Ready()
{
if (_resource != null)
UpdateContents();
}
public override void _Process(double delta)
{
if (_resource != null)
{
Disabled = _recharging || LevelController.Instance.LevelData.SunCount < _resource.Cost;
FocusMode = Disabled ? FocusModeEnum.None : FocusModeEnum.All;
}
}
public void SetPlantResource( PlantResource resource )
{
@ -43,8 +53,7 @@ public partial class PlantSlot : TextureButton
public void Recharge()
{
Disabled = true;
FocusMode = FocusModeEnum.None;
_recharging = true;
ReleaseFocus();
@ -53,7 +62,6 @@ public partial class PlantSlot : TextureButton
private void Timeout()
{
Disabled = false;
FocusMode = FocusModeEnum.All;
_recharging = false;
}
}

View file

@ -0,0 +1,39 @@
using Godot;
using System;
public partial class ShovelButton : TextureButton
{
private RayCast2D _shovelCast;
public override void _Ready()
{
_shovelCast = GetNode<RayCast2D>("RayCast2D");
_shovelCast.CallDeferred("reparent", GetTree().CurrentScene);
}
private void OnFocusExited()
{
ButtonPressed = false;
}
public override void _Process(double delta)
{
_shovelCast.GlobalPosition = _shovelCast.GetGlobalMousePosition();
}
public override void _Toggled(bool toggledOn)
{
_shovelCast.Enabled = toggledOn;
Cursor.Instance.shovel = toggledOn;
Cursor.Instance.UpdateCursor();
}
public override void _Input(InputEvent @event)
{
if (@event.IsActionPressed("primary_action"))
{
if (_shovelCast.IsColliding() && (_shovelCast.GetCollider() as CollisionObject2D).GetParent() is RuntimePlantData plant)
{
plant.Kill();
}
ButtonPressed = false;
}
}
}

View file

@ -7,6 +7,7 @@ public partial class PlantField : Node2D
private PlantResource _resource;
private PlantSlot _slot;
private bool _previousCanPlace;
public override void _Ready()
{
LevelController.Instance.PlantField = this;
@ -32,49 +33,52 @@ public partial class PlantField : Node2D
bool inBoundary = expected_pos.X > Utility.LeftFieldBoundary.X && expected_pos.X < Utility.RightFieldBoundary.X && expected_pos.Y > Utility.LeftFieldBoundary.Y && expected_pos.Y < Utility.RightFieldBoundary.Y;
bool _canPlace = _resource != null
bool canPlace = _resource != null
&& inBoundary
&& LevelController.Instance.Pools.EntityField[_resource.Layer].ContainsKey(expected_pos) == false
&& LevelController.Instance.LevelData.CheckSpendSun(_resource.Cost);
// Setting visuals
if (_canPlace)
if (_previousCanPlace != canPlace)
{
Material.Set("shader_parameter/amount", 0);
Cursor.Instance.SetPlantCursor();
if (canPlace)
{
Material.Set("shader_parameter/amount", 0);
}
else
{
Material.Set("shader_parameter/amount", 1);
}
Cursor.Instance.plant = canPlace;
Cursor.Instance.UpdateCursor();
}
_previousCanPlace = canPlace;
if (canPlace)
_plantSetter.GlobalPosition = expected_pos;
}
else
{
Cursor.Instance.SetDefaultCursor();
Material.Set("shader_parameter/amount", 1);
}
_plantSetter.Texture = _resource == null ? null : _resource.Preview;
}
// Spawning plant
if (Input.IsMouseButtonPressed(MouseButton.Left)
&& _canPlace )
public override void _UnhandledInput(InputEvent @event)
{
if (@event.IsActionPressed("cancel_plant") && _slot != null)
{
_slot.ReleaseFocus();
}
if (@event.IsActionPressed("primary_action") && _previousCanPlace)
{
var plant = _resource.Scene.Instantiate<RuntimePlantData>();
LevelController.Instance.Pools.Plants.AddChild(plant);
plant.GlobalPosition = expected_pos;
plant.GlobalPosition = (_plantSetter.GlobalPosition / tile).Ceil() * tile - new Vector2(20, 14);
plant.Layer = _resource.Layer;
LevelController.Instance.Pools.EntityField[_resource.Layer].Add(expected_pos, plant as IEntity);
LevelController.Instance.Pools.EntityField[_resource.Layer].Add(plant.GlobalPosition, plant as IEntity);
LevelController.Instance.LevelData.SpendSun(_resource.Cost);
// Unfocusing and recharging slot
_slot.Recharge();
}
}
public override void _Input(InputEvent @event)
{
if (@event.IsActionPressed("cancel_plant"))
{
_slot.ReleaseFocus();
}
}
}

View file

@ -0,0 +1,28 @@
using Godot;
using System;
public partial class SunSpawner : Node
{
[Export]
public int MinSun = 25;
[Export]
public int MaxSun = 25;
[Export]
private PackedScene SunScene;
public void Spawn()
{
float x = GD.Randf()*9*Utility.TileWidth;
uint y = GD.Randi() % 5;
var sun = SunScene.Instantiate<Sun>();
LevelController.Instance.Pools.Projectiles.AddChild(sun);
sun.GlobalPosition = new Vector2(Utility.LeftFieldBoundary.X + x, -90);
var moveTween = CreateTween();
moveTween.TweenProperty(sun,"global_position", new Vector2(Utility.LeftFieldBoundary.X + x,
Utility.LeftFieldBoundary.Y + Utility.TileHeight * y + Utility.TileHeight/2.0f),9-y);
}
}

View file

@ -38,8 +38,13 @@ public partial class RuntimePlantData : Node2D, IEntity
if (_hp <= 0)
{
LevelController.Instance.Pools.EntityField[Layer].Remove(GlobalPosition);
QueueFree();
Kill();
}
}
public void Kill()
{
LevelController.Instance.Pools.EntityField[Layer].Remove(GlobalPosition);
QueueFree();
}
}