address linter warnings
This commit is contained in:
parent
386350ab1f
commit
53d2c27082
|
|
@ -22,7 +22,7 @@ public protocol ComponentProvider {
|
||||||
|
|
||||||
/// - Returns: struct/class instance that confirms to Hashable protocol
|
/// - Returns: struct/class instance that confirms to Hashable protocol
|
||||||
var identifier: AnyHashable { get }
|
var identifier: AnyHashable { get }
|
||||||
|
|
||||||
/// Used to request a component from the provider.
|
/// Used to request a component from the provider.
|
||||||
/// - Returns: A component for use in the state that the entity is entering
|
/// - Returns: A component for use in the state that the entity is entering
|
||||||
func getComponent() -> Component
|
func getComponent() -> Component
|
||||||
|
|
@ -34,7 +34,7 @@ public protocol ComponentProvider {
|
||||||
/// is passed to the provider at initialisation.
|
/// is passed to the provider at initialisation.
|
||||||
public class ComponentInstanceProvider {
|
public class ComponentInstanceProvider {
|
||||||
private var instance: Component
|
private var instance: Component
|
||||||
|
|
||||||
/// Initializer
|
/// Initializer
|
||||||
/// - Parameter instance: The instance to return whenever a component is requested.
|
/// - Parameter instance: The instance to return whenever a component is requested.
|
||||||
public init(instance: Component) {
|
public init(instance: Component) {
|
||||||
|
|
@ -49,7 +49,7 @@ extension ComponentInstanceProvider: ComponentProvider {
|
||||||
public var identifier: AnyHashable {
|
public var identifier: AnyHashable {
|
||||||
ObjectIdentifier(instance)
|
ObjectIdentifier(instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used to request a component from this provider
|
/// Used to request a component from this provider
|
||||||
/// - Returns: The instance
|
/// - Returns: The instance
|
||||||
public func getComponent() -> Component {
|
public func getComponent() -> Component {
|
||||||
|
|
@ -63,12 +63,12 @@ extension ComponentInstanceProvider: ComponentProvider {
|
||||||
/// is created when requested and is of the type passed in to the initializer.
|
/// is created when requested and is of the type passed in to the initializer.
|
||||||
public class ComponentTypeProvider {
|
public class ComponentTypeProvider {
|
||||||
private var componentType: ComponentInitializable.Type
|
private var componentType: ComponentInitializable.Type
|
||||||
|
|
||||||
/// Used to compare this provider with others. Any ComponentTypeProvider that returns
|
/// Used to compare this provider with others. Any ComponentTypeProvider that returns
|
||||||
/// the same type will be regarded as equivalent.
|
/// the same type will be regarded as equivalent.
|
||||||
/// - Returns:ObjectIdentifier of the type of the instances created
|
/// - Returns:ObjectIdentifier of the type of the instances created
|
||||||
public let identifier: AnyHashable
|
public let identifier: AnyHashable
|
||||||
|
|
||||||
/// Initializer
|
/// Initializer
|
||||||
/// - Parameter type: The type of the instances to be created
|
/// - Parameter type: The type of the instances to be created
|
||||||
public init(type: ComponentInitializable.Type) {
|
public init(type: ComponentInitializable.Type) {
|
||||||
|
|
@ -90,12 +90,12 @@ extension ComponentTypeProvider: ComponentProvider {
|
||||||
/// This component provider always returns the same instance of the component. The instance
|
/// This component provider always returns the same instance of the component. The instance
|
||||||
/// is created when first required and is of the type passed in to the initializer.
|
/// is created when first required and is of the type passed in to the initializer.
|
||||||
public class ComponentSingletonProvider {
|
public class ComponentSingletonProvider {
|
||||||
lazy private var instance: Component = {
|
private lazy var instance: Component = {
|
||||||
componentType.init()
|
componentType.init()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
private var componentType: ComponentInitializable.Type
|
private var componentType: ComponentInitializable.Type
|
||||||
|
|
||||||
/// Initializer
|
/// Initializer
|
||||||
/// - Parameter type: The type of the single instance
|
/// - Parameter type: The type of the single instance
|
||||||
public init(type: ComponentInitializable.Type) {
|
public init(type: ComponentInitializable.Type) {
|
||||||
|
|
@ -110,7 +110,7 @@ extension ComponentSingletonProvider: ComponentProvider {
|
||||||
public var identifier: AnyHashable {
|
public var identifier: AnyHashable {
|
||||||
ObjectIdentifier(instance)
|
ObjectIdentifier(instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used to request a component from this provider
|
/// Used to request a component from this provider
|
||||||
/// - Returns: The single instance
|
/// - Returns: The single instance
|
||||||
public func getComponent() -> Component {
|
public func getComponent() -> Component {
|
||||||
|
|
@ -125,17 +125,17 @@ extension ComponentSingletonProvider: ComponentProvider {
|
||||||
public class DynamicComponentProvider {
|
public class DynamicComponentProvider {
|
||||||
/// Wrapper for closure to make it hashable
|
/// Wrapper for closure to make it hashable
|
||||||
public class Closure {
|
public class Closure {
|
||||||
let closure: () -> Component
|
let provideComponent: () -> Component
|
||||||
|
|
||||||
/// Initializer
|
/// Initializer
|
||||||
/// - Parameter closure: Swift closure returning component of the appropriate type
|
/// - Parameter provideComponent: Swift closure returning component of the appropriate type
|
||||||
public init(closure: @escaping () -> Component) {
|
public init(provideComponent: @escaping () -> Component) {
|
||||||
self.closure = closure
|
self.provideComponent = provideComponent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private let closure: Closure
|
private let closure: Closure
|
||||||
|
|
||||||
|
|
||||||
/// Initializer
|
/// Initializer
|
||||||
/// - Parameter closure: Instance of Closure class. A wrapper around closure that will
|
/// - Parameter closure: Instance of Closure class. A wrapper around closure that will
|
||||||
/// return the component instance when called.
|
/// return the component instance when called.
|
||||||
|
|
@ -151,11 +151,11 @@ extension DynamicComponentProvider: ComponentProvider {
|
||||||
public var identifier: AnyHashable {
|
public var identifier: AnyHashable {
|
||||||
ObjectIdentifier(closure)
|
ObjectIdentifier(closure)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used to request a component from this provider
|
/// Used to request a component from this provider
|
||||||
/// - Returns: The instance returned by calling the closure
|
/// - Returns: The instance returned by calling the closure
|
||||||
public func getComponent() -> Component {
|
public func getComponent() -> Component {
|
||||||
closure.closure()
|
closure.provideComponent()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -165,24 +165,25 @@ extension DynamicComponentProvider: ComponentProvider {
|
||||||
/// are used to add components to the entity when this state is entered.
|
/// are used to add components to the entity when this state is entered.
|
||||||
public class EntityState {
|
public class EntityState {
|
||||||
internal var providers = [ComponentIdentifier: ComponentProvider]()
|
internal var providers = [ComponentIdentifier: ComponentProvider]()
|
||||||
|
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
/// Add a new ComponentMapping to this state. The mapping is a utility class that is used to
|
/// Add a new ComponentMapping to this state. The mapping is a utility class that is used to
|
||||||
/// map a component type to the provider that provides the component.
|
/// map a component type to the provider that provides the component.
|
||||||
/// - Parameter type: The type of component to be mapped
|
/// - Parameter type: The type of component to be mapped
|
||||||
/// - Returns: The component mapping to use when setting the provider for the component
|
/// - Returns: The component mapping to use when setting the provider for the component
|
||||||
@discardableResult public func add(_ type: ComponentInitializable.Type) -> StateComponentMapping {
|
@discardableResult
|
||||||
|
public func add(_ type: ComponentInitializable.Type) -> StateComponentMapping {
|
||||||
StateComponentMapping(creatingState: self, type: type)
|
StateComponentMapping(creatingState: self, type: type)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the ComponentProvider for a particular component type.
|
/// Get the ComponentProvider for a particular component type.
|
||||||
/// - Parameter type: The type of component to get the provider for
|
/// - Parameter type: The type of component to get the provider for
|
||||||
/// - Returns: The ComponentProvider
|
/// - Returns: The ComponentProvider
|
||||||
public func get(_ type: ComponentInitializable.Type) -> ComponentProvider? {
|
public func get(_ type: ComponentInitializable.Type) -> ComponentProvider? {
|
||||||
providers[type.identifier]
|
providers[type.identifier]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// To determine whether this state has a provider for a specific component type.
|
/// To determine whether this state has a provider for a specific component type.
|
||||||
/// - Parameter type: The type of component to look for a provider for
|
/// - Parameter type: The type of component to look for a provider for
|
||||||
/// - Returns: true if there is a provider for the given type, false otherwise
|
/// - Returns: true if there is a provider for the given type, false otherwise
|
||||||
|
|
@ -198,7 +199,7 @@ public class StateComponentMapping {
|
||||||
private var componentType: ComponentInitializable.Type
|
private var componentType: ComponentInitializable.Type
|
||||||
private let creatingState: EntityState
|
private let creatingState: EntityState
|
||||||
private var provider: ComponentProvider
|
private var provider: ComponentProvider
|
||||||
|
|
||||||
/// Used internally, the initializer creates a component mapping. The constructor
|
/// Used internally, the initializer creates a component mapping. The constructor
|
||||||
/// creates a ComponentTypeProvider as the default mapping, which will be replaced
|
/// creates a ComponentTypeProvider as the default mapping, which will be replaced
|
||||||
/// by more specific mappings if other methods are called.
|
/// by more specific mappings if other methods are called.
|
||||||
|
|
@ -210,26 +211,28 @@ public class StateComponentMapping {
|
||||||
componentType = type
|
componentType = type
|
||||||
provider = ComponentTypeProvider(type: type)
|
provider = ComponentTypeProvider(type: type)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a mapping for the component type to a specific component instance. A
|
/// Creates a mapping for the component type to a specific component instance. A
|
||||||
/// ComponentInstanceProvider is used for the mapping.
|
/// ComponentInstanceProvider is used for the mapping.
|
||||||
/// - Parameter component: The component instance to use for the mapping
|
/// - Parameter component: The component instance to use for the mapping
|
||||||
/// - Returns: This ComponentMapping, so more modifications can be applied
|
/// - Returns: This ComponentMapping, so more modifications can be applied
|
||||||
@discardableResult public func withInstance(_ component: Component) -> StateComponentMapping {
|
@discardableResult
|
||||||
|
public func withInstance(_ component: Component) -> StateComponentMapping {
|
||||||
setProvider(ComponentInstanceProvider(instance: component))
|
setProvider(ComponentInstanceProvider(instance: component))
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a mapping for the component type to new instances of the provided type.
|
/// Creates a mapping for the component type to new instances of the provided type.
|
||||||
/// The type should be the same as or extend the type for this mapping. A ComponentTypeProvider
|
/// The type should be the same as or extend the type for this mapping. A ComponentTypeProvider
|
||||||
/// is used for the mapping.
|
/// is used for the mapping.
|
||||||
/// - Parameter type: The type of components to be created by this mapping
|
/// - Parameter type: The type of components to be created by this mapping
|
||||||
/// - Returns: This ComponentMapping, so more modifications can be applied
|
/// - Returns: This ComponentMapping, so more modifications can be applied
|
||||||
@discardableResult public func withType(_ type: ComponentInitializable.Type) -> Self {
|
@discardableResult
|
||||||
|
public func withType(_ type: ComponentInitializable.Type) -> Self {
|
||||||
setProvider(ComponentTypeProvider(type: type))
|
setProvider(ComponentTypeProvider(type: type))
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a mapping for the component type to a single instance of the provided type.
|
/// Creates a mapping for the component type to a single instance of the provided type.
|
||||||
/// The instance is not created until it is first requested. The type should be the same
|
/// The instance is not created until it is first requested. The type should be the same
|
||||||
/// as or extend the type for this mapping. A ComponentSingletonProvider is used for
|
/// as or extend the type for this mapping. A ComponentSingletonProvider is used for
|
||||||
|
|
@ -237,28 +240,31 @@ public class StateComponentMapping {
|
||||||
/// - Parameter type: The type of the single instance to be created. If omitted, the type of the
|
/// - Parameter type: The type of the single instance to be created. If omitted, the type of the
|
||||||
/// mapping is used.
|
/// mapping is used.
|
||||||
/// - Returns: This ComponentMapping, so more modifications can be applied
|
/// - Returns: This ComponentMapping, so more modifications can be applied
|
||||||
@discardableResult public func withSingleton(_ type: ComponentInitializable.Type?) -> Self {
|
@discardableResult
|
||||||
|
public func withSingleton(_ type: ComponentInitializable.Type?) -> Self {
|
||||||
setProvider(ComponentSingletonProvider(type: type ?? componentType))
|
setProvider(ComponentSingletonProvider(type: type ?? componentType))
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a mapping for the component type to a method call. A
|
/// Creates a mapping for the component type to a method call. A
|
||||||
/// DynamicComponentProvider is used for the mapping.
|
/// DynamicComponentProvider is used for the mapping.
|
||||||
/// - Parameter method: The method to return the component instance
|
/// - Parameter method: The method to return the component instance
|
||||||
/// - Returns: This ComponentMapping, so more modifications can be applied
|
/// - Returns: This ComponentMapping, so more modifications can be applied
|
||||||
@discardableResult public func withMethod(_ closure: DynamicComponentProvider.Closure) -> Self {
|
@discardableResult
|
||||||
|
public func withMethod(_ closure: DynamicComponentProvider.Closure) -> Self {
|
||||||
setProvider(DynamicComponentProvider(closure: closure))
|
setProvider(DynamicComponentProvider(closure: closure))
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a mapping for the component type to any ComponentProvider.
|
/// Creates a mapping for the component type to any ComponentProvider.
|
||||||
/// - Parameter provider: The component provider to use.
|
/// - Parameter provider: The component provider to use.
|
||||||
/// - Returns: This ComponentMapping, so more modifications can be applied.
|
/// - Returns: This ComponentMapping, so more modifications can be applied.
|
||||||
@discardableResult public func withProvider(_ provider: ComponentProvider) -> Self {
|
@discardableResult
|
||||||
|
public func withProvider(_ provider: ComponentProvider) -> Self {
|
||||||
setProvider(provider)
|
setProvider(provider)
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Maps through to the add method of the EntityState that this mapping belongs to
|
/// Maps through to the add method of the EntityState that this mapping belongs to
|
||||||
/// so that a fluent interface can be used when configuring entity states.
|
/// so that a fluent interface can be used when configuring entity states.
|
||||||
/// - Parameter type: The type of component to add a mapping to the state for
|
/// - Parameter type: The type of component to add a mapping to the state for
|
||||||
|
|
@ -266,7 +272,7 @@ public class StateComponentMapping {
|
||||||
public func add(_ type: ComponentInitializable.Type) -> StateComponentMapping {
|
public func add(_ type: ComponentInitializable.Type) -> StateComponentMapping {
|
||||||
creatingState.add(type)
|
creatingState.add(type)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setProvider(_ provider: ComponentProvider) {
|
private func setProvider(_ provider: ComponentProvider) {
|
||||||
self.provider = provider
|
self.provider = provider
|
||||||
creatingState.providers[componentType.identifier] = provider
|
creatingState.providers[componentType.identifier] = provider
|
||||||
|
|
@ -282,28 +288,29 @@ public class StateComponentMapping {
|
||||||
/// - Parameter StateName: Generic hashable state name type
|
/// - Parameter StateName: Generic hashable state name type
|
||||||
public class EntityStateMachine<StateName: Hashable> {
|
public class EntityStateMachine<StateName: Hashable> {
|
||||||
private var states: [StateName: EntityState]
|
private var states: [StateName: EntityState]
|
||||||
|
|
||||||
/// The current state of the state machine.
|
/// The current state of the state machine.
|
||||||
private var currentState: EntityState?
|
private var currentState: EntityState?
|
||||||
|
|
||||||
/// The entity whose state machine this is
|
/// The entity whose state machine this is
|
||||||
public var entity: Entity
|
public var entity: Entity
|
||||||
|
|
||||||
/// Initializer. Creates an EntityStateMachine.
|
/// Initializer. Creates an EntityStateMachine.
|
||||||
public init(entity: Entity) {
|
public init(entity: Entity) {
|
||||||
self.entity = entity
|
self.entity = entity
|
||||||
states = [:]
|
states = [:]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a state to this state machine.
|
/// Add a state to this state machine.
|
||||||
/// - Parameter name: The name of this state - used to identify it later in the changeState method call.
|
/// - Parameter name: The name of this state - used to identify it later in the changeState method call.
|
||||||
/// - Parameter state: The state.
|
/// - Parameter state: The state.
|
||||||
/// - Returns: This state machine, so methods can be chained.
|
/// - Returns: This state machine, so methods can be chained.
|
||||||
@discardableResult public func addState(name: StateName, state: EntityState) -> Self {
|
@discardableResult
|
||||||
|
public func addState(name: StateName, state: EntityState) -> Self {
|
||||||
states[name] = state
|
states[name] = state
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new state in this state machine.
|
/// Create a new state in this state machine.
|
||||||
/// - Parameter name: The name of the new state - used to identify it later in the changeState method call.
|
/// - Parameter name: The name of the new state - used to identify it later in the changeState method call.
|
||||||
/// - Returns: The new EntityState object that is the state. This will need to be configured with
|
/// - Returns: The new EntityState object that is the state. This will need to be configured with
|
||||||
|
|
@ -313,8 +320,7 @@ public class EntityStateMachine<StateName: Hashable> {
|
||||||
states[name] = state
|
states[name] = state
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Change to a new state. The components from the old state will be removed and the components
|
/// Change to a new state. The components from the old state will be removed and the components
|
||||||
/// for the new state will be added.
|
/// for the new state will be added.
|
||||||
/// - Parameter name: The name of the state to change to.
|
/// - Parameter name: The name of the state to change to.
|
||||||
|
|
@ -322,33 +328,33 @@ public class EntityStateMachine<StateName: Hashable> {
|
||||||
guard let newState = states[name] else {
|
guard let newState = states[name] else {
|
||||||
fatalError("Entity state '\(name)' doesn't exist")
|
fatalError("Entity state '\(name)' doesn't exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
if newState === currentState {
|
if newState === currentState {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var toAdd: [ComponentIdentifier: ComponentProvider]
|
var toAdd: [ComponentIdentifier: ComponentProvider]
|
||||||
|
|
||||||
if let currentState = currentState {
|
if let currentState = currentState {
|
||||||
toAdd = .init()
|
toAdd = .init()
|
||||||
for t in newState.providers {
|
for (identifier, provider) in newState.providers {
|
||||||
toAdd[t.key] = t.value
|
toAdd[identifier] = provider
|
||||||
}
|
}
|
||||||
|
|
||||||
for t in currentState.providers {
|
for (identifier, _) in currentState.providers {
|
||||||
if let other = toAdd[t.key], let current = currentState.providers[t.key],
|
if let other = toAdd[identifier], let current = currentState.providers[identifier],
|
||||||
current.identifier == other.identifier {
|
current.identifier == other.identifier {
|
||||||
toAdd[t.key] = nil
|
toAdd[identifier] = nil
|
||||||
} else {
|
} else {
|
||||||
entity.remove(t.key)
|
entity.remove(identifier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
toAdd = newState.providers
|
toAdd = newState.providers
|
||||||
}
|
}
|
||||||
|
|
||||||
for t in toAdd {
|
for (identifier, _) in toAdd {
|
||||||
guard let component = toAdd[t.key]?.getComponent() else {
|
guard let component = toAdd[identifier]?.getComponent() else {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
entity.assign(component)
|
entity.assign(component)
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,6 @@ class ComponentTypeProviderTests: XCTestCase {
|
||||||
self.value = false
|
self.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ComponentSingletonProviderTests: XCTestCase {
|
class ComponentSingletonProviderTests: XCTestCase {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue