Skip to content

Commit

Permalink
add blink effect when player takes damage
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven committed Mar 31, 2024
1 parent 0273a67 commit 28246aa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.quillraven.github.quillyjumper.component

import com.github.quillraven.fleks.Component
import com.github.quillraven.fleks.ComponentType

data class Blink(
var maxTime: Float,
val blinkRatio: Float, // amount in seconds after a blink is happening
var timer: Float = 0f,
) : Component<Blink> {
override fun type() = Blink

companion object : ComponentType<Blink>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class GameScreen(
add(StateSystem())
add(AnimationSystem())
add(CameraSystem())
add(BlinkSystem())
add(RenderSystem())
if (gameProperties.debugPhysic) {
add(PhysicRenderDebugSystem())
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.Blink
import com.quillraven.github.quillyjumper.component.Graphic

class BlinkSystem : IteratingSystem(family { all(Blink, Graphic) }) {

override fun onTickEntity(entity: Entity) {
val blinkCmp = entity[Blink]
val (maxTime, blinkRatio) = blinkCmp
val (sprite) = entity[Graphic]

if (maxTime <= 0f) {
// blinking is done -> remove it
sprite.setAlpha(1f)
entity.configure { it -= Blink }
return
}

blinkCmp.maxTime -= deltaTime
blinkCmp.timer += deltaTime
if (blinkCmp.timer >= blinkRatio) {
blinkCmp.timer = 0f
sprite.setAlpha(if (sprite.color.a == 0f) 1f else 0f)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import com.github.quillraven.fleks.World.Companion.family
import com.github.quillraven.fleks.World.Companion.inject
import com.quillraven.github.quillyjumper.SoundAsset
import com.quillraven.github.quillyjumper.audio.AudioService
import com.quillraven.github.quillyjumper.component.DamageTaken
import com.quillraven.github.quillyjumper.component.EntityTag
import com.quillraven.github.quillyjumper.component.Invulnerable
import com.quillraven.github.quillyjumper.component.Life
import com.quillraven.github.quillyjumper.component.*
import com.quillraven.github.quillyjumper.event.EntityDamageEvent
import com.quillraven.github.quillyjumper.event.GameEventDispatcher
import ktx.log.logger
Expand All @@ -28,7 +25,10 @@ class DamageSystem(

if (entity has EntityTag.PLAYER) {
// player becomes invulnerable after taking damage
entity.configure { it += Invulnerable(1.5f) }
entity.configure {
it += Invulnerable(1.5f)
it += Blink(maxTime = 1.5f, blinkRatio = 0.2f)
}
}
}

Expand Down

0 comments on commit 28246aa

Please sign in to comment.