Skip to content

Commit

Permalink
Added new extentions
Browse files Browse the repository at this point in the history
  • Loading branch information
Digitalroot committed Jan 25, 2022
1 parent 2a948e9 commit 989386e
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyName>Digitalroot.Valheim.Common.Extensions</AssemblyName>
<TargetFrameworks>net462</TargetFrameworks>
<LangVersion>9</LangVersion>
<LangVersion>10</LangVersion>
<PackageId>Digitalroot.Valheim.Common.Extensions</PackageId>
<RootNamespace>Digitalroot.Valheim.Common.Extensions</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
Expand Down Expand Up @@ -44,8 +44,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Digitalroot.Valheim.Common.References" Version="0.204.5" />
<PackageReference Include="UnityEngine.Modules" Version="[2019.4.32]">
<PackageReference Include="Digitalroot.Valheim.Common.References" Version="0.205.7" />
<PackageReference Include="UnityEngine.Modules" Version="[2019.4.31]">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/Digitalroot.Valheim.Common.Extensions/FloatExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Digitalroot.Valheim.Common.Extensions;

public static class FloatExtensions
{
public static int ToInt32(this float value) => Convert.ToInt32(value);
}
305 changes: 305 additions & 0 deletions src/Digitalroot.Valheim.Common.Extensions/GameObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
using JetBrains.Annotations;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Random = UnityEngine.Random;

// ReSharper disable InconsistentNaming

