84 lines
3.2 KiB
C#
84 lines
3.2 KiB
C#
using BoneSync.Sync;
|
|
using StressLevelZero.Combat;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace BoneSync.Networking.Messages
|
|
{
|
|
public struct ObjectHealthInfo
|
|
{
|
|
public float _health;
|
|
public int _hits;
|
|
public Vector3 normal;
|
|
public float damage;
|
|
public bool crit;
|
|
public AttackType attackType;
|
|
public ObjectHealthInfo(float health, int hits, Vector3 normal, float damage, bool crit, AttackType attackType)
|
|
{
|
|
_health = health;
|
|
_hits = hits;
|
|
this.normal = normal;
|
|
this.damage = damage;
|
|
this.crit = crit;
|
|
this.attackType = attackType;
|
|
}
|
|
}
|
|
public enum ObjectDamageType
|
|
{
|
|
Unknown = 0,
|
|
SyncHealth = 1,
|
|
DestructibleTakeDamage = 2,
|
|
PropHealthTakeDamage = 3,
|
|
}
|
|
public struct ObjectDamageInfo
|
|
{
|
|
public ushort objectId;
|
|
public ObjectDamageType eventType;
|
|
public ObjectHealthInfo objectHealthInfo;
|
|
}
|
|
|
|
[PacketType(PacketType.ObjectEvent), PacketReliability(PacketReliability.UnreliableNoDelay)]
|
|
public class ObjectDamageMessage : NetworkMessage
|
|
{
|
|
public ObjectDamageInfo objectEventInfo => _objectEventInfo;
|
|
private ObjectDamageInfo _objectEventInfo;
|
|
|
|
public ObjectDamageMessage(ObjectDamageInfo objectEventInfo)
|
|
{
|
|
_objectEventInfo = objectEventInfo;
|
|
byteEncoder.WriteUShort(_objectEventInfo.objectId);
|
|
byteEncoder.WriteByte((byte)_objectEventInfo.eventType);
|
|
byteEncoder.WriteFloat(_objectEventInfo.objectHealthInfo._health);
|
|
byteEncoder.WriteInt(_objectEventInfo.objectHealthInfo._hits);
|
|
if (_objectEventInfo.eventType == ObjectDamageType.SyncHealth) return; // No need to write the rest of the data
|
|
byteEncoder.WriteVector3(_objectEventInfo.objectHealthInfo.normal);
|
|
byteEncoder.WriteFloat(_objectEventInfo.objectHealthInfo.damage);
|
|
byteEncoder.WriteBool(_objectEventInfo.objectHealthInfo.crit);
|
|
byteEncoder.WriteByte((byte)_objectEventInfo.objectHealthInfo.attackType);
|
|
}
|
|
|
|
public ObjectDamageMessage(Packet packet)
|
|
{
|
|
byteEncoder.WriteBytes(packet.Data);
|
|
_objectEventInfo.objectId = byteEncoder.ReadUShort();
|
|
_objectEventInfo.eventType = (ObjectDamageType)byteEncoder.ReadByte();
|
|
_objectEventInfo.objectHealthInfo._health = byteEncoder.ReadFloat();
|
|
_objectEventInfo.objectHealthInfo._hits = byteEncoder.ReadInt();
|
|
if (_objectEventInfo.eventType == ObjectDamageType.SyncHealth) return; // No need to read the rest of the data
|
|
_objectEventInfo.objectHealthInfo.normal = byteEncoder.ReadVector3();
|
|
_objectEventInfo.objectHealthInfo.damage = byteEncoder.ReadFloat();
|
|
_objectEventInfo.objectHealthInfo.crit = byteEncoder.ReadBool();
|
|
_objectEventInfo.objectHealthInfo.attackType = (AttackType)byteEncoder.ReadByte();
|
|
}
|
|
|
|
public override void Execute()
|
|
{
|
|
ObjectSync.OnObjectDamageMessage(this);
|
|
}
|
|
}
|
|
}
|