Skip to content

juan-medina/kecs

Repository files navigation

Kotlin Entity Component System

Kotlin Cross-platform Entity Component System

KECS

License: Apache2 Docs Download Download

Platforms

Platform: JVM Platform: Linux x64 Platform: Windows x64 Platform: JS Platform: MAC

Info

KECS is a Cross-platform Entity Component System design to create concurrent applications, such games, more simple and without the need of using multiple threads neither fibers nor corutines.

It allows separating data from behavior and get rid of deep object oriented inheritance.

Due to data-oriented design allow modern processors to highly optimize it for an over perform of more traditional systems.

If you like to learn more about what is an ECS we try to give some clarification on this section.

Installation

Currently, KECS is available in jcenter, first we need to add the repositories, if we do not have them.

Add jcenter to gradle

repositories {
    jcenter()
}

Add jcenter to maven

<repositories>
    <repository>
        <id>jcenter</id>
        <name>bintray</name>
        <url>https://jcenter.bintray.com</url>
    </repository>
</repositories>

This will install the multi-platform module for all the platforms in your system.

Multi-platform Gradle project

If you are creating a multi-platform project you need to add to the platforms that you need

kotlin {
    sourceSets {
        jvmMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation 'com.juanmedina:kecs-jvm:1.0.2'
            }
        }
        linuxMain {
            dependencies {
                implementation 'com.juanmedina:kecs-linux:1.0.2'
            }
        }
        mingwMain {
            dependencies {
                implementation 'com.juanmedina:kecs-mingw:1.0.2'
            }
        }
    }
}

Single-platform Gradle project

For just adding as dependency for a simple platform you could do this in gradle:

dependencies {
    implementation 'com.juanmedina:kecs-jvm:1.0.2'
}

Single-platform Maven project

If you use maven you need to include the dependencies of the platforms that you target:

<dependency>
    <groupId>com.juanmedina</groupId>
    <artifactId>kecs-jvm</artifactId>
    <version>1.0.2</version>
</dependency>

Basic Usage

This is a basic example, check the user guide, the advance example, or the API Documentation for learning more about using KECS or try the Playground for practical experimentation.

data class Velocity(val x: Float, val y: Float)

data class Position(var x: Float, var y: Float) {
    operator fun plusAssign(velocity: Velocity) {
        x += velocity.x
        y += velocity.y
    }
}

class MoveSystem : System() {
    override fun update(delta: Float, total: Float, world: World) {
        world.pairs<Velocity, Position> { (vel, pos) ->
            pos += vel
        }
    }
}

fun example() {
    val world = world {
        +MoveSystem()
    }

    val ent1 = world.add {
        +Position(0.0f, 0.0f)
        +Velocity(1.0f, 2.0f)
    }

    val ent2 = world.add {
        +Position(0.0f, 0.0f)
        +Velocity(1.5f, 2.5f)
    }

    val ent3 = world.add {
        +Position(0.0f, 0.0f)
    }

    while(...) {
        world.update()
    }
}

License

    Copyright (C) 2020 Juan Medina

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.