namespace Digitalroot.Valheim.Common.Extensions
{
public static class GameObjectExtensions
{
public static GameObject AddLedgeJumping(this GameObject prefab)
{
prefab.GetOrAddMonoBehaviour<AutoJumpLedge>();
return prefab;
}

public static bool HasParent(this GameObject prefab) => prefab.transform.GetParent() != null;

public static GameObject GetParent(this GameObject prefab) => prefab.HasParent() ? prefab.transform.GetParent().gameObject : null;

public static bool IsBoss(this GameObject prefab) => prefab.GetComponent<Character>()?.IsBoss() ?? false;

private static Vector3 GetScale(string itemName, Vector3 currentScale)
{
List<string> names = new()
{
"shield"
, "axe"
, "mace"
};

if (names.Contains(itemName.ToLowerInvariant()))
{
return Vector3.one * 2;
}

return Vector3.one;
}

public static string GetUniqueName(this GameObject prefab)
{
List<string> paths = new();

var parent = prefab.transform.GetParent();

while (parent != null)
{
paths.Add(parent.name);
parent = parent.GetParent();
}

var sb = new StringBuilder();
for (var i = paths.Count; i > 0; i--)
{
sb.Append(paths[i - 1]).Append('.');
}

sb.Append(prefab.name);
return sb.ToString();
}

/// <summary>
/// Sets De-spawn In Day to false;
/// </summary>
/// <param name="prefab">MonsterAI</param>
/// <returns></returns>
public static GameObject AsDayWalker(this GameObject prefab)
{
var monsterAI = prefab.GetComponent<MonsterAI>();
if (monsterAI == null) return prefab;

monsterAI.SetDespawnInDay(false);
return prefab;
}

/// <summary>
/// Sets De-spawn In Day to true;
/// </summary>
/// <param name="prefab">MonsterAI</param>
/// <returns></returns>
public static GameObject AsNightStalker(this GameObject prefab)
{
var monsterAI = prefab.GetComponent<MonsterAI>();
if (monsterAI == null) return prefab;

monsterAI.SetDespawnInDay(true);
return prefab;
}

/// <summary>
/// Configure MonsterAI to patrol spawn point.
/// </summary>
/// <param name="prefab">MonsterAI</param>
/// <returns></returns>
public static GameObject AsSpawnPointPatroler(this GameObject prefab)
{
var monsterAI = prefab.GetComponent<MonsterAI>();
if (monsterAI == null) return prefab;

if (prefab.HasParent())
{
prefab.AsSpawnPointPatroler(prefab.transform.parent.transform.position);
}
else
{
monsterAI.SetPatrolPoint();
}

return prefab;
}

/// <summary>
/// Configure MonsterAI to patrol spawn point.
/// </summary>
/// <param name="prefab">MonsterAI</param>
/// <returns></returns>
public static GameObject AsSpawnPointPatroler(this GameObject prefab, Vector3 point)
{
var monsterAI = prefab.GetComponent<MonsterAI>();
if (monsterAI == null) return prefab;

monsterAI.SetPatrolPoint(point);

return prefab;
}

/// <summary>
/// Configure MonsterAI to jump randomly.
/// </summary>
/// <param name="prefab">MonsterAI</param>
/// <param name="min">Min random range.</param>
/// <param name="max">Max random range.</param>
/// <returns></returns>
public static GameObject AsRandomJumper(this GameObject prefab, float min = 5f, float max = 9f)
{
var monsterAI = prefab.GetComponent<MonsterAI>();
if (monsterAI == null) return prefab;
if (monsterAI.m_randomFly) return prefab;
monsterAI.m_jumpInterval = Random.Range(min, max);
return prefab;
}

/// <summary>
/// Configure the base AI to not flee
/// </summary>
/// <param name="prefab">MonsterAI</param>
/// <returns></returns>
public static GameObject AsNoFleeing(this GameObject prefab)
{
var monsterAI = prefab.GetComponent<MonsterAI>();
if (monsterAI == null) return prefab;
monsterAI.m_fleeIfLowHealth = 0f;
return prefab;
}

/// <summary>
/// Sets Path Agent Type
/// </summary>
/// <param name="prefab">MonsterAI</param>
/// <param name="agentType"></param>
/// <returns></returns>
public static GameObject SetPathAgentType(this GameObject prefab, Pathfinding.AgentType agentType)
{
var monsterAI = prefab.GetComponent<MonsterAI>();
if (monsterAI == null) return prefab;
if (monsterAI.m_randomFly) return prefab;

monsterAI.m_pathAgentType = agentType;

return prefab;
}

/// <summary>
/// Set the level of the prefab.
/// </summary>
/// <param name="prefab">Character</param>
/// <param name="levelMin"></param>
/// <param name="levelMax"></param>
/// <returns></returns>
public static GameObject SetLevel(this GameObject prefab, int levelMin, int levelMax)
{
var level = levelMin == levelMax ? levelMax : Random.Range(levelMin, levelMax + 1);
var character = prefab.GetComponent<Character>();
character?.SetLevel(level);
character?.SetupMaxHealth();

// prefab.SendMessage("SetLevel", level, SendMessageOptions.RequireReceiver);
return prefab;
}

/// <summary>
/// Set prefab's local position.
/// </summary>
/// <param name="prefab"></param>
/// <param name="i"></param>
/// <returns></returns>
public static GameObject SetLocalPosition(this GameObject prefab, int i)
{
switch (i)
{
case 1:
prefab.transform.localPosition += Vector3.left * 2.5f;
break;

case 2:
prefab.transform.localPosition += Vector3.right * 2.5f;
break;

case 3:
prefab.transform.localPosition += Vector3.forward * 2.5f;
break;

case 4:
prefab.transform.localPosition += Vector3.back * 2.5f;
break;
}

return prefab;
}

/// <summary>
/// Set prefab's local scale
/// </summary>
/// <param name="prefab"></param>
/// <param name="scaleSize"></param>
/// <returns></returns>
[UsedImplicitly]
public static GameObject SetLocalScale(this GameObject prefab, float scaleSize)
{
prefab.SetLocalScale(new Vector3(scaleSize, scaleSize, scaleSize));
return prefab;
}

/// <summary>
/// Set prefab's local scale
/// </summary>
/// <param name="prefab"></param>
/// <param name="scaleSize"></param>
/// <returns></returns>
[UsedImplicitly]
public static GameObject SetLocalScale(this GameObject prefab, Vector3 scaleSize)
{
var zNetView = prefab.GetComponent<ZNetView>();
zNetView.m_syncInitialScale = true;
zNetView.SetLocalScale(scaleSize);
return prefab;
}

/// <summary>
/// Set prefab's local scale
/// </summary>
/// <param name="prefab"></param>
/// <param name="quaternion"></param>
/// <returns></returns>
public static GameObject SetLocalRotation(this GameObject prefab, Quaternion quaternion)
{
prefab.transform.rotation = quaternion;
return prefab;
}

public static GameObject ScaleEquipment(this GameObject prefab)
{
var visEquipment = prefab.GetComponent<VisEquipment>();
if (visEquipment == null) return prefab;
if (visEquipment.m_leftItemInstance != null)
{
for (var i = 0; i < visEquipment.m_leftItemInstance.transform.childCount; i++)
{
var item = visEquipment.m_leftItemInstance.transform.GetChild(i);
item.localScale = GetScale(item.gameObject.name, item.localScale);
}
}

if (visEquipment.m_rightItemInstance != null)
{
for (var i = 0; i < visEquipment.m_rightItemInstance.transform.childCount; i++)
{
var item = visEquipment.m_rightItemInstance.transform.GetChild(i);
item.localScale = GetScale(item.gameObject.name, item.localScale);
}
}

return prefab;
}

/// <summary>
/// Returns the component of Type type. If one doesn't already exist on the GameObject it will be added.
/// </summary>
/// <remarks>
/// Inspired by Jotunn JVL
/// Source: https://wiki.unity3d.com/index.php/GetOrAddComponent
/// </remarks>
/// <typeparam name="T">The type of Component to return.</typeparam>
/// <param name="gameObject">The GameObject the Component is attached to.</param>
/// <returns>Returns the component of Type T</returns>
[UsedImplicitly]
public static T GetOrAddMonoBehaviour<T>([NotNull] this GameObject gameObject)
where T : MonoBehaviour
{
return gameObject.GetComponent<T>() ?? gameObject.AddComponent<T>();
}
}
}
8 changes: 8 additions & 0 deletions src/Digitalroot.Valheim.Common.Extensions/Int32Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Digitalroot.Valheim.Common.Extensions;

