Skip to content

Commit

Permalink
Refurbish plumbing and update versions
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-arold committed May 2, 2022
1 parent d8a4859 commit c24947c
Show file tree
Hide file tree
Showing 50 changed files with 2,535 additions and 525 deletions.
81 changes: 45 additions & 36 deletions amethyst.core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,73 +1,95 @@
@file:Suppress("UnstableApiUsage")

import Libs.cobaltCore
import Libs.kotlinxCollectionsImmutable
import Libs.kotlinxCoroutines
import TestLibs.kotlinTestAnnotationsCommon
import TestLibs.kotlinTestCommon
import Libraries.kotlinReflect
import Libraries.kotlinTestAnnotationsCommon
import Libraries.kotlinTestCommon
import Libraries.kotlinTestJs
import Libraries.kotlinTestJunit
import Libraries.kotlinxCollectionsImmutable
import Libraries.kotlinxCoroutines
import Libraries.cobaltCore

import org.jetbrains.dokka.gradle.DokkaTask
import java.net.URL

plugins {
kotlin("multiplatform")
id("org.jetbrains.dokka")
id("maven-publish")
id("signing")
id("org.jetbrains.dokka")
}


kotlin {

jvm {
withJava()
jvmTarget(JavaVersion.VERSION_1_8)
compilations.all {
kotlinOptions {
apiVersion = "1.5"
languageVersion = "1.5"
}
}
}

js {
js(BOTH) {
browser {
testTask { enabled = false }
testTask {
useKarma {
useChromeHeadless()
}
}
}
nodejs()
}

sourceSets {
commonMain {

val commonMain by getting {
dependencies {
api(kotlin("reflect"))
api(kotlinxCoroutines)
api(kotlinReflect)
api(kotlinxCollectionsImmutable)

api(cobaltCore)
api("org.jetbrains.kotlinx:kotlinx-datetime:0.1.1")
}
}

val commonTest by getting {
dependencies {
implementation(kotlinTestCommon)
implementation(kotlinTestAnnotationsCommon)
}
}
val jvmMain by getting {
dependencies {
}
}
val jvmMain by getting {}
val jvmTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
}
}
val jsMain by getting {
dependencies {
implementation(kotlinTestJunit)
}
}
val jsMain by getting {}
val jsTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-js"))
implementation(kotlinTestJs)
}
}
}
}

publishing {
publishWith(
project = project,
module = "amethyst.core",
desc = "Core component of Amethyst."
)
}

signing {
isRequired = false
sign(publishing.publications)
}

tasks {
withType<DokkaTask>().configureEach {
dokkaSourceSets {
Expand All @@ -89,16 +111,3 @@ tasks {
}
}
}

publishing {
publishWith(
project = project,
module = "amethyst.core",
desc = "Core component of Amethyst."
)
}

signing {
isRequired = false
sign(publishing.publications)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.hexworks.amethyst.api.Attribute
import org.hexworks.amethyst.api.entity.Entity
import org.hexworks.amethyst.api.mutator.AttributeMutator
import org.hexworks.amethyst.internal.accessor.DefaultAttributeMutator
import org.hexworks.cobalt.datatypes.Maybe
import kotlin.jvm.JvmStatic
import kotlin.reflect.KClass

Expand All @@ -21,9 +20,11 @@ interface AttributeAccessor {
val hasAttributes: Boolean

/**
* Returns the [Attribute] of the given class [T] (if any).
* Returns the [Attribute] of the given class [T] or throws an exception if the
* attribute cannot be found.
*/
fun <T : Attribute> findAttribute(klass: KClass<T>) = Maybe.ofNullable(findAttributeOrNull(klass))
fun <T : Attribute> findAttribute(klass: KClass<T>) =
findAttributeOrNull(klass) ?: error("Cannot find attribute using class: $klass")

/**
* Returns the [Attribute] of the given class [T] (if any).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.hexworks.amethyst.api.entity.Entity
import org.hexworks.amethyst.api.mutator.BehaviorMutator
import org.hexworks.amethyst.api.system.Behavior
import org.hexworks.amethyst.internal.accessor.DefaultBehaviorMutator
import org.hexworks.cobalt.datatypes.Maybe
import kotlin.jvm.JvmStatic
import kotlin.reflect.KClass

Expand All @@ -22,9 +21,11 @@ interface BehaviorAccessor<C : Context> {
val hasBehaviors: Boolean

/**
* Returns the [Behavior] of the given class [T] (if any).
* Returns the [Behavior] of the given class [T] or throws an exception if the
* behavior cannot be found.
*/
fun <T : Behavior<C>> findBehavior(klass: KClass<T>) = Maybe.ofNullable(findBehaviorOrNull(klass))
fun <T : Behavior<C>> findBehavior(klass: KClass<T>) =
findBehaviorOrNull(klass) ?: error("Cannot find behavior using class: $klass")

/**
* Returns the [Behavior] of the given class [T] (if any).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.hexworks.amethyst.api.extensions.FacetWithContext
import org.hexworks.amethyst.api.mutator.FacetMutator
import org.hexworks.amethyst.api.system.Facet
import org.hexworks.amethyst.internal.accessor.DefaultFacetMutator
import org.hexworks.cobalt.datatypes.Maybe
import kotlin.jvm.JvmStatic
import kotlin.reflect.KClass

Expand All @@ -24,9 +23,11 @@ interface FacetAccessor<C : Context> {
val hasFacets: Boolean

/**
* Returns the [Facet] of the given class [T] (if any).
* Returns the [Facet] of the given class [T] or throws an exception if the
* facet cannot be found.
*/
fun <T : FacetWithContext<C>> findFacet(klass: KClass<T>) = Maybe.ofNullable(findFacetOrNull(klass))
fun <T : FacetWithContext<C>> findFacet(klass: KClass<T>) =
findFacetOrNull(klass) ?: error("Cannot find facet using class: $klass")

/**
* Returns the [Facet] of the given class [T] (if any).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.reflect.KClass
* Base class that can be used to create custom [Behavior] implementations.
*/
abstract class BaseBehavior<C : Context>(
vararg mandatoryAttribute: KClass<out Attribute>
vararg mandatoryAttribute: KClass<out Attribute>
) : Behavior<C> {
override val id = UUID.randomUUID()
override val mandatoryAttributes: Set<KClass<out Attribute>> = mandatoryAttribute.toSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import org.hexworks.cobalt.core.api.UUID
* Base class that can be used to create custom [MutableEntity] implementations.
*/
abstract class BaseEntity<T : EntityType, C : Context>(
override val type: T,
attributes: Set<Attribute> = setOf(),
facets: Set<FacetWithContext<C>> = setOf(),
behaviors: Set<Behavior<C>> = setOf()
override val type: T,
attributes: Set<Attribute> = setOf(),
facets: Set<FacetWithContext<C>> = setOf(),
behaviors: Set<Behavior<C>> = setOf()
) : MutableEntity<T, C>,
AttributeMutator by AttributeMutator.create(attributes),
FacetMutator<C> by FacetMutator.create(facets),
BehaviorMutator<C> by BehaviorMutator.create(behaviors) {
AttributeMutator by AttributeMutator.create(attributes),
FacetMutator<C> by FacetMutator.create(facets),
BehaviorMutator<C> by BehaviorMutator.create(behaviors) {

override val id = UUID.randomUUID()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import org.hexworks.cobalt.core.api.UUID
* Base class which can be used for [EntityType]s.
*/
abstract class BaseEntityType(
override val name: String = "unknown",
override val description: String = "",
override val id: UUID = UUID.randomUUID()
override val name: String = "unknown",
override val description: String = "",
override val id: UUID = UUID.randomUUID()
) : EntityType {
final override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

package org.hexworks.amethyst.api.base

import org.hexworks.amethyst.api.*
import org.hexworks.amethyst.api.message.StateChanged
import org.hexworks.amethyst.api.Attribute
import org.hexworks.amethyst.api.Context
import org.hexworks.amethyst.api.Message
import org.hexworks.amethyst.api.Pass
import org.hexworks.amethyst.api.Response
import org.hexworks.amethyst.api.system.Facet
import org.hexworks.cobalt.core.api.UUID
import kotlin.reflect.KClass

abstract class BaseFacet<C : Context, P : Message<C>>(
override val messageType: KClass<P>,
vararg mandatoryAttribute: KClass<out Attribute>
override val messageType: KClass<P>,
vararg mandatoryAttribute: KClass<out Attribute>
) : Facet<C, P> {
override val id = UUID.randomUUID()
override val mandatoryAttributes: Set<KClass<out Attribute>> = mandatoryAttribute.toSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

package org.hexworks.amethyst.api.base

import org.hexworks.amethyst.api.*
import org.hexworks.amethyst.api.Attribute
import org.hexworks.amethyst.api.Context
import org.hexworks.amethyst.api.Message
import org.hexworks.amethyst.api.system.StateFacet
import kotlin.reflect.KClass

abstract class BaseStateFacet<C : Context, P : Message<C>>(
messageType: KClass<P>,
vararg mandatoryAttributes: KClass<out Attribute>
messageType: KClass<P>,
vararg mandatoryAttributes: KClass<out Attribute>
) : BaseFacet<C, P>(messageType, *mandatoryAttributes), StateFacet<C, P>
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ class EntityBuilder<T : EntityType, C : Context>(private val type: T) {
val finalBehaviors = behaviors.toSet()
val finalFacets = facets.toSet()
val missingAttributes = finalFacets.flatMap { it.mandatoryAttributes }
.plus(finalBehaviors.flatMap { it.mandatoryAttributes })
.toSet()
.subtract(this.attributes.map { it::class })
.plus(finalBehaviors.flatMap { it.mandatoryAttributes })
.toSet()
.subtract(this.attributes.map { it::class }.toSet())
require(missingAttributes.isEmpty()) {
"Can't create Entity because there are missing attributes: ${missingAttributes.joinToString { it.simpleName!! }}."
}
return DefaultEntity(
type = type,
attributes = finalAttributes,
facets = finalFacets,
behaviors = finalBehaviors
type = type,
attributes = finalAttributes,
facets = finalFacets,
behaviors = finalBehaviors
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.hexworks.amethyst.api.entity

import org.hexworks.amethyst.api.Attribute
import org.hexworks.amethyst.api.Message
import org.hexworks.amethyst.api.Context
import org.hexworks.amethyst.api.Message
import org.hexworks.amethyst.api.Response
import org.hexworks.amethyst.api.system.Behavior
import org.hexworks.amethyst.api.accessor.AttributeAccessor
import org.hexworks.amethyst.api.accessor.BehaviorAccessor
import org.hexworks.amethyst.api.accessor.FacetAccessor
import org.hexworks.amethyst.api.system.Behavior
import org.hexworks.amethyst.api.system.System
import org.hexworks.cobalt.core.api.UUID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import kotlin.reflect.KClass
* the acceptable message type.
*/
fun <C : Context, A : Message<C>, M : A> Facet<C, M>.toStateMachine(
commonAncestor: KClass<A>
commonAncestor: KClass<A>
): Facet<C, A> = StateMachineFacet(
messageType = commonAncestor,
initialState = this
messageType = commonAncestor,
initialState = this
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import org.hexworks.amethyst.api.entity.EntityType
import org.hexworks.amethyst.api.system.Facet

data class StateChanged<C : Context>(
override val context: C,
override val source: Entity<EntityType, C>,
val oldState: Facet<C, out Message<C>>,
val newState: Facet<C, out Message<C>>
override val context: C,
override val source: Entity<EntityType, C>,
val oldState: Facet<C, out Message<C>>,
val newState: Facet<C, out Message<C>>
) : Message<C>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface FacetMutator<C : Context> : FacetAccessor<C> {

@JvmStatic
fun <C : Context> create(
facets: Set<FacetWithContext<C>>
facets: Set<FacetWithContext<C>>
): FacetMutator<C> = DefaultFacetMutator(facets)
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.hexworks.amethyst.api.system

import org.hexworks.amethyst.api.Message
import org.hexworks.amethyst.api.Context
import org.hexworks.amethyst.api.entity.EntityType

/**
* A [Actor] is a [System] that's a combination of a [Behavior] and a [Facet].
* This means that it performs autonomous actions on [update] and can respond
* to [Message]s.
*
* A good example of an [Actor] is a [System] that handles the `energy` of an
* entity. On each update it is drained a little bit, but when actions are
* performed that require `energy` a bigger amount will be subtracted.
*/
interface Actor<T : EntityType, C : Context, P : Message<C>> : Facet<C, P>, Behavior<C>
package org.hexworks.amethyst.api.system

import org.hexworks.amethyst.api.Message
import org.hexworks.amethyst.api.Context
import org.hexworks.amethyst.api.entity.EntityType

/**
* A [Actor] is a [System] that's a combination of a [Behavior] and a [Facet].
* This means that it performs autonomous actions on [update] and can respond
* to [Message]s.
*
* A good example of an [Actor] is a [System] that handles the `energy` of an
* entity. On each update it is drained a little bit, but when actions are
* performed that require `energy` a bigger amount will be subtracted.
*/
interface Actor<T : EntityType, C : Context, P : Message<C>> : Facet<C, P>, Behavior<C>
Loading

0 comments on commit c24947c

Please sign in to comment.