Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Emit particles at initial Entity zIndex instead of zIndex 0 #1319

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ open class ParticleComponent(val emitter: ParticleEmitter) : Component() {

override fun onUpdate(tpf: Double) {
if (parent.world == null) {
parent.zIndex = entity.zIndex
entity.world.addEntity(parent)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB (almaslvl@gmail.com).
* See LICENSE for details.
*/
@file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE")
package com.almasb.fxgl.particle

import com.almasb.fxgl.entity.Entity
import com.almasb.fxgl.entity.GameWorld
import com.almasb.fxgl.entity.component.ComponentHelper
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

/**
*
* @author Jean-Rene Lavoie (jeanrlavoie@gmail.com)
*/
class ParticleComponentTest {

private lateinit var world: GameWorld
private lateinit var particle: ParticleComponent

@BeforeEach
fun setUp() {
world = GameWorld()
particle = ParticleComponent(ParticleEmitter())
}

@Test
fun `Create ParticleComponent with zIndex`() {
assertNull(particle.entity)
assertNotNull(particle.parent)
assertThat(particle.parent.zIndex, `is`(0))

val e = Entity()
e.zIndex = 100

ComponentHelper.setEntity(particle, e)
world.addEntity(e)
particle.onAdded()
particle.onUpdate(1.0)

assertThat(particle.parent.zIndex, `is`(100))

e.zIndex = 200
particle.onUpdate(1.0)

assertThat(particle.parent.zIndex, `is`(100))
Copy link
Owner

Choose a reason for hiding this comment

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

Shouldn't particle.parent.zIndex be 200 here, to match e.zIndex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The particle inherits the initial Entity zIndex. The second test is to validate that the particle zIndex is then independent which allows mutating particles independently after creation (moving some over, others under).
All particles are under the same parent, this allows full control over their display position after creation.

Copy link
Contributor Author

@DeathPhoenix22 DeathPhoenix22 Mar 21, 2024

Choose a reason for hiding this comment

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

Example: You have a Rain Emitter. The emitter is the "source" / "origin" spawning point. You spawn droplets and reduce their independent zIndex to emulate their position in a Parallax, for example. This means that droplets are able to show over a tree, but then it could show under the tree once it's zIndex reached the "behind the tree" value. Or even better, smoke particles generated in the air can get behind objects on the ground gradually.

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for the explanation. I quite like the idea of particles being under / over entities, though not sure we currently support that, given we only have 1 parent and its zIndex is fixed. Something for later discussion perhaps. As far as the issue is concerned, I think it's fixed.

}

}
Loading