summaryrefslogtreecommitdiff
path: root/ROUNDS/Photon.Pun.Simple/MountThrow.cs
blob: 532663fc75bc338fe2ab261a0b270384f2c85347 (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
using System.Collections.Generic;
using UnityEngine;

namespace Photon.Pun.Simple;

[RequireComponent(typeof(Mount))]
public class MountThrow : NetComponent, IOnPreUpdate, IOnPreSimulate
{
	public KeyCode throwKey;

	public Mount mount;

	public bool fromRoot = true;

	public bool inheritRBVelocity = true;

	public Vector3 offset = new Vector3(0f, 3f, 0f);

	public Vector3 velocity = new Vector3(0f, 1f, 5f);

	private bool throwQueued;

	public override void OnAwake()
	{
		base.OnAwake();
		if (mount == null)
		{
			mount = GetComponent<Mount>();
		}
	}

	public void OnPreUpdate()
	{
		if (base.IsMine && Input.GetKeyDown(throwKey))
		{
			throwQueued = true;
		}
	}

	public void OnPreSimulate(int frameId, int subFrameId)
	{
		if (!throwQueued)
		{
			return;
		}
		throwQueued = false;
		List<IMountable> mountedObjs = mount.mountedObjs;
		for (int i = 0; i < mountedObjs.Count; i++)
		{
			IMountable mountable = mountedObjs[i];
			Rigidbody rb = mountable.Rb;
			if ((bool)rb && mountable.IsThrowable)
			{
				SyncState syncState = mountable as SyncState;
				if ((bool)syncState)
				{
					Transform transform = (fromRoot ? mount.mountsLookup.transform : base.transform);
					Vector3 position = transform.TransformPoint(offset);
					Quaternion rotation = transform.rotation;
					Vector3 vector = ((inheritRBVelocity && (bool)rb) ? (rb.velocity + transform.TransformVector(velocity)) : transform.TransformVector(velocity));
					syncState.Throw(position, rotation, vector);
				}
			}
		}
	}
}