Last active
September 16, 2019 10:58
-
-
Save gluschenko/895ab99527255375b3f68b5c400e2147 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Forge; | |
public class NicknameHUD | |
{ | |
struct Entity | |
{ | |
public Vector3 pos; | |
public CardEntity card; | |
} | |
const float MaxDistance = 50; | |
const float ScaleMin = 0.5f; | |
const float ScaleMax = 1f; | |
readonly static Queue<Entity> queue = new Queue<Entity>(); | |
public static void Push(Vector3 pos, CardEntity card) | |
{ | |
queue.Enqueue(new Entity { pos = pos, card = card }); | |
} | |
public static void Draw() | |
{ | |
if (Camera.main != null) | |
{ | |
while (queue.Count > 0) | |
{ | |
var entity = queue.Dequeue(); | |
if (IsVisible(entity.pos) && entity.card != null) | |
{ | |
Color alt = FGUIColors.Parse(entity.card.color, Color.white); | |
Color color = TryGetColor(entity.card.nickname, alt); | |
Vector3 pos = Camera.main.WorldToScreenPoint(entity.pos); | |
// | |
float dist = Vector3.Distance(Camera.main.transform.position, entity.pos); | |
float diff = dist / MaxDistance; | |
float scale = Mathf.Clamp(Mathf.Lerp(ScaleMax, ScaleMin, diff), ScaleMin, ScaleMax); | |
float opacity = Mathf.Clamp(Mathf.Lerp(1, 0, diff), 0, 1); | |
// | |
DrawNickname(pos, entity.card.nickname, color, scale, opacity); | |
} | |
} | |
} | |
} | |
// Around pivot | |
public static void DrawNickname(Vector2 pos, string text, Color color, float scale = 1, float opacity = 1) | |
{ | |
pos = new Vector2(pos.x, Screen.height - pos.y); | |
Vector2 size = FGUI.CalcSize(FGUIStyles.LabelMiddleCenter, text) * 1.2f; | |
Rect rect = new Rect(pos - size / 2f, size); | |
FGUI.Resize(scale, pos); | |
float temp = FGUI.TextOpacity; | |
FGUI.TextOpacity = opacity; | |
FGUI.LabelOutline(rect, text, FGUIStyles.LabelMiddleCenter, Color.black); | |
FGUI.Label(rect, text, FGUIStyles.LabelMiddleCenter, color); | |
FGUI.TextOpacity = temp; | |
FGUI.UndoResize(); | |
} | |
// GUI | |
public static Vector2 DrawNickname(Rect rect, string text, Color color) | |
{ | |
Vector2 pos = rect.position; | |
Vector2 size = FGUI.CalcSize(FGUIStyles.LabelMiddleLeft, text); | |
rect = new Rect(pos, size); | |
FGUI.LabelOutline(rect, text, FGUIStyles.LabelMiddleLeft, Color.black); | |
FGUI.Label(rect, text, FGUIStyles.LabelMiddleLeft, color); | |
return size; | |
} | |
public static Vector2 DrawNickname(Rect rect, CardEntity card) | |
{ | |
Color alt = FGUIColors.Parse(card.color, Color.white); | |
Color color = TryGetColor(card.nickname, alt); | |
return DrawNickname(rect, card.nickname, color); | |
} | |
// Utils | |
public static bool IsVisible(Vector3 pos) | |
{ | |
if (Camera.main != null) | |
{ | |
Vector3 camPos = Camera.main.transform.forward; | |
Vector3 delta = pos - Camera.main.transform.position; | |
float dot = Dot(camPos, delta); | |
if (dot >= 0f) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
public static float Dot(Vector3 A, Vector3 B) | |
{ | |
return A.x * B.x + A.y * B.y + A.z * B.z; | |
} | |
// | |
public static Color TryGetColor(string nickname, Color alt) | |
{ | |
nickname = nickname.ToLower(); | |
string[] arr = nickname.Split(new char[] { '<', '>' }, StringSplitOptions.RemoveEmptyEntries); | |
foreach (string a in arr) | |
{ | |
string[] aa = a.Split(new char[] { '=' }); | |
if (aa.Length > 1) | |
{ | |
return FGUIColors.Parse(aa[1].Trim()); | |
} | |
} | |
return alt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment