Reorganizes Project (#41)
This commit is contained in:
parent
08bf44af9a
commit
3614a66aee
3499 changed files with 79 additions and 55 deletions
171
Projects/Scripts/Spells/Base/SpellRegistry.cs
Normal file
171
Projects/Scripts/Spells/Base/SpellRegistry.cs
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Spells
|
||||
{
|
||||
public class SpellRegistry
|
||||
{
|
||||
private static Type[] m_Types = new Type[700];
|
||||
private static int m_Count;
|
||||
|
||||
private static Dictionary<Type, int> m_IDsFromTypes = new Dictionary<Type, int>(m_Types.Length);
|
||||
|
||||
private static object[] m_Params = new object[2];
|
||||
|
||||
private static string[] m_CircleNames =
|
||||
{
|
||||
"First",
|
||||
"Second",
|
||||
"Third",
|
||||
"Fourth",
|
||||
"Fifth",
|
||||
"Sixth",
|
||||
"Seventh",
|
||||
"Eighth",
|
||||
"Necromancy",
|
||||
"Chivalry",
|
||||
"Bushido",
|
||||
"Ninjitsu",
|
||||
"Spellweaving"
|
||||
};
|
||||
|
||||
public static Type[] Types
|
||||
{
|
||||
get
|
||||
{
|
||||
m_Count = -1;
|
||||
return m_Types;
|
||||
}
|
||||
}
|
||||
|
||||
//What IS this used for anyways.
|
||||
public static int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_Count == -1)
|
||||
{
|
||||
m_Count = 0;
|
||||
|
||||
for (int i = 0; i < m_Types.Length; ++i)
|
||||
if (m_Types[i] != null)
|
||||
++m_Count;
|
||||
}
|
||||
|
||||
return m_Count;
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<int, SpecialMove> SpecialMoves{ get; } = new Dictionary<int, SpecialMove>();
|
||||
|
||||
public static int GetRegistryNumber(ISpell s)
|
||||
{
|
||||
return GetRegistryNumber(s.GetType());
|
||||
}
|
||||
|
||||
public static int GetRegistryNumber(SpecialMove s)
|
||||
{
|
||||
return GetRegistryNumber(s.GetType());
|
||||
}
|
||||
|
||||
public static int GetRegistryNumber(Type type)
|
||||
{
|
||||
return m_IDsFromTypes.TryGetValue(type, out int value) ? value : -1;
|
||||
}
|
||||
|
||||
public static void Register(int spellID, Type type)
|
||||
{
|
||||
if (spellID < 0 || spellID >= m_Types.Length)
|
||||
return;
|
||||
|
||||
if (m_Types[spellID] == null)
|
||||
++m_Count;
|
||||
|
||||
m_Types[spellID] = type;
|
||||
|
||||
if (!m_IDsFromTypes.ContainsKey(type))
|
||||
m_IDsFromTypes.Add(type, spellID);
|
||||
|
||||
if (type.IsSubclassOf(typeof(SpecialMove)))
|
||||
{
|
||||
SpecialMove spm = null;
|
||||
|
||||
try
|
||||
{
|
||||
spm = Activator.CreateInstance(type) as SpecialMove;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
if (spm != null)
|
||||
SpecialMoves.Add(spellID, spm);
|
||||
}
|
||||
}
|
||||
|
||||
public static SpecialMove GetSpecialMove(int spellID)
|
||||
{
|
||||
if (spellID < 0 || spellID >= m_Types.Length)
|
||||
return null;
|
||||
|
||||
Type t = m_Types[spellID];
|
||||
|
||||
if (t == null || !t.IsSubclassOf(typeof(SpecialMove)))
|
||||
return null;
|
||||
|
||||
SpecialMoves.TryGetValue(spellID, out SpecialMove move);
|
||||
return move;
|
||||
}
|
||||
|
||||
public static Spell NewSpell(int spellID, Mobile caster, Item scroll)
|
||||
{
|
||||
if (spellID < 0 || spellID >= m_Types.Length)
|
||||
return null;
|
||||
|
||||
Type t = m_Types[spellID];
|
||||
|
||||
if (t?.IsSubclassOf(typeof(SpecialMove)) == false)
|
||||
{
|
||||
m_Params[0] = caster;
|
||||
m_Params[1] = scroll;
|
||||
|
||||
try
|
||||
{
|
||||
return (Spell)Activator.CreateInstance(t, m_Params);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Spell NewSpell(string name, Mobile caster, Item scroll)
|
||||
{
|
||||
for (int i = 0; i < m_CircleNames.Length; ++i)
|
||||
{
|
||||
Type t = ScriptCompiler.FindTypeByFullName($"Server.Spells.{m_CircleNames[i]}.{name}");
|
||||
|
||||
if (t?.IsSubclassOf(typeof(SpecialMove)) == false)
|
||||
{
|
||||
m_Params[0] = caster;
|
||||
m_Params[1] = scroll;
|
||||
|
||||
try
|
||||
{
|
||||
return (Spell)Activator.CreateInstance(t, m_Params);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue