/// <summary> /// Removes and destroys replay objects that don't exist in the current frame /// </summary> private void RemoveReplayObjects() { int AIid = GameController.SideAIs[GameController.Side]; List <GameObject> visibleEnemies = CurrentFrame[AIid].GetProperty <RecordableState.ExtendedAI>().VisibleEnemies; Dictionary <GameObject, Vector3> lastPosition = CurrentFrame[AIid].GetProperty <RecordableState.ExtendedAI>().LastEnemyPositions; // destroy objects that dont exist in the frame and add their id to a list List <int> replayRefsToRemove = new List <int>(); foreach (KeyValuePair <int, GameObject> pair in ReplayRefs) { int id = pair.Key; bool isVisible = visibleEnemies.Contains(GameController.RecordableRefs[id]); if (CurrentFrame.ContainsKey(id)) { RecordableState.RecordableState state = CurrentFrame[id]; Side?side = null; if (state.GetProperty <RecordableState.BaseAI>() != null) { side = state.GetProperty <RecordableState.BaseAI>().Side; } RecordableState.OperatorState operatorState = state.GetProperty <RecordableState.OperatorState>(); if ((side != null && side != GameController.Side && !isVisible) || (side == GameController.Side && !operatorState.IsAlive)) { GameObject obj = pair.Value; Destroy(obj); replayRefsToRemove.Add(id); } } else { GameObject obj = pair.Value; Destroy(obj); replayRefsToRemove.Add(id); } } // using the created list remove all elements from replayRefs that point to a null object foreach (int id in replayRefsToRemove) { ReplayRefs.Remove(id); } /*List<int> removeFromMarkers = new List<int>(); * foreach (KeyValuePair<int, GameObject> pair in Markers) { * int id = pair.Key; * if (!lastPosition.ContainsKey(GameController.RecordableRefs[id])) { * removeFromMarkers.Add(id); * } * } * * foreach (int id in removeFromMarkers) { * Destroy(Markers[id]); * Markers.Remove(id); * }*/ }
/// <summary> /// Updates and creates replay objects to match the frame state. /// </summary> private void UpdateReplayObjects() { int AIid = GameController.SideAIs[GameController.Side]; List <GameObject> visibleEnemies = CurrentFrame[AIid].GetProperty <RecordableState.ExtendedAI>().VisibleEnemies; Dictionary <GameObject, Vector3> lastPosition = CurrentFrame[AIid].GetProperty <RecordableState.ExtendedAI>().LastEnemyPositions; foreach (GameObject obj in MarkersList) { Destroy(obj); } List <GameObject> newList = new List <GameObject>(); foreach (KeyValuePair <GameObject, Vector3> pair in lastPosition) { Vector3 pos = pair.Value; GameObject marker = Instantiate(GameController.Unknown, pos, Quaternion.identity); newList.Add(marker); } MarkersList = newList; foreach (KeyValuePair <int, RecordableState.RecordableState> pair in CurrentFrame) { int id = pair.Key; RecordableState.RecordableState state = CurrentFrame[id]; bool hasReplayType = GameController.RecordableReplayTypes.ContainsKey(id); if (hasReplayType) { GameObject obj = null; bool isVisible = visibleEnemies.Contains(GameController.RecordableRefs[id]); // if replay object doesn't exist: create one if (!ReplayRefs.ContainsKey(id)) { if (state.GetProperty <RecordableState.BaseAI>() != null) { Side side = state.GetProperty <RecordableState.BaseAI>().Side; RecordableState.OperatorState operatorState = state.GetProperty <RecordableState.OperatorState>(); if (side == GameController.Side && (operatorState == null || operatorState.IsAlive == true)) { obj = Instantiate(GameController.RecordableReplayTypes[id]); ReplayRefs.Add(id, obj); } else if (side != GameController.Side) { if (isVisible) { obj = Instantiate(GameController.RecordableReplayTypes[id]); // create replay type ReplayRefs.Add(id, obj); /*if (Markers.ContainsKey(id)) { * Destroy(Markers[id]); * Markers.Remove(id); * }*/ } /*else { * if (lastPosition.ContainsKey(GameController.RecordableRefs[id])) { * if (!Markers.ContainsKey(id)) { * Markers.Add(id, Instantiate(GameController.Unknown, lastPosition[GameController.RecordableRefs[id]], Quaternion.identity)); * } * else { * Markers[id].transform.position = lastPosition[GameController.RecordableRefs[id]]; * } * } else { * if (Markers.ContainsKey(id)) { * Destroy(Markers[id]); * Markers.Remove(id); * } * } * }*/ } } else { obj = Instantiate(GameController.RecordableReplayTypes[id]); ReplayRefs.Add(id, obj); } } else { obj = ReplayRefs[id]; } // update state if (obj != null) { if (state.GetProperty <RecordableState.Audio>() != null) { for (int i = LastFrameAsInt + 1; i <= CurrentFrameAsInt; i++) { RecordableState.Audio audio = GameController.Frames[i][id].GetProperty <RecordableState.Audio>(); if (audio.StartPlay == true) { AudioSource source = obj.GetComponent <AudioSource>(); source.PlayOneShot(source.clip); break; } } } state.SetToObject(obj); } } } }