summaryrefslogtreecommitdiff
path: root/Client/Assembly-CSharp/InnerNet/InnerNetObject.cs
blob: 129bf06d8d8d51759d8037b730684f02b386fb53 (plain)
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
86
87
88
89
90
91
92
93
94
using System;
using Hazel;
using UnityEngine;

namespace InnerNet
{
	public abstract class InnerNetObject : MonoBehaviour, IComparable<InnerNetObject>
	{
        // 这个数据是不是我(本玩家)
		public bool AmOwner
		{
			get
			{
				return this.OwnerId == AmongUsClient.Instance.ClientId;
			}
		}

        // 这个字段用处未知,应该是用来标记此类的派生类
		public uint SpawnId;

        // 每一个此类数据(及派生数据)都会在创建的时候分配一个netId
        // 每个这类数据都有一个场景内唯一的netId,会在广播Rpc调用的时候用来找到对应的数据结构
		public uint NetId;

        // 此类数据公用的脏数据标记为,用来决定是否同步这个数据(即用MessageWriter写入)
        public uint DirtyBits;

        /*
        public enum SpawnFlags : byte
	    {
		    None = 0,
		    IsClientCharacter = 1
	    }
        */
        public SpawnFlags SpawnFlags;

        /* 
        public enum SendOption : byte
        {
            None = 0,
            Reliable = 1
        }
        */
        public SendOption sendMode = SendOption.Reliable;

        // 数据对应的玩家ID,对于本玩家自己的数据会有特殊处理
		public int OwnerId;

		protected bool DespawnOnDestroy = true;

		public void Despawn()
		{
			UnityEngine.Object.Destroy(base.gameObject);
			AmongUsClient.Instance.Despawn(this);
		}

		public virtual void OnDestroy()
		{
			if (AmongUsClient.Instance && this.NetId != 4294967295U)
			{
				if (this.DespawnOnDestroy && this.AmOwner)
				{
					AmongUsClient.Instance.Despawn(this);
					return;
				}
				AmongUsClient.Instance.RemoveNetObject(this);
			}
		}

		public abstract void HandleRpc(byte callId, MessageReader reader);

		public abstract bool Serialize(MessageWriter writer, bool initialState);

		public abstract void Deserialize(MessageReader reader, bool initialState);

		public int CompareTo(InnerNetObject other)
		{
			if (this.NetId > other.NetId)
			{
				return 1;
			}
			if (this.NetId < other.NetId)
			{
				return -1;
			}
			return 0;
		}

		protected void SetDirtyBit(uint val)
		{
			this.DirtyBits |= val;
		}
	}
}