public static class Int32Extensions
{
public static float ToFloat(this int value) => Convert.ToSingle(value);
}
12 changes: 6 additions & 6 deletions src/Digitalroot.Valheim.Common.Extensions/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
".NETFramework,Version=v4.6.2": {
"Digitalroot.Valheim.Common.References": {
"type": "Direct",
"requested": "[0.204.5, )",
"resolved": "0.204.5",
"contentHash": "5+E+7FDWDKAYzL8A1GfBFWmxu2p6LLbd+D5PR79RCAVmJTn/b2cPXdTCiNNe5/msrcpdyaGT3GFra1AjY/V+aA=="
"requested": "[0.205.7, )",
"resolved": "0.205.7",
"contentHash": "iUIYAsg5HXZj3S4yhUECEuZJk98X8RG75wkJrlmIvfriwIZgV57wt4S6ym3KQNHvx87sxSbU3raSPMPTSfi0hw=="
},
"UnityEngine.Modules": {
"type": "Direct",
"requested": "[2019.4.32, 2019.4.32]",
"resolved": "2019.4.32",
"contentHash": "7KmS0Nz+s89igz/tDBRk9PEmWqlwReQZf9E6tl6/uV5DndbaR5WInqis4y6Fwq1B++2leamS7qPffnW5+WQwUw=="
"requested": "[2019.4.31, 2019.4.31]",
"resolved": "2019.4.31",
"contentHash": "xZjdfGGzZw6nnrn4auRNis6WnAlsvqY1XXioEGJnSku+CylESgEt/sTLYlrbrQ0srnLfSCfx80vR8z4SsI1EuQ=="
}
}
}
Expand Down

0 comments on commit 989386e

Please sign in to comment.