-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgrammingLevel.cs
More file actions
156 lines (126 loc) · 5.55 KB
/
ProgrammingLevel.cs
File metadata and controls
156 lines (126 loc) · 5.55 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using DG.Tweening;
using Sirenix.OdinInspector;
using UnityEngine;
namespace RLO.Science.BasicsOfProgramming
{
public class ProgrammingLevel : MonoBehaviour
{
public List<Transform> TileTransforms;
public float CamSize => _camSize;
public Transform CamFocus => _camFocus;
public Tile StartTile => _startTile;
public int DatesToCollect => _datesToCollect;
[SerializeField] private float _camSize;
[SerializeField] private Transform _camFocus;
[SerializeField] private Tile _startTile;
[SerializeField] private Tile[] _tileControllers;
[SerializeField] private Pickup[] _pickups;
[SerializeField] private int _datesToCollect;
public void SpawnLevel(Action onComplete = null)
{
foreach (var pickup in _pickups) pickup.gameObject.SetActive(false);
var animDuration = 0.6f;
var delayMod = 1.6f;
var tileTforms = new List<Transform>(TileTransforms);
var delays = AnimationSequenceDelay.Calculate(tileTforms, _startTile.transform);
for (int i = 0; i < TileTransforms.Count; i++)
{
var tgtPos = tileTforms[i].localPosition;
var from = new Vector3(tgtPos.x, tgtPos.y - 5, tgtPos.z);
tileTforms[i].DOLocalMove(from, animDuration).From().SetDelay(delays[i]/delayMod).SetEase(Ease.OutCirc);
var t = tileTforms[i].DOScale(Vector3.zero, animDuration).From().SetDelay(delays[i]/delayMod).SetEase(Ease.InSine);
if (i == TileTransforms.Count - 1) t.OnComplete(() =>
{
foreach (var pickup in _pickups)
{
pickup.gameObject.SetActive(true);
pickup.Spawn();
}
onComplete?.Invoke();
});
}
}
public void DespawnLevel(Action onComplete = null)
{
var split = .03f;
var stuff = new List<Transform>(TileTransforms).Shuffle();
for (int i = 0; i < TileTransforms.Count; i++)
{
var t = stuff[i].DOScale(Vector3.zero, .4f).SetDelay(i * split).SetEase(Ease.InSine);
if (i == TileTransforms.Count - 1) t.OnComplete(() => onComplete?.Invoke());
}
}
public void RestartLevel(Action onComplete = null)
{
var tiles = new List<Transform>(TileTransforms);
var delays = AnimationSequenceDelay.Calculate(tiles, _startTile.transform);
foreach(var pickup in _pickups) pickup.OnPreReset();
for (int i = 0; i < tiles.Count; i++)
{
var t = tiles[i].DOPunchPosition(-Vector3.up / 4, .25f, 2, .5f)
.SetDelay(delays[i]/3)
.SetEase(Ease.InOutSine);
if (i == tiles.Count - 1) t.OnComplete(() =>
{
foreach (var pickup in _pickups) pickup.Spawn();
onComplete?.Invoke();
});
}
}
[Button(ButtonSizes.Large, Name = "Set names, links and references")]
private void DEV_SetupLevel()
{
_tileControllers = GetComponentsInChildren<Tile>();
TileTransforms = new List<Transform>(_tileControllers.Select(x => x.transform));
_pickups = GetComponentsInChildren<Pickup>();
var totalTiles = TileTransforms.Count;
var tileList = new List<Tile>();
void VisitTile(Tile tile, ref List<Tile> visited, int limit)
{
if (visited.Contains(tile)) return;
visited.Add(tile);
if (visited.Count == limit) return;
tile.DetectNeighbours();
var n = tile.Links[DIR.North];
var s = tile.Links[DIR.South];
var e = tile.Links[DIR.East];
var w = tile.Links[DIR.West];
if (n) VisitTile(n, ref visited, limit);
if (s) VisitTile(s, ref visited, limit);
if (e) VisitTile(e, ref visited, limit);
if (w) VisitTile(w, ref visited, limit);
}
VisitTile(_tileControllers[0], ref tileList, totalTiles);
for (int i = 0; i < tileList.Count; i++)
{
var idString = i.ToString();
for (int j = idString.Length; j < 3; j++)
{
idString = "0" + idString;
}
tileList[i].gameObject.name = $"Tile_{idString}_{gameObject.name}";
}
}
private static class AnimationSequenceDelay
{
private static float xFactor = 0.09f;
private static float zFactor = 0.15f;
public static float[] Calculate(List<Transform> items, Transform center)
{
var result = new float[items.Count];
var reference = center.position;
for (int i = 0; i < items.Count; i++)
{
var itemPos = items[i].position;
var diff = reference - itemPos;
var delay = Mathf.Abs(diff.z * zFactor) + Mathf.Abs(diff.x * xFactor);
result[i] = delay;
}
return result;
}
}
}
}