Created
October 11, 2015 08:14
-
-
Save patoi/718845c47ab3a260610b to your computer and use it in GitHub Desktop.
Swift Components and Entities
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
//: Playground - noun: a place where people can play | |
import GameplayKit | |
class ShootingComponent: GKComponent { | |
let power: Int | |
init(power: Int) { | |
self.power = power | |
} | |
func shooting(target: GKEntity) { | |
let targetHealthComponent = target.componentForClass(HealtComponent) | |
targetHealthComponent!.updateHealth(-power) | |
print("new health is \(targetHealthComponent!.health)") | |
} | |
} | |
class HealtComponent: GKComponent { | |
var health: Int | |
init(health: Int) { | |
self.health = health | |
} | |
func updateHealth(health: Int) -> Void { | |
self.health += health | |
} | |
override func updateWithDeltaTime(seconds: NSTimeInterval) { | |
updateHealth(1) | |
print("update health to \(health)") | |
} | |
} | |
// creating componenets and entities | |
var enemyShootingComp = ShootingComponent(power: 10) | |
var myShootingComp = ShootingComponent(power: 20) | |
var enemyHealthComp = HealtComponent(health: 100) | |
var myHealthComp = HealtComponent(health: 100) | |
let enemy = GKEntity() | |
let mySelf = GKEntity() | |
enemy.addComponent(enemyShootingComp) | |
enemy.addComponent(enemyHealthComp) | |
mySelf.addComponent(myShootingComp) | |
mySelf.addComponent(myHealthComp) | |
// game system: health system created | |
// calling all health components | |
class HealthSystem: GKComponentSystem { | |
// start all component's time dependent task | |
override func updateWithDeltaTime(seconds: NSTimeInterval) { | |
let clist = self.components | |
for comp in clist { | |
print("self healing called") | |
comp.updateWithDeltaTime(seconds) | |
} | |
} | |
} | |
// just HealthComponent class will be accepted in the HealthSystem | |
let healthSystem = HealthSystem(componentClass: HealtComponent.self) | |
healthSystem.addComponentWithEntity(enemy) | |
healthSystem.addComponentWithEntity(mySelf) | |
print("---> time dependent task started") | |
// update all health components: enemy and my health | |
healthSystem.updateWithDeltaTime(10) | |
// starting actions: shootings | |
print("\n---> action/event dependent task started") | |
let attackerEnemyShootingComp = enemy.componentForClass(ShootingComponent) | |
attackerEnemyShootingComp?.shooting(mySelf) | |
let myAttackShootingComp = mySelf.componentForClass(ShootingComponent) | |
myAttackShootingComp?.shooting(enemy) |
Nice, very simple to understand
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OUTPUT:
---> time dependent task started
self healing called
update health to 101
self healing called
update health to 101
---> action/event dependent task started
new health is 91
new health is 81