Skip to content

Commit

Permalink
Animations: idle after attack or hit
Browse files Browse the repository at this point in the history
- Idle after attack or hit recived animation
- Minor fixes
  • Loading branch information
WyrnCael committed Jun 14, 2018
1 parent 607e93c commit 0308485
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 72 deletions.
10 changes: 0 additions & 10 deletions Assets/TRPGMaker/Database/Attributes.meta

This file was deleted.

12 changes: 6 additions & 6 deletions Assets/TRPGMaker/Database/Database.asset
Original file line number Diff line number Diff line change
Expand Up @@ -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}
2 changes: 1 addition & 1 deletion Assets/TRPGMaker/Scripts/Connectors/ITRPGMapConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
42 changes: 29 additions & 13 deletions Assets/TRPGMaker/Scripts/Connectors/IsoUnityConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>()
GameEvent animationEvent = null;

if (then == "")
{
{"decorationanimator", entity.decorationAnimator},
{"animation", animationName},
{"synchronous", true }
});
animationEvent = new GameEvent("animate entity", new Dictionary<string, object>()
{
{"decorationanimator", entity.decorationAnimator},
{"animation", animationName},
{"synchronous", true }
});
} else
{
animationEvent = new GameEvent("animate entity", new Dictionary<string, object>()
{
{"decorationanimator", entity.decorationAnimator},
{"animation", animationName},
{"then", then },
{"synchronous", true }
});
}

Game.main.enqueueEvent(animationEvent);
yield return new WaitForEventFinished(animationEvent);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -461,8 +475,8 @@ public List<Cell> GetPathFromCharToChar(CharacterScript character, CharacterScri
IsoUnity.Cell targetCell = target.transform.parent.transform.GetComponent(typeof(IsoUnity.Cell)) as IsoUnity.Cell;
List<CellWithDistance> openList = new List<CellWithDistance>();
List<CellWithDistance> closeList = new List<CellWithDistance>();
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)
Expand All @@ -485,7 +499,8 @@ public List<Cell> 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<CharacterScript>() == 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)
Expand Down Expand Up @@ -534,7 +549,8 @@ private List<CharacterScript> 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<CharacterScript>() == null) || (eventType != EventTypes.MOVE && eventType != EventTypes.IA_MOVE)))
{
if (!openList.Any(x => x.cell == neighbour))
{
Expand Down
31 changes: 17 additions & 14 deletions Assets/TRPGMaker/Scripts/Game/CharacterScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using System.Linq;
using UnityEditor;

public class CharacterScript : MonoBehaviour {
public class CharacterScript : MonoBehaviour
{

private Vector2 pos;
[SerializeField]
Expand All @@ -13,30 +14,30 @@ 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);
var renderer = gameObject.GetComponent<Renderer>();
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()
{

}


Expand Down Expand Up @@ -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();

Expand All @@ -88,15 +90,16 @@ public override void OnInspectorGUI()
EditorGUI.BeginChangeCheck();

EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Team");
EditorGUILayout.LabelField("Team");
if (characterScript.character != null)
{
//index = characters.
index = EditorGUILayout.Popup(index, Database.Instance.teams.Where(x => x.characters.Exists(y => y.name == characterScript.character.name)).Select(item => item.name).ToArray());
}
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();
Expand Down
92 changes: 64 additions & 28 deletions Assets/TRPGMaker/Scripts/Game/GamePlayManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void Turn()
*/
AttributeTurn();
break;
default:
default:
break;
}
}
Expand Down Expand Up @@ -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();
}));
}

}));

}));
}

Expand All @@ -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)
Expand All @@ -263,6 +273,7 @@ public void SkillEvent(Skills skill)
}
}
}
objectCanvas.SetActive(false);
Turn();
}), skill);
}
Expand All @@ -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();
}));
}

}));


}));
}

Expand All @@ -284,10 +317,10 @@ public void moveEventIA()
int nearestDistance = int.MaxValue;
List<Cell> 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<Cell> cellsToCharacter = connector.GetPathFromCharToChar(character, charact);
if(cellsToCharacter != null && cellsToCharacter.Count < nearestDistance)
if (cellsToCharacter != null && cellsToCharacter.Count < nearestDistance)
{
nearestPath = cellsToCharacter;
nearestDistance = cellsToCharacter.Count;
Expand All @@ -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
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<CharacterScript>()) {
if(character.character == null)
foreach (CharacterScript character in FindObjectsOfType<CharacterScript>())
{
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 + "'");
Expand Down Expand Up @@ -568,7 +604,7 @@ private void skillButtonListener(RectTransform rtCanvas)

foreach (List<Skills> skillList in skills)
{
for(int i = 0; i < skillList.Count; i++)
for (int i = 0; i < skillList.Count; i++)
{
Skills skill = skillList[i];

Expand Down

0 comments on commit 0308485

Please sign in to comment.