Cleanup entity implementation

This commit is contained in:
Christian Treffs 2019-08-20 15:43:15 +02:00
parent 7c98d5af88
commit 89f0a92557
3 changed files with 94 additions and 82 deletions

View File

@ -5,91 +5,103 @@
// Created by Christian Treffs on 08.10.17.
//
/// **Entity**
///
/// An entity is a general purpose object.
/// It only consists of a unique id (EntityIdentifier).
/// Components can be assigned to an entity to give it behavior or functionality.
/// An entity creates the relationship between all it's assigned components.
public struct Entity: UniqueEntityIdentifiable {
@usableFromInline unowned let nexus: Nexus // FIXME: weak/unowned reference?
@usableFromInline unowned let nexus: Nexus
public private(set) var identifier: EntityIdentifier = .invalid
/// The unique entity identifier.
public private(set) var identifier: EntityIdentifier
internal init(nexus: Nexus, id: EntityIdentifier) {
self.nexus = nexus
self.identifier = id
}
self.nexus = nexus
self.identifier = id
}
/// Returns the number of components for this entity.
public var numComponents: Int {
return nexus.count(components: identifier)
}
/// Checks if a component with given type is assigned to this entity.
/// - Parameter type: the component type.
public func has<C>(_ type: C.Type) -> Bool where C: Component {
return has(type.identifier)
}
/// Checks if a component with a given component identifier is assigned to this entity.
/// - Parameter compId: the component identifier.
public func has(_ compId: ComponentIdentifier) -> Bool {
return nexus.has(componentId: compId, entityIdx: identifier.index)
}
/// Checks if this entity has any components.
public var hasComponents: Bool {
return nexus.count(components: identifier) > 0
}
/// Add one or more components to this entity.
/// - Parameter components: one or more components.
@discardableResult
public func assign(_ components: Component...) -> Entity {
for component: Component in components {
assign(component)
}
return self
}
/// Add a component to this entity.
/// - Parameter component: a component.
@discardableResult
public func assign(_ component: Component) -> Entity {
nexus.assign(component: component, to: self)
return self
}
/// Add a typed component to this entity.
/// - Parameter component: the typed component.
@discardableResult
public func assign<C>(_ component: C) -> Entity where C: Component {
nexus.assign(component: component, to: self)
return self
}
/// Remove a component from this entity.
/// - Parameter component: the component.
@discardableResult
public func remove<C>(_ component: C) -> Entity where C: Component {
return remove(component.identifier)
}
/// Remove a component by type from this entity.
/// - Parameter compType: the component type.
@discardableResult
public func remove<C>(_ compType: C.Type) -> Entity where C: Component {
return remove(compType.identifier)
}
/// Remove a component by id from this entity.
/// - Parameter compId: the component id.
@discardableResult
public func remove(_ compId: ComponentIdentifier) -> Entity {
nexus.remove(component: compId, from: identifier)
return self
}
/// Remove all components from this entity.
public func removeAll() {
nexus.removeAll(componentes: identifier)
}
/// Destroy this entity.
public func destroy() {
nexus.destroy(entity: self)
}
}
// MARK: - Equatable
extension Entity: Equatable { }
// MARK: - number of components
public extension Entity {
var numComponents: Int {
return nexus.count(components: identifier)
}
}
// MARK: - has component(s)
public extension Entity {
func has<C>(_ type: C.Type) -> Bool where C: Component {
return has(type.identifier)
}
func has(_ compId: ComponentIdentifier) -> Bool {
return nexus.has(componentId: compId, entityIdx: identifier.index)
}
var hasComponents: Bool {
return nexus.count(components: identifier) > 0
}
}
// MARK: - add component(s)
public extension Entity {
@discardableResult
func assign(_ components: Component...) -> Entity {
for component: Component in components {
assign(component)
}
return self
}
@discardableResult
func assign(_ component: Component) -> Entity {
nexus.assign(component: component, to: self)
return self
}
@discardableResult
func assign<C>(_ component: C) -> Entity where C: Component {
nexus.assign(component: component, to: self)
return self
}
}
// MARK: - remove component(s)
public extension Entity {
@discardableResult
func remove<C>(_ component: C) -> Entity where C: Component {
return remove(component.identifier)
}
@discardableResult
func remove<C>(_ compType: C.Type) -> Entity where C: Component {
return remove(compType.identifier)
}
@discardableResult
func remove(_ compId: ComponentIdentifier) -> Entity {
nexus.remove(component: compId, from: identifier)
return self
}
func clear() {
nexus.clear(componentes: identifier)
}
}
// MARK: - destroy/deinit entity
public extension Entity {
func destroy() {
nexus.destroy(entity: self)
}
}

View File

@ -97,7 +97,7 @@ public extension Nexus {
}
@discardableResult
final func clear(componentes entityId: EntityIdentifier) -> Bool {
final func removeAll(componentes entityId: EntityIdentifier) -> Bool {
guard let allComponents = get(components: entityId) else {
report("clearing components form entity \(entityId) with no components")
return false

View File

@ -58,7 +58,7 @@ extension Nexus {
return false
}
if clear(componentes: entityId) {
if removeAll(componentes: entityId) {
update(familyMembership: entityId)
}