diff --git a/Libraries/Flakkari4Unity/Source/Entity.cs b/Libraries/Flakkari4Unity/Source/Entity.cs new file mode 100644 index 0000000..35d9f09 --- /dev/null +++ b/Libraries/Flakkari4Unity/Source/Entity.cs @@ -0,0 +1,226 @@ +using System; +using UnityEngine; + +using CurrentProtocol = Flakkari4Unity.Protocol.V1; + +namespace Flakkari4Unity.ECS +{ + public class Entity : MonoBehaviour + { + public ulong Id { get; set; } + + public void CreateComponents(byte[] data) + { + for (int i = 0; i < data.Length; i++) + { + switch ((CurrentProtocol.ComponentId)data[i]) + { + case CurrentProtocol.ComponentId.CHILD: + i++; + var child = gameObject.AddComponent(); + child.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.EVOLVE: + i++; + var evolve = gameObject.AddComponent(); + evolve.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.HEALTH: + i++; + var health = gameObject.AddComponent(); + health.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.ID: + i++; + Id = BitConverter.ToUInt64(data, i); + i += sizeof(ulong); + break; + + case CurrentProtocol.ComponentId.LEVEL: + i++; + var level = gameObject.AddComponent(); + level.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.PARENT: + i++; + var parent = gameObject.AddComponent(); + parent.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.TAG: + i++; + var tag = gameObject.AddComponent(); + tag.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.TEMPLATE: + i++; + var template_ = gameObject.AddComponent(); + template_.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.TIMER: + i++; + var timer = gameObject.AddComponent(); + timer.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.WEAPON: + i++; + var weapon = gameObject.AddComponent(); + weapon.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.TRANSFORM_3D: + i++; + var existingTransform = gameObject.GetComponent(); + existingTransform.position = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2)); + i += sizeof(float) * 3; + existingTransform.rotation = new Quaternion(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2), BitConverter.ToSingle(data, i + sizeof(float) * 3)); + i += sizeof(float) * 4; + existingTransform.localScale = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2)); + i += sizeof(float) * 3; + break; + + case CurrentProtocol.ComponentId.MOVABLE_3D: + i++; + var movable = gameObject.AddComponent(); + movable.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.CONTROL_3D: + i++; + var control = gameObject.AddComponent(); + control.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.BOXCOLLIDER: + i++; + var boxCollider = gameObject.AddComponent(); + boxCollider.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.SPHERECOLLIDER: + i++; + var sphereCollider = gameObject.AddComponent(); + sphereCollider.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.RIGIDBODY_3D: + i++; + var rigidbody = gameObject.GetComponent() ?? gameObject.AddComponent(); + rigidbody.Deserialize(data, ref i); + break; + } + } + } + + public void UpdateComponents(byte[] data) + { + for (int i = 0; i < data.Length; i++) + { + switch ((CurrentProtocol.ComponentId)data[i]) + { + case CurrentProtocol.ComponentId.CHILD: + i++; + var child = gameObject.GetComponent(); + child.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.EVOLVE: + i++; + var evolve = gameObject.GetComponent(); + evolve.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.HEALTH: + i++; + var health = gameObject.GetComponent(); + health.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.LEVEL: + i++; + var level = gameObject.GetComponent(); + level.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.PARENT: + i++; + var parent = gameObject.GetComponent(); + parent.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.TAG: + i++; + var tag = gameObject.GetComponent(); + tag.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.TEMPLATE: + i++; + var template_ = gameObject.GetComponent(); + template_.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.TIMER: + i++; + var timer = gameObject.GetComponent(); + timer.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.WEAPON: + i++; + var weapon = gameObject.GetComponent(); + weapon.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.TRANSFORM_3D: + i++; + var transform = gameObject.GetComponent(); + transform.position = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2)); + i += sizeof(float) * 3; + transform.rotation = new Quaternion(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2), BitConverter.ToSingle(data, i + sizeof(float) * 3)); + i += sizeof(float) * 4; + transform.localScale = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2)); + i += sizeof(float) * 3; + break; + + case CurrentProtocol.ComponentId.MOVABLE_3D: + i++; + var movable = gameObject.GetComponent(); + movable.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.CONTROL_3D: + i++; + var control = gameObject.GetComponent(); + control.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.BOXCOLLIDER: + i++; + var boxCollider = gameObject.GetComponent(); + boxCollider.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.SPHERECOLLIDER: + i++; + var sphereCollider = gameObject.GetComponent(); + sphereCollider.Deserialize(data, ref i); + break; + + case CurrentProtocol.ComponentId.RIGIDBODY_3D: + i++; + var rigidbody = gameObject.GetComponent(); + rigidbody.Deserialize(data, ref i); + break; + } + } + } + } +}