Skip to content

Commit

Permalink
A lot:
Browse files Browse the repository at this point in the history
 - Redesign Main Menu
 - Improve jump mechanics
 - Add utility functions
 - Other misc. changes
  • Loading branch information
srikavin committed Feb 9, 2020
1 parent 0e353b8 commit 2e53c41
Show file tree
Hide file tree
Showing 27 changed files with 661 additions and 146 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ project(":core") {
compile "io.github.libktx:ktx-scene2d:$ktxVersion"
compile "io.github.libktx:ktx-style:$ktxVersion"

compile "com.strongjoshua:libgdx-inGameConsole:1.0.0"

compile "net.onedaybeard.artemis:artemis-odb:2.2.0"

api "com.rafaskoberg.gdx:typing-label:1.1.0"
Expand Down
165 changes: 65 additions & 100 deletions core/src/me/srikavin/fbla/game/FBLAGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,34 @@ import com.artemis.WorldConfigurationBuilder
import com.artemis.managers.TagManager
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.assets.AssetManager
import com.badlogic.gdx.assets.loaders.FileHandleResolver
import com.badlogic.gdx.assets.loaders.SkinLoader
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader
import com.badlogic.gdx.maps.tiled.TiledMap
import com.badlogic.gdx.maps.tiled.TmxMapLoader
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.utils.ObjectMap
import com.badlogic.gdx.utils.viewport.ExtendViewport
import com.badlogic.gdx.utils.viewport.FitViewport
import ktx.actors.onClick
import com.strongjoshua.console.CommandExecutor
import com.strongjoshua.console.GUIConsole
import ktx.assets.disposeSafely
import me.srikavin.fbla.game.Scene.PLAYING
import me.srikavin.fbla.game.Scene.TITLE
import me.srikavin.fbla.game.ecs.system.*
import me.srikavin.fbla.game.map.MapLoader
import me.srikavin.fbla.game.physics.ContactListenerManager
import me.srikavin.fbla.game.ui.MainMenu

const val cameraScale = 45f

Expand All @@ -44,12 +43,15 @@ enum class Scene {
}

class FBLAGame : ApplicationAdapter() {
lateinit var camera: OrthographicCamera
lateinit var world: World
lateinit var batch: SpriteBatch
lateinit var skin: Skin
var assetManager = AssetManager()
lateinit var splashImage: Texture
private lateinit var camera: OrthographicCamera
private lateinit var world: World
private lateinit var batch: SpriteBatch
private lateinit var skin: Skin
private var assetManager = AssetManager()
private lateinit var splashImage: Texture
private lateinit var console: GUIConsole

private lateinit var mainMenuUI: MainMenu

private var scene: Scene = Scene.LOADING

Expand All @@ -61,7 +63,7 @@ class FBLAGame : ApplicationAdapter() {
camera.zoom = 1f
camera.update()
} else if (scene == TITLE) {
stageBg.viewport.update(width, height)
mainMenuUI.build()
}
}

Expand All @@ -70,7 +72,7 @@ class FBLAGame : ApplicationAdapter() {
camera.position.x = 0f
camera.position.y = cameraScale * (9f / 16f) * 0.75f

val physicsWorld = com.badlogic.gdx.physics.box2d.World(Vector2(0f, -23f), true)
val physicsWorld = com.badlogic.gdx.physics.box2d.World(Vector2(0f, -20f), true)

val stage = Stage(ExtendViewport(640f, 480f))
val root = Table(skin)
Expand Down Expand Up @@ -98,7 +100,7 @@ class FBLAGame : ApplicationAdapter() {
DialogueSystem(),
TriggerSystem(listenerManager),
UISystem(),
PhysicsDebugSystem(physicsWorld, debug = false)
PhysicsDebugSystem(physicsWorld, debug = true)
)
.with(TagManager())
.build()
Expand All @@ -114,12 +116,26 @@ class FBLAGame : ApplicationAdapter() {

world = World(config)
mapLoader.loadMap(world, "assets/maps/level1.tmx")
}

private lateinit var titleBg: TextureRegion
private lateinit var stageBg: Stage
console = GUIConsole()

@Suppress("unused")
console.setCommandExecutor(object : CommandExecutor() {
fun loadLevel(name: String) {
mapLoader.loadMap(world, "assets/maps/${name}.tmx")
}

fun debug(boolean: Boolean) {
world.getSystem(PhysicsDebugSystem::class.java).debug = boolean
}
})
console.displayKeyID = Input.Keys.ALT_RIGHT

}

override fun create() {
Colors.put("accent", Color.valueOf("#AA3E39"))

batch = SpriteBatch()
splashImage = Texture(Gdx.files.internal("assets/graphics/titlelogo.png"))

Expand All @@ -129,106 +145,54 @@ class FBLAGame : ApplicationAdapter() {
assetManager.setLoader(FreeTypeFontGenerator::class.java, FreeTypeFontGeneratorLoader(resolver))
assetManager.setLoader(BitmapFont::class.java, ".ttf", FreetypeFontLoader(resolver))

val parameter = FreetypeFontLoader.FreeTypeFontLoaderParameter()
parameter.fontParameters.size = 48
parameter.fontFileName = "assets/fonts/Kenney Pixel.ttf"
assetManager.load("KenneyPixel48.ttf", BitmapFont::class.java, parameter)
val font = assetManager.finishLoadingAsset<BitmapFont>("KenneyPixel48.ttf")

val fontMap = ObjectMap<String, Any>()
fontMap.put("KenneyPixel", font)

assetManager.load("assets/skin/skin.json", Skin::class.java, SkinLoader.SkinParameter(fontMap))
}

fun afterLoad() {
skin = assetManager.get<Skin>("assets/skin/skin.json")
skin.add("KenneyPixel", assetManager.get("KenneyPixel48.ttf"))
}

fun initTitleScreen() {
camera = OrthographicCamera(cameraScale, cameraScale * (9f / 16f))
titleBg = TextureRegion(Texture(Gdx.files.internal("assets/graphics/homescreen.png")))

stageBg = Stage(FitViewport(640f, 640f))
val table = Table(skin)
table.setFillParent(true)

table.background(TextureRegionDrawable(titleBg))
stageBg.addActor(table)

table.bottom()

val vertGroup = VerticalGroup()
vertGroup.padBottom(150f)
table.add(vertGroup)

vertGroup.space(3f)

val play = Button(skin, "green").apply {
add("Play")
pad(10f)
fun newCursor(name: String): Cursor {
val p = Pixmap(Gdx.files.internal("assets/cursors/$name.png"))
return Gdx.graphics.newCursor(p, 32, 32)
}

val instructions = Button(skin).apply {
add("Instructions").actor.setFontScale(0.75f)
pad(5f)
}
Gdx.graphics.setCursor(newCursor("cursor"))

val exit = Button(skin, "red").apply {
add("Quit").actor.setFontScale(0.7f)
pad(10f)
val parameter = FreetypeFontLoader.FreeTypeFontLoaderParameter().apply {
fontParameters.apply {
size = 22
fontFileName = "assets/fonts/font.ttf"
gamma = .8f
}
}

play.onClick {
startGame()
scene = PLAYING
assetManager.load("defaultFont.ttf", BitmapFont::class.java, parameter)
val font = assetManager.finishLoadingAsset<BitmapFont>("defaultFont.ttf")
font.data.apply {
markupEnabled = true
}

instructions.onClick {
val dialog = object : Dialog("Instructions", skin) {
override fun result(obj: Any?) {
this.remove()
}
}
dialog.titleLabel.setFillParent(true)
dialog.text(Label("\n" +
"Use the arrow keys to move.\n" +
"Use the mouse or displayed numerical key\nto choose options on screen.\n" +
"Use the escape key to quit.\n" +
"Collect coins to increase your score!",
skin, "black").apply {
setFontScale(.75f)
})

dialog.button("Close")
dialog.show(stageBg)
}
val fontMap = ObjectMap<String, Any>()
fontMap.put("defaultFont", font)

assetManager.load("assets/skin/skin.json", Skin::class.java, SkinLoader.SkinParameter(fontMap))
}

exit.onClick {
Gdx.app.exit()
fun afterLoad() {
skin = assetManager.get("assets/skin/skin.json")
mainMenuUI = MainMenu(skin) {
startGame()
mainMenuUI.disposeSafely()
scene = PLAYING
}

vertGroup.addActor(play)
vertGroup.addActor(instructions)
vertGroup.addActor(exit)
Gdx.input.inputProcessor = stageBg
scene = TITLE
}

override fun render() {
when (scene) {
PLAYING -> {
world.setDelta(Gdx.graphics.deltaTime)
world.process()
console.draw()
}
TITLE -> {
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
Gdx.gl.glClearColor(20f, 20f, 20f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT or GL20.GL_DEPTH_BUFFER_BIT)

stageBg.act()
stageBg.draw()
mainMenuUI.render()
}
Scene.LOADING -> {
// Render splash image
Expand All @@ -241,7 +205,8 @@ class FBLAGame : ApplicationAdapter() {
// Continue loading resources
if (assetManager.update()) {
afterLoad()
initTitleScreen()
mainMenuUI.build()
scene = TITLE
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions core/src/me/srikavin/fbla/game/KeyBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.badlogic.gdx.Input
/**
* List of all possible actions to do within the game
*/
enum class Actions {
enum class GameActions {
MOVE_LEFT,
MOVE_RIGHT,
JUMP,
Expand All @@ -16,14 +16,14 @@ enum class Actions {
/**
* A list of bindings between actions and Gdx [Input] keys
*/
class KeyBindings(val bindings: Map<Actions, Int>) {
class KeyBindings(val bindings: Map<GameActions, Int>) {
constructor() : this(
mapOf<Actions, Int>(
Pair(Actions.MOVE_LEFT, Input.Keys.LEFT),
Pair(Actions.MOVE_RIGHT, Input.Keys.RIGHT),
Pair(Actions.JUMP, Input.Keys.UP),
Pair(Actions.USE, Input.Keys.E),
Pair(Actions.QUIT, Input.Keys.ESCAPE)
mapOf<GameActions, Int>(
Pair(GameActions.MOVE_LEFT, Input.Keys.LEFT),
Pair(GameActions.MOVE_RIGHT, Input.Keys.RIGHT),
Pair(GameActions.JUMP, Input.Keys.UP),
Pair(GameActions.USE, Input.Keys.E),
Pair(GameActions.QUIT, Input.Keys.ESCAPE)
)
)
}
39 changes: 39 additions & 0 deletions core/src/me/srikavin/fbla/game/Util.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.srikavin.fbla.game

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.InputMultiplexer
import com.badlogic.gdx.InputProcessor
import com.badlogic.gdx.utils.Array
import me.srikavin.fbla.game.minigame.Minigame
import kotlin.reflect.KProperty
Expand Down Expand Up @@ -29,3 +32,39 @@ class MapTriggerDelegate(val name: String) {
mapTriggerProperties.properties.put(name, value)
}
}

fun unregisterInputHandler(processor: InputProcessor) {
val cur = Gdx.input.inputProcessor ?: return

if (cur == processor) {
Gdx.input.inputProcessor = null
}

if (cur is InputMultiplexer && cur.processors.items.contains(processor)) {
cur.removeProcessor(processor)
return
}
}

fun registerInputHandler(processor: InputProcessor) {
val cur = Gdx.input.inputProcessor

if (cur == null) {
Gdx.input.inputProcessor = InputMultiplexer(processor)
return
}

if (cur is InputMultiplexer) {
if (cur.processors.items.contains(processor)) {
// do nothing
return
}

cur.addProcessor(processor)
} else {
if (cur != processor) {
Gdx.input.inputProcessor = InputMultiplexer(cur, processor)
}
Gdx.input.inputProcessor = InputMultiplexer(cur)
}
}
9 changes: 9 additions & 0 deletions core/src/me/srikavin/fbla/game/dialogue/DialogueCallable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ abstract class DialogueCallable {
}
}

/**
* This function delays future calls to allow for speech-style dialogues.
*/
fun delay() {
runBlocking {
delay(750)
}
}

/**
* Sends a request to receive a response from the user based on the given list of choices. This call blocks until
* the user has chosen a response.
Expand Down
2 changes: 1 addition & 1 deletion core/src/me/srikavin/fbla/game/dialogue/DialogueManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DialogueManager(private val stage: Stage, skin: Skin) {
"meeting" to DialogueMeeting(),
"make_chapter" to DialogueMakeChapter(),
"job_interview" to DialogueJobInterview(),
"speech" to DialogueSpeech(),
"office" to DialogueSpeech(),
"letter_rec" to DialogueLetterRec(),
"fbla_knowledge" to QuizFBLAKnowledge(),
"animal" to DialogueAnimal(),
Expand Down
Loading

0 comments on commit 2e53c41

Please sign in to comment.