﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using ProtoBuf;

public class PhysicNetted : MonoBehaviour,INetted
{
    // Start is called before the first frame update
    public int objID { get; set; }
    public NettedBehavior gObj { get; set; }
    public Connection owner { get; set; }

    Rigidbody rb;
    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        //gObj = gameObject.gameObject;
    }
    public void Receive(MemoryStream ms)
    {
        using (ms)
        {
            SynTransform t = Serializer.Deserialize<SynTransform>(ms);
            rb.velocity = t.position.Vector();
            rb.angularVelocity = t.scale.Vector();
            rb.drag = t.rotation.w;
            rb.angularDrag = t.rotation.x;
        }
    }
    public bool Send(out byte[] data)
    {
        MemoryStream ms = new MemoryStream();
        SynTransform t = new SynTransform();
        t.position = new SynVector3(rb.velocity);
        t.scale = new SynVector3(rb.angularVelocity);
        t.rotation = new SynQuaterion(new Quaternion(rb.angularDrag,0,0,rb.drag));
        //t.rotation.w = rb.drag;
        //t.rotation.x = rb.angularDrag;
        Serializer.Serialize<SynTransform>(ms, t);
        data = ms.ToArray();
        ms.Dispose();
        return true;
    }
}
