-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/entity-component-system'
- Loading branch information
Showing
37 changed files
with
1,541 additions
and
241 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package net.zomis.games | ||
|
||
import klogging.KLoggers | ||
import net.zomis.core.events.EventSystem | ||
import kotlin.reflect.KClass | ||
|
||
// TODO: Is it possible to use a FeatureKey<T> and pass that to dependent features? Then use this key to fetch with some typesafety | ||
// TODO: Maybe even FeatureKey<T, R> to know what the feature was added *to* as well? | ||
typealias Feature = (Features, EventSystem) -> Unit | ||
|
||
/** | ||
* Acts as both a data storage and a way to plugin new event listeners. | ||
*/ | ||
class Features(val events: EventSystem?) { | ||
|
||
private val logger = KLoggers.logger(this) | ||
val data = mutableSetOf<Any>() | ||
|
||
operator fun <T: Any> get(clazz: KClass<T>): T { | ||
val value = data.find({ clazz.isInstance(it) }) | ||
return if (value != null) value as T else | ||
throw NullPointerException("No such component: $clazz on $this. Available features are ${data.map { it::class }}") | ||
} | ||
|
||
fun <T: Any> addData(dataToAdd: T): T { | ||
logger.info("$this adding data $dataToAdd") | ||
data.add(dataToAdd) | ||
return dataToAdd | ||
} | ||
|
||
fun add(feature: Feature): Features { | ||
feature(this, events!!) | ||
return this | ||
} | ||
|
||
} |
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package net.zomis.games.core | ||
|
||
import net.zomis.core.events.EventRegistrator | ||
import net.zomis.core.events.EventSystem | ||
|
||
typealias GameSystem = EventRegistrator | ||
|
||
class World(val events: EventSystem = EventSystem()) { | ||
|
||
private val entitiesById = mutableMapOf<String, Entity>() | ||
val core: Entity = createEntity() | ||
private var id = 0 | ||
|
||
fun createEntity(): Entity { | ||
val result = Entity(this, (id++).toString()) | ||
entitiesById[result.id] = result | ||
return result | ||
} | ||
|
||
fun entities(): Sequence<Entity> { | ||
return entitiesById.values.asSequence() | ||
} | ||
|
||
fun entityById(id: String): Entity? { | ||
return entitiesById[id] | ||
} | ||
|
||
fun system(system: GameSystem) { | ||
events.with(system) | ||
} | ||
|
||
fun <E: Any> execute(event: E): E { | ||
return this.events.execute(event) | ||
} | ||
|
||
} |
129 changes: 115 additions & 14 deletions
129
games-core/src/main/kotlin/net/zomis/games/ecs/UTTT.kt
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
Oops, something went wrong.