This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate Fleks ECS into korge + example (#472)
* Add fleks ECS unchanged * Adapt registering of systems in World Instantiation of generic classes is not possible in Kotlin multiplatform. Thus, instead of instantiating a generic class in the SystemService the system function gets a factory method for calling the constructor of the system. * Add Injector to IntervalSystem * Integate creation of ComponentMapper objects in systems * Integrate Family creation First version which works. * Add singleton for injecting objects into systems * Implement adding of component listener in world configuration The component listener will be added together with registering the component in the world configuration. That was done because when registering the component listener the base type of the component is needed. Without reflections that is not possible to detect it from the component listener template type. Thus, adding component and listener objects together is the most elegant workaround - I guess. * Update with latest changes from Fleks project * Add comments and rename cmp to comp * Add sources for ECS example * Update working example with enhanced spawner The spawner should be able to spawn objects which can spawn objects themselves. This is needed for the meteor object. It spawns fire trails. * Implemented spawning of meteorid objects - It is now possible to spawn objects which themselves spawn another objects. - Added variation of acceleration which makes the animation of the file trails looking more dynamic. * Implement dependency injection by type names In case of injection of multiple dependencies with the same type it is now possible to specify the type name on injection to the world configuration. When passing that dependency in a system or component listener than the type name can be given as an argument to Inject.dependency() function call. Updated the readme to reflect that injection of dependencies is possible by providing an optional type name as parameter together with the depencency object. * Add rigidbody and destruct component and update systems Rigidbody and destruct components were added which are use to control movement and destruction of entities. For that a new system called DestructSystem was added. With it the destruction of an entity can triger creation of new objects like explosions, etc. Rigidbody component details will control how an entity is influenced by gravity, friction or damping. This still needs to be implemented yet in the MoveSystem. * Add gravity and impulse to explosions * My pixels against Putin * Move Fleks under korge-fleks and update fleks-ecs example code Source code for Fleks ECS was moved under korge-fleks to decouple it from the example code. Updated fleks-ecs example code with source comments. * Convert unit tests of korge-fleks into junit4 style * Fix exceptions in unit tests * Add missing android manifest file * Fix excepton with injection on Kotlin native * Fix BitArray equal function and add test cases The equal function of BitArray was unsafe regarding comparing objects of different type. Added unit tests to avoid regression.
- Loading branch information
Showing
45 changed files
with
4,620 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Simon Klausner | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,5 @@ | ||
description = "Multiplatform Game Engine written in Kotlin" | ||
|
||
dependencies { | ||
add("commonMainApi", project(":korio")) | ||
} |
139 changes: 139 additions & 0 deletions
139
korge-fleks/src/benchmarks/kotlin/com/github/quillraven/fleks/benchmark/artemis.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.github.quillraven.fleks.benchmark | ||
|
||
import com.artemis.Component | ||
import com.artemis.ComponentMapper | ||
import com.artemis.World | ||
import com.artemis.WorldConfigurationBuilder | ||
import com.artemis.annotations.All | ||
import com.artemis.annotations.Exclude | ||
import com.artemis.annotations.One | ||
import com.artemis.systems.IteratingSystem | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
data class ArtemisPosition(var x: Float = 0f, var y: Float = 0f) : Component() | ||
|
||
data class ArtemisLife(var life: Float = 0f) : Component() | ||
|
||
data class ArtemisSprite(var path: String = "", var animationTime: Float = 0f) : Component() | ||
|
||
@All(ArtemisPosition::class) | ||
class ArtemisSystemSimple : IteratingSystem() { | ||
private lateinit var mapper: ComponentMapper<ArtemisPosition> | ||
|
||
override fun process(entityId: Int) { | ||
mapper[entityId].x++ | ||
} | ||
} | ||
|
||
@All(ArtemisPosition::class) | ||
@Exclude(ArtemisLife::class) | ||
@One(ArtemisSprite::class) | ||
class ArtemisSystemComplex1 : IteratingSystem() { | ||
private var processCalls = 0 | ||
private lateinit var positions: ComponentMapper<ArtemisPosition> | ||
private lateinit var lifes: ComponentMapper<ArtemisLife> | ||
private lateinit var sprites: ComponentMapper<ArtemisSprite> | ||
|
||
override fun process(entityId: Int) { | ||
if (processCalls % 2 == 0) { | ||
positions[entityId].x++ | ||
lifes.create(entityId) | ||
} else { | ||
positions.remove(entityId) | ||
} | ||
sprites[entityId].animationTime++ | ||
++processCalls | ||
} | ||
} | ||
|
||
@One(ArtemisPosition::class, ArtemisLife::class, ArtemisSprite::class) | ||
class ArtemisSystemComplex2 : IteratingSystem() { | ||
private lateinit var positions: ComponentMapper<ArtemisPosition> | ||
private lateinit var lifes: ComponentMapper<ArtemisLife> | ||
|
||
override fun process(entityId: Int) { | ||
lifes.remove(entityId) | ||
positions.create(entityId) | ||
} | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
open class ArtemisStateAddRemove { | ||
lateinit var world: World | ||
|
||
@Setup(value = Level.Iteration) | ||
fun setup() { | ||
world = World(WorldConfigurationBuilder().run { | ||
build() | ||
}) | ||
} | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
open class ArtemisStateSimple { | ||
lateinit var world: World | ||
|
||
@Setup(value = Level.Iteration) | ||
fun setup() { | ||
world = World(WorldConfigurationBuilder().run { | ||
with(ArtemisSystemSimple()) | ||
build() | ||
}) | ||
|
||
repeat(NUM_ENTITIES) { | ||
world.createEntity().edit().create(ArtemisPosition::class.java) | ||
} | ||
} | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
open class ArtemisStateComplex { | ||
lateinit var world: World | ||
|
||
@Setup(value = Level.Iteration) | ||
fun setup() { | ||
world = World(WorldConfigurationBuilder().run { | ||
with(ArtemisSystemComplex1()) | ||
with(ArtemisSystemComplex2()) | ||
build() | ||
}) | ||
|
||
repeat(NUM_ENTITIES) { | ||
val entityEdit = world.createEntity().edit() | ||
entityEdit.create(ArtemisPosition::class.java) | ||
entityEdit.create(ArtemisSprite::class.java) | ||
} | ||
} | ||
} | ||
|
||
@Fork(1) | ||
@Warmup(iterations = WARMUPS) | ||
@Measurement(iterations = ITERATIONS, time = TIME, timeUnit = TimeUnit.SECONDS) | ||
open class ArtemisBenchmark { | ||
@Benchmark | ||
fun addRemove(state: ArtemisStateAddRemove) { | ||
repeat(NUM_ENTITIES) { | ||
state.world.createEntity().edit().create(ArtemisPosition::class.java) | ||
} | ||
repeat(NUM_ENTITIES) { | ||
state.world.delete(it) | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun simple(state: ArtemisStateSimple) { | ||
repeat(WORLD_UPDATES) { | ||
state.world.delta = 1f | ||
state.world.process() | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun complex(state: ArtemisStateComplex) { | ||
repeat(WORLD_UPDATES) { | ||
state.world.delta = 1f | ||
state.world.process() | ||
} | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
korge-fleks/src/benchmarks/kotlin/com/github/quillraven/fleks/benchmark/ashley.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.github.quillraven.fleks.benchmark | ||
|
||
import com.badlogic.ashley.core.* | ||
import com.badlogic.ashley.systems.IteratingSystem | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
data class AshleyPosition( | ||
var x: Float = 0f, | ||
var y: Float = 0f | ||
) : Component { | ||
companion object { | ||
val MAPPER: ComponentMapper<AshleyPosition> = ComponentMapper.getFor(AshleyPosition::class.java) | ||
} | ||
} | ||
|
||
data class AshleyLife( | ||
var life: Float = 0f | ||
) : Component | ||
|
||
data class AshleySprite( | ||
var path: String = "", | ||
var animationTime: Float = 0f | ||
) : Component { | ||
companion object { | ||
val MAPPER: ComponentMapper<AshleySprite> = ComponentMapper.getFor(AshleySprite::class.java) | ||
} | ||
} | ||
|
||
class AshleySystemSimple : IteratingSystem(Family.all(AshleyPosition::class.java).get()) { | ||
override fun processEntity(entity: Entity?, deltaTime: Float) { | ||
AshleyPosition.MAPPER.get(entity).x++ | ||
} | ||
} | ||
|
||
class AshleySystemComplex1 : IteratingSystem( | ||
Family | ||
.all(AshleyPosition::class.java) | ||
.exclude(AshleyLife::class.java) | ||
.one(AshleySprite::class.java) | ||
.get() | ||
) { | ||
private var processCalls = 0 | ||
|
||
override fun processEntity(entity: Entity?, deltaTime: Float) { | ||
if (processCalls % 2 == 0) { | ||
AshleyPosition.MAPPER.get(entity).x++ | ||
entity?.add(engine.createComponent(AshleyLife::class.java)) | ||
} else { | ||
entity?.remove(AshleyPosition::class.java) | ||
} | ||
AshleySprite.MAPPER.get(entity).animationTime++ | ||
++processCalls | ||
} | ||
} | ||
|
||
class AshleySystemComplex2 : IteratingSystem( | ||
Family | ||
.one(AshleyPosition::class.java, AshleyLife::class.java, AshleySprite::class.java) | ||
.get() | ||
) { | ||
override fun processEntity(entity: Entity?, deltaTime: Float) { | ||
entity?.remove(AshleyLife::class.java) | ||
entity?.add(engine.createComponent(AshleyPosition::class.java)) | ||
} | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
open class AshleyStateAddRemove { | ||
lateinit var engine: Engine | ||
|
||
@Setup(value = Level.Iteration) | ||
fun setup() { | ||
engine = Engine() | ||
engine.addSystem(AshleySystemSimple()) | ||
} | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
open class AshleyStateSimple { | ||
lateinit var engine: Engine | ||
|
||
@Setup(value = Level.Iteration) | ||
fun setup() { | ||
engine = Engine() | ||
engine.addSystem(AshleySystemSimple()) | ||
repeat(NUM_ENTITIES) { | ||
val cmp = engine.createComponent(AshleyPosition::class.java) | ||
val entity = engine.createEntity() | ||
entity.add(cmp) | ||
engine.addEntity(entity) | ||
} | ||
} | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
open class AshleyStateComplex { | ||
lateinit var engine: Engine | ||
|
||
@Setup(value = Level.Iteration) | ||
fun setup() { | ||
engine = Engine() | ||
engine.addSystem(AshleySystemComplex1()) | ||
engine.addSystem(AshleySystemComplex2()) | ||
repeat(NUM_ENTITIES) { | ||
val entity = engine.createEntity() | ||
entity.add(engine.createComponent(AshleyPosition::class.java)) | ||
entity.add(engine.createComponent(AshleySprite::class.java)) | ||
engine.addEntity(entity) | ||
} | ||
} | ||
} | ||
|
||
@Fork(1) | ||
@Warmup(iterations = WARMUPS) | ||
@Measurement(iterations = ITERATIONS, time = TIME, timeUnit = TimeUnit.SECONDS) | ||
open class AshleyBenchmark { | ||
@Benchmark | ||
fun addRemove(state: AshleyStateAddRemove) { | ||
repeat(NUM_ENTITIES) { | ||
val cmp = state.engine.createComponent(AshleyPosition::class.java) | ||
val entity = state.engine.createEntity() | ||
entity.add(cmp) | ||
state.engine.addEntity(entity) | ||
} | ||
state.engine.removeAllEntities() | ||
} | ||
|
||
@Benchmark | ||
fun simple(state: AshleyStateSimple) { | ||
repeat(WORLD_UPDATES) { state.engine.update(1f) } | ||
} | ||
|
||
@Benchmark | ||
fun complex(state: AshleyStateComplex) { | ||
repeat(WORLD_UPDATES) { state.engine.update(1f) } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
korge-fleks/src/benchmarks/kotlin/com/github/quillraven/fleks/benchmark/data.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.github.quillraven.fleks.benchmark | ||
|
||
const val NUM_ENTITIES = 10_000 | ||
const val WORLD_UPDATES = 1_000 | ||
const val WARMUPS = 3 | ||
const val ITERATIONS = 3 | ||
const val TIME = 3 // in seconds |
Oops, something went wrong.