Created
June 26, 2018 02:00
-
-
Save juliomarcos/d1bb442c03820450cb3679809391be2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CarboLog; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(DropsAnimator))] | |
public class Arbiter : MonoBehaviour { | |
const string DROP_TAG = "Drop"; | |
public GameObject playerPartyGameObject; | |
public GameObject enemyPartyGameObject; | |
public GameObject formationsContainer; | |
public Menu menuController; | |
public bool anyActionHappening; | |
private BattleGUI battleGUI; | |
private Player player; | |
private SyncroAttack playerSyncroAttack; | |
private Battle battle; | |
private EnemyAI enemyAI; | |
private IBattleSorter sorter; | |
private int thisTurnBattleSequencesToFinish; | |
private BattleAction lastBattleAction; | |
private DropsDropper dropsDropper; | |
private DropsPool dropsPool; | |
private DropsAnimator dropsAnimator; | |
public BattleAble LastAttacker { | |
get { | |
return lastBattleAction.Attacker; | |
} | |
} | |
public void Awake() | |
{ | |
dropsAnimator = GetComponent<DropsAnimator> (); | |
player = playerPartyGameObject.GetComponent<Player>(); | |
battleGUI = GameObject.Find("Battle GUI").GetComponent<BattleGUI>(); | |
} | |
// TODO: get this data from the other scene or some static field | |
public void Start() | |
{ | |
// StartBattle(EnemyEncounters.LavaLand_Molternn); | |
// StartBattle(EnemyEncounters.BigMushroomBoss); | |
StartBattle(EnemyEncounters.LavaLand_ThreeMolternns); | |
} | |
private void ArrangePartyMember(Formation formation, Transform characterTransform, int childIndex) { | |
Transform formationTransform = null; | |
switch (formation) { | |
case Formation.SSS: | |
formationTransform = formationsContainer.transform.FindChild("PartyFormation_SSS"); | |
break; | |
default: | |
formationTransform = formationsContainer.transform.FindChild("PartyFormation_One"); | |
break; | |
} | |
var localPosition = formationTransform.GetChild(childIndex).localPosition; | |
characterTransform.localPosition = localPosition; | |
} | |
public void StartBattle(EnemyEncounters enemyEnum) | |
{ | |
Debug.Log("Starting Battle with " + enemyEnum); | |
var encounterFactory = new EncounterFactory(player.Party); | |
battle = encounterFactory.Against(enemyEnum); | |
int enemyIndex = 0; | |
foreach (BattleAble enemyBattleAble in battle.enemyParty) { | |
enemyBattleAble.gameObjectRef.transform.parent = enemyPartyGameObject.transform; | |
// enemyBattleAble.gameObjectRef.transform.localPosition = Vector3.zero; | |
ArrangePartyMember(battle.enemyFormation, enemyBattleAble.gameObjectRef.transform, enemyIndex++); | |
var enemyBattleableBehaviour = enemyBattleAble.gameObjectRef.GetComponent<BattleableBehaviour>(); | |
enemyBattleableBehaviour.AnimationStarted += OnAnimationStarted; | |
enemyBattleableBehaviour.AnimationFinished += OnAnimationFinished; | |
enemyBattleableBehaviour.EnemyMayTakeDamage += OnEnemyMayTakeDamage; | |
enemyBattleableBehaviour.RecordInitialPosition(); | |
} | |
dropsDropper = new DropsDropper(); | |
dropsPool = new DropsPool(new List<Drop>(), dropsAnimator); | |
enemyAI = new OnlyAttackFirstComponentAI(battle); | |
enemyAI.BattleDecisionMade += OnBattleDecisionMade; | |
sorter = new EqualitarainBattleSorter(battle); | |
var playerBattleableBehaviour = playerPartyGameObject.transform.GetChild(0).GetComponent<BattleableBehaviour>(); | |
playerBattleableBehaviour.AnimationStarted += OnAnimationStarted; | |
playerBattleableBehaviour.AnimationFinished += OnAnimationFinished; | |
playerBattleableBehaviour.EnemyMayTakeDamage += OnEnemyMayTakeDamage; | |
playerBattleableBehaviour.RecordInitialPosition(); | |
playerSyncroAttack = playerPartyGameObject.transform.GetChild(0).GetComponent<SyncroAttack>(); | |
if (playerSyncroAttack != null) { | |
playerSyncroAttack.AttackDoneInSync += OnAttackDoneInSync; | |
} | |
menuController.enemyParty = battle.enemyParty; | |
menuController.BattleDecisionMade += OnBattleDecisionMade; | |
Log.d(String.Format("{0}\n{1}", player.mainCharacter, player.mainCharacter.PrintStatus())); | |
NextTurn(); | |
} | |
void NextTurn() | |
{ | |
Debug.Log("Turn is starting!"); | |
anyActionHappening = false; | |
thisTurnBattleSequencesToFinish = 2; | |
var turnOwner = sorter.GetNextCombatent(); | |
Debug.Log(string.Format("Turn Owner: {0}", turnOwner)); | |
if (battle.playerParty.Contains(turnOwner)) | |
{ | |
menuController.GiveTurnTo(turnOwner); | |
} else { | |
Debug.Log("Enemy AI is making a choice"); | |
enemyAI.ThinkAndAct(turnOwner); | |
} | |
} | |
public void OnAttackDoneInSync(object sender, EventArgs e) { | |
lastBattleAction.Multiplier = 1.5f; | |
lastBattleAction.IsCritical = true; | |
} | |
public void OnBattleDecisionMade(object sender, BattleArgs args) { | |
Log.d("OnBattleDecisionMade", args.BattleAction); | |
if (anyActionHappening) { | |
// Won't do it. There's something going on already. | |
Log.d("Won't do it. There's something going on already."); | |
return; | |
} | |
menuController.ReleaseOwner(); | |
anyActionHappening = true; | |
lastBattleAction = args.BattleAction; | |
if (args.BattleAction.Kind == BattleAction.ActionKind.Attack) { | |
var attackerBattleAbleBehaviour = lastBattleAction.Attacker.gameObjectRef.GetComponent<BattleableBehaviour> (); | |
attackerBattleAbleBehaviour.attacking = true; | |
foreach (BattleAble defensor in lastBattleAction.Defensors) { | |
attackerBattleAbleBehaviour.Attack(defensor, args.BattleAction.Hits); | |
defensor.gameObjectRef.GetComponent<BattleableBehaviour> ().attacking = false; | |
} | |
} | |
if (args.BattleAction.Attacker == player.mainCharacter && playerSyncroAttack != null) { | |
playerSyncroAttack.SignalAttackAnimationStarted(); | |
} | |
remainingHitsThisTurn = lastBattleAction.Hits; | |
} | |
private void DropEngineStep(BattleAction battleAction, bool missed) { | |
var newDrops = dropsDropper.MayDropDrops(battleAction, missed); | |
if (newDrops.Count > 0) { | |
dropsPool.Inject(newDrops, battleAction.Attacker.gameObjectRef.transform.position); | |
} | |
} | |
public void OnAnimationStarted(object sender, EventArgs e) { | |
//thisTurnAnimationCounter += 1; | |
} | |
void OnAnimationFinished(object sender, EventArgs args) { | |
thisTurnBattleSequencesToFinish -= 1; | |
// Debug.Log(String.Format("Animation Finished. Animations to finish: {0}", thisTurnBattleSequencesToFinish)); | |
if (thisTurnBattleSequencesToFinish == 0) { | |
BattleResult battleResult; | |
if (battle.IsOver(out battleResult)) { | |
if (battleResult == BattleResult.PlayerLoss) { | |
battleGUI.GameOver(); | |
} else { | |
battleGUI.ExpScreen(); | |
} | |
} else { | |
NextTurn(); | |
} | |
} | |
} | |
int remainingHitsThisTurn; | |
void OnEnemyMayTakeDamage(object sender, EventArgs args) { | |
Debug.Log("OnEnemyMayTakeDamage()"); | |
bool missed = false; | |
foreach (BattleAble defensor in lastBattleAction.Defensors) { | |
if ( lastBattleAction.Kind == BattleAction.ActionKind.Attack && BattleCalculator.DoesHit(lastBattleAction.Attacker, lastBattleAction.Defensors[0]) || | |
lastBattleAction.Kind == BattleAction.ActionKind.Skill) { | |
var damageDone = BattleCalculator.Damage(lastBattleAction.Attacker, lastBattleAction.Defensors[0], lastBattleAction.Multiplier); | |
var specialFxPrefab = lastBattleAction.SpecialFxPrefabReadyToKickIn; | |
defensor.TakeDamage(damageDone, specialFxPrefab, lastBattleAction.IsCritical); | |
} else { | |
missed = true; | |
defensor.PerformEvadeAnim(); | |
} | |
} | |
remainingHitsThisTurn -= 1; | |
if (remainingHitsThisTurn == 0) { | |
DropEngineStep (lastBattleAction, missed); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class BattleAble { | |
public GameObject gameObjectRef; | |
public string Name { get; set; } | |
public int CurrentHp { get; set; } | |
virtual public int HP { get; set; } | |
virtual public int ATK { get; set; } | |
virtual public int DEF { get; set; } | |
virtual public int SPD { get; set; } | |
virtual public float HIT { get; set; } | |
virtual public float EVD { get; set; } | |
virtual public int EXP { get; set; } | |
public Stats OriginalStats { get; set; } | |
public Stats CurrentStats { get; set; } | |
public List<Skill> Skills { get; set; } | |
public Stack<Effect> Effects { get; set; } | |
public bool Dead { get; set; } | |
public BattleAble () | |
{ | |
Dead = false; | |
} | |
public string BareHandSpecialFxPrefabName { get; set; } | |
public void TakeDamage(int damage, GameObject fxPrefab, bool isCritical) { | |
CurrentHp -= damage; | |
if (CurrentHp <= 0) { | |
Dead = true; | |
gameObjectRef.GetComponent<BattleableBehaviour>().StartDieSequence(damage, fxPrefab); | |
} else { | |
gameObjectRef.GetComponent<BattleableBehaviour>().StartTakeDamageSequence(damage, fxPrefab, isCritical); | |
} | |
} | |
public void PerformEvadeAnim () | |
{ | |
gameObjectRef.GetComponent<BattleableBehaviour>().StartMissSequence(); | |
} | |
public string PrintStatus() { | |
return string.Format("HP:{0}\tATK:{1}\tDEF:{2}",HP, ATK, DEF); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class PlayerCharacter : BattleAble { | |
public CharacterGear Gear { get; set; } | |
public int WeaponsATKSum() | |
{ | |
return Gear.weapon1.ATK + Gear.weapon2.ATK; | |
} | |
int EquipmentsDEFSum () | |
{ | |
return Gear.armor.DEF; | |
} | |
override public int HP | |
{ | |
get | |
{ | |
return (CurrentStats.VIT * CurrentStats.VIT) / 4 + | |
CurrentStats.VIT * 10 + | |
CurrentStats.LVL * 2; | |
} | |
} | |
override public int ATK | |
{ | |
get | |
{ | |
return WeaponsATKSum() * (CurrentStats.STR * CurrentStats.STR * CurrentStats.STR) / 8 + | |
(CurrentStats.STR * CurrentStats.STR) / 8; | |
} | |
} | |
override public int DEF | |
{ | |
get { return EquipmentsDEFSum() + CurrentStats.VIT / 4; } | |
} | |
override public float HIT | |
{ | |
get { return CurrentStats.DEX; } | |
} | |
public void InitializeCurrentStats() { | |
CurrentStats = new Stats { | |
STR = OriginalStats.STR, | |
VIT = OriginalStats.VIT, | |
LVL = OriginalStats.LVL, | |
}; | |
CurrentHp = HP; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment