From 0308485d39e20660c3ebac904ce3c4b9755ffd64 Mon Sep 17 00:00:00 2001 From: WyrnCael Date: Thu, 14 Jun 2018 16:55:10 +0200 Subject: [PATCH] Animations: idle after attack or hit - Idle after attack or hit recived animation - Minor fixes --- Assets/TRPGMaker/Database/Attributes.meta | 10 -- Assets/TRPGMaker/Database/Database.asset | 12 +-- .../Scripts/Connectors/ITRPGMapConnector.cs | 2 +- .../Scripts/Connectors/IsoUnityConnector.cs | 42 ++++++--- .../TRPGMaker/Scripts/Game/CharacterScript.cs | 31 ++++--- .../TRPGMaker/Scripts/Game/GamePlayManager.cs | 92 +++++++++++++------ 6 files changed, 117 insertions(+), 72 deletions(-) delete mode 100644 Assets/TRPGMaker/Database/Attributes.meta diff --git a/Assets/TRPGMaker/Database/Attributes.meta b/Assets/TRPGMaker/Database/Attributes.meta deleted file mode 100644 index d2f8ccd..0000000 --- a/Assets/TRPGMaker/Database/Attributes.meta +++ /dev/null @@ -1,10 +0,0 @@ -fileFormatVersion: 2 -guid: 4e96bebc00a3d1e44b08938b03a03994 -folderAsset: yes -timeCreated: 1526581018 -licenseType: Free -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/TRPGMaker/Database/Database.asset b/Assets/TRPGMaker/Database/Database.asset index db303e6..13d3a64 100644 --- a/Assets/TRPGMaker/Database/Database.asset +++ b/Assets/TRPGMaker/Database/Database.asset @@ -22,9 +22,9 @@ MonoBehaviour: _TRPGOptions: _gameType: 0 _turnType: 0 - _healthAttribute: {fileID: 11400000, guid: 570466ba20bcf074aa9cb2a7bc75d9aa, type: 2} - _damageAttribute: {fileID: 11400000, guid: 570466ba20bcf074aa9cb2a7bc75d9aa, type: 2} - _moveRange: {fileID: 11400000, guid: 2bacf50ce194d1b46b929529f5723ad4, type: 2} - _moveHeight: {fileID: 11400000, guid: 2bacf50ce194d1b46b929529f5723ad4, type: 2} - _attackRange: {fileID: 11400000, guid: 2bacf50ce194d1b46b929529f5723ad4, type: 2} - _attackHeight: {fileID: 11400000, guid: 2bacf50ce194d1b46b929529f5723ad4, type: 2} + _healthAttribute: {fileID: 0} + _damageAttribute: {fileID: 0} + _moveRange: {fileID: 0} + _moveHeight: {fileID: 0} + _attackRange: {fileID: 0} + _attackHeight: {fileID: 0} diff --git a/Assets/TRPGMaker/Scripts/Connectors/ITRPGMapConnector.cs b/Assets/TRPGMaker/Scripts/Connectors/ITRPGMapConnector.cs index cee433e..a92e132 100644 --- a/Assets/TRPGMaker/Scripts/Connectors/ITRPGMapConnector.cs +++ b/Assets/TRPGMaker/Scripts/Connectors/ITRPGMapConnector.cs @@ -18,7 +18,7 @@ public interface ITRPGMapConnector { void SetCharacterPosition(CharacterScript character, Cell cell, SetCharacterPositionCallBack callback); void ShowArea(CharacterScript character, EventTypes eventType, ShowAreaCallBack callback, Skills skill = null); void Skills(CharacterScript character, EventTypes eventType, SkillsCallBack callback, Skills skill = null); - void TriggerAnimation(CharacterScript character, string animationName, MoveCameraToCallback callback); + void TriggerAnimation(CharacterScript character, string animationName, string then, MoveCameraToCallback callback); CharacterScript GetCharacterAtCell(Cell cell); Cell GetCellAtCharacter(CharacterScript character); diff --git a/Assets/TRPGMaker/Scripts/Connectors/IsoUnityConnector.cs b/Assets/TRPGMaker/Scripts/Connectors/IsoUnityConnector.cs index eeb236c..39ae22e 100644 --- a/Assets/TRPGMaker/Scripts/Connectors/IsoUnityConnector.cs +++ b/Assets/TRPGMaker/Scripts/Connectors/IsoUnityConnector.cs @@ -241,22 +241,36 @@ private IEnumerator MoveCameraToCharacterAsync(CharacterScript character, MoveCa } // Trigger animation - public void TriggerAnimation(CharacterScript character, string animationName, MoveCameraToCallback callback) + public void TriggerAnimation(CharacterScript character, string animationName, string then, MoveCameraToCallback callback) { - StartCoroutine(triggerAnimationAsync(character, animationName, callback)); + StartCoroutine(triggerAnimationAsync(character, animationName, then, callback)); } // Async method for trigger animation - private IEnumerator triggerAnimationAsync(CharacterScript character, string animationName, MoveCameraToCallback callback) + private IEnumerator triggerAnimationAsync(CharacterScript character, string animationName, string then, MoveCameraToCallback callback) { Entity entity = character.transform.GetComponent(typeof(Entity)) as Entity; - var animationEvent = new GameEvent("animate entity", new Dictionary() + GameEvent animationEvent = null; + + if (then == "") { - {"decorationanimator", entity.decorationAnimator}, - {"animation", animationName}, - {"synchronous", true } - }); + animationEvent = new GameEvent("animate entity", new Dictionary() + { + {"decorationanimator", entity.decorationAnimator}, + {"animation", animationName}, + {"synchronous", true } + }); + } else + { + animationEvent = new GameEvent("animate entity", new Dictionary() + { + {"decorationanimator", entity.decorationAnimator}, + {"animation", animationName}, + {"then", then }, + {"synchronous", true } + }); + } Game.main.enqueueEvent(animationEvent); yield return new WaitForEventFinished(animationEvent); @@ -329,7 +343,7 @@ private IEnumerator IAMoveAsync(CharacterScript character, Cell destiny, MoveCha try { - CalculateDistanceArea(entity, characterCurrentCell, EventTypes.IA_MOVE, character.character.attributesWithFormulas.Find(x => x.attribute.id == attackRange.id).value, character.character.attributesWithFormulas.Find(x => x.attribute.id == attackHeight.id).value); + CalculateDistanceArea(entity, characterCurrentCell, EventTypes.IA_MOVE, character.character.attributesWithFormulas.Find(x => x.attribute.id == moveRange.id).value, character.character.attributesWithFormulas.Find(x => x.attribute.id == moveHeight.id).value); } catch (NullReferenceException e) { @@ -461,8 +475,8 @@ public List GetPathFromCharToChar(CharacterScript character, CharacterScri IsoUnity.Cell targetCell = target.transform.parent.transform.GetComponent(typeof(IsoUnity.Cell)) as IsoUnity.Cell; List openList = new List(); List closeList = new List(); - float attackRan = character.character.attributesWithFormulas.Find(x => x.attribute.id == attackRange.id).value; - float heighMax = character.character.attributesWithFormulas.Find(x => x.attribute.id == attackHeight.id).value; + float attackRan = character.character.attributesWithFormulas.Find(x => x.attribute.id == moveRange.id).value; + float heighMax = character.character.attributesWithFormulas.Find(x => x.attribute.id == moveHeight.id).value; openList.Add(new CellWithDistance(currentCell, 0, null)); while (openList.Count > 0) @@ -485,7 +499,8 @@ public List GetPathFromCharToChar(CharacterScript character, CharacterScri float distanceManhattan = Mathf.Abs(current.cell.Map.getCoords(current.cell.gameObject).x - targetCell.Map.getCoords(targetCell.gameObject).x) + Mathf.Abs(current.cell.Map.getCoords(current.cell.gameObject).y - targetCell.Map.getCoords(targetCell.gameObject).y); foreach (IsoUnity.Cell neighbour in current.cell.Map.getNeightbours(current.cell)) { - if (neighbour != null && !closeList.Any(x => x.cell == neighbour) && neighbour.Walkable) + if (neighbour != null && !closeList.Any(x => x.cell == neighbour) && neighbour.Walkable && + ((neighbour != currentCell && neighbour != targetCell && neighbour.transform.GetComponentInChildren() == null) || (neighbour == currentCell || neighbour == targetCell))) { float distanceManhattanFromCurrentToNeigh = Mathf.Abs(current.cell.Map.getCoords(current.cell.gameObject).x - neighbour.Map.getCoords(neighbour.gameObject).x) + Mathf.Abs(current.cell.Map.getCoords(current.cell.gameObject).y - neighbour.Map.getCoords(neighbour.gameObject).y); if (distanceManhattanFromCurrentToNeigh <= 1 && Mathf.Abs(neighbour.Height - current.cell.Height) <= heighMax) @@ -534,7 +549,8 @@ private List CalculateDistanceArea(Entity entity, IsoUnity.Cell float distanceManhattanFromCurrentToNeigh = Mathf.Abs(current.cell.Map.getCoords(current.cell.gameObject).x - neighbour.Map.getCoords(neighbour.gameObject).x) + Mathf.Abs(current.cell.Map.getCoords(current.cell.gameObject).y - neighbour.Map.getCoords(neighbour.gameObject).y); float distanceManhattanFromCharacterToNeigh = current.distanceFromCharacter + distanceManhattanFromCurrentToNeigh; if (distanceManhattanFromCurrentToNeigh <= 1 && distanceManhattanFromCharacterToNeigh <= distanceMax && - Mathf.Abs(neighbour.Height - current.cell.Height) <= heighMax) + Mathf.Abs(neighbour.Height - current.cell.Height) <= heighMax && (((eventType == EventTypes.IA_MOVE || eventType == EventTypes.MOVE) && + neighbour.transform.GetComponentInChildren() == null) || (eventType != EventTypes.MOVE && eventType != EventTypes.IA_MOVE))) { if (!openList.Any(x => x.cell == neighbour)) { diff --git a/Assets/TRPGMaker/Scripts/Game/CharacterScript.cs b/Assets/TRPGMaker/Scripts/Game/CharacterScript.cs index d7706bf..361d49f 100644 --- a/Assets/TRPGMaker/Scripts/Game/CharacterScript.cs +++ b/Assets/TRPGMaker/Scripts/Game/CharacterScript.cs @@ -4,7 +4,8 @@ using System.Linq; using UnityEditor; -public class CharacterScript : MonoBehaviour { +public class CharacterScript : MonoBehaviour +{ private Vector2 pos; [SerializeField] @@ -13,11 +14,11 @@ public class CharacterScript : MonoBehaviour { private void Start() { - + } private void OnGUI() - { + { if (character != null) { AttributeValue attribute = character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id); @@ -25,18 +26,18 @@ private void OnGUI() float height = renderer.bounds.size.y * 15; pos = Camera.main.WorldToScreenPoint(transform.position); pos.y = Screen.height - pos.y - height; - Rect boxRect = new Rect(pos.x - 50, pos.y - 40, 100, 20); + Rect boxRect = new Rect(pos.x - 50, pos.y - 40, 100, 20); if (!boxRect.Contains(Event.current.mousePosition)) { GUI.Box(boxRect, attribute.value + "/" + attribute.maxValue); - } - } + } + } } void OnMouseEnter() { - + } @@ -65,19 +66,20 @@ public class CharacterScriptEditor : Editor private int index = -1; private CharacterScript characterScript; - private void OnEnable() - { + private void OnEnable() + { characterScript = (CharacterScript)target; - if (characterScript.team != null) { + if (characterScript.team != null) + { index = Database.Instance.teams.Where(x => x.characters.Exists(y => y.name == characterScript.character.name)).ToList().IndexOf(characterScript.team); } else { Debug.Log("No team assigned in some characters!"); } - } + } - public override void OnInspectorGUI() + public override void OnInspectorGUI() { serializedObject.Update(); @@ -88,7 +90,7 @@ public override void OnInspectorGUI() EditorGUI.BeginChangeCheck(); EditorGUILayout.BeginHorizontal(); - EditorGUILayout.LabelField("Team"); + EditorGUILayout.LabelField("Team"); if (characterScript.character != null) { //index = characters. @@ -96,7 +98,8 @@ public override void OnInspectorGUI() } EditorGUILayout.EndHorizontal(); - if (EditorGUI.EndChangeCheck()) { + if (EditorGUI.EndChangeCheck()) + { characterScript.team = Database.Instance.teams.Where(x => x.characters.Exists(y => y.name == characterScript.character.name)).ToArray()[index]; } serializedObject.ApplyModifiedProperties(); diff --git a/Assets/TRPGMaker/Scripts/Game/GamePlayManager.cs b/Assets/TRPGMaker/Scripts/Game/GamePlayManager.cs index 9e2e6e3..11159c3 100644 --- a/Assets/TRPGMaker/Scripts/Game/GamePlayManager.cs +++ b/Assets/TRPGMaker/Scripts/Game/GamePlayManager.cs @@ -94,7 +94,7 @@ private void Turn() */ AttributeTurn(); break; - default: + default: break; } } @@ -215,27 +215,37 @@ public void AttackEvent() connector.cleanCells(); connector.ShowArea(character, EventTypes.ATTACK, ShowAreaCallBackParametrizedCallback(character, (character1, selectedCell, result1) => { + CharacterScript characterDestAttack = connector.GetCharacterAtCell(selectedCell); - characterDestAttack.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value -= character.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.damageAttribute.id).value; + characterDestAttack.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value -= character.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.damageAttribute.id).value; - if (characterDestAttack.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value <= 0) + connector.TriggerAnimation(character, "attack", "idle", MoveCameraToParametrizedCallback(character, (character6, resul6t) => { - characterDestAttack.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value = 0; - // Dead Animation - connector.TriggerAnimation(characterDestAttack, "die", MoveCameraToParametrizedCallback(characterDestAttack, (character5, resul5t) => + + if (characterDestAttack.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value <= 0) { - attack = true; - objectCanvas.SetActive(false); - Turn(); - })); - } - else - { - attack = true; - objectCanvas.SetActive(false); - Turn(); - } + characterDestAttack.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value = 0; + // Dead Animation + connector.TriggerAnimation(characterDestAttack, "die", "", MoveCameraToParametrizedCallback(characterDestAttack, (character5, resul5t) => + { + attack = true; + objectCanvas.SetActive(false); + Turn(); + })); + } + else + { + connector.TriggerAnimation(characterDestAttack, "gothit", "idle", MoveCameraToParametrizedCallback(characterDestAttack, (character5, resul5t) => + { + attack = true; + objectCanvas.SetActive(false); + Turn(); + })); + } + + })); + })); } @@ -244,7 +254,7 @@ public void SkillEvent(Skills skill) connector.cleanCells(); connector.Skills(character, EventTypes.SKILL, SkillsCallBackParametrizedCallback(character, (character, selectedCell, result, targets) => { - foreach(CharacterScript target in targets) + foreach (CharacterScript target in targets) { var f = FormulaScript.Create(""); foreach (Formula formula in skill.formulas) @@ -263,6 +273,7 @@ public void SkillEvent(Skills skill) } } } + objectCanvas.SetActive(false); Turn(); }), skill); } @@ -271,8 +282,30 @@ public void attackEventIA(CharacterScript target) { connector.IAAttack(character, target, ShowAreaCallBackParametrizedCallback(character, (character1, selectedCell, result1) => { - target.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value -= character.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.damageAttribute.id).value; - Turn(); + + connector.TriggerAnimation(character, "attack", "idle", MoveCameraToParametrizedCallback(character, (character6, resul6t) => + { + + target.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value -= character.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.damageAttribute.id).value; + + if (target.character.attributesWithFormulas.Find(x => x.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value <= 0) { + connector.TriggerAnimation(target, "die", "", MoveCameraToParametrizedCallback(target, (character7, result7) => + { + //DEAD + Turn(); + })); + + } else { + connector.TriggerAnimation(target, "gothit", "idle", MoveCameraToParametrizedCallback(target, (character7, result7) => + { + //hit + Turn(); + })); + } + + })); + + })); } @@ -284,10 +317,10 @@ public void moveEventIA() int nearestDistance = int.MaxValue; List nearestPath = null; - foreach(CharacterScript charact in characters.Where(x => x.team != character.team)) + foreach (CharacterScript charact in characters.Where(x => x.team != character.team)) { List cellsToCharacter = connector.GetPathFromCharToChar(character, charact); - if(cellsToCharacter != null && cellsToCharacter.Count < nearestDistance) + if (cellsToCharacter != null && cellsToCharacter.Count < nearestDistance) { nearestPath = cellsToCharacter; nearestDistance = cellsToCharacter.Count; @@ -303,7 +336,8 @@ public void moveEventIA() // Can't be 0, because is the enemy target position if (nearestPath.Count == 0) Turn(); - else { + else + { if (nearestPath.Count - 1 < moveRange) destinyCell = nearestPath.Count - 2; // -2 because -1 is the enemy target position else @@ -338,7 +372,7 @@ private void IATurnManager() targets = targets.Where(x => x.team != character.team).OrderByDescending(x => x.character.attributesWithFormulas.Find(y => y.attribute.id == Database.Instance.battleOptions.healthAttribute.id).value).ToList(); // If any character is attackable, attack - if(targets.Count > 0) + if (targets.Count > 0) { attackEventIA(targets.First()); } @@ -367,14 +401,16 @@ private bool checkAttributes() requiredAttributes.Add(Database.Instance.battleOptions.attackHeight); // Check if all characters scripts contains this attributes and a character - foreach (CharacterScript character in FindObjectsOfType()) { - if(character.character == null) + foreach (CharacterScript character in FindObjectsOfType()) + { + if (character.character == null) { Debug.Log("Missing 'character' in some or many 'character script' components"); return false; } character.character.calculateFormulas(); - foreach (Attribute attribute in requiredAttributes) { + foreach (Attribute attribute in requiredAttributes) + { if (!character.character.attributesWithFormulas.Any(x => x.attribute == attribute)) { Debug.Log("Character '" + character.character.name + "' doesn't have attribute '" + attribute.name + "'"); @@ -568,7 +604,7 @@ private void skillButtonListener(RectTransform rtCanvas) foreach (List skillList in skills) { - for(int i = 0; i < skillList.Count; i++) + for (int i = 0; i < skillList.Count; i++) { Skills skill = skillList[i];