something new

imgbot
MehdiAyadi 2 years ago
parent 6e16cefac6
commit 0707dd9077

@ -3,11 +3,14 @@ package com.last.commit
import GameState
import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.assets.AssetManager
import com.last.commit.audio.SoundEngine
import com.last.commit.config.GameSettings
import com.last.commit.config.TimeTravelAssetManager
import com.last.commit.inventory.Inventory
import com.last.commit.screen.*
import java.awt.AWTEventMulticaster
import java.sql.Time
/** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */
@ -23,10 +26,11 @@ class Game : Game() {
override fun create() {
createState()
createScreens()
changeScreen(Screens.GAME)
inputProcessor = GameInputProcessor(this)
Gdx.input.inputProcessor = inputProcessor
changeScreen(Screens.MAIN_MENU)
}
private fun createScreens() {
@ -37,20 +41,21 @@ class Game : Game() {
}
fun changeScreen(screen : Screens) {
println("changing screen to $screen")
when (screen) {
Screens.MAIN_MENU -> setScreen(menu)
Screens.SETTINGS -> setScreen(settings)
Screens.GAME -> setScreen(gameplay)
}
inputProcessor.activeScreen = getScreen() as TimeTravelScreen
}
fun createState() {
state = GameState(
Inventory(),
GameSettings(),
SoundEngine()
SoundEngine(),
TimeTravelAssetManager()
)
}
}

@ -6,19 +6,13 @@ import com.last.commit.screen.TimeTravelScreen
class GameInputProcessor(val game: Game, ) : InputProcessor{
val activeScreen = game.screen as TimeTravelScreen
lateinit var activeScreen: TimeTravelScreen
override fun keyUp(keycode: Int): Boolean {
//activeScreen.handleKeyInput(keycode)
return false
}
override fun keyTyped(character: Char): Boolean {
val characterUpperCase = character.uppercase()
val characterKey = Input.Keys.valueOf(characterUpperCase)
activeScreen.handleKeyInput(characterKey)
return false
}
@ -28,6 +22,9 @@ class GameInputProcessor(val game: Game, ) : InputProcessor{
}
override fun keyDown(keycode: Int): Boolean {
game.state.settings.getAction(keycode)?.let {
activeScreen.handleKeyInput(it)
}
return false
}

@ -1,10 +1,12 @@
import com.last.commit.inventory.Inventory
import com.last.commit.config.GameSettings
import com.last.commit.audio.SoundEngine
import com.last.commit.config.TimeTravelAssetManager
data class GameState(
val inventory: Inventory,
val settings: GameSettings,
val soundEngine: SoundEngine,
var mapDescription: String = "2020"
val assetManager: TimeTravelAssetManager,
var mapDescription: String = "2020",
)

@ -11,19 +11,29 @@ public class SoundEngine {
private val musicTracks: ThreadLocal<HashMap<String, Music>> =
ThreadLocal.withInitial() { HashMap() }
lateinit var backgroundMusic : Music
fun play(gameSound: GameSound, volume: Float = 1f) {
if (gameSound is GameSoundEffect) {
val sound = loadEffect(gameSound.name)
sound.play(volume)
println("Playing sound ${gameSound.name}")
} else if (gameSound is GameMusic) {
val music = loadMusic(gameSound.name)
music.volume = volume
music.setLooping(true)
music.play()
backgroundMusic = loadMusic(gameSound.name)
backgroundMusic.volume = volume
backgroundMusic.setLooping(true)
backgroundMusic.play()
}
}
fun stop() {
backgroundMusic.pause()
}
fun resume() {
backgroundMusic.play()
}
private fun loadEffect(name: String): Sound {
return loadSound("effects/$name")
}

@ -6,7 +6,7 @@ enum class ActionCommand {
UP, DOWN, LEFT, RIGHT,
//interaction
OPEN_INVENTORY, TIME_TRAVEL,
TIME_TRAVEL,
INTERACT,
//program interaction

@ -0,0 +1,22 @@
package com.last.commit.config
import com.badlogic.gdx.assets.AssetManager
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.scenes.scene2d.ui.Skin
class TimeTravelAssetManager : AssetManager() {
val UI_TEXTURE_PATH = "ui/uiskin.json"
init {
loadTextures()
}
fun loadTextures() {
load(UI_TEXTURE_PATH, Skin::class.java)
}
fun getUiTexture() : Skin {
return get(UI_TEXTURE_PATH)
}
}

@ -44,6 +44,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
val highlightColor = Color(0f, 0f, 1f, 0.5f)
var pause = true
var uiStage: UIStage
@ -61,38 +62,52 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
shapeRenderer.setAutoShapeType(true)
player.addItemToInventory("drill")
}
override fun handleKeyInput(keyCode: Int) {
gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.25f)
if (gameState.settings.getAction(keyCode) == ActionCommand.INTERACT) {
openDoor()
} else if (gameState.settings.getAction(keyCode) == ActionCommand.TIME_TRAVEL) {
map.teleport(player)
} else if (keyCode == Input.Keys.P) {
gameState.inventory.add("compass")
uiStage.refresh()
}
override fun handleKeyInput(action: ActionCommand) {
if (!pause) {
when (action) {
ActionCommand.INTERACT -> {
openDoor()
}
ActionCommand.TIME_TRAVEL -> {
map.teleport(player)
}
}
println(action)
println(gameState.settings.getKeyCode(ActionCommand.OPEN_MENU))
if (action == ActionCommand.OPEN_MENU) {
//Gdx.app.exit()
parent.changeScreen(Screens.MAIN_MENU)
}
}
if (gameState.settings.getAction(keyCode) == ActionCommand.OPEN_MENU) {
//Gdx.app.exit()
parent.changeScreen(Screens.MAIN_MENU)
//TODO
/*
else if (action == Input.Keys.P) {
gameState.inventory.add("compass")
inventoryStage.refresh()
}
*/
}
override fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) {
val mouseCoordinates: Vector2 = toWorldCoordinates(screenX.toFloat(), screenY.toFloat())
println("Mouse World coordinates is ${mouseCoordinates.x}:${mouseCoordinates.y}")
if (!pause) {
val playerDirection: Vector2 = player.getAbsoluteDirection()
println("Player interactor is ${playerDirection.x}:${playerDirection.y}")
map.interactWith(playerDirection.x, playerDirection.y, player.getCollider())
val mouseCoordinates: Vector2 = toWorldCoordinates(screenX.toFloat(), screenY.toFloat())
println("Mouse World coordinates is ${mouseCoordinates.x}:${mouseCoordinates.y}")
val playerDirection: Vector2 = player.getAbsoluteDirection()
println("Player interactor is ${playerDirection.x}:${playerDirection.y}")
map.interactWith(playerDirection.x, playerDirection.y, player.getCollider())
}
}
override fun show() {
// Prepare your screen here.
gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.25f)
resume()
}
fun loadGameConfig(): GameConfig {
@ -103,24 +118,25 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
}
override fun render(delta: Float) {
if (!pause) {
handleInput(delta)
handleMapBorderCollision()
handleInput()
handleMapBorderCollision()
val mousePosition: Vector2 = getMousePosition()
player.lookAt(mousePosition)
val mousePosition: Vector2 = getMousePosition()
player.lookAt(mousePosition)
batch.projectionMatrix = camera.combined
batch.begin()
this.map.render(batch, camera, delta)
this.player.render(batch)
batch.end()
batch.projectionMatrix = camera.combined
batch.begin()
this.map.render(batch, camera, delta)
this.player.render(batch)
batch.end()
val interactables = map.getInteractablesAt(player.getAbsoluteDirection())
renderInteractables(interactables)
val interactables = map.getInteractablesAt(player.getAbsoluteDirection())
renderInteractables(interactables)
uiStage.act(delta)
uiStage.draw()
uiStage.act(delta)
uiStage.draw()
}
}
fun renderInteractables(interactables: List<Interactable>) {
@ -156,7 +172,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
this.player.setPosition(playerX, playerY)
}
private fun handleInput() {
private fun handleInput(delta : Float) {
val horizontalMovement = Vector2()
if (isKeyPressed(gameState.settings.getKeyCode(ActionCommand.LEFT))) {
horizontalMovement.sub(Vector2.X)
@ -262,14 +278,19 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
}
override fun pause() {
// Invoked when your application is paused.
pause = true
gameState.soundEngine.stop()
}
override fun resume() {
pause = false
gameState.soundEngine.resume();
// Invoked when your application is resumed after pause.
}
override fun hide() {
pause()
}
override fun dispose() {

@ -1,37 +1,117 @@
package com.last.commit.screen
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener
import com.badlogic.gdx.utils.viewport.ScreenViewport
import com.last.commit.ColorState
import com.last.commit.Game
import com.last.commit.config.ActionCommand
class MainMenu(val parent: Game) : TimeTravelScreen() {
var open = true
override fun handleKeyInput(keyCode: Int) {
var stage: Stage
var table = Table()
val uiSkin: Skin
init {
parent.state.assetManager.finishLoading()
stage = Stage(ScreenViewport())
uiSkin = parent.state.assetManager.getUiTexture()
}
override fun handleKeyInput(action: ActionCommand) {
parent.changeScreen(Screens.GAME)
}
override fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) {
stage.touchDown(screenX, screenY, pointer, button)
stage.touchUp(screenX, screenY, pointer, button)
}
override fun show() {
open = true
stage.addActor(table);
val y = stage.viewport.screenHeight.toFloat() / 6
val x = stage.viewport.screenWidth.toFloat() / 4
println("x: $x, y: $y")
//add buttons to table
renderTable(x, y);
}
fun renderTable(x: Float, y: Float) {
table.reset()
table.setFillParent(true);
val newGame = TextButton("Play Game", uiSkin)
val preferences = TextButton("Preferences", uiSkin)
preferences.setSize(stage.viewport.screenWidth.toFloat(), stage.viewport.screenHeight.toFloat())
val exit = TextButton("Exit", uiSkin)
table.row().size(x, y);
table.add(newGame).fillX().uniformX()
table.row().pad(10F, 0F, 10F, 0F)
table.row().size(x, y);
table.add(preferences).fillX().uniformX()
table.row().size(x, y);
table.add(exit).fillX().uniformX()
exit.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
Gdx.app.exit()
}
})
newGame.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
parent.changeScreen(Screens.GAME)
}
})
preferences.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
parent.changeScreen(Screens.SETTINGS)
}
})
}
private var delta: Float = 0.0f
val state = ColorState()
override fun render(delta: Float) {
this.delta = delta
state.step((delta * 1000).toLong())
// Draw your screen here. "delta" is the time since last render in seconds.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
Gdx.gl.glClearColor(state.red, state.green, state.blue, 1f)
// tell our stage to do actions and draw itself
stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
stage.draw();
}
override fun resize(width: Int, height: Int) {
println("width $width, height $height")
stage.viewport.update(width, height, true);
val y = stage.viewport.screenHeight.toFloat() / 6
val x = stage.viewport.screenWidth.toFloat() / 4
renderTable(x, y)
}
override fun pause() {
@ -41,9 +121,9 @@ class MainMenu(val parent: Game) : TimeTravelScreen() {
}
override fun hide() {
open = false
}
override fun dispose() {
stage.dispose();
}
}

@ -5,11 +5,13 @@ import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.GL20
import com.last.commit.ColorState
import com.last.commit.Game
import com.last.commit.config.ActionCommand
class Settings (val parent: Game) : TimeTravelScreen() {
var open = true
override fun handleKeyInput(keyCode: Int) {
override fun handleKeyInput(action: ActionCommand) {
}
override fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) {

@ -1,9 +1,10 @@
package com.last.commit.screen
import com.badlogic.gdx.Screen
import com.last.commit.config.ActionCommand
abstract class TimeTravelScreen : Screen{
abstract fun handleKeyInput(keyCode : Int)
abstract fun handleKeyInput(action : ActionCommand)
abstract fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int)
}
Loading…
Cancel
Save