1
This commit is contained in:
@@ -119,7 +119,7 @@ namespace BITKit.Entities
|
||||
}
|
||||
public class DamageService:MonoBehaviour,IDamageService
|
||||
{
|
||||
internal static IDamageService Singleton { get; set; }
|
||||
public static IDamageService Singleton { get;private set; }
|
||||
private readonly Queue<DamageMessage> Messages = new();
|
||||
|
||||
[SerializeReference,SubclassSelector] private INetClient netClient;
|
||||
|
@@ -4,19 +4,52 @@ using UnityEngine;
|
||||
using BITKit;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class EntityHitbox : EntityBehavior,IDamagable
|
||||
public class EntityHitbox : MonoBehaviour,IDamagable
|
||||
{
|
||||
public IHealth Health => _health;
|
||||
public IHealth Health
|
||||
{
|
||||
get
|
||||
{
|
||||
EnsureConfigure();
|
||||
return _health;
|
||||
}
|
||||
}
|
||||
|
||||
public IUnityEntity UnityEntity
|
||||
{
|
||||
get
|
||||
{
|
||||
EnsureConfigure();
|
||||
return _unityEntity;
|
||||
}
|
||||
}
|
||||
public Rigidbody Rigidbody
|
||||
{
|
||||
get=>m_rigidbody;
|
||||
set=>m_rigidbody=value;
|
||||
}
|
||||
|
||||
IUnityEntity IDamagable.UnityEntity => UnityEntity;
|
||||
public Rigidbody Rigidbody => m_rigidbody;
|
||||
public void GiveDamage(DamageMessage message)
|
||||
{
|
||||
UnityEntity.Invoke(message);
|
||||
if (_unityEntity is not null)
|
||||
UnityEntity.Invoke(message);
|
||||
}
|
||||
|
||||
[SerializeField]private Rigidbody m_rigidbody;
|
||||
|
||||
[Inject]
|
||||
[Inject(true)]
|
||||
private IHealth _health;
|
||||
|
||||
private IUnityEntity _unityEntity;
|
||||
|
||||
private bool _initialized;
|
||||
|
||||
private void EnsureConfigure()
|
||||
{
|
||||
if (_initialized) return;
|
||||
_unityEntity = GetComponentInParent<IUnityEntity>(true);
|
||||
_unityEntity?.Inject(this);
|
||||
_initialized = true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,77 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BITFALL.Rig;
|
||||
using BITKit.Entities;
|
||||
using Cysharp.Threading.Tasks.Triggers;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class EntityHitboxBuilder : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Collider[] colliders;
|
||||
[SerializeField] private EntityHitbox[] hitboxes;
|
||||
|
||||
[SerializeReference,SubclassSelector] private IReference[] tagReferences;
|
||||
[BIT]
|
||||
private void Build()
|
||||
{
|
||||
foreach (var x in hitboxes)
|
||||
{
|
||||
DestroyImmediate(x.gameObject);
|
||||
}
|
||||
hitboxes = new EntityHitbox[colliders.Length];
|
||||
for (var i = 0; i < colliders.Length; i++)
|
||||
{
|
||||
var collider = colliders[i];
|
||||
var newGameObject = new GameObject($"Hitbox_{collider.gameObject.name}");
|
||||
|
||||
newGameObject.layer = gameObject.layer;
|
||||
|
||||
newGameObject.transform.SetParent(transform);
|
||||
var transform1 = collider.transform;
|
||||
newGameObject.transform.position = transform1.position;
|
||||
newGameObject.transform.rotation = transform1.rotation;
|
||||
var hitbox =hitboxes[i] = newGameObject.AddComponent<EntityHitbox>();
|
||||
var tag = newGameObject.AddComponent<Tag>();
|
||||
|
||||
var tickOverride = newGameObject.AddComponent<TickOverrideTransform>();
|
||||
|
||||
tickOverride.Source = newGameObject.transform;
|
||||
tickOverride.Target = collider.transform;
|
||||
|
||||
if (collider.TryGetComponent<Rigidbody>(out var newRigidbody))
|
||||
{
|
||||
hitbox.Rigidbody = newRigidbody;
|
||||
}
|
||||
|
||||
switch (collider)
|
||||
{
|
||||
case BoxCollider boxCollider:
|
||||
var box = newGameObject.AddComponent<BoxCollider>();
|
||||
box.size = boxCollider.size;
|
||||
box.center = boxCollider.center;
|
||||
break;
|
||||
case SphereCollider sphereCollider:
|
||||
var sphere = newGameObject.AddComponent<SphereCollider>();
|
||||
sphere.radius = sphereCollider.radius;
|
||||
sphere.center = sphereCollider.center;
|
||||
break;
|
||||
case CapsuleCollider capsuleCollider:
|
||||
var capsule = newGameObject.AddComponent<CapsuleCollider>();
|
||||
capsule.radius = capsuleCollider.radius;
|
||||
capsule.height = capsuleCollider.height;
|
||||
capsule.direction = capsuleCollider.direction;
|
||||
capsule.center = capsuleCollider.center;
|
||||
break;
|
||||
}
|
||||
|
||||
tag.SetTags(tagReferences);
|
||||
EditorUtility.SetDirty(hitbox);
|
||||
EditorUtility.SetDirty(tag);
|
||||
}
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"name": "BITKit.Entities.Physics.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0",
|
||||
"GUID:a3de65b07192e7d49bad7b4032d681de",
|
||||
"GUID:7efac18f239530141802fb139776f333"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -1,144 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BITKit.Entities.Physics;
|
||||
using UnityEngine;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
[CustomType(typeof(IEntityPhysics))]
|
||||
public class EntityPhysics : EntityBehavior,IEntityPhysics
|
||||
{
|
||||
[SerializeField] private Animator animator;
|
||||
[SerializeField] private Rigidbody[] rigidbodies;
|
||||
[SerializeField] private Collider[] ragdollColliders;
|
||||
[SerializeField] private Joint[] joints;
|
||||
[SerializeField] private new Rigidbody rigidbody;
|
||||
|
||||
[Inject(true)]
|
||||
private IHealth _health;
|
||||
|
||||
[Inject(true)] private IEntityOverride _override;
|
||||
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointXMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointYMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointZMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointAngularXMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointAngularYMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointAngularZMotions=new();
|
||||
|
||||
public override void OnAwake()
|
||||
{
|
||||
_health.OnSetAlive += OnSetAlive;
|
||||
foreach (var x in joints)
|
||||
{
|
||||
switch (x)
|
||||
{
|
||||
case ConfigurableJoint configurableJoint:
|
||||
_jointXMotions.Add(configurableJoint, configurableJoint.xMotion);
|
||||
_jointYMotions.Add(configurableJoint, configurableJoint.yMotion);
|
||||
_jointZMotions.Add(configurableJoint, configurableJoint.zMotion);
|
||||
_jointAngularXMotions.Add(configurableJoint, configurableJoint.angularXMotion);
|
||||
_jointAngularYMotions.Add(configurableJoint, configurableJoint.angularYMotion);
|
||||
_jointAngularZMotions.Add(configurableJoint, configurableJoint.angularZMotion);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_override is not null)
|
||||
_override.OnOverride += OnOverride;
|
||||
}
|
||||
|
||||
private void OnOverride(bool obj)
|
||||
{
|
||||
EnsureConf();
|
||||
}
|
||||
|
||||
private void OnSetAlive(bool alive)
|
||||
{
|
||||
EnsureConf();
|
||||
}
|
||||
|
||||
private async void EnsureConf()
|
||||
{
|
||||
var allow = (_health, _override) switch
|
||||
{
|
||||
(not null,not null)=>_health.IsAlive || _override.IsOvering,
|
||||
(not null,null)=>_health.IsAlive,
|
||||
(null,not null)=>_override.IsOvering,
|
||||
_=>false,
|
||||
};
|
||||
if (animator)
|
||||
{
|
||||
animator.enabled = allow;
|
||||
}
|
||||
try
|
||||
{
|
||||
await Task.Delay(10, destroyCancellationToken);
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
foreach (var x in rigidbodies)
|
||||
{
|
||||
//x.isKinematic = alive;
|
||||
x.gameObject.SetActive(!allow);
|
||||
}
|
||||
foreach (var x in ragdollColliders)
|
||||
{
|
||||
x.enabled = !allow;
|
||||
}
|
||||
OnSetPhysics?.Invoke(!allow);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
foreach (var joint in joints)
|
||||
{
|
||||
switch (joint)
|
||||
{
|
||||
case ConfigurableJoint configurableJoint:
|
||||
configurableJoint.xMotion = allow ? _jointXMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.yMotion = allow ? _jointYMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.zMotion = allow ? _jointZMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.angularXMotion =
|
||||
allow ? _jointAngularXMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.angularYMotion =
|
||||
allow ? _jointAngularYMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.angularZMotion =
|
||||
allow ? _jointAngularZMotions[joint] : ConfigurableJointMotion.Free;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public Vector3 Center => rigidbody.worldCenterOfMass;
|
||||
public bool IsPhysics { get; private set; }
|
||||
public Vector3 Velocity
|
||||
{
|
||||
get=>rigidbody.velocity;
|
||||
set
|
||||
{
|
||||
foreach (var x in rigidbodies)
|
||||
{
|
||||
x.velocity = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public Action<bool> OnSetPhysics { get; set; }
|
||||
public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force)
|
||||
{
|
||||
foreach (var x in rigidbodies)
|
||||
{
|
||||
x.AddForce(force, mode);
|
||||
}
|
||||
}
|
||||
public void AddTorque(Vector3 torque, ForceMode mode = ForceMode.Force)
|
||||
{
|
||||
foreach (var x in rigidbodies)
|
||||
{
|
||||
x.AddTorque(torque, mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user