2024-04-28 13:55:19 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using ColdMint.scripts.debug;
|
|
|
|
using ColdMint.scripts.projectile;
|
2024-06-10 13:05:18 +00:00
|
|
|
using ColdMint.scripts.utils;
|
2024-04-28 13:55:19 +00:00
|
|
|
using Godot;
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.weapon;
|
|
|
|
|
2024-06-05 13:38:45 +00:00
|
|
|
/// <summary>
|
|
|
|
/// <para>Projectile weapons</para>
|
|
|
|
/// <para>抛射体武器</para>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
///<para>These weapons can fire projectiles to attack the enemy.For example: guns and scepters.Generate a bullet to attack the enemy.</para>
|
|
|
|
///<para>这类武器可发射抛射体,攻击敌人。例如:枪支和法杖。生成一个子弹攻击敌人。</para>
|
|
|
|
/// </remarks>
|
2024-04-28 13:55:19 +00:00
|
|
|
public partial class ProjectileWeapon : WeaponTemplate
|
|
|
|
{
|
2024-06-05 13:38:45 +00:00
|
|
|
/// <summary>
|
|
|
|
/// <para>The formation position of the projectile</para>
|
|
|
|
/// <para>抛射体的生成位置</para>
|
|
|
|
/// </summary>
|
2024-05-08 10:22:04 +00:00
|
|
|
private Marker2D? _marker2D;
|
2024-06-10 13:05:18 +00:00
|
|
|
|
2024-06-05 13:38:45 +00:00
|
|
|
/// <summary>
|
|
|
|
/// <para>List of projectiles</para>
|
|
|
|
/// <para>抛射体列表</para>
|
|
|
|
/// </summary>
|
2024-05-08 10:22:04 +00:00
|
|
|
private string[]? _projectiles;
|
2024-06-10 13:05:18 +00:00
|
|
|
|
2024-05-08 10:22:04 +00:00
|
|
|
private Dictionary<string, PackedScene>? _projectileCache;
|
|
|
|
private Node2D? _projectileContainer;
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
base._Ready();
|
|
|
|
_marker2D = GetNode<Marker2D>("Marker2D");
|
|
|
|
_projectileCache = new Dictionary<string, PackedScene>();
|
|
|
|
_projectiles = GetMeta("Projectiles", "").AsStringArray();
|
|
|
|
foreach (var projectileItem in _projectiles)
|
|
|
|
{
|
|
|
|
var packedScene = GD.Load<PackedScene>(projectileItem);
|
2024-06-05 13:38:45 +00:00
|
|
|
if (packedScene == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2024-06-10 13:05:18 +00:00
|
|
|
|
2024-05-08 10:22:04 +00:00
|
|
|
_projectileCache.Add(projectileItem, packedScene);
|
|
|
|
}
|
|
|
|
|
|
|
|
_projectileContainer = GetNode("/root/Game/ProjectileContainer") as Node2D;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void DoFire(Node2D? owner, Vector2 enemyGlobalPosition)
|
|
|
|
{
|
2024-06-05 13:38:45 +00:00
|
|
|
if (_projectileCache == null || _projectiles == null || owner == null || _projectileContainer == null ||
|
|
|
|
_marker2D == null)
|
2024-05-08 10:22:04 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2024-06-10 13:05:18 +00:00
|
|
|
|
2024-06-05 13:38:45 +00:00
|
|
|
if (_projectiles.IsEmpty())
|
2024-05-08 10:22:04 +00:00
|
|
|
{
|
2024-06-05 13:38:45 +00:00
|
|
|
LogCat.LogError("projectiles_is_empty");
|
2024-05-08 10:22:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-06-10 13:05:18 +00:00
|
|
|
|
2024-05-08 10:22:04 +00:00
|
|
|
//Get the first projectile
|
2024-06-05 13:38:45 +00:00
|
|
|
//获取第一个抛射体
|
2024-05-08 10:22:04 +00:00
|
|
|
var projectileScene = _projectileCache[_projectiles[0]];
|
2024-06-10 13:05:18 +00:00
|
|
|
var projectile = NodeUtils.InstantiatePackedScene<ProjectileTemplate>(projectileScene, _projectileContainer);
|
|
|
|
if (projectile == null) return;
|
|
|
|
projectile.Owner = owner;
|
|
|
|
projectile.Velocity = (enemyGlobalPosition - _marker2D.GlobalPosition).Normalized() * projectile.Speed;
|
|
|
|
projectile.Position = _marker2D.GlobalPosition;
|
2024-05-08 10:22:04 +00:00
|
|
|
}
|
|
|
|
}
|