droppable items and win

This commit is contained in:
Rendo 2025-07-20 22:11:23 +05:00
commit 5bdbfa4d82
47 changed files with 820 additions and 85 deletions

View file

@ -0,0 +1 @@
uid://b2j41pq6cmpgm

View file

@ -0,0 +1,17 @@
using Godot;
[GlobalClass]
public abstract partial class DroppableItem : Area2D
{
[Signal] public delegate void PickedUpEventHandler();
public override void _InputEvent(Viewport viewport, InputEvent @event, int shapeIdx)
{
if (@event.IsActionPressed("primary_action"))
{
PickUp();
}
}
public abstract void PickUp();
}

View file

@ -0,0 +1 @@
uid://cwf2y3pxi6psc

View file

@ -0,0 +1,25 @@
using Godot;
using Newlon;
public partial class DroppableSeedpacket : DroppableItem
{
public PlantResource plant;
[Export] private Label _cost;
[Export] private TextureRect _icon;
[Export] private TextureRect _packet;
public override void _Ready()
{
_cost.Text = plant.Cost.ToString();
_icon.Texture = plant.Preview;
if (plant.customFrame != null)
{
_packet.Texture = plant.customFrame.frame;
_cost.LabelSettings = plant.customFrame.font;
}
}
public override void PickUp()
{
EmitSignal(SignalName.PickedUp);
}
}

View file

@ -0,0 +1 @@
uid://gymo10tjruj2

View file

@ -1,9 +1,10 @@
using Godot;
using Newlon;
public partial class ExitButton : Button
{
public override void _Pressed()
{
GetTree().ChangeSceneToFile("uid://bfstrli64u23y");
LevelController.Instance.EndLevel();
}
}

View file

@ -1,4 +1,5 @@
using Godot;
using Newlon.Components.Level;
namespace Newlon.Components.GUI;
@ -12,10 +13,32 @@ public partial class FastForwardButton : Button
private int speed = 1;
public override void _Ready()
{
RuntimeLevelData.Instance.OnLevelStateChanged += OnLevelStateChanged;
}
private void OnLevelStateChanged(RuntimeLevelData.LevelStates state)
{
if (state == RuntimeLevelData.LevelStates.Game) return;
speed = 1;
Update();
}
public void OnPressed()
{
speed = Mathf.Wrap(speed+1, 1, 4);
if (RuntimeLevelData.Instance.GetLevelState() != RuntimeLevelData.LevelStates.Game) return;
speed = Mathf.Wrap(speed + 1, 1, 4);
flashAnimator.Play("flash");
Update();
}
private void Update()
{
switch (speed)
{
case 1:
@ -29,8 +52,6 @@ public partial class FastForwardButton : Button
break;
}
flashAnimator.Play("flash");
Engine.TimeScale = speed;
}
}

View file

@ -27,7 +27,7 @@ public partial class PauseMenu : Control
}
public void Exit()
{
GetTree().ChangeSceneToFile("uid://bfstrli64u23y");
LevelController.Instance.EndLevel();
currently_paused = false;
}
public static void Pause()

View file

@ -1,12 +1,12 @@
using Godot;
using System;
using Newlon;
public partial class RestartButton : Button
{
public override void _Pressed()
{
GetTree().Paused = false;
GetTree().ReloadCurrentScene();
LevelController.Instance.RestartLevel();
}
}

View file

@ -0,0 +1,34 @@
using Godot;
using Newlon;
using Newlon.Components;
using Newlon.Components.Plants;
public partial class RewardScene : Node
{
public override void _Ready()
{
var reward = LevelController.Instance.Reward;
var subviewport = GetNode<SubViewport>("%SubViewport");
var nameLabel = GetNode<Label>("%NameLabel");
var descriptionLabel = GetNode<RichTextLabel>("%DescriptionLabel");
var continueButton = GetNode<Button>("%ContinueButton");
var field = GetNode<Sprite2D>("%Field");
nameLabel.Text = Tr(reward.Name);
descriptionLabel.Text = Tr(reward.Description);
continueButton.Pressed += LevelController.Instance.ReturnToInitiator;
if (reward is PlantReward plantReward && plantReward.Plant.customFrame != null)
{
field.Texture = plantReward.Plant.customFrame.almanachField;
}
var rewardedObject = reward.GetPreview().Instantiate();
subviewport.AddChild(rewardedObject);
if (rewardedObject is Entity entity)
{
entity.DisableBrain();
}
}
}

View file

@ -0,0 +1 @@
uid://cqlghusry0hej

View file

@ -1,6 +1,7 @@
using Godot;
[GlobalClass]
[Tool]
public partial class CustomSeedpacketFrame : Resource
{
[Export] public Texture2D frame;

View file

@ -9,7 +9,10 @@ namespace Newlon;
public partial class LevelController : Node
{
const string REWARD_SCENE_UID = "uid://cwck7e1tt057k";
public static LevelController Instance { get; private set; }
public RewardResource Reward { get; private set; }
private string pathToInitiator;
private bool _isLevelRunning = false;
@ -17,7 +20,7 @@ public partial class LevelController : Node
{
Instance = this;
}
public override void _ExitTree()
{
Instance = null;
@ -31,12 +34,13 @@ public partial class LevelController : Node
{
if (_isLevelRunning)
return;
pathToInitiator = GetTree().CurrentScene.SceneFilePath;
RuntimeLevelData.LevelResource = levelResource;
GetTree().ChangeSceneToPacked(levelTileset);
_isLevelRunning = true;
}
public void RestartLevel()
@ -49,8 +53,32 @@ public partial class LevelController : Node
{
if (_isLevelRunning == false)
return;
RuntimeLevelData.LevelResource = null;
_isLevelRunning = false;
if (Reward != null)
{
GetTree().ChangeSceneToFile(REWARD_SCENE_UID);
}
else
{
ReturnToInitiator();
}
}
public void ReturnToInitiator()
{
if (pathToInitiator == null) return;
GetTree().ChangeSceneToFile(pathToInitiator);
pathToInitiator = null;
}
public void SetReward(RewardResource reward)
{
Reward = reward;
}
}

View file

@ -13,9 +13,12 @@ public partial class LevelRunner : Node
public float waveHealth = 0;
public float waveHealthMax = 0;
public List<RuntimeZombieData> zombies = [];
[Export] private MoneyReward defaultReward;
[Export] private RowSpawner rowSpawner;
[Export] private Timer waveTimer;
[Export] private float approachNotificationTime;
[Export] private AnimationPlayer player;
[Export] private Node rewardParent;
[Signal] public delegate void ResourceChangedEventHandler(AdventureLevelResource resource);
[Signal] public delegate void WaveChangedEventHandler(int to);
[Signal] public delegate void HugeWaveApproachingCallbackEventHandler();
@ -59,7 +62,7 @@ public partial class LevelRunner : Node
rowSpawner.Add(resource.waves[waveIndex].zombiesOrdered);
ClearZombies();
if (waveIndex == resource.waves.Count - 1)
{
EmitSignal(SignalName.FinalWaveInitiated);
@ -110,18 +113,46 @@ public partial class LevelRunner : Node
waveHealthMax += zombie.GetMaxHPMixed();
waveHealth = waveHealthMax;
zombie.HPChangedMixed += OnHPChanged;
if (waveIndex == resource.waves.Count - 1)
{
zombie.HasBeenKilled += (who) =>
{
if (waveHealth <= 0)
{
RuntimeLevelData.Instance.SetLevelState(RuntimeLevelData.LevelStates.Win);
}
};
zombie.HasBeenKilled += OnLastZombieKilled;
}
}
private void OnLastZombieKilled(RuntimeZombieData who)
{
if (waveHealth > 0) return;
RuntimeLevelData.Instance.SetLevelState(RuntimeLevelData.LevelStates.Win);
var reward = resource.reward.Scene.Instantiate<DroppableItem>();
if (reward is DroppableSeedpacket seedpacket && resource.reward is PlantReward plantReward)
{
seedpacket.plant = plantReward.Plant;
}
LevelController.Instance.SetReward(resource.reward);
player.Play("win");
Callable.From(() =>
{
rewardParent.AddChild(reward);
reward.GlobalPosition = who.GlobalPosition + Vector2.Up * FieldParams.TileHeight * 0.5f;
var dropmover = new DropMover();
reward.AddChild(dropmover);
}).CallDeferred();
reward.PickedUp += () =>
{
var tween = CreateTween();
var camera = GetViewport().GetCamera2D();
tween.TweenProperty(reward, "global_position", camera.GlobalPosition, 4.0);
tween.Parallel().TweenProperty(reward, "global_scale", new Vector2(3.0f, 3.0f), 4.0);
tween.TweenInterval(1.0);
tween.TweenCallback(Callable.From(LevelController.Instance.EndLevel));
reward.InputPickable = false;
};
}
}

View file

@ -20,7 +20,7 @@ public partial class LoseCheckbox : Area2D
if (zombieData.HP <= 0) return;
RuntimeLevelData.Instance.SetLevelState(RuntimeLevelData.LevelStates.Loose);
Engine.TimeScale = 1.0;
fadeAnimation.Play("fade");
fadeAnimation.Play("loose");
GetTree().Paused = true;
PhysicsServer2D.SetActive(true);
Callable.From(()=>

View file

@ -0,0 +1,13 @@
using Godot;
[GlobalClass]
[Tool]
public partial class MoneyReward : RewardResource
{
[Export] public int Cost;
public override PackedScene GetPreview()
{
return Scene;
}
}

View file

@ -0,0 +1 @@
uid://dtfmy3los1si1

View file

@ -3,6 +3,7 @@ using Godot;
namespace Newlon;
[GlobalClass]
[Tool]
public partial class PlantResource : DisplayResource
{
}

View file

@ -0,0 +1,15 @@
using Godot;
using Newlon;
[GlobalClass]
[Tool]
public partial class PlantReward : RewardResource
{
[Export]
public PlantResource Plant { get; private set; }
public override PackedScene GetPreview()
{
return Plant.Scene;
}
}

View file

@ -0,0 +1 @@
uid://c8e40t5nbo83r

View file

@ -0,0 +1,12 @@
using Godot;
[GlobalClass]
[Tool]
public abstract partial class RewardResource : Resource
{
[Export]
public PackedScene Scene { get; private set; }
[Export] public string Name { get; private set; }
[Export] public string Description { get; private set; }
public abstract PackedScene GetPreview();
}

View file

@ -0,0 +1 @@
uid://co3cs8suo67gw