145 lines
4.7 KiB
C#
145 lines
4.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public static class ExtensionMethods
|
|
{
|
|
public static bool GetBool(this Dictionary<string, string> dictionary, string key)
|
|
{
|
|
if (dictionary.ContainsKey(key))
|
|
{
|
|
var output = false;
|
|
bool.TryParse(dictionary[key], out output);
|
|
return output;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
//public static void SetBool(this Dictionary<string, string> dictionary, string key, bool value)
|
|
//{
|
|
// if (dictionary.ContainsKey(key))
|
|
// dictionary[key] = value.ToString();
|
|
// else
|
|
// dictionary.Add(key, value.ToString());
|
|
//}
|
|
|
|
public static string GetString(this Dictionary<string, string> dictionary, string key)
|
|
{
|
|
if (dictionary.ContainsKey(key))
|
|
{
|
|
return dictionary[key];
|
|
}
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
//public static void SetString(this Dictionary<string, string> dictionary, string key, string value)
|
|
//{
|
|
// if (dictionary.ContainsKey(key))
|
|
// dictionary[key] = value;
|
|
// else
|
|
// dictionary.Add(key, value);
|
|
//}
|
|
|
|
public static int GetInt(this Dictionary<string, string> dictionary, string key)
|
|
{
|
|
if (dictionary.ContainsKey(key))
|
|
{
|
|
var output = 0;
|
|
int.TryParse(dictionary[key], out output);
|
|
return output;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
//public static void SetInt(this Dictionary<string, string> dictionary, string key, int value)
|
|
//{
|
|
// if (dictionary.ContainsKey(key))
|
|
// dictionary[key] = value.ToString();
|
|
// else
|
|
// dictionary.Add(key, value.ToString());
|
|
//}
|
|
|
|
public static void Set(this Dictionary<string, string> dictionary, string key, object value)
|
|
{
|
|
if (dictionary.ContainsKey(key))
|
|
dictionary[key] = value.ToString();
|
|
else
|
|
dictionary.Add(key, value.ToString());
|
|
}
|
|
|
|
|
|
|
|
// EntityAlive extensions for attack targeting
|
|
|
|
public static Ray GetAttackRay(this EntityAlive entityAlive)
|
|
{
|
|
return new Ray(entityAlive.GetAttackOrigin(), entityAlive.GetAttackVector());
|
|
}
|
|
|
|
public static Vector3 GetAttackOrigin(this EntityAlive entityAlive)
|
|
{
|
|
Vector3 v = entityAlive.position + new Vector3(0f, entityAlive.GetAttackHeight(), 0f);
|
|
return v;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a height closer to ground level than the eyes height
|
|
/// </summary>
|
|
/// <param name="entityAlive"></param>
|
|
/// <returns></returns>
|
|
public static float GetAttackHeight(this EntityAlive entityAlive)
|
|
{
|
|
/*
|
|
// disabled 21/06/2024 - todo: find a better formula
|
|
var rightArm = entityAlive.GetComponentsInChildren<Collider>().Where(x => x.CompareTag("E_BP_RArm")).FirstOrDefault();
|
|
if (rightArm != null)
|
|
{
|
|
var worldPos = entityAlive.transform.position;
|
|
var rArmPos = rightArm.transform.position;
|
|
var armHeight = MathF.Abs(rArmPos.y - worldPos.y);
|
|
//Log.Out($"entity: {entityAlive}, worldPos: {worldPos}, armPos: {rArmPos}, armHeight: {armHeight}");
|
|
return armHeight;
|
|
}*/
|
|
|
|
// load generic default values?
|
|
|
|
var walkType = entityAlive.GetWalkType();
|
|
if (walkType == 21) return 0.2f; // 21 is crawler
|
|
if (walkType == 22) return 0.5f; // 22 is spider
|
|
|
|
// any other case?
|
|
|
|
return 0.7f; // because tall zombies can't hit snakes...
|
|
}
|
|
|
|
public static Vector3 GetAttackVector(this EntityAlive entityAlive)
|
|
{
|
|
var myTarget = entityAlive.GetAttackTarget();
|
|
|
|
if (myTarget == null) return entityAlive.GetDefaultLookVector();
|
|
|
|
var col = myTarget.GetComponentsInChildren<Collider>().Where(x => x.CompareTag(RebirthUtilities.GetRandomBodyTag())).FirstOrDefault();
|
|
|
|
if (col == null) return entityAlive.GetDefaultLookVector();
|
|
|
|
Vector3 v = (col.transform.position - (entityAlive.transform.position + new Vector3(0f, entityAlive.GetAttackHeight(), 0f)));
|
|
//Log.Out($"Attack vector: {v}");
|
|
return v;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the base class(EntityAlive) default look vector
|
|
/// </summary>
|
|
/// <param name="entityAlive"></param>
|
|
/// <returns></returns>
|
|
public static Vector3 GetDefaultLookVector(this EntityAlive entityAlive)
|
|
{
|
|
float num = Mathf.Cos(entityAlive.rotation.y * 0.0175f - MathF.PI);
|
|
float num2 = Mathf.Sin(entityAlive.rotation.y * 0.0175f - MathF.PI);
|
|
float num3 = 0f - Mathf.Cos(entityAlive.rotation.x * 0.0175f);
|
|
float y = Mathf.Sin(entityAlive.rotation.x * 0.0175f);
|
|
return new Vector3(num2 * num3, y, num * num3);
|
|
}
|
|
} |