Created
April 28, 2016 01:22
-
-
Save bradhilton/cf4dbd466bf7a756c19a1bcce12e3294 to your computer and use it in GitHub Desktop.
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
import Foundation | |
protocol Mixin { | |
var storage: [String : Any] { get set } | |
} | |
protocol CanMoveMixin : Mixin { | |
var position: CGPoint { get set } | |
var speed: CGVector { get set } | |
mutating func move(forTimeSlice timeSlice: CGFloat) | |
} | |
extension CanMoveMixin { | |
var position: CGPoint { | |
get { | |
return storage["position"] as? CGPoint ?? CGPoint.zero | |
} | |
set { | |
storage["position"] = newValue | |
} | |
} | |
var speed: CGVector { | |
get { | |
return storage["speed"] as? CGVector ?? CGVector.zero | |
} | |
set { | |
storage["speed"] = newValue | |
} | |
} | |
mutating func move(forTimeSlice timeSlice: CGFloat) { | |
position = CGPoint(x: position.x + timeSlice * speed.dx, | |
y: position.y + timeSlice * speed.dy) | |
} | |
} | |
protocol HealthMixin : Mixin { | |
var health: CGFloat { get set } | |
mutating func absorb(damage: CGFloat) | |
} | |
extension HealthMixin { | |
var health: CGFloat { | |
get { | |
return storage["health"] as? CGFloat ?? 0 | |
} | |
set { | |
storage["health"] = newValue | |
} | |
} | |
mutating func absorb(damage: CGFloat) { | |
health -= damage | |
} | |
} | |
struct Player: CanMoveMixin, HealthMixin { | |
var storage: [String : Any] | |
} | |
struct Enemy: CanMoveMixin { | |
var storage: [String : Any] | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment