Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Integrate Fleks ECS into korge + example #472

Merged
merged 28 commits into from
Mar 14, 2022

Conversation

jobe-m
Copy link
Contributor

@jobe-m jobe-m commented Mar 9, 2022

This PR integrates the Entity Component System Fleks as a sub-"module" korge-fleks into the Korge landscape. I have choosen that way because currently it is not possible to add Fleks as an (external) import to the example code.

The Fleks repo (https://github.com/Quillraven/Fleks) contains a branch "kmp" which in principle contains the same code as under "korge-fleks" in this PR. The kmp branch was pushed by me into Fleks. It contains all necessary changes to get the Fleks codebase usable with Korge (and Kotlin multiplatform in general - I guess). But Fleks is currently exported in a "multiplatform" incompatible way to maven and therefore it can not yet be used in a Korge project.

The Fleks repo and also "Korge-fleks" contains unit tests which were also adapted to the changes for mulitplatform.

The added example "fleks-ecs" shows the ECS system in action :) It creates a lot of objects which are drawn out from one sprite animation. I hope that it shows how to use Fleks in a game. When I have time I will update the example sources with more comments and descriptions. Maybe we can also create a section for Fleks on the Korge documentation pages.

Currently I only have one problem. It looks like that the sources from korge-fleks are not exported properly to maven-local. Thus I cannot use Fleks in my own game project. Maybe there is some configuration in gradle missing which would properly export Fleks from Korge-fleks into maven so that it can be used in stand alone projects by just adding Korge as a dependency. This is fixed.

Also I don't know if you want to keep Fleks as part of Korge for long time or if you would like to have it externalized (in its own origin git repo from Quillraven). The benefit of having Fleks in Korge would be that we could adapt, optimize and integrate it further to be a companion to Korge. E.g. writing Systems which are integrated into Korges animation/image views, etc. So that anybody who wants to use the ECS with Korge do not need to write an "Animation" or "Sprite system" by its own.

jobe-m added 18 commits March 9, 2022 13:52
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.
First version which works.
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.
The spawner should be able to spawn objects which can spawn objects
themselves. This is needed for the meteor object. It spawns fire trails.
- 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.
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.
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.
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.
@jobe-m jobe-m requested a review from soywiz March 9, 2022 14:45
@jobe-m jobe-m changed the title Feature/integrate fleks ecs into korge Integrate Fleks ECS into korge + example Mar 9, 2022
@soywiz
Copy link
Member

soywiz commented Mar 9, 2022

😲 Awesome!

I'll check in detail later this week, since this is pretty big! In the mean time, CI seems to be broken somehow:
https://github.com/korlibs/korge-next/runs/5481838669?check_suite_focus=true#step:6:228

Maybe it is using junit 5, while the korlibs are using kotlin/test with junit 4?

@Quillraven
Copy link

@soywiz I am using Kotlin test 1.6.0 dependency with the useJUnitPlatform configuration as seen in the build.gradle file: https://github.com/Quillraven/Fleks/blob/master/build.gradle.kts

I personally have no preference about which testing framework is used. So it can be changed to whatever is compatible with KorGE but do you know which adjustments need to be done in the Gradle file?

Also, I have almost no experience with KMP and I think @jobe-m has little Gradle experience. Do you know someone who could help with setting up the build file properly and the sourcesets to correctly publish it to maven?

I already commented it in the KMP issue of the repository but at the moment I have little time to get more familiar with the KMP specifics :(

@jobe-m
Copy link
Contributor Author

jobe-m commented Mar 10, 2022

Thanks for already taking a look at my PR guys :)
Lets keep fleks (multiplatform version) for now in Korge. Thus I will look into the unit tests and do changes to make them passing also in the Korge environment.
@soywiz please take your time, no hurry :)

@jobe-m
Copy link
Contributor Author

jobe-m commented Mar 13, 2022

I fixed some issues in the module tests. But it looks that there are still exceptions when running the tests for Kotlin native. I guess the problem is on the code side. Not in the unit tests. Because when I try to compile and run the fleks example on Kotlin native than running the kexe crashes.
I will take a look and try to find the problem.

EDIT: Ok, I guess, I found it. The problem is with the Inject singleton object which is immutable on Kotlin native when not defined as ThreadLocal. So, looks I will need to rewrite the injection a bit again ;-)

@jobe-m jobe-m marked this pull request as draft March 13, 2022 17:06
@soywiz
Copy link
Member

soywiz commented Mar 13, 2022

@jobe-m for InvalidMutability exception, you have to mark global variables with @ThreadLocal

@jobe-m jobe-m marked this pull request as ready for review March 13, 2022 18:47
if (other == null) return false
if (this === other) return true

other as BitArray
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail instead of returning false, in the case other is from another type, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check :)

Copy link
Member

@soywiz soywiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Really nice!

BTW. Is the PR ready to merge, or something is missing? (it is still in draft)

Could you move the ImageAnimation.Direction.ONCE_* functionality to another PR?
So in the next release appears as new functionality not tied only to fleks?

@jobe-m
Copy link
Contributor Author

jobe-m commented Mar 14, 2022

Could you move the ImageAnimation.Direction.ONCE_* functionality to another PR? So in the next release appears as new functionality not tied only to fleks?

Yes, good idea. I will do it, too.

EDIT: Created another PR #488

The equal function of BitArray was unsafe regarding comparing objects of
different type. Added unit tests to avoid regression.
@jobe-m
Copy link
Contributor Author

jobe-m commented Mar 14, 2022

YES :)
I got korge-fleks properly exported so that it can be used by an external project which includes Korge.
For that I needed to add a "supportFleks()" function into the korge-gradle-plugin.

@jobe-m jobe-m requested a review from soywiz March 14, 2022 12:56
@jobe-m jobe-m force-pushed the feature/Integrate_Fleks_ECS_into_Korge branch from 88eb177 to 1aaa952 Compare March 14, 2022 17:09
@jobe-m
Copy link
Contributor Author

jobe-m commented Mar 14, 2022

@soywiz the PR should be now ready to merge :)

@soywiz soywiz self-requested a review March 14, 2022 18:54
Copy link
Member

@soywiz soywiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

@jobe-m jobe-m merged commit 12ef256 into main Mar 14, 2022
@jobe-m jobe-m deleted the feature/Integrate_Fleks_ECS_into_Korge branch March 14, 2022 18:56
@jobe-m
Copy link
Contributor Author

jobe-m commented Mar 14, 2022

Wow, finally it's ready and usable from Korge. Thanks to @Quillraven for creating Fleks and sharing it with us all :)

@soywiz
Copy link
Member

soywiz commented Mar 14, 2022

Yeah +1. Thanks @Quillraven & thanks @jobe-m !

fleks

@soywiz
Copy link
Member

soywiz commented Mar 14, 2022

Just triggered 2.6.3 with this:

https://github.com/korlibs/korge-next/releases/tag/v2.6.3

@Quillraven
Copy link

That's really cool, thanks guys!

@jobe-m can you please create another PR for Fleks with the important changes? I think I will make a "next" branch for the next version that combines KMP and the JVM version. We can put your changes then already in this branch for now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants