NetworkedBehaviour.cs 4.87 KB
Newer Older
Ben W's avatar
Ben W committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using DarkRift;
using Gather1.Server;

class NetworkedBehaviour : MonoBehaviour
{
    public enum SyncType { None, Transform }
    public ushort UID
    {
        get => _uid;
        set
        {
            if (_uid != 0)
            {
                Debug.LogWarning("Tried to change UID");
                return;
            }
            _uid = value;
        }
    }
    private ushort _uid;
    public GameObject Prefab;
    public Action<Message, DarkRiftReader> MessageReceived;

    public bool ClientAuth = false;
    public SyncType Sync = SyncType.None;
    [SerializeField] private float _threshold = 0.1f;
    private Vector3 _lastPosition;
    private Vector3 _lastRotation;
    private float _rotXDifferent;
    private float _rotYDifferent;
    private float _rotZDifferent;

    public virtual void OnEnable() => MessageReceived += NewMessage;

    public virtual void OnDisable() => MessageReceived -= NewMessage;

    public virtual void Initialize(DarkRiftReader reader)
    {
        _uid = reader.ReadUInt16();

        Vector3 position = new Vector3()
        {
            x = reader.ReadSingle(),
            y = reader.ReadSingle(),
            z = reader.ReadSingle()
        };
        Vector3 rotation = new Vector3()
        {
            x = reader.ReadSingle(),
            y = reader.ReadSingle(),
            z = reader.ReadSingle()
        };

        transform.position = position;
        transform.rotation = Quaternion.Euler(rotation);
        NetworkManager.NetworkedObjects.Add(_uid, this);
        Debug.Log($"Added {_uid} to Networked Objects");
    }

    public virtual void InitilizeData(out Message message, out DarkRiftWriter writer)
    {
        writer = DarkRiftWriter.Create();

        writer.Write(Prefab.name);
        writer.Write(UID);
        Vector3 pos = transform.position;
        writer.Write(pos.x);
        writer.Write(pos.y);
        writer.Write(pos.z);
        Vector3 rot = transform.rotation.eulerAngles;
        writer.Write(rot.x);
        writer.Write(rot.y);
        writer.Write(rot.z);

        message = Message.Create((ushort)NetworkTags.SpawnPrefab, writer);
    }

    public virtual void Update()
    {
Ben W's avatar
Ben W committed
86
87
        if ((!ClientAuth && !NetworkManager.IsServer) ||
            Sync == SyncType.None)
Ben W's avatar
Ben W committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
            return;
        SyncTransform();
    }

    private void SyncTransform()
    {
        _rotXDifferent = _lastRotation.x - transform.rotation.eulerAngles.x;
        _rotYDifferent = _lastRotation.y - transform.rotation.eulerAngles.y;
        _rotZDifferent = _lastRotation.z - transform.rotation.eulerAngles.z;

        if (_rotXDifferent < 0)
            _rotXDifferent = -_rotXDifferent;
        if (_rotYDifferent < 0)
            _rotYDifferent = -_rotYDifferent;
        if (_rotZDifferent < 0)
            _rotZDifferent = -_rotZDifferent;

        if (Vector3.Distance(_lastPosition, transform.position) > _threshold ||
            _rotXDifferent > _threshold ||
            _rotYDifferent > _threshold ||
            _rotZDifferent > _threshold)
        {
            _lastPosition = transform.position;
            _lastRotation = transform.rotation.eulerAngles;

            using (DarkRiftWriter writer = DarkRiftWriter.Create())
            {
                writer.Write(UID);

                writer.Write(_lastPosition.x);
                writer.Write(_lastPosition.y);
                writer.Write(_lastPosition.z);

                writer.Write(_lastRotation.x);
                writer.Write(_lastRotation.y);
                writer.Write(_lastRotation.z);

                NetworkManager.SendMessage(NetworkTags.MoveObject, writer, SendMode.Unreliable);
Ben W's avatar
Ben W committed
126
                Debug.Log("Sent MoveObject Message");
Ben W's avatar
Ben W committed
127
128
129
130
131
132
            }
        }
    }

    private void UpdateTransform(DarkRiftReader reader)
    {
Ben W's avatar
Ben W committed
133
134
        if (ClientAuth && !NetworkManager.IsServer)
            return;
Ben W's avatar
Ben W committed
135
        Debug.Log($"Received MoveObject Message");
Ben W's avatar
Ben W committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        _lastPosition = reader.ReadVector3();
        _lastRotation = reader.ReadVector3();

        transform.position = _lastPosition;
        transform.rotation = Quaternion.Euler(_lastRotation);

        if (!NetworkManager.IsServer)
            return;

        using (DarkRiftWriter writer = DarkRiftWriter.Create())
        {
            writer.Write(UID);

            writer.Write(_lastPosition);
            writer.Write(_lastRotation);

            using (Message message = Message.Create((ushort)NetworkTags.MoveObject, writer))
            {
                NetworkManager.ServerSendMessageToAll(message, SendMode.Unreliable);
            }
        }
    }

    private void NewMessage(Message message, DarkRiftReader reader)
    {
        switch (message.Tag)
        {
            case (ushort)NetworkTags.MoveObject:
                UpdateTransform(reader);
                break;
        }
    }
}