Created
July 27, 2016 04:21
-
-
Save JCThePants/ff7def6b46f4692bc6af70a8ea61db5f to your computer and use it in GitHub Desktop.
PlayerLives struct
This file contains 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
public struct PlayerLives { | |
public static readonly PlayerLives Infinite = new PlayerLives(-1); | |
private readonly int _lives; | |
public readonly bool IsInfinite; | |
public PlayerLives(int lives) { | |
_lives = lives; | |
IsInfinite = lives < 0; | |
} | |
public override int GetHashCode() { | |
return _lives; | |
} | |
public override bool Equals(object obj) { | |
if (obj is PlayerLives) { | |
PlayerLives other = ((PlayerLives)obj); | |
return other.IsInfinite == IsInfinite && other._lives == _lives; | |
} | |
return false; | |
} | |
public override string ToString() { | |
return _lives.ToString(); | |
} | |
// equals | |
public static bool operator ==(PlayerLives lives1, PlayerLives lives2) { | |
return lives1._lives == lives2._lives; | |
} | |
// not equals | |
public static bool operator !=(PlayerLives lives1, PlayerLives lives2) { | |
return lives1._lives != lives2._lives; | |
} | |
// equals | |
public static bool operator ==(PlayerLives lives1, int lives2) { | |
return lives1._lives == lives2; | |
} | |
// not equals | |
public static bool operator !=(PlayerLives lives1, int lives2) { | |
return lives1._lives != lives2; | |
} | |
// addition | |
public static PlayerLives operator ++(PlayerLives lives1) { | |
if (lives1.IsInfinite) | |
return Infinite; | |
return new PlayerLives(lives1._lives + 1); | |
} | |
// subtraction | |
public static PlayerLives operator --(PlayerLives lives1) { | |
if (lives1.IsInfinite) | |
return Infinite; | |
return new PlayerLives(Math.Max(0, lives1._lives - 1)); | |
} | |
// addition | |
public static PlayerLives operator +(PlayerLives lives1, PlayerLives lives2) { | |
if (lives1.IsInfinite || lives2.IsInfinite) | |
return Infinite; | |
return new PlayerLives(lives1._lives + lives2._lives); | |
} | |
// subtraction | |
public static PlayerLives operator -(PlayerLives lives1, PlayerLives lives2) { | |
if (lives1.IsInfinite || lives2.IsInfinite) | |
return Infinite; | |
return new PlayerLives(Math.Max(0, lives1._lives - lives2._lives)); | |
} | |
// addition | |
public static PlayerLives operator +(PlayerLives lives1, int lives2) { | |
if (lives1.IsInfinite) | |
return Infinite; | |
return new PlayerLives(lives1._lives + lives2); | |
} | |
// subtraction | |
public static PlayerLives operator -(PlayerLives lives1, int lives2) { | |
if (lives1.IsInfinite) | |
return Infinite; | |
return new PlayerLives(Math.Max(0, lives1._lives - lives2)); | |
} | |
// auto cast to integer | |
public static implicit operator int(PlayerLives lives) { | |
if (lives.IsInfinite) | |
return 1; | |
return lives._lives; | |
} | |
// auto cast integer to PlayerLives | |
public static implicit operator PlayerLives(int lives) { | |
if (lives < 0) | |
return Infinite; | |
return new PlayerLives(lives); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment