Skip to content

Commit

Permalink
add flash shader when player is hurt
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven committed Apr 1, 2024
1 parent a68d255 commit 7c29ad3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.quillraven.github.quillyjumper.component

import com.badlogic.gdx.graphics.Color
import com.github.quillraven.fleks.Component
import com.github.quillraven.fleks.ComponentType

data class Flash(
val color: Color,
var weight: Float,
var amount: Int,
val delay: Float,
var delayTimer: Float = delay,
var doFlash: Boolean = true,
) : Component<Flash> {
override fun type() = Flash

companion object : ComponentType<Flash>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class GameScreen(
add(AnimationSystem())
add(CameraSystem())
add(BlinkSystem())
add(FlashSystem())
add(RenderSystem())
if (gameProperties.debugPhysic) {
add(PhysicRenderDebugSystem())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quillraven.github.quillyjumper.system

import com.badlogic.gdx.graphics.Color
import com.github.quillraven.fleks.Entity
import com.github.quillraven.fleks.IteratingSystem
import com.github.quillraven.fleks.World.Companion.family
Expand Down Expand Up @@ -27,7 +28,8 @@ class DamageSystem(
// player becomes invulnerable after taking damage
entity.configure {
it += Invulnerable(1.5f)
it += Blink(maxTime = 1.5f, blinkRatio = 0.2f)
it += Blink(maxTime = 1.5f, blinkRatio = 0.1f)
it += Flash(color = Color.RED, weight = 0.75f, amount = 1, delay = 0.15f)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.quillraven.github.quillyjumper.system

import com.github.quillraven.fleks.Entity
import com.github.quillraven.fleks.IteratingSystem
import com.github.quillraven.fleks.World.Companion.family
import com.quillraven.github.quillyjumper.component.Flash
import com.quillraven.github.quillyjumper.component.Graphic

class FlashSystem : IteratingSystem(family { all(Flash, Graphic) }) {

override fun onTickEntity(entity: Entity) {
val flashCmp = entity[Flash]
val (_, _, amount, delay, delayTimer) = flashCmp

if (amount <= 0) {
// flash is done -> remove it
entity.configure { it -= Flash }
return
}

if (delayTimer <= 0f) {
if (flashCmp.doFlash) {
flashCmp.amount--
}
flashCmp.delayTimer = delay
flashCmp.doFlash = !flashCmp.doFlash
}
flashCmp.delayTimer -= deltaTime
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import com.github.quillraven.fleks.IntervalSystem
import com.github.quillraven.fleks.World.Companion.family
import com.github.quillraven.fleks.World.Companion.inject
import com.github.quillraven.fleks.collection.compareEntityBy
import com.quillraven.github.quillyjumper.Assets
import com.quillraven.github.quillyjumper.Quillyjumper
import com.quillraven.github.quillyjumper.ShaderAsset
import com.quillraven.github.quillyjumper.component.EntityTag.BACKGROUND
import com.quillraven.github.quillyjumper.component.EntityTag.FOREGROUND
import com.quillraven.github.quillyjumper.component.Flash
import com.quillraven.github.quillyjumper.component.Graphic
import com.quillraven.github.quillyjumper.event.GameEvent
import com.quillraven.github.quillyjumper.event.GameEventListener
import com.quillraven.github.quillyjumper.event.MapChangeEvent
import com.quillraven.github.quillyjumper.tiled.TiledService.Companion.isObjectsLayer
import ktx.assets.disposeSafely
import ktx.graphics.use
import ktx.tiled.use

class RenderSystem(
Expand All @@ -29,6 +33,7 @@ class RenderSystem(
private val uiViewport: Viewport = inject("uiViewport"),
private val stage: Stage = inject(),
private val gameCamera: OrthographicCamera = inject(),
assets: Assets = inject()
) : IntervalSystem(), GameEventListener {

private val mapRenderer = OrthogonalTiledMapRenderer(null, Quillyjumper.UNIT_SCALE, batch)
Expand All @@ -40,6 +45,10 @@ class RenderSystem(
private val entities = family { all(Graphic).none(BACKGROUND, FOREGROUND) }
private val fgdEntities = family { all(Graphic, FOREGROUND) }

private val flashShader = assets[ShaderAsset.FLASH]
private val uLocFlashColor = flashShader.getUniformLocation("u_FlashColor")
private val uLocFlashWeight = flashShader.getUniformLocation("u_FlashWeight")

override fun onTick() {
// game rendering
gameViewport.apply()
Expand All @@ -64,7 +73,30 @@ class RenderSystem(

private fun Family.renderEntities() {
sort(entityComparator)
forEach { it[Graphic].sprite.draw(batch) }
forEach { entity ->
val flashCmp = entity.getOrNull(Flash)
if (flashCmp != null && flashCmp.doFlash) {
if (batch.shader != flashShader) {
batch.shader = flashShader
}
flashShader.use {
flashShader.setUniformf(uLocFlashColor, flashCmp.color)
flashShader.setUniformf(uLocFlashWeight, flashCmp.weight)
}
} else {
batch.resetShader()
}

entity[Graphic].sprite.draw(batch)
}

batch.resetShader()
}

private fun Batch.resetShader() {
if (shader != null) {
shader = null
}
}

override fun onEvent(event: GameEvent) {
Expand Down
8 changes: 4 additions & 4 deletions tiled-project.tiled-session
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
},
"activeFile": "assets/maps/objects.tsx",
"expandedProjectPaths": [
"assets",
"lwjgl3/build",
"assets/maps",
".",
"lwjgl3/build"
"assets"
],
"fileStates": {
"": {
Expand Down Expand Up @@ -49,8 +49,8 @@
"scale": 11,
"selectedLayer": 3,
"viewCenter": {
"x": 165,
"y": 93.22727272727273
"x": 348.54545454545456,
"y": 101.5909090909091
}
}
},
Expand Down

0 comments on commit 7c29ad3

Please sign in to comment.