This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tag.cs
81 lines (65 loc) · 2.33 KB
/
Tag.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
using Astrum.AstralCore.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Astrum
{
partial class AstralTags
{
public class Tag
{
public static List<Tag> tags = new();
public static event Action<Tag> OnTagRegistered = new(_ => { });
public readonly int priority;
public readonly List<TagInstance> instances = new();
internal readonly Func<Player, TagData> evaluator;
public Tag(Func<Player, TagData> evaluator, int priority = 0)
{
this.evaluator = evaluator;
this.priority = priority;
tags.Add(this);
tags = tags.OrderByDescending(tag => tag.priority).ToList();
OnTagRegistered(this);
}
public void CalculateAll() => instances.ForEach(x => x?.Calculate());
}
public struct TagData
{
public bool enabled;
public string text;
public Color textColor;
public Color background;
}
public class TagInstance
{
public PlayerData player;
public Transform parent;
public ImageThreeSlice image;
public TMPro.TextMeshProUGUI text;
public Tag tag;
public TagInstance(PlayerData player, Tag tag, Transform transform)
{
this.player = player;
this.tag = tag;
parent = transform.parent;
image = parent.GetComponent<ImageThreeSlice>();
text = transform.GetComponent<TMPro.TextMeshProUGUI>();
}
public void Calculate()
{
if (parent == null) return;
TagData data = tag.evaluator(player.player);
if (data.enabled != parent.gameObject.activeSelf)
{
player.RecalculatePositions();
if (!(parent.gameObject.active = data.enabled))
return;
}
if (data.text != null) text.text = data.text;
if (data.textColor != null) text.color = data.textColor;
if (data.background != null) image.color = data.background;
}
}
}
}