summaryrefslogtreecommitdiff
path: root/GameCode/BounceEffectRetarget.cs
blob: 8e1ef79c4b566211736412074bd89a3c403bec18 (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
using System.Collections;
using Photon.Pun;
using Sonigon;
using UnityEngine;

public class BounceEffectRetarget : BounceEffect
{
	[Header("Sound")]
	public SoundEvent soundTargetBounceTargetPlayer;

	private MoveTransform move;

	private PhotonView view;

	private void Start()
	{
		view = GetComponentInParent<PhotonView>();
		move = GetComponentInParent<MoveTransform>();
		GetComponentInParent<ChildRPC>().childRPCsVector2.Add("TargetBounce", SetNewVel);
		GetComponentInParent<ChildRPC>().childRPCsInt.Add("TargetBounceLine", DrawLineTo);
	}

	public override void DoBounce(HitInfo hit)
	{
		StartCoroutine(DelayMove(hit));
	}

	private void ActuallyDoBounce(int playerId)
	{
		Player playerWithID = PlayerManager.instance.GetPlayerWithID(playerId);
		if ((bool)playerWithID)
		{
			GetComponentInParent<ChildRPC>().CallFunction("TargetBounce", (playerWithID.data.playerVel.position + Vector2.up * move.GetUpwardsCompensation(base.transform.position, playerWithID.data.playerVel.position) - (Vector2)base.transform.position).normalized * move.velocity.magnitude);
			SoundManager.Instance.PlayAtPosition(soundTargetBounceTargetPlayer, SoundManager.Instance.GetTransform(), base.transform);
		}
		else
		{
			GetComponentInParent<ChildRPC>().CallFunction("TargetBounce", move.velocity);
		}
	}

	private void SetNewVel(Vector2 newVel)
	{
		move.enabled = true;
		move.velocity = newVel;
	}

	private Player FindTarget(HitInfo hit)
	{
		Player closestPlayer = PlayerManager.instance.GetClosestPlayer(base.transform.position + (Vector3)hit.normal * 0.1f);
		if (PlayerManager.instance.CanSeePlayer(base.transform.position, closestPlayer).canSee)
		{
			return closestPlayer;
		}
		return null;
	}

	private IEnumerator DelayMove(HitInfo hit)
	{
		Player p = FindTarget(hit);
		if ((bool)p && view.IsMine)
		{
			GetComponentInParent<ChildRPC>().CallFunction("TargetBounceLine", p.playerID);
		}
		move.enabled = false;
		if ((bool)hit.rigidbody)
		{
			move.GetComponent<RayCastTrail>().IgnoreRigFor(hit.rigidbody, 0.5f);
		}
		yield return new WaitForSeconds(0.1f);
		if (view.IsMine)
		{
			if ((bool)p)
			{
				ActuallyDoBounce(p.playerID);
			}
			else
			{
				ActuallyDoBounce(-1);
			}
		}
	}

	private void DrawLineTo(int playerID)
	{
		Player playerWithID = PlayerManager.instance.GetPlayerWithID(playerID);
		if ((bool)playerWithID)
		{
			StartCoroutine(DrawLine(playerWithID.transform));
		}
	}

	private IEnumerator DrawLine(Transform target)
	{
		LineEffect line = GetComponentInChildren<LineEffect>(includeInactive: true);
		line.StartDraw();
		while ((bool)line)
		{
			line.DrawLine(base.transform.position, target.position);
			yield return null;
		}
	}
}