Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
251 lines (243 sloc) 8.48 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
/// <summary>
/// <para>Author: KEMBL</para>
/// <para>Support: http://mehanos.ru</para>
/// This class holds initial spawn parameters for WorldItem object. It makes easy watching for when, where, who spawns objects
/// </summary>
public class SpawnContext : IDisposable
{
/// <summary>
/// Static counter for trace amount of all spawned objects and give id for current
/// </summary>
static int Counter;
/// <summary>
/// Array with all contexts for program lifetime
/// </summary>
public static Dictionary<int, SpawnContext> SpawnList = new Dictionary<int, SpawnContext>();
/// <summary>
/// Unique number for current object, convenient for understand order of object spawning
/// </summary>
public int Id;
/// <summary>
/// Main object who spawns current context
/// </summary>
public long ItemId;
/// <summary>
/// Method which have to be called after object spawn
/// </summary>
public PreComponentStart ComponentConfigDelegate;
/// <summary>
/// Iterface Id where spawn was performed
/// </summary>
public int SpawnInterfaceId;
/// <summary>
/// Object spawn position according to scene and game map
/// </summary>
public Vector3 Position = new Vector3();
/// <summary>
/// Object spawn rotation according to scene and game map
/// </summary>
public Quaternion Rotation = Quaternion.identity;
/// <summary>
/// Object spawn scale according to scene and game map
/// </summary>
public Vector3 Scale;
/// <summary>
/// Add new spawn context to Item and return new Id for Item
/// </summary>
/// <param name="itemId">who spawns this object</param>
/// <param name="componentConfigDelegate">which method to call before this object activation</param>
/// <param name="spawnInterfaceId">in which interface we spawn it</param>
/// <param name="position">required object pos</param>
/// <param name="rotation">required rot</param>
/// <param name="scale">required scale</param>
/// <returns></returns>
static public SpawnContext SetGet(long itemId, PreComponentStart componentConfigDelegate, int spawnInterfaceId, Vector3 position, Quaternion rotation, Vector3 scale)
{
int counter = Set(itemId, componentConfigDelegate, spawnInterfaceId, position, rotation, scale);
return Get(counter);
}
/// <summary>
/// Add new spawn context
/// </summary>
/// <param name="itemId">who spawns this object</param>
/// <param name="componentConfigDelegate">which method to call before this object activation</param>
/// <param name="spawnInterfaceId">in which interface we spawn it</param>
/// <param name="position">required object pos</param>
/// <param name="rotation">required rot</param>
/// <param name="scale">required scale</param>
/// <returns></returns>
static public int Set(long itemId, PreComponentStart componentConfigDelegate, int spawnInterfaceId, Vector3 position, Quaternion rotation, Vector3 scale)
{
Counter++; //increase spawned items counter
SpawnList.Add( //memo new context where current item was spawned
Counter, new SpawnContext() {
Id = Counter,
ItemId = itemId,
ComponentConfigDelegate = componentConfigDelegate,
SpawnInterfaceId = spawnInterfaceId,
Position = position,
Rotation = rotation,
Scale = scale
}
);
#if UNITY_EDITOR
//Debug.Log(Time.time + ">SpawnContext.Set(): " + SpawnList[Counter]);
#endif
return Counter;
}
/// <summary>
/// Search info by context Id
/// </summary>
/// <param name="id">Context Id</param>
/// <returns></returns>
static public SpawnContext Get(int id)
{
if (SpawnList.ContainsKey(id)) return SpawnList[id];
#if UNITY_EDITOR
//Debug.LogError(Time.time + ">Get(): Spawn context Id " + id + " not exists!");
#endif
return null;
}
/// <summary>
/// Remove Context info by its Id
/// </summary>
/// <param name="id"></param>
static public void Remove(int id)
{
if (!SpawnList.ContainsKey(id))
{
#if UNITY_EDITOR
Debug.LogError(Time.time + ">Remove(): Spawn context Id " + id + " not exists!");
#endif
return;
}
SpawnList[id].ComponentConfigDelegate = null; // unlink delegate
SpawnList.Remove(id);
}
/// <summary>
/// Remove Context info by Item Id
/// </summary>
/// <param name="itemId"></param>
static public void RemoveByItem(long itemId)
{
SpawnContext sc = Search(itemId); //search fro context with given ItemId
if (sc == null)
{
#if UNITY_EDITOR
//Debug.LogError(Time.time + ">RemoveByItem(): Spawn context for item Id " + itemId + " not exists!");
#endif
return;
}
#if UNITY_EDITOR
//Debug.Log(Time.time + ">SpawnContext.RemoveByItem(): " + sc);
#endif
Remove(sc.Id); //remove context by id
}
/// <summary>
/// Search Context info by Item Id
/// </summary>
/// <param name="id"></param>
static public SpawnContext Search(long itemId)
{
// search context with given ItemId
return SpawnList.Values.ToList().Find(var => var.ItemId == itemId);
}
/// <summary>
/// Read current context from given WorldItem
/// </summary>
/// <param name="wi"></param>
/// <returns>SpawnContext</returns>
static public SpawnContext Read(WorldItem wi)
{
if ( wi == null ) //check args
{
#if UNITY_EDITOR
Debug.LogError(Time.time + ">SpawnContext.Read(): Null WorldItem argument!");
#endif
return null;
}
return Read(wi.GameObject); //read context from GameObject
}
/// <summary>
/// Read current context from given GameObject
/// </summary>
/// <param name="go"></param>
/// <returns>SpawnContext</returns>
static public SpawnContext Read(GameObject go)
{
if (go == null)//check args
{
#if UNITY_EDITOR
Debug.LogError(Time.time + ">SpawnContext.Read(): Null GameObject argument!");
#endif
return null;
}
ObjectInformation oi = go.GetComponent<ObjectInformation>(); //read object Info
if (oi == null)
{
#if UNITY_EDITOR
Debug.LogError(Time.time + ">SpawnContext.Read(): No ObjectInformation on object " + go.name + ", cant read spawn params!", go);
#endif
return null;
}
SpawnContext sc = SpawnContext.Get(oi.SpawnContextId); // search spawn context info its Id
if (sc == null)
{
#if UNITY_EDITOR
Debug.LogError(Time.time + ">SpawnContext.Read(): No SpawnContext on object " + go.name + ", cant read spawn params!", go);
#endif
return null;
}
return sc;
}
/// <summary>
/// Remove all class exemplars. Often when map changes or game ends.
/// </summary>
public static void DisposeAll()
{
#if UNITY_EDITOR
if (SpawnList.Keys.Count > 0) // count debug
Debug.Log(Time.time + ">SpawnContext.Dispose(): Clean from " + SpawnList.Keys.Count + " item spawn contexts with Counter = " + Counter + "!");
#endif
// remove all objects in SpawnList
foreach (KeyValuePair<int,SpawnContext> kv in SpawnList)
{
kv.Value.Dispose(); // dispose current exemplar
}
Counter = 0;
SpawnList.Clear();
}
/// <summary>
/// Dispose current exemplar
/// </summary>
public void Dispose()
{
#if UNITY_EDITOR
//Debug.Log(Time.time + ">SpawnContext.Dispose(): " + this);
#endif
ComponentConfigDelegate = null; //unlink delegate
}
#if UNITY_EDITOR
/// <summary>
/// Override ToString for better debug
/// </summary>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("SpawnContext Id {0}", Id);
sb.AppendFormat(", ItemId: {0}", ItemId);
sb.AppendFormat(", ComponentConfigDelegate: {0}", (ComponentConfigDelegate == null ? "Null" : "Setted"));
sb.AppendFormat(", SpawnInterfaceId: {0}", SpawnInterfaceId);
sb.AppendFormat(", Position: {0}", Position);
sb.AppendFormat(", Rotation: {0}", Rotation);
sb.AppendFormat(", Scale: {0}", Scale);
sb.AppendFormat(", Counter: {0}", Counter);
return sb.ToString();
}
#endif
}