Skip to content

Commit

Permalink
feat: added video cutscenes, closes #1120
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Jul 15, 2023
1 parent 67e7db4 commit cb70500
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions fxgl-gameplay/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
requires com.almasb.fxgl.core;
requires com.almasb.fxgl.scene;
requires javafx.controls;
requires javafx.media;
requires com.fasterxml.jackson.annotation;

exports com.almasb.fxgl.achievement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.almasb.fxgl.cutscene.dialogue.DialogueScene
import com.almasb.fxgl.cutscene.dialogue.FunctionCallHandler
import com.almasb.fxgl.logging.Logger
import com.almasb.fxgl.scene.SceneService
import javafx.scene.media.MediaView

/**
*
Expand All @@ -31,7 +32,8 @@ class CutsceneService : EngineService() {
private var gameVars: PropertyMap? = null

private val scene by lazy { CutsceneScene(sceneService) }
val dialogueScene by lazy { DialogueScene(sceneService) }
private val dialogueScene by lazy { DialogueScene(sceneService) }
private val videoScene by lazy { VideoScene(sceneService) }

@JvmOverloads fun startCutscene(cutscene: Cutscene, onFinished: Runnable = EmptyRunnable) {
scene.assetLoader = assetLoader
Expand All @@ -50,6 +52,10 @@ class CutsceneService : EngineService() {
dialogueScene.start(dialogueGraph, context, functionHandler, onFinished)
}

@JvmOverloads fun startVideoCutscene(video: MediaView, onFinished: Runnable = EmptyRunnable) {
videoScene.start(video, onFinished)
}

override fun onGameReady(vars: PropertyMap) {
gameVars = vars
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB ([email protected]).
* See LICENSE for details.
*/

package com.almasb.fxgl.cutscene

import com.almasb.fxgl.input.UserAction
import com.almasb.fxgl.input.view.KeyView
import com.almasb.fxgl.scene.SceneService
import com.almasb.fxgl.scene.SubScene
import javafx.scene.input.KeyCode
import javafx.scene.media.MediaView
import javafx.scene.paint.Color

/**
*
* @author Almas Baimagambetov ([email protected])
*/
class VideoScene(private val sceneService: SceneService) : SubScene() {

private lateinit var video: MediaView
private lateinit var onFinished: Runnable

init {
input.addAction(object : UserAction("Skip Cutscene") {
override fun onActionBegin() {
endScene()
}
}, KeyCode.ENTER)

val keyView = KeyView(KeyCode.ENTER, Color.GREENYELLOW, 18.0)
keyView.translateX = sceneService.prefWidth - 80.0
keyView.translateY = sceneService.prefHeight - 40.0

contentRoot.children += keyView
}

private fun endScene() {
contentRoot.children.removeAt(0)

video.mediaPlayer.stop()
sceneService.popSubScene()
onFinished.run()
}

fun start(video: MediaView, onFinished: Runnable) {
this.video = video
this.onFinished = onFinished

video.mediaPlayer.play()
video.mediaPlayer.setOnEndOfMedia { endScene() }

contentRoot.children.add(0, video)

sceneService.pushSubScene(this)
}
}

0 comments on commit cb70500

Please sign in to comment.