BITFALL/Assets/Artists/Scripts/Props/Prop_Throw.cs

58 lines
1.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
using BITKit.Entities;
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace BITFALL.Props
{
public class Prop_Throw : MonoBehaviour
{
[SerializeField] private new Rigidbody rigidbody;
[SerializeField] private int damage;
[SerializeField] private int force;
[SerializeField] private LayerMask layerMask;
private void FixedUpdate()
{
if (Physics.Linecast(rigidbody.position, rigidbody.position + rigidbody.velocity * Time.fixedDeltaTime,
out var hit,
layerMask
))
{
var _rigidbody = hit.rigidbody;
if(hit.collider.transform.TryGetComponent<IDamagable>(out var damagable))
{
damagable.GiveDamage(new DamageMessage()
{
Target = damagable.UnityEntity,
Damage = damage,
Location = new Location()
{
position = hit.point,
rotation = Quaternion.LookRotation(hit.normal),
forward = hit.normal,
}
});
if(damagable.Rigidbody is not null)
{
_rigidbody = damagable.Rigidbody;
}
}
if (_rigidbody)
{
_rigidbody.AddForceAtPositionAsync((hit.point-rigidbody.position).normalized * force, hit.point, ForceMode.Impulse).Forget();
}
rigidbody.Sleep();
if (TryGetComponent<Prop_ReplaceOnSleep>(out var replace))
{
replace.ReplaceImmediate(hit.collider.transform);
}
}
}
}
}