125 lines
3.2 KiB
C#
125 lines
3.2 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class Misc {
|
|
|
|
public static bool checkBit(int mask, int bit)
|
|
{
|
|
return (mask & (1 << bit)) != 0;
|
|
}
|
|
|
|
public static int Clamp(int value, int min, int max) {
|
|
if (value > max) {
|
|
return max;
|
|
} else if (value < min) {
|
|
return min;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
|
|
public static bool VectorInsideCircle(Vector2 circleCenter, int rad, Vector2 vec)
|
|
{
|
|
float a, b;
|
|
if (vec.x > circleCenter.x)
|
|
{
|
|
a = vec.x - circleCenter.x;
|
|
} else
|
|
{
|
|
a = circleCenter.x - vec.x;
|
|
}
|
|
if (vec.y > circleCenter.y)
|
|
{
|
|
b = vec.y - circleCenter.y;
|
|
}
|
|
else
|
|
{
|
|
b = circleCenter.y - vec.y;
|
|
}
|
|
float c = Mathf.Sqrt((a * a) + (b * b));
|
|
if (c < rad)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
//make first character in a string uppercase
|
|
public static string uppercaseFirst(string s)
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
return char.ToUpper(s[0]) + s.Substring(1);
|
|
}
|
|
|
|
//is character allowed in a chat message?
|
|
public static bool charAllowedInChatMessage(char c)
|
|
{
|
|
//letters
|
|
if (c >= 'a' && c <= 'z')
|
|
return true;
|
|
//uppercase letters
|
|
if (c >= 'A' && c <= 'Z')
|
|
return true;
|
|
//numbers
|
|
if (c >= '0' && c <= '9')
|
|
return true;
|
|
//special
|
|
switch (c)
|
|
{
|
|
case '/':
|
|
case ' ':
|
|
case '-':
|
|
case '\'':
|
|
case '"':
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static string UppercaseFirst(string s)
|
|
{
|
|
// Check for empty string.
|
|
if (string.IsNullOrEmpty(s))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
// Return char and concat substring.
|
|
return char.ToUpper(s[0]) + s.Substring(1);
|
|
}
|
|
|
|
public static Terrain GetClosestCurrentTerrain(Vector3 entityPos)
|
|
{
|
|
#if UNITY_EDITOR
|
|
Terrain[] terrains = Terrain.activeTerrains;
|
|
#else
|
|
Terrain[] terrains = Terrain.activeTerrains;
|
|
#endif
|
|
|
|
if (terrains.Length < 1)
|
|
return null;
|
|
|
|
//Get the closest one to the player
|
|
var center = new Vector3(terrains[0].transform.position.x + terrains[0].terrainData.size.x / 2, entityPos.y, terrains[0].transform.position.z + terrains[0].terrainData.size.z / 2);
|
|
float lowDist = (center - entityPos).sqrMagnitude;
|
|
var terrainIndex = 0;
|
|
|
|
for (int i = 0; i < terrains.Length; i++)
|
|
{
|
|
center = new Vector3(terrains[i].transform.position.x + terrains[i].terrainData.size.x / 2, entityPos.y, terrains[i].transform.position.z + terrains[i].terrainData.size.z / 2);
|
|
|
|
//Find the distance and check if it is lower than the last one then store it
|
|
var dist = (center - entityPos).sqrMagnitude;
|
|
if (dist < lowDist)
|
|
{
|
|
lowDist = dist;
|
|
terrainIndex = i;
|
|
}
|
|
}
|
|
return terrains[terrainIndex];
|
|
}
|
|
}
|