Add new components: BrickThrowComponent, BulletComponent, CageComponent, ChaseLevelComponent, CleanupComponent, and ThrowInputResource classes; implement game saving and loading logic in SaveSystem
This commit is contained in:
70
scripts/components/BrickThrowComponent.cs
Normal file
70
scripts/components/BrickThrowComponent.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
public partial class BrickThrowComponent : Node
|
||||
{
|
||||
[Export] public PackedScene BrickScene { get; set; }
|
||||
[Export] public float FireRate { get; set; } = 1.0f;
|
||||
[Export] public PlayerController PlayerController { get; set; }
|
||||
[Export] public ThrowInputResource ThrowInputBehavior { get; set; }
|
||||
|
||||
private bool _canThrow = true;
|
||||
private Timer _timer;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
SetupTimer();
|
||||
_canThrow = true;
|
||||
|
||||
if (ThrowInputBehavior != null) ThrowInputBehavior.ThrowRequested += ThrowBrick;
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
ThrowInputBehavior?.ProcessInput(@event);
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
ThrowInputBehavior?.Update(delta);
|
||||
}
|
||||
|
||||
private void SetupTimer()
|
||||
{
|
||||
_timer.WaitTime = FireRate;
|
||||
_timer.OneShot = false;
|
||||
_timer.Autostart = false;
|
||||
_timer.Timeout += OnTimerTimeout;
|
||||
}
|
||||
|
||||
private void OnTimerTimeout()
|
||||
{
|
||||
_canThrow = true;
|
||||
}
|
||||
|
||||
private void ThrowBrick(float powerMultiplier = 1f)
|
||||
{
|
||||
if (!_canThrow || PlayerController == null || BrickScene == null)
|
||||
return;
|
||||
|
||||
var instance = BrickScene.Instantiate<Node2D>();
|
||||
var init = instance.GetNodeOrNull<ProjectileInitComponent>("ProjectileInitComponent");
|
||||
if (init != null && PlayerController.CurrentMovement is PlatformMovementComponent)
|
||||
{
|
||||
init.Initialize(new ProjectileInitParams()
|
||||
{
|
||||
Position = PlayerController.GlobalPosition,
|
||||
Rotation = PlayerController.Rotation,
|
||||
Direction = PlayerController.CurrentMovement.LastDirection,
|
||||
PowerMultiplier = powerMultiplier,
|
||||
});
|
||||
}
|
||||
|
||||
GetTree().CurrentScene.AddChild(instance);
|
||||
_canThrow = false;
|
||||
_timer.Start();
|
||||
}
|
||||
}
|
45
scripts/components/BulletComponent.cs
Normal file
45
scripts/components/BulletComponent.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
public partial class BulletComponent : Node
|
||||
{
|
||||
[Export] public Area2D Area { get; set; }
|
||||
[Export] public TerrainHitFx TerrainHitFx { get; set; }
|
||||
[Export] public Sprite2D BulletSprite { get; set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Area.BodyEntered += OnBodyEntered;
|
||||
}
|
||||
|
||||
private void OnBodyEntered(Node2D body)
|
||||
{
|
||||
if (body is TileMapLayer)
|
||||
{
|
||||
if (BulletSprite != null)
|
||||
{
|
||||
BulletSprite.Visible = false;
|
||||
}
|
||||
|
||||
PlayTerrainHitFx();
|
||||
return;
|
||||
}
|
||||
|
||||
Owner.QueueFree();
|
||||
}
|
||||
|
||||
private void OnAreaEntered(Area2D area)
|
||||
{
|
||||
Owner.QueueFree();
|
||||
}
|
||||
|
||||
private void PlayTerrainHitFx()
|
||||
{
|
||||
if (TerrainHitFx == null) return;
|
||||
|
||||
TerrainHitFx.TriggerFx();
|
||||
|
||||
Owner.QueueFree();
|
||||
}
|
||||
}
|
47
scripts/components/CageComponent.cs
Normal file
47
scripts/components/CageComponent.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
public partial class CageComponent : Node
|
||||
{
|
||||
[Export] public LeverComponent Lever { get; set; }
|
||||
[Export] public Vector2 MoveValue { get; set; } = new(0, -100f);
|
||||
[Export] public float TweenDuration { get; set; } = 0.5f;
|
||||
[Export] public bool ShouldFree { get; set; } = true;
|
||||
|
||||
private const string LeverGroupName = "levers";
|
||||
|
||||
public override async void _Ready()
|
||||
{
|
||||
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
|
||||
if (Lever == null)
|
||||
{
|
||||
var leverNodes = GetTree().GetNodesInGroup(LeverGroupName);
|
||||
foreach (var leverNode in leverNodes)
|
||||
{
|
||||
var lever = leverNode.GetNodeOrNull<LeverComponent>("LeverComponent");
|
||||
if (lever != null) lever.Activated += OnLeverActivated;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLeverActivated()
|
||||
{
|
||||
var tween = CreateTween();
|
||||
if (Owner is Node2D root)
|
||||
{
|
||||
var endPosition = root.Position + MoveValue;
|
||||
tween.TweenProperty(root, "position", endPosition, TweenDuration);
|
||||
}
|
||||
|
||||
tween.TweenCallback(Callable.From(OnTweenCompleted));
|
||||
|
||||
}
|
||||
|
||||
private void OnTweenCompleted()
|
||||
{
|
||||
if (!ShouldFree) return;
|
||||
Owner.QueueFree();
|
||||
}
|
||||
}
|
79
scripts/components/ChaseLevelComponent.cs
Normal file
79
scripts/components/ChaseLevelComponent.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
public partial class ChaseLevelComponent : Node
|
||||
{
|
||||
[Export] public float ChaseSpeed { get; set; } = 200.0f;
|
||||
[Export] public Marker2D ChaseTarget { get; set; }
|
||||
[Export] public GodotObject PhantomCamera { get; set; }
|
||||
[Export] public float MinimumDistance { get; set; } = 10f;
|
||||
|
||||
[Signal]
|
||||
public delegate void ChaseStartedEventHandler();
|
||||
|
||||
[Signal]
|
||||
public delegate void ChaseStoppedEventHandler();
|
||||
|
||||
private bool _isChasing = false;
|
||||
private Node2D _previousCameraFollowTarget = null;
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (!_isChasing) return;
|
||||
if (ChaseTarget == null) return;
|
||||
|
||||
if (CheckIfReachedTarget())
|
||||
{
|
||||
StopChasing();
|
||||
return;
|
||||
}
|
||||
|
||||
var targetPosition = ChaseTarget.GlobalPosition;
|
||||
|
||||
if (Owner is not Node2D root) return;
|
||||
|
||||
var direction = (targetPosition - root.GlobalPosition).Normalized();
|
||||
root.GlobalPosition += direction * ChaseSpeed * (float)delta;
|
||||
}
|
||||
|
||||
public void OnShipEntered()
|
||||
{
|
||||
if (ChaseTarget == null || PhantomCamera == null)
|
||||
return;
|
||||
|
||||
if (_isChasing) return;
|
||||
|
||||
_previousCameraFollowTarget = (Node2D)PhantomCamera.Call("get_follow_target");
|
||||
PhantomCamera.Call("set_follow_target", Owner as Node2D);
|
||||
EmitSignalChaseStarted();
|
||||
_isChasing = true;
|
||||
}
|
||||
|
||||
public void OnShipExited()
|
||||
{
|
||||
StopChasing();
|
||||
}
|
||||
|
||||
private bool CheckIfReachedTarget()
|
||||
{
|
||||
if (ChaseTarget == null)
|
||||
return false;
|
||||
|
||||
if (Owner is not Node2D root) return false;
|
||||
|
||||
var targetPosition = ChaseTarget.GlobalPosition;
|
||||
var currentPosition = root.GlobalPosition;
|
||||
return currentPosition.DistanceTo(targetPosition) <= MinimumDistance;
|
||||
|
||||
}
|
||||
|
||||
private void StopChasing()
|
||||
{
|
||||
if (PhantomCamera == null) return;
|
||||
|
||||
PhantomCamera.Call("set_follow_target", _previousCameraFollowTarget);
|
||||
EmitSignalChaseStopped();
|
||||
_isChasing = false;
|
||||
}
|
||||
}
|
11
scripts/components/CleanupComponent.cs
Normal file
11
scripts/components/CleanupComponent.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
public partial class CleanupComponent : Node
|
||||
{
|
||||
public void CleanUp()
|
||||
{
|
||||
Owner.QueueFree();
|
||||
}
|
||||
}
|
@@ -17,6 +17,7 @@ public partial class ShipMovementComponent : Node, IMovement
|
||||
private Vector2 _velocity = Vector2.Zero;
|
||||
|
||||
public Vector2 Velocity => _velocity;
|
||||
public Vector2 LastDirection => _velocity.Normalized();
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
|
Reference in New Issue
Block a user