summaryrefslogtreecommitdiff
path: root/ROUNDS/Photon.Pun.Simple/NetComponent.cs
blob: 887cd89bc9ae594c3159b4b9081c378302629a44 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using UnityEngine;

namespace Photon.Pun.Simple;

[HelpURL("https://doc.photonengine.com/en-us/pun/current/gameplay/simple/simpleoverview")]
public abstract class NetComponent : MonoBehaviour, IOnJoinedRoom, IOnAwake, IOnStart, IOnEnable, IOnDisable, IOnAuthorityChanged
{
	[HideInInspector]
	[SerializeField]
	protected int prefabInstanceId;

	protected NetObject netObj;

	protected PhotonView photonView;

	protected bool hadFirstAuthorityAssgn;

	public NetObject NetObj => netObj;

	public RigidbodyType RigidbodyType { get; private set; }

	public int ViewID => photonView.ViewID;

	public PhotonView PhotonView => photonView;

	public bool IsMine => photonView.IsMine;

	public int ControllerActorNr => photonView.ControllerActorNr;

	protected virtual void Reset()
	{
	}

	protected virtual void OnValidate()
	{
	}

	public virtual void OnJoinedRoom()
	{
	}

	public void Awake()
	{
		if (!base.transform.GetParentComponent<NetObject>())
		{
			OnAwakeInitialize(isNetObject: false);
		}
	}

	public virtual void OnAwake()
	{
		netObj = base.transform.GetParentComponent<NetObject>();
		EnsureComponentsDependenciesExist();
		OnAwakeInitialize(isNetObject: true);
	}

	public virtual void OnAwakeInitialize(bool isNetObject)
	{
	}

	protected virtual NetObject EnsureComponentsDependenciesExist()
	{
		if (!netObj)
		{
			netObj = base.transform.GetParentComponent<NetObject>();
		}
		if ((bool)netObj)
		{
			photonView = netObj.GetComponent<PhotonView>();
			RigidbodyType = (netObj.Rb ? RigidbodyType.RB : (netObj.Rb2D ? RigidbodyType.RB2D : RigidbodyType.None));
			return netObj;
		}
		Debug.LogError("NetComponent derived class cannot find a NetObject on '" + base.transform.root.name + "'.");
		return null;
	}

	public virtual void Start()
	{
		if (!netObj)
		{
			OnStartInitialize(isNetObject: false);
		}
	}

	public virtual void OnStart()
	{
		OnStartInitialize(isNetObject: true);
	}

	public virtual void OnStartInitialize(bool isNetObject)
	{
	}

	public virtual void OnPostEnable()
	{
	}

	public virtual void OnPostDisable()
	{
		hadFirstAuthorityAssgn = false;
	}

	public virtual void OnAuthorityChanged(bool isMine, bool controllerChanged)
	{
		if (controllerChanged && !hadFirstAuthorityAssgn)
		{
			OnFirstAuthorityAssign(isMine, controllerChanged);
			hadFirstAuthorityAssgn = true;
		}
	}

	public virtual void OnFirstAuthorityAssign(bool isMine, bool asServer)
	{
	}
}