+ All Categories

Tdd in unity

Date post: 18-Jun-2015
Category:
Upload: eric-smith
View: 1,240 times
Download: 2 times
Share this document with a friend
Popular Tags:
49
?
Transcript
Page 1: Tdd in unity

?

Page 2: Tdd in unity

int idSurface::Split( const idPlane &plane, const float epsilon, idSurface **front, idSurface **back, int *frontOnPlaneEdges = NULL, int *backOnPlaneEdges = NULL) const;

http://kotaku.com/5975610/the-exceptional-beauty-of-doom-3s-source-code

Page 3: Tdd in unity

Software Craftsman Game Developer

Tests

Clarity

SOLID

Speed

No Crash

Memory

Page 4: Tdd in unity
Page 5: Tdd in unity

Test Driven UnityA Sane Approach

Page 6: Tdd in unity

Software Craftsman Game Developer

Tests

Clarity

SOLID

Speed

No Crash

Memory

Page 7: Tdd in unity
Page 8: Tdd in unity
Page 9: Tdd in unity
Page 10: Tdd in unity
Page 11: Tdd in unity

QuickTime™ and aH.264 decompressor

are needed to see this picture.

Page 12: Tdd in unity

Eskimo!

Page 13: Tdd in unity

QuickTime™ and aH.264 decompressor

are needed to see this picture.

Page 14: Tdd in unity

Meta-Fail

Page 15: Tdd in unity

Is It Fun?

Page 16: Tdd in unity

Cubicle Wars!

Page 17: Tdd in unity

UnityMakes your life easier when it’s not making it

harder

Page 18: Tdd in unity

Scripts

Page 19: Tdd in unity
Page 20: Tdd in unity

public class NewScript : MonoBehaviour {

// Use this for initializationvoid Start () {

}

// Update is called once per framevoid Update () {

}}

Page 21: Tdd in unity
Page 22: Tdd in unity
Page 23: Tdd in unity

void Awake() {stateMachine = new CubicleWarsStateMachine(

new HumanPlayer("Player1"),new HumanPlayer("Player2"));

Page 24: Tdd in unity

[Test]public void ItAllowsAddingAUnitToAPlayer(){

var unit = Substitute.For<Unit>();

stateMachine.AddUnitToPlayer("PlayerOne", unit);stateMachine.AddUnitToPlayer("PlayerTwo", unit);

playerOne.Received().AddUnit(unit);playerTwo.Received().AddUnit(unit);

}

Page 25: Tdd in unity

Updating the View

Page 26: Tdd in unity

Update the View

public void Attack(Unit unit){ if (CurrentState == State.Attacking){ unit.AttackWith(CurrentPlayer.Weapon());

Page 27: Tdd in unity

Shared Interfaces

Page 28: Tdd in unity

C# Eventspublic delegate void GameOverEvent(String winner);

...

public event GameOverEvent GameOver = delegate { };

...

private void AnnouncePlayerWins(){GameOver(CurrentPlayer.Name);}

Page 29: Tdd in unity

Wiring the Events

machine.GameOver += delegate(string winner) {winMessage.SendMessage("ShowWinner", String.Format("{0} wins!", winner));};

Page 30: Tdd in unity
Page 31: Tdd in unity

Health

Page 32: Tdd in unity

“MVC”

Page 33: Tdd in unity

Demo

Page 34: Tdd in unity
Page 35: Tdd in unity

What is in the Library

•Game Models (Units, Characters)

•State Machines

•Effects

•Anything that doesn’t depend on UnityEngine

Page 36: Tdd in unity

State Machine

Game State Machine

StateState EventEvent ActionAction DestinatioDestinationn

WaitingForSelection ClickWeapon TryToSelectUnit Selecting

Selecting AssignWeapon SwapWaitingUnit Attacking

Attacking ClickWeapon ResolveAttack ResolvingAttack

ResolvingAttack PlayerDead PlayerWins

ResolvingAttack NextTurn SwitchPlayers SwitchingPlayers

SwitchingPlayers SwitchedPlayers WaitingForSelection

Page 37: Tdd in unity

Effectspublic class SineWave

{

protected float Amplitude { get; set; }

protected float Frequency { get; set; }

protected float Offset { get; set; }

public SineWave(float amplitude, float frequency, float offset)

{

Amplitude = amplitude;

Frequency = frequency;

Offset = offset;

}

public float at(float time)

{

return (Amplitude * (float)Math.Sin(Frequency * time))

+ Offset;

}

}

Page 38: Tdd in unity

Effects[Test]public void ItReturnsANormalSinWaveOnTime(){

var sineWave = new SineWave(1, 1, 0);

Assert.AreEqual(0, sineWave.at(0));Assert.AreEqual(1, sineWave.at((float) Math.PI /

2.0f));}

Page 39: Tdd in unity

Game Unitspublic StandardUnit(ConflictResolver resolver, UnityObject unity){

Resolver = resolver;Health = unity.InitialHealth;UnitName = unity.Name;

}

public void AttackWith (Unit enemy){

Health -= enemy.AttackStrengthAgainst (this);Attacked();

}

Page 40: Tdd in unity

Live Code!

Page 41: Tdd in unity

Software Craftsman Game Developer

Tests

Clarity

SOLID

Speed

No Crash

Memory

Page 42: Tdd in unity

The Ultimate Test

Page 43: Tdd in unity
Page 44: Tdd in unity

ControllersComponents.Add (new UnitController(this, GameData.GlobalData.PlayerOneName, (UnitData) GameData.DataFor(GameData.GlobalData.PlayerOneName).Sales));

Page 45: Tdd in unity

Controllerspublic class UnitController : DrawableGameComponent, UnityObject{

protected String player;protected Model model;protected UnitData initialData;protected Unit unit;

public int InitialHealth {get {

return initialData.Health;}

}

public string Name {get {

return initialData.Name;}

}

public UnitController (Game game, String player, UnitData initialData) : base(game){

var warGame = game as Startup;this.player = player;this.initialData = initialData;

unit = new StandardUnit(GameData.Resolver, this);

warGame.MouseClick += (s, args) => CheckMouseClick(s, args as ClickEventArgs);}

Page 46: Tdd in unity

Viewpublic UnitView (Game game, String player, Model model, UnitData initialData, Unit unit) : base(game){

this.model = model;this.unit = unit;this.initialData = initialData;this.player = player;wave = new SineWave(AMPLITUDE, FREQUENCY, OFFSET);waiting = false;

unit.Waiting += () => waiting = true;unit.DoneWaiting += () => waiting = false;

}

Page 47: Tdd in unity

The Ultimate Test #2

Page 48: Tdd in unity

Remember

•There’s no AssertPretty, and no AssertFun

•TDD needs to be fast to be effective

•Separate your concerns

•Slides with bullets suck

Page 49: Tdd in unity

Thanks!

@paytonruleswww.paytonrules.com

www.8thlight.comwww.github.com/paytonrules


Recommended