//////////internal /// <summary> /// Spawn this entity, resets stats, set action to spawning, then later calls OnEntitySpawnFinish. /// NOTE: calls after an update to ensure Awake and Start is called. /// </summary> protected virtual void OnSpawned() { if (mPoolData == null) { mPoolData = GetComponent <PoolDataController>(); } mState = mPrevState = StateInvalid; //avoid invalid updates //allow activator to start and check if we need to spawn now or later //ensure start is called before spawning if we are freshly allocated from entity manager if (activator != null) { activator.Start(); if (activator.deactivateOnStart) { mDoSpawnOnWake = true; //do it later when we wake up } else { StartCoroutine(DoSpawn()); } } else { StartCoroutine(DoSpawn()); } }
/// <summary> /// Call this if you want to manually "spawn" an entity on scene (make sure to set activateOnStart=false), will not do anything if Spawn has already been called /// </summary> public void Spawn(GenericParams parms) { if (mIsSpawned) { return; } // initialize data if (mPoolData == null) { mPoolData = GetComponent <PoolDataController>(); } mState = mPrevState = StateInvalid; //avoid invalid updates mSpawnParams = parms; // //allow activator to start and check if we need to spawn now or later //ensure start is called before spawning if we are freshly allocated from entity manager if (activator != null) { activator.Start(); //do it later when we wake up if (!activator.deactivateOnStart) { ExecuteSpawn(); } } else { ExecuteSpawn(); } }
public PoolDataController Allocate(string group, string name, Transform parent) { if (mAvailable.Count == 0) { if (mActives.Count + 1 > maxCapacity) { Debug.LogWarning(template.name + " is expanding beyond max capacity: " + maxCapacity); Expand(group, maxCapacity); } else { Expand(group, 1); } } PoolDataController pdc = mAvailable[mAvailable.Count - 1]; mAvailable.RemoveAt(mAvailable.Count - 1); pdc.claimed = false; Transform t = pdc.transform; t.name = string.IsNullOrEmpty(name) ? template.name + (mNameHolder++) : name; t.SetParent(parent == null ? defaultParent : parent, false); t.gameObject.SetActive(true); mActives.Add(pdc); return(pdc); }
void OnDespawnEntity(M8.PoolDataController pdc) { if (CheckSpawn(pdc)) { mStageSpawnChecks.Remove(pdc); } }
public void Release(PoolDataController pdc) { if (pdc.claimed) //already claimed { return; } FactoryData dat; if (mFactory.TryGetValue(pdc.factoryKey, out dat)) { if (despawnCallback != null) { despawnCallback(pdc); } pdc.Despawn(); dat.Release(pdc); } else { Debug.LogWarning("Unable to find type: " + pdc.factoryKey + " in " + group + ", failed to release."); } }
/// <summary> /// Release all currently spawned objects based on given type. /// </summary> public void ReleaseAllByType(string type) { FactoryData factory; if (mFactory.TryGetValue(type, out factory)) { for (int i = 0; i < factory.actives.Count; i++) { PoolDataController pdc = factory.actives[i]; if (despawnCallback != null) { despawnCallback(pdc); } pdc.Despawn(); factory.ReleaseIgnoreActiveList(pdc); } factory.actives.Clear(); } else { Debug.LogWarning("Unable to find type: " + type); } }
public void Expand(string group, int num) { mCapacity += num; if (mCapacity > maxCapacity) { maxCapacity = mCapacity; } for (int i = 0; i < num; i++) { //PoolDataController Transform t = (Transform)Object.Instantiate(template); t.name = template.name; t.parent = mInactiveHolder; t.localPosition = Vector3.zero; PoolDataController pdc = t.GetComponent <PoolDataController>(); if (pdc == null) { pdc = t.gameObject.AddComponent <PoolDataController>(); } pdc.group = group; pdc.factoryKey = template.name; mAvailable.Add(pdc); } }
void OnSpawnEntity(M8.PoolDataController pdc) { if (CheckSpawn(pdc)) { mStageSpawnChecks.Add(pdc); } }
public static void ReleaseFromGroup(string group, PoolDataController pdc) { PoolController pc = GetPool(group); if (pc != null) { pc.Release(pdc); } }
/// <summary> /// Only ReleaseAll and ReleaseAllByType should call this /// </summary> public void ReleaseIgnoreActiveList(PoolDataController pdc) { pdc.gameObject.SetActive(false); Transform t = pdc.transform; t.SetParent(mInactiveHolder, false); mAvailable.Add(pdc); }
public SpawnInfo(SceneSerializerSpawnItem item) { PoolDataController pdc = item.GetComponent <PoolDataController>(); id = item.id; grp = pdc.group; type = pdc.factoryKey; pos = item.transform.position; rot = item.transform.rotation; }
public static void ReleaseAuto(PoolDataController poolCtrl) { PoolController pc = GetPool(poolCtrl.group); if (pc != null) { pc.Release(poolCtrl); } else { poolCtrl.gameObject.SetActive(false); } }
public void Release(Transform entity) { PoolDataController pdc = entity.GetComponent <PoolDataController>(); if (pdc) { Release(pdc); } else //not in the pool, just kill it { Object.Destroy(entity.gameObject); } }
public static void ReleaseAuto(GameObject entity) { //NOTE: don't really need to destroy PoolDataController pdc = entity.GetComponent <PoolDataController>(); if (pdc != null) { ReleaseAuto(pdc); } else { entity.SetActive(false); } }
void OnBlobRelease(M8.PoolDataController pdc) { pdc.despawnCallback -= OnBlobRelease; for (int i = 0; i < mBlobActives.Count; i++) { var blob = mBlobActives[i]; if (blob && blob.poolData == pdc) { mBlobActives.RemoveAt(i); break; } } }
void M8.IPoolSpawnComplete.OnSpawnComplete() { if (!mIsPoolCtrlChecked) { mPoolCtrl = GetComponent <M8.PoolDataController>(); mIsPoolCtrlChecked = true; } if (body) { body.simulated = false; } mRout = StartCoroutine(DoSpawn()); }
/// <summary> /// Only ReleaseAll and ReleaseAllByType should set removeFromActive to false /// </summary> public void Release(PoolDataController pdc, bool removeFromActive = true) { pdc.claimed = true; Transform t = pdc.transform; t.SetParent(mInactiveHolder, false); if (removeFromActive) { mActives.Remove(pdc); } mAvailable.Add(pdc); }
bool CheckSpawn(M8.PoolDataController pdc) { bool isValidSpawn = false; for (int i = 0; i < stageSpawnTagChecks.Length; i++) { if (pdc.CompareTag(stageSpawnTagChecks[i])) { isValidSpawn = true; break; } } return(isValidSpawn); }
public void Release(PoolDataController pdc) { pdc.Despawn(); FactoryData dat; if (mFactory.TryGetValue(pdc.factoryKey, out dat)) { dat.Release(pdc); } else { Debug.LogWarning("Unable to find type: " + pdc.factoryKey + " in " + group + ", failed to release."); } }
/// <summary> /// Only ReleaseAll and ReleaseAllByType should set removeFromActive to false /// </summary> public void Release(PoolDataController pdc, bool removeFromActive = true) { pdc.claimed = true; Transform t = pdc.transform; t.parent = mInactiveHolder; t.localPosition = Vector3.zero; if (removeFromActive) { mActives.Remove(pdc); } mAvailable.Add(pdc); }
protected virtual void Awake() { mPoolData = GetComponent <PoolDataController>(); mSerializer = GetComponent <SceneSerializer>(); if (activator == null) { activator = GetComponentInChildren <EntityActivator>(); } if (activator != null) { activator.awakeCallback += ActivatorWakeUp; activator.sleepCallback += ActivatorSleep; } }
/// <summary> /// Releases all currently spawned objects of all types. /// </summary> public void ReleaseAll() { foreach (KeyValuePair <string, FactoryData> itm in mFactory) { FactoryData factory = itm.Value; for (int i = 0; i < factory.actives.Count; i++) { PoolDataController pdc = factory.actives[i]; pdc.SendMessage(OnDespawnMessage, null, SendMessageOptions.DontRequireReceiver); factory.Release(pdc, false); } factory.actives.Clear(); } }
/// <summary> /// Releases all currently spawned objects of all types. /// </summary> public void ReleaseAll() { foreach (KeyValuePair <string, FactoryData> itm in mFactory) { FactoryData factory = itm.Value; for (int i = 0; i < factory.actives.Count; i++) { PoolDataController pdc = factory.actives[i]; pdc.Despawn(); factory.Release(pdc, false); } factory.actives.Clear(); } }
public void Expand(string group, int num) { mCapacity += num; if (mCapacity > maxCapacity) { int diff = mCapacity - maxCapacity; maxCapacity = mCapacity; mActives.Expand(diff); mAvailable.Expand(diff); } for (int i = 0; i < num; i++) { var pdc = PoolDataController.Generate(name, template, group, mInactiveHolder); mAvailable.Add(pdc); } }
void M8.IPoolSpawn.OnSpawned(M8.GenericParams parms) { if (!mPoolData) { mPoolData = GetComponent <M8.PoolDataController>(); } SpriteShape spriteShape = null; Vector2 dir = Vector2.zero; if (parms != null) { if (parms.ContainsKey(parmSpriteShape)) { spriteShape = parms.GetValue <SpriteShape>(parmSpriteShape); } if (parms.ContainsKey(parmDir)) { dir = parms.GetValue <Vector2>(parmDir); } } if (spriteShape) { spriteShapeCtrl.spriteShape = spriteShape; } transform.localEulerAngles = new Vector3(0f, 0f, Random.Range(0f, 360f)); spriteShapeCtrl.enabled = true; mBody.simulated = true; dir = M8.MathUtil.RotateAngle(dir, dirAngleRange.random); mBody.AddForce(dir * impulseRange.random, ForceMode2D.Impulse); spriteShapeCtrl.spriteShapeRenderer.color = mDefaultColor; StartCoroutine(DoAlive()); }
/// <summary> /// Release all currently spawned objects based on given type. /// </summary> public void ReleaseAllByType(string type) { FactoryData factory; if (mFactory.TryGetValue(type, out factory)) { for (int i = 0; i < factory.actives.Count; i++) { PoolDataController pdc = factory.actives[i]; pdc.Despawn(); factory.Release(pdc, false); } factory.actives.Clear(); } else { Debug.LogWarning("Unable to find type: " + type); } }
/// <summary> /// Release all currently spawned objects based on given type. /// </summary> public void ReleaseAllByType(string type) { FactoryData factory; if (mFactory.TryGetValue(type, out factory)) { for (int i = 0; i < factory.actives.Count; i++) { PoolDataController pdc = factory.actives[i]; pdc.SendMessage(OnDespawnMessage, null, SendMessageOptions.DontRequireReceiver); factory.Release(pdc, false); } factory.actives.Clear(); } else { Debug.LogWarning("Unable to find type: " + type); } }
public void Release(Transform entity) { entity.SendMessage(OnDespawnMessage, null, SendMessageOptions.DontRequireReceiver); PoolDataController pdc = entity.GetComponent <PoolDataController>(); if (pdc) { FactoryData dat; if (mFactory.TryGetValue(pdc.factoryKey, out dat)) { dat.Release(pdc); } else { Debug.LogWarning("Unable to find type: " + pdc.factoryKey + " in " + group + ", failed to release."); } } else //not in the pool, just kill it { Object.Destroy(entity.gameObject); } }
/// <summary> /// Releases all currently spawned objects of all types. /// </summary> public void ReleaseAll() { foreach (KeyValuePair <string, FactoryData> itm in mFactory) { FactoryData factory = itm.Value; for (int i = 0; i < factory.actives.Count; i++) { PoolDataController pdc = factory.actives[i]; if (despawnCallback != null) { despawnCallback(pdc); } pdc.Despawn(); factory.ReleaseIgnoreActiveList(pdc); } factory.actives.Clear(); } }
public static PoolDataController Generate(string factoryKey, GameObject template, string group, Transform parent) { var go = Instantiate(template); go.name = template.name; go.SetActive(false); go.transform.SetParent(parent, false); PoolDataController pdc = go.GetComponent <PoolDataController>(); if (pdc == null) { pdc = go.AddComponent <PoolDataController>(); } pdc.group = group; pdc.factoryKey = !string.IsNullOrEmpty(factoryKey) ? factoryKey : template.name; pdc.mIsSpawned = false; return(pdc); }