mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-09 00:35:53 +00:00
Moved events inside the player
This commit is contained in:
parent
11a22684d4
commit
7d9db8cfb6
7 changed files with 57 additions and 75 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
|
|
@ -7,6 +8,7 @@ namespace Cirno.Scripts.Components.Actors;
|
|||
|
||||
public partial class PlayerWeaponProvider : Node2D
|
||||
{
|
||||
[Export] public PackedScene WeaponTemplate { get; private set; }
|
||||
public Array<Weapon> EquippedWeapons { get; set; } = new Array<Weapon>();
|
||||
public int CurrentWeaponIndex { get; set; } = 0;
|
||||
|
||||
|
|
@ -21,6 +23,16 @@ public partial class PlayerWeaponProvider : Node2D
|
|||
public override void _Ready()
|
||||
{
|
||||
_inventoryManager = this.GetInventoryManager();
|
||||
|
||||
_inventoryManager.WeaponEquip += this.EquipWeapon;
|
||||
|
||||
_inventoryManager.ItemAdded += (LootItem item, int amount) =>
|
||||
{
|
||||
if (item.Item == ItemTypes.Weapon)
|
||||
{
|
||||
SpawnPlayerWeapon(item);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void Init(PlayerMovement parent)
|
||||
|
|
@ -81,4 +93,23 @@ public partial class PlayerWeaponProvider : Node2D
|
|||
EquippedWeapon.Shoot();
|
||||
}
|
||||
|
||||
private void SpawnPlayerWeapon(LootItem startingItem)
|
||||
{
|
||||
if (WeaponTemplate == null)
|
||||
{
|
||||
GD.Print("Could not spawn weapon because template is null");
|
||||
return;
|
||||
}
|
||||
|
||||
var weapon = this.CreateSibling<Weapon>(WeaponTemplate);
|
||||
weapon.WeaponData = startingItem.WeaponData;
|
||||
|
||||
this.AddWeapon(weapon);
|
||||
|
||||
if (this.EquippedWeapon == null)
|
||||
{
|
||||
this.EquipWeapon(weapon);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,8 +28,6 @@ public partial class GameManager : Node2D
|
|||
|
||||
//[Export] public Marker2D PlayerSpawnMarker { get; set; }
|
||||
|
||||
[Export] public PackedScene WeaponTemplate { get; private set; }
|
||||
|
||||
[Export] public Array<LootItem> StartingEquipment { get; private set; } = new();
|
||||
|
||||
private InventoryManager _inventoryManager { get; set; }
|
||||
|
|
@ -150,21 +148,6 @@ public partial class GameManager : Node2D
|
|||
return;
|
||||
}
|
||||
|
||||
if (_inventoryManager is not null)
|
||||
{
|
||||
_inventoryManager.ItemAdded += (LootItem item, int amount) =>
|
||||
{
|
||||
if (item.Item == ItemTypes.Weapon)
|
||||
{
|
||||
SpawnPlayerWeapon(item);
|
||||
}
|
||||
};
|
||||
|
||||
_inventoryManager.WeaponEquip += _player.EquipWeapon;
|
||||
|
||||
_inventoryManager.ItemUsed += _player.UseItem;
|
||||
}
|
||||
|
||||
// Wait before the player is fully initialized before spawning weapons on it
|
||||
CallDeferred(MethodName.SpawnWeapons);
|
||||
}
|
||||
|
|
@ -252,25 +235,6 @@ public partial class GameManager : Node2D
|
|||
}
|
||||
}
|
||||
|
||||
private void SpawnPlayerWeapon(LootItem startingItem)
|
||||
{
|
||||
if (WeaponTemplate == null)
|
||||
{
|
||||
GD.Print("Could not spawn weapon because template is null");
|
||||
return;
|
||||
}
|
||||
|
||||
var weapon = _player.CreateChild<Weapon>(WeaponTemplate);
|
||||
weapon.WeaponData = startingItem.WeaponData;
|
||||
|
||||
_player.AddWeapon(weapon);
|
||||
|
||||
if (_player.EquippedWeapon == null)
|
||||
{
|
||||
_player.EquipWeapon(weapon);
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnBulletsContainer()
|
||||
{
|
||||
_bulletsContainer = new Node2D();
|
||||
|
|
|
|||
|
|
@ -163,6 +163,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
|||
_selector.Visible = false;
|
||||
}
|
||||
|
||||
_inventoryManager.ItemUsed += this.UseItem;
|
||||
|
||||
_lastCheckPointPosition = GlobalPosition;
|
||||
|
||||
_ = UnTeleport();
|
||||
|
|
@ -260,16 +262,6 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
|||
_weaponProvider.EquipWeapon(weapon);
|
||||
}
|
||||
|
||||
public void NextWeapon()
|
||||
{
|
||||
_weaponProvider.NextWeapon();
|
||||
}
|
||||
|
||||
public void PreviousWeapon()
|
||||
{
|
||||
_weaponProvider.PreviousWeapon();
|
||||
}
|
||||
|
||||
private void FindInteractable()
|
||||
{
|
||||
var selected = _selector.SelectedInteractable;
|
||||
|
|
@ -394,12 +386,12 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
|||
|
||||
if (Input.IsActionJustPressed(_nextWeaponActionName))
|
||||
{
|
||||
NextWeapon();
|
||||
_weaponProvider.NextWeapon();
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed(_previousWeaponActionName))
|
||||
{
|
||||
PreviousWeapon();
|
||||
_weaponProvider.PreviousWeapon();
|
||||
}
|
||||
|
||||
_crosshair.Position = CalculateCrosshairPosition();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue