// Parent name (who's anchoring the entity) public static string ParentEntityName(GameEntityModel model){ GameEntityModel parentEntityModel = StateManager.state.GetModel(model.parentEntity) as GameEntityModel; if (parentEntityModel == null) return null; AnimationModel animModel = GameEntityController.GetAnimationModel(parentEntityModel); if (animModel == null) return null; return animModel.characterName; }
public GameEntityModel( State state, PhysicWorldModel worldModel, PhysicPointModel physicsModel, AnimationModel animationModel, Model inputModel, string controllerFactoryId, string viewFactoryId, int updatingOrder ) : base(controllerFactoryId, viewFactoryId, updatingOrder) { animationModel.ownerId = this.Index; physicsModel.ownerId = this.Index; physicsModelId = state.AddModel(physicsModel); worldModel.pointModels.Add(physicsModelId); animationModelId = state.AddModel(animationModel); if (inputModel != null) { inputModelId = state.AddModel(inputModel); } else { inputModelId = new ModelReference(ModelReference.InvalidModelIndex); } anchoredEntities = new List <ModelReference>(); ownedEntities = new List <ModelReference>(); customVariables = new Dictionary <string, int>(); mIsFacingRight = true; }
public static GameObject GetCharacterShadow(GameEntityModel model, GameObject root) { Storage.Character storageCharacter; AnimationModel animModel = StateManager.state.GetModel(model.animationModelId) as AnimationModel; if (animModel == null) { return(null); } if (!loadedCharacters.TryGetValue(animModel.characterName, out storageCharacter)) { return(null); } if (string.IsNullOrEmpty(storageCharacter.shadowName)) { return(null); } Transform shadow = root.transform.FindChild(storageCharacter.shadowName); if (shadow == null || shadow == root.transform) { return(null); } return(shadow.gameObject); }
// Frame private static EventCondition <GameEntityModel> .EvaluationDelegate BuildFrame(Storage.GenericParameter parameter, out int keyFrame, Storage.CharacterAnimation animation) { keyFrame = InvalidKeyframe; // Read subject, operator, numerator subject, numerator variable, frame number int subjectId = parameter.SafeInt(0); ConditionUtils <GameEntityModel> .ComparisonOperation comparisonOperator = (ConditionUtils <GameEntityModel> .ComparisonOperation)parameter.SafeInt(1); int numeratorSubjectId = parameter.SafeInt(2); string numeratorSubjectVarName = parameter.SafeString(0); int staticComparisonFrame = parameter.SafeInt(3); // If it's equal to a static frame, no delegate required, return frame directly if (subjectId == (int)CharacterSubjectsBuilder.PredefinedSubjects.self && comparisonOperator == ConditionUtils <GameEntityModel> .ComparisonOperation.equal && numeratorSubjectId - 2 == (int)CharacterSubjectsBuilder.PredefinedSubjects.none ) { keyFrame = staticComparisonFrame; if (keyFrame < 0) { keyFrame = animation.numFrames - 1; } return(null); } // Else return delegate return(delegate(GameEntityModel mainModel, List <GameEntityModel>[] subjectModels){ AnimationModel animModel = StateManager.state.GetModel(mainModel.animationModelId) as AnimationModel; if (animModel == null) { return false; } return CompareWithNumerator(mainModel, numeratorSubjectId, numeratorSubjectVarName, (int)animModel.currentFrame, staticComparisonFrame, comparisonOperator, subjectModels); }); }
// Anchored animation name public static string AnchoredEntityAnimation(GameEntityModel model, int anchorId){ if (model.anchoredEntities == null || model.anchoredEntities.Count <= anchorId || model.anchoredEntities[anchorId] == null) return null; GameEntityModel anchoredEntityModel = StateManager.state.GetModel(model.anchoredEntities[anchorId]) as GameEntityModel; if (anchoredEntityModel == null) return null; AnimationModel animModel = GameEntityController.GetAnimationModel(anchoredEntityModel); if (animModel == null) return null; return animModel.animationName; }
// Forces the animation of an anchored entity, so that it can't be messed with it's current animation events public static void SetAnchoredEntityAnimation(GameEntityModel model, int anchorId, string animationName){ if (model.anchoredEntities == null || model.anchoredEntities.Count <= anchorId) return; GameEntityModel anchoredEntityModel = StateManager.state.GetModel(model.anchoredEntities[anchorId]) as GameEntityModel; if (anchoredEntityModel != null){ AnimationModel anchoredAnimationModel = GameEntityController.GetAnimationModel(anchoredEntityModel); AnimationController anchoredAnimController = anchoredAnimationModel.Controller() as AnimationController; if (anchoredAnimController != null){ // Force animation, so that it ignores any desired transition from a previous animation update anchoredAnimController.ForceAnimation(anchoredAnimationModel, animationName); } } }
// Get a character portrait by entity id public static Sprite GetCharacterPortrait(uint modelId) { GameEntityModel ownerModel = StateManager.state.GetModel(modelId) as GameEntityModel; if (ownerModel == null) { return(null); } AnimationModel animModel = StateManager.state.GetModel(ownerModel.animationModelId) as AnimationModel; if (animModel == null) { return(null); } string prefabName = null; if (animModel.viewModelName != null) { prefabName = animModel.viewModelName; } else { prefabName = animModel.characterName; } Storage.Character storageCharacter; if (!loadedCharacters.TryGetValue(animModel.characterName, out storageCharacter)) { return(null); } int index = Array.FindIndex <string>(storageCharacter.viewModels, x => x.Equals(prefabName)); if (index < 0) { return(null); } string spriteName = storageCharacter.portraits[index]; if (!loadedPortraitSprites.ContainsKey(spriteName)) { return(null); } Sprite portrait; loadedPortraitSprites.TryGetValue(spriteName, out portrait); return(portrait); }
// 'walk' private static EventAction <GameEntityModel> .ExecutionDelegate BuildSetAnimation(Storage.GenericParameter parameter) { string animationName = parameter.SafeString(0); FixedFloat transitionTime = parameter.SafeFloat(0); return(delegate(GameEntityModel model, List <GameEntityModel>[] subjectModels){ AnimationModel animModel = StateManager.state.GetModel(model.animationModelId) as AnimationModel; if (animModel == null) { return; } animModel.SetNextAnimation(animationName, 0); AnimationView view = animModel.View() as AnimationView; if (view != null) { view.transitionTime = (float)transitionTime; } }); }
// Check collision against other entity public bool CollisionCollisionCheck(GameEntityModel model, GameEntityModel otherModel) { AnimationModel animModel = GetAnimationModel(model); AnimationModel otherAnimModel = GetAnimationModel(otherModel); AnimationController animController = animModel.Controller() as AnimationController; FixedVector3 position = GetRealPosition(model); FixedVector3 otherPosition = GetRealPosition(otherModel); if (animController.CollisionCollisionCheck(animModel, position, model.IsFacingRight(), otherAnimModel, otherPosition, otherModel.IsFacingRight())) { // Both entities get knowing they hit each other GameEntityController otherController = otherModel.Controller() as GameEntityController; otherController.lastCollisionEntityId = model.Index; lastCollisionEntityId = otherModel.Index; // Debug.Log("Collision detected"); return(true); } return(false); }
// Check Hit against other entity public void HitCollisionCheck(GameEntityModel model, GameEntityModel otherModel) { AnimationModel animModel = GetAnimationModel(model); AnimationModel otherAnimModel = GetAnimationModel(otherModel); AnimationController animController = animModel.Controller() as AnimationController; FixedVector3 position = GetRealPosition(model); FixedVector3 otherPosition = GetRealPosition(otherModel); HitInformation hitInformation = animController.HitCollisionCheck( animModel, position, model.IsFacingRight(), otherAnimModel, otherPosition, otherModel.IsFacingRight() ); if (hitInformation != null) { // Both entities get knowing one hit the other GameEntityController otherController = otherModel.Controller() as GameEntityController; otherController.lastHurts.Add(hitInformation.HitWithEntity(model.Index)); lastHits.Add(hitInformation.HitWithEntity(otherModel.Index)); // Debug.Log(model.Index + " hit " + otherModel.Index); } }
private static EventCondition <GameEntityModel> .EvaluationDelegate BuildAnimation(Storage.GenericParameter parameter, out int keyFrame, Storage.CharacterAnimation animation) { keyFrame = InvalidKeyframe; // Read anchor options, anchor IDs and if it's a single subject AnyOrAllOptions options = (AnyOrAllOptions)parameter.SafeInt(1); string[] names = parameter.SafeStringsList(0); return(delegate(GameEntityModel mainModel, List <GameEntityModel>[] subjectModels){ AnimationModel animModel = StateManager.state.GetModel(mainModel.animationModelId) as AnimationModel; bool anyOf = options == AnyOrAllOptions.anyOf; // "smart" loop here foreach (string name in names) { if (animModel.animationName == name) { return anyOf; } } return !anyOf; }); }
public GameObject GetGameObject(uint modelId, string prefabName = null, Transform parent = null) { // "guess" character name if necessary GameEntityModel ownerModel = StateManager.state.GetModel(modelId) as GameEntityModel; AnimationModel animModel = null; if (ownerModel != null) { animModel = StateManager.state.GetModel(ownerModel.animationModelId) as AnimationModel; } if (prefabName == null) { if (animModel == null) { return(null); } if (animModel.viewModelName != null) { prefabName = animModel.viewModelName; } else { prefabName = animModel.characterName; } } if (prefabName == null || modelId == ModelReference.InvalidModelIndex) { // Can't create it at the momment return(null); } // Try get from the pool GameObjectData objData; string originalPrefabName; if (gameObjects.TryGetValue(modelId, out objData) && prefabNamesByOwner.TryGetValue(modelId, out originalPrefabName) && originalPrefabName == prefabName ) { return(objData.obj); } // Doesn't exist yet string characterName = null; if (animModel != null) { characterName = animModel.characterName; } // Instantiate it far, far away UnityEngine.Object prefab; if (!prefabs.TryGetValue(prefabName, out prefab)) { prefab = CharacterLoader.LoadViewModel(characterName, prefabName); if (prefab == null) { Debug.LogError("Failed to load prefab: " + prefabName); } prefabs.Add(prefabName, prefab); } GameObject obj = GameObject.Instantiate(prefab) as GameObject; obj.transform.position = new Vector3(float.MinValue, float.MaxValue, float.MinValue); if (animModel != null) { gameObjects[modelId] = new GameObjectData(obj, CharacterLoader.GetCharacterAnchorNames(animModel.characterName)); } else { gameObjects[modelId] = new GameObjectData(obj, null); } prefabNamesByOwner[modelId] = prefabName; if (parent != null) { obj.transform.SetParent(parent); } // If there's a physics point model, translate the newly created obj to there if (ownerModel != null) { PhysicPointModel pointModel = StateManager.state.GetModel(ownerModel.physicsModelId) as PhysicPointModel; if (pointModel != null) { obj.transform.position = pointModel.position.AsVector3(); } } return(obj); }
// Load a prefab from the same bundle of the entity model, and set it's lifetime public GameObject FireAndForget(GameEntityModel model, string objName, int lifetime = 0) { AnimationModel animModel = null; if (model != null) { animModel = StateManager.state.GetModel(model.animationModelId) as AnimationModel; } string prefabName; if (animModel == null) { return(null); } if (animModel.viewModelName != null) { prefabName = animModel.viewModelName; } else { prefabName = animModel.characterName; } string[] pathItems = prefabName.Split(CharacterLoader.prefabDelimiter.ToCharArray()); pathItems[1] = objName; prefabName = pathItems[0] + CharacterLoader.prefabDelimiter + objName; // Instantiate it UnityEngine.Object prefab; if (!prefabs.TryGetValue(prefabName, out prefab)) { if (pathItems != null && pathItems.Length > 1) { string url = "file://" + CharacterLoader.charactersModelsPath + pathItems[0]; WWW www = WWW.LoadFromCacheOrDownload(url, 1); if (www.assetBundle == null) { Debug.LogError("Failed to load bundle at " + url); } // Load model prefab from bundle prefab = www.assetBundle.LoadAsset <GameObject>(pathItems[1]); www.assetBundle.Unload(false); prefabs[prefabName] = prefab; } } if (prefab != null) { GameObject obj = GameObject.Instantiate(prefab) as GameObject; obj.transform.position = new Vector3(float.MinValue, float.MaxValue, float.MinValue); if (lifetime > 0) { obj.AddComponent <FireAndForgetBehaviour>(); obj.GetComponent <FireAndForgetBehaviour>().lifetime = lifetime; } return(obj); } else { Debug.LogError("Failed to load prefab: " + prefabName); return(null); } }