-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoinController.cs
144 lines (114 loc) · 4.93 KB
/
CoinController.cs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Coin_Controller : MonoBehaviour
{
private Game_Manager gm;
private gameObject loadedChunk;
private Text coinCounterUI;
private GameObject target;
public GameObject coinTrailPrefab;
private GameObject coinTrail;
public GameObject coinParticlePrefab;
private GameObject coinParticle;
public bool rewardCoins = false;
private bool inCoinMagnetRadius = false;
private bool startTrackingPos = false;
public List<AudioClip> coinPickupSounds;
public AudioSource coinAudioSource;
void Start()
{
// if the loaded chunk is flipped, flip the coin to face the correct direciton
if (loadedChunk.transform.localEulerAngles == new Vector3(0, 180, 0))
this.transform.localEulerAngles = new Vector3(0, 180, 0);
// randomly shift the coin on the Z axis to avoid overlap/clipping
this.transform.localPosition += new Vector3(0, 0, Random.Range(-1.2f, -0.65f));
gm = Game_Manager.instance;
coinCounterUI = gm.coinAmount_txt;
target = gm.coinCountPos;
}
private void OnTriggerEnter2D(Collider2D collision)
{
// if the ball hits the coin
if (collision.gameObject == gm.ballCoinCollider)
{
TapticManager.Impact(ImpactFeedback.Light);
// if coinCount is being tracked (via a daily challenge), add one more to the count.
if (PlayerPrefs.GetInt("trackCoinAmount", 1) == 0)
PlayerPrefs.SetInt("coinsGathered", PlayerPrefs.GetInt("coinsGathered", 0) + 1);
CoinParticle(this.transform);
startTrackingPos = true;
StartCoroutine("CoinCollect");
coinTrail = Instantiate(coinTrailPrefab, this.transform);
}
// if the ball has an active Coin Magnet
if (collision.gameObject.GetComponent<coinMagnet_Controller>() != null)
{
inCoinMagnetRadius = true;
StartCoroutine("CoinMagnetCollect");
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.GetComponent<coinMagnet_Controller>() != null)
inCoinMagnetRadius = false;
}
IEnumerator CoinCollect()
{
PlayCoinPickupSound();
if (coinTrail == null)
coinTrail = Instantiate(coinTrailPrefab, this.transform);
if (rewardCoins) // coins earned through a challenge shouldn't have a trail
coinTrail.GetComponent<TrailRenderer>().sortingOrder = 101;
var elapsedTime = 0f;
var startingPos = this.transform.position;
var timeToCollect = 0.2f;
Vector2 arcPoint = startingPos + new Vector3(target.transform.position.x - startingPos.x, target.transform.position.y - startingPos.y + Random.Range(-1f, 2.5f), 0);
while (elapsedTime < timeToCollect)
{
var timer = (elapsedTime / timeToCollect);
// moves the coins in an arc with a lerp between two lerps, lerpception?
Vector2 m1 = Vector2.Lerp(startingPos, arcPoint, timer);
Vector2 m2 = Vector2.Lerp(arcPoint, target.transform.position, timer);
var move = Vector2.Lerp(m1, m2, timer);
this.transform.position = move;
elapsedTime += Time.deltaTime;
yield return null;
}
if (elapsedTime >= timeToCollect)
{
PlayerPrefs.SetFloat("Coins", PlayerPrefs.GetFloat("Coins") + 1);
gm.RefreshCoinCount();
CoinParticle(target.transform);
coinTrail.GetComponent<TrailRenderer>().emitting = false;
this.GetComponent<SpriteRenderer>().enabled = false;
gm.StartCoroutine("CoinCountBump");
Destroy(this.gameObject, 0.3f);
}
}
IEnumerator CoinMagnetCollect()
{
var startingPos = this.transform.position;
// bring coin to ball, which then initiates normal CoinCollect
while (inCoinMagnetRadius)
{
this.transform.position = Vector2.Lerp(this.transform.position, gm.ballObj.transform.position, Time.deltaTime * 7);
yield return null;
}
}
void CoinParticle(Transform pos)
{
coinParticle = Instantiate(coinParticlePrefab);
coinParticle.SetActive(true);
coinParticle.transform.position = new Vector3(pos.position.x, pos.position.y, 0);
coinParticle.transform.SetParent(gm.cameraObj.transform);
coinParticle.GetComponent<ParticleSystem>().Emit(5);
Destroy(coinParticle, 0.7f);
}
public void PlayCoinPickupSound()
{
var coinPickupSoundRandom = Random.Range(0, coinPickupSounds.Count);
coinAudioSource.PlayOneShot(coinPickupSounds[coinPickupSoundRandom]);
}
}