Wednesday, June 29, 2016

Code Philosophy


As mentioned before, I started with this idea that I would write "engine code" for my Card game, where I could basically play a full game in the console without even touching Unity.   I left hooks in my code so that information could be passed back to the UI whenever an event happened.  For instance, here's a sample of code from my "attack" function.


string AttackResult = bf.processAttack(nextCard);
//Play the attack animation on the player ("Active" or "Defending" returned) or on the card ("Card")
//Play the attack animation + the ability animation on any other ("Stalwart" or "Evasion" or "Retaliation")
//Update the defending card and/or player health in UI

//If there is a card there check to see if it's health is <= 0
if (nextCard <= bf.defendingBattlefield.Count - 1)
{
 //Check to see if the card was killed
 if (bf.defendingBattlefield[nextCard].health <= 0)
 {
  // Remove the card from the battlefield
  Card deadCard = bf.defendingBattlefield[nextCard].card;
  bf.defendingBattlefield.RemoveAt(nextCard);

  // Add the static card to the defending player's graveyard
  bf.defendingGraveyard.Add(deadCard);
  // Play animation from battlefield to graveyard 
 }
}

//Lower the boost, if any
bf.processRelevantAffix_AttackEnd();

//Update the UI with the card's lowered attack

I left myself notes to remind me that when implementing a function I'd need to return something to the UI, so that an animation could be played.  I didn't think of this from the start, so some refactoring was needed.   This caused me to move some of the logic from the "engine" code into my "driver" (which will end up being the UI code).   Something to keep in mind when starting my next project as well, if I follow the same backend/frontend paradigm.   

Impact With Unity
There were some growing pains when migrating my code over to Unity.   Firstly and most importantly, Unity uses .NET 2.0 so a lot of the code for SQLite that I was using was unusable when I moved it over to Unity.   My plan was to make a DLL and just put it into Unity.  However, because of the differing function sets this did not work.  I worked and worked at the DLL and ended up just moving the code in file by file, fixing code as I went along.  

I still like the DLL idea - but I'll work on getting that going another time.

S/O to http://hilite.me/ for formatting my code for this post!

No comments:

Post a Comment