summaryrefslogtreecommitdiff
path: root/GameCode/MoveSequence.cs
blob: 1437614a7f9d3683c94e5470c9c55e11453f915d (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
using Photon.Pun;
using UnityEngine;

public class MoveSequence : MonoBehaviour
{
	private int targetID;

	public Vector2[] positions;

	public float drag = 1f;

	public float spring = 1f;

	public float cap = 1f;

	public float threshold = 1f;

	public float timeAtPos;

	private float counter;

	private Vector2 startPos;

	private Vector2 velocity;

	private Rigidbody2D rig;

	private Map map;

	private string myKey;

	private void Start()
	{
		base.gameObject.layer = 17;
		startPos = base.transform.localPosition;
		rig = GetComponent<Rigidbody2D>();
		map = GetComponentInParent<Map>();
		myKey = "MapObect " + GetComponentInParent<Map>().levelID + " " + base.transform.GetSiblingIndex();
		MapManager.instance.GetComponent<ChildRPC>().childRPCsInt.Add(myKey, RPCA_SetTargetID);
	}

	private void OnDestroy()
	{
		if ((bool)MapManager.instance)
		{
			MapManager.instance.GetComponent<ChildRPC>().childRPCsInt.Remove(myKey);
		}
	}

	private void OnDrawGizmos()
	{
		for (int i = 0; i < positions.Length; i++)
		{
			Gizmos.DrawSphere((Vector2)base.transform.position + positions[i], 0.25f + (float)i * 0.15f);
		}
	}

	private void Update()
	{
		if (MapTransition.isTransitioning || !map.hasEntered)
		{
			return;
		}
		Vector2 vector = positions[targetID] + startPos;
		Vector2 vector2 = vector - (Vector2)base.transform.position;
		vector2 = Vector3.ClampMagnitude(vector2, cap);
		if ((bool)rig)
		{
			rig.gravityScale = 0f;
			rig.AddForce(vector2 * spring * CappedDeltaTime.time * rig.mass);
		}
		else
		{
			velocity += vector2 * spring * CappedDeltaTime.time;
			velocity -= velocity * drag * CappedDeltaTime.time;
			base.transform.position += (Vector3)velocity * TimeHandler.deltaTime;
		}
		if (!PhotonNetwork.IsMasterClient || !(Vector2.Distance(base.transform.position, vector) < threshold))
		{
			return;
		}
		counter += TimeHandler.deltaTime;
		if (counter > timeAtPos)
		{
			targetID++;
			if (targetID >= positions.Length)
			{
				targetID = 0;
			}
			MapManager.instance.GetComponent<ChildRPC>().CallFunction(myKey, targetID);
			counter = 0f;
		}
	}

	private void RPCA_SetTargetID(int setValue)
	{
		targetID = setValue;
	}
}