Files
Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/NodeCanvas/BulletTask.cs

82 lines
2.4 KiB
C#
Raw Normal View History

2025-06-24 23:49:13 +08:00
using System.Collections;
using System.Collections.Generic;
using BITFALL;
using BITFALL.Bullet;
using BITKit;
using BITKit.Entities;
using Microsoft.Extensions.DependencyInjection;
using NodeCanvas.Framework;
using UnityEngine;
namespace Net.Project.B.NodeCanvas
{
public class BulletTask : ActionTask
{
public BBParameter<GameObject> Target;
public BBParameter<GameObject> GameObject;
public BBParameter<Transform> ViewPosition;
public BBParameter<float> AccuracyOffset;
public BBParameter<float> Interval;
private readonly IntervalUpdate _intervalUpdate = new(1);
private IBulletService _bulletService;
private readonly BulletData _bulletData = new()
{
InitialDamage = 10,
MaxDamage = 10,
StartSpeed = 256,
};
protected override string OnInit()
{
_bulletService = BITApp.ServiceProvider.GetRequiredService<IBulletService>();
var entitiesService = BITApp.ServiceProvider.GetRequiredService<IEntitiesService>();
if (entitiesService.TryGetEntity(GameObject.value.GetInstanceID(), out var entity))
{
_bulletData.Initiator = entity.Id;
}
return base.OnInit();
}
protected override void OnExecute()
{
EndAction();
_intervalUpdate.Interval = Interval.value;
if (_intervalUpdate.AllowUpdate is false)
{
return;
}
if (!Target.value)return;
var targetPosition = Target.value.transform.position;
if (Target.value.TryGetComponent<CapsuleCollider>(out var capsuleCollider))
{
targetPosition = capsuleCollider.transform.position +
capsuleCollider.transform.rotation * capsuleCollider.center;
}
var direction = targetPosition - ViewPosition.value.position;
direction = Quaternion.LookRotation(direction) * Quaternion.Euler(Random.onUnitSphere * AccuracyOffset.value) * Vector3.forward;
_bulletData.Rotation = Quaternion.LookRotation(direction);
_bulletData.Forward = direction;
_bulletData.Position = ViewPosition.value.position;
_bulletService.Spawn(_bulletData);
}
}
}