mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
117 lines
2.7 KiB
C#
117 lines
2.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Cirno.Scripts.Resources;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
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;
|
|
|
|
private InventoryManager _inventoryManager;
|
|
|
|
public Weapon EquippedWeapon { get; set; }
|
|
|
|
private CharacterBody2D _parent;
|
|
|
|
//public Vector2 FacingDirection
|
|
|
|
public override void _Ready()
|
|
{
|
|
|
|
}
|
|
|
|
public void Init(CharacterBody2D parent)
|
|
{
|
|
_parent = parent;
|
|
|
|
_inventoryManager = this.GetInventoryManager();
|
|
|
|
_inventoryManager.WeaponEquip += this.EquipWeapon;
|
|
|
|
_inventoryManager.ItemAdded += (LootItem item, int amount) =>
|
|
{
|
|
if (item.Item == ItemTypes.Weapon)
|
|
{
|
|
SpawnPlayerWeapon(item);
|
|
}
|
|
};
|
|
}
|
|
|
|
public void AddWeapon(Weapon weapon)
|
|
{
|
|
EquippedWeapons.Add(weapon);
|
|
}
|
|
|
|
public void EquipWeapon(string itemKey)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(itemKey)) return;
|
|
var weapon = EquippedWeapons.FirstOrDefault(x => x.WeaponData.ItemKey == itemKey);
|
|
|
|
if (weapon is null) return;
|
|
|
|
EquipWeapon(weapon);
|
|
}
|
|
|
|
// Triggered by event in inventorymanager
|
|
public void EquipWeapon(Weapon weapon)
|
|
{
|
|
EquippedWeapon = weapon;
|
|
CurrentWeaponIndex = EquippedWeapons.IndexOf(weapon);
|
|
}
|
|
|
|
public void NextWeapon()
|
|
{
|
|
CurrentWeaponIndex += 1;
|
|
if (CurrentWeaponIndex > EquippedWeapons.Count - 1)
|
|
{
|
|
CurrentWeaponIndex = EquippedWeapons.Count - 1;
|
|
}
|
|
|
|
EquipWeapon(EquippedWeapons[CurrentWeaponIndex]);
|
|
}
|
|
|
|
public void PreviousWeapon()
|
|
{
|
|
CurrentWeaponIndex -= 1;
|
|
if (CurrentWeaponIndex < 0)
|
|
{
|
|
CurrentWeaponIndex = 0;
|
|
}
|
|
|
|
EquipWeapon(EquippedWeapons[CurrentWeaponIndex]);
|
|
}
|
|
|
|
public void Shoot(Vector2 direction)
|
|
{
|
|
if (EquippedWeapon == null) return;
|
|
|
|
|
|
EquippedWeapon.ShootDirection = direction;
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|