Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed assets/abilities/explosion-card-temp.png
Binary file not shown.
Binary file removed assets/abilities/explosion-card.png
Binary file not shown.
Binary file removed assets/abilities/shield-card.png
Binary file not shown.
Binary file removed assets/abilities/swap-card.png
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ class AbilityItemFactory(
when (abilityType) {
AbilityType.SHIELD -> TextureRegion(assetManager.get("abilities/cards/shieldCard.png", Texture::class.java))
AbilityType.EXPLOSION -> TextureRegion(assetManager.get("abilities/cards/explosionCard.png", Texture::class.java))
// TODO: update all cards
AbilityType.SWAP -> TextureRegion(assetManager.get("abilities/swap-card.png", Texture::class.java))
AbilityType.SWAP -> TextureRegion(assetManager.get("abilities/cards/swapCard.png", Texture::class.java))
AbilityType.MIRROR -> TextureRegion(assetManager.get("abilities/cards/mirrorCard.png", Texture::class.java))
AbilityType.NEW_MOVEMENT -> TextureRegion(assetManager.get("abilities/new_movement-card.png", Texture::class.java))
AbilityType.NEW_MOVEMENT -> TextureRegion(assetManager.get("abilities/cards/new_movementCard.png", Texture::class.java))
}

fun createAbilityItem(abilityType: AbilityType): Entity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,14 @@ class PieceFactory(
}

fun createPawn(
isPlayerOne: Boolean,
position: Position,
color: PlayerColor,
stage: Stage,
) = createPiece(position, PieceType.PAWN, color, stage).apply {
getComponent(MovementRuleComponent::class.java).apply {
val pawnDirections: List<Vector2>
val pawnCaptureDirections: List<Vector2>
val pawnStartDirections: List<Vector2>
if (isPlayerOne) {
pawnDirections = listOf(Vector2(0f, 1f))
pawnCaptureDirections = listOf(Vector2(1f, 1f), Vector2(-1f, 1f))
pawnStartDirections = listOf(Vector2(0f, 2f))
} else {
pawnDirections = listOf(Vector2(0f, -1f))
pawnCaptureDirections = listOf(Vector2(1f, -1f), Vector2(-1f, -1f))
pawnStartDirections = listOf(Vector2(0f, -2f))
}
val pawnDirections: List<Vector2> = listOf(Vector2(0f, 1f))
val pawnCaptureDirections: List<Vector2> = listOf(Vector2(1f, 1f), Vector2(-1f, 1f))
val pawnStartDirections: List<Vector2> = listOf(Vector2(0f, 2f))

addPattern(
MovementPattern(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.badlogic.ashley.core.Engine
import com.badlogic.ashley.core.Family
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.scenes.scene2d.Stage
import io.github.chessevolved.components.ActorComponent
import io.github.chessevolved.components.PieceTypeComponent
import io.github.chessevolved.components.PlayerColorComponent
import io.github.chessevolved.components.PositionComponent
Expand Down Expand Up @@ -34,8 +35,14 @@ object EcsEntityMapper {
fun extractStateFromEngine(engine: Engine): Pair<List<PieceDto>, List<BoardSquareDto>> {
val pieces =
engine.getEntitiesFor(pieceFamily).map { entity ->
val piecePosition = PositionComponent.mapper.get(entity)

PieceDto(
position = PositionComponent.mapper.get(entity).position,
position =
Position(
piecePosition.position.x,
GameSettings.getBoardSize() - 1 - piecePosition.position.y,
),
type = PieceTypeComponent.mapper.get(entity).type,
color = PlayerColorComponent.mapper.get(entity).color,
)
Expand All @@ -60,6 +67,7 @@ object EcsEntityMapper {
receivedBoardSquares: List<BoardSquareDto>,
) {
Gdx.app.log("ECSEntityMapper", "Applying received state to engine...")

try {
val existingPieceEntities = engine.getEntitiesFor(pieceFamily)
val existingPiecesMap =
Expand Down Expand Up @@ -91,7 +99,10 @@ object EcsEntityMapper {
val pos = PositionComponent.mapper.get(existingEntity).position
if (pos !in currentPositions) {
Gdx.app.debug("ECSEntityMapper", "Removing piece no longer in state at $pos")
val actorComponent = ActorComponent.mapper.get(existingEntity)
actorComponent.actor.remove()
engine.removeEntity(existingEntity)
println("ActorComp: $actorComponent")
}
}

Expand Down Expand Up @@ -130,7 +141,7 @@ object EcsEntityMapper {
pieceData: PieceDto,
) {
when (pieceData.type) {
PieceType.PAWN -> pieceFactory.createPawn(true, pieceData.position, pieceData.color, stage)
PieceType.PAWN -> pieceFactory.createPawn(pieceData.position, pieceData.color, stage)
PieceType.ROOK -> pieceFactory.createRook(pieceData.position, pieceData.color, stage)
PieceType.KNIGHT -> pieceFactory.createKnight(pieceData.position, pieceData.color, stage)
PieceType.BISHOP -> pieceFactory.createBishop(pieceData.position, pieceData.color, stage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object Game {
try {
SupabaseGameHandler.joinGame(gameId, ::onGameRowUpdate)
this.inGame = true
this.currentTurn = PlayerColor.WHITE
} catch (e: Exception) {
throw Exception("Problem with joining game: " + e.message)
}
Expand All @@ -36,6 +37,11 @@ object Game {
}
}

suspend fun deleteGame() {
leaveGame()
SupabaseGameHandler.deleteGameRow(Lobby.getLobbyId()!!)
}

suspend fun askForRematch() {
if (!isInGame() && !hasAskedForRematch) {
throw IllegalStateException("Can't ask for rematch if not in a game or have already asked for rematch!")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.github.chessevolved.singletons

import io.github.chessevolved.dtos.SettingsDto
import io.github.chessevolved.enums.PlayerColor

object GameSettings {
private var fogOfWar: Boolean = false
private var boardSize: Int = 8
var clientPlayerColor: PlayerColor = PlayerColor.WHITE
var opponentPlayerColor: PlayerColor = PlayerColor.BLACK
var isSecondPlayer: Boolean = false

/**
* The current setting of FOW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.chessevolved.singletons

import com.badlogic.gdx.Gdx
import io.github.chessevolved.dtos.LobbyDto
import io.github.chessevolved.enums.PlayerColor
import io.github.chessevolved.singletons.supabase.SupabaseLobbyHandler

object Lobby {
Expand All @@ -19,6 +20,9 @@ object Lobby {
try {
SupabaseLobbyHandler.joinLobby(lobbyId, ::onLobbyRowUpdate)
this.lobbyId = lobbyId
GameSettings.clientPlayerColor = PlayerColor.BLACK
GameSettings.opponentPlayerColor = PlayerColor.WHITE
GameSettings.isSecondPlayer = true
} catch (e: Exception) {
throw e
}
Expand All @@ -32,6 +36,7 @@ object Lobby {
try {
SupabaseLobbyHandler.leaveLobbyNoUpdateSecondPlayer(lobbyId!!)
SupabaseLobbyHandler.joinLobbyNoUpdateSecondPlayer(lobbyId!!, ::onLobbyRowUpdate)
onLobbyRowUpdate(getLobby())
} catch (e: Exception) {
Gdx.app.error("Lobby", "Error when joining rematch lobby: " + e.message)
}
Expand All @@ -44,6 +49,7 @@ object Lobby {
try {
SupabaseLobbyHandler.leaveLobbyNoUpdateSecondPlayer(lobbyId!!)
SupabaseLobbyHandler.joinLobby(lobbyId!!, ::onLobbyRowUpdate)
onLobbyRowUpdate(getLobby())
} catch (e: Exception) {
throw e
}
Expand All @@ -54,6 +60,10 @@ object Lobby {
val lobbyId = SupabaseLobbyHandler.createLobby(::onLobbyRowUpdate)
this.lobbyId = lobbyId
Gdx.app.log("Lobby", "Creating lobby with ID: $lobbyId...")
GameSettings.clientPlayerColor = PlayerColor.WHITE
GameSettings.opponentPlayerColor = PlayerColor.BLACK
GameSettings.isSecondPlayer = true
println("Player Color: ${GameSettings.clientPlayerColor}")
} catch (e: Exception) {
throw Exception("Problem when creating lobby! " + e.message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.serialization.json.Json
object SupabaseGameHandler {
private val supabase = getSupabaseClient()
private const val SUPABASE_GAME_TABLE_NAME = "games"
var sendingGameState = false

suspend fun joinGame(
lobbyCode: String,
Expand Down Expand Up @@ -76,6 +77,18 @@ object SupabaseGameHandler {
SupabaseChannelManager.unsubscribeFromChannel("game_$lobbyCode")
}

suspend fun deleteGameRow(lobbyCode: String) {
try {
supabase.from(SUPABASE_GAME_TABLE_NAME).delete {
filter {
eq("lobby_code", lobbyCode)
}
}
} catch (e: PostgrestRestException) {
throw e
}
}

suspend fun requestRematch(lobbyCode: String) {
try {
val response =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import io.github.chessevolved.components.SelectionComponent
import io.github.chessevolved.components.WeatherEventComponent
import io.github.chessevolved.data.Position
import io.github.chessevolved.singletons.EcsEngine
import io.github.chessevolved.singletons.Game
import io.github.chessevolved.singletons.GameSettings

class InputSystem :
IteratingSystem(
Expand Down Expand Up @@ -98,6 +100,9 @@ class InputSystem :

class InputService {
fun clickPieceAtPosition(position: Position) {
println("piece clicked")
if (Game.getCurrentTurn() != GameSettings.clientPlayerColor) return

val entity =
EcsEngine
.getEntitiesFor(Family.all(PieceTypeComponent::class.java).get())
Expand All @@ -107,6 +112,7 @@ class InputService {
}

fun clickBoardSquareAtPosition(position: Position) {
println("boardsquare clicked")
val entity =
EcsEngine
.getEntitiesFor(Family.all(WeatherEventComponent::class.java).get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.github.chessevolved.components.PositionComponent
import io.github.chessevolved.components.ValidMovesComponent
import io.github.chessevolved.components.WeatherEventComponent
import io.github.chessevolved.singletons.EcsEngine
import io.github.chessevolved.singletons.GameSettings

class SelectionEntityListener(
private val boardSize: Int,
Expand All @@ -35,6 +36,10 @@ class SelectionEntityListener(
piece.remove(CanBeCapturedComponent::class.java)
}

if (PlayerColorComponent.mapper.get(entity).color != GameSettings.clientPlayerColor) {
return
}

val availablePositions =
moveValidator.checkAvailablePositions(
PlayerColorComponent.mapper.get(entity).color,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ class Navigator(
}

fun goBack() {
print("${PresenterManager.getCurrent()} before")
if (!PresenterManager.isEmpty()) {
PresenterManager.pop()
}
if (PresenterManager.isEmpty()) {
PresenterManager.push(createMenuPresenter())
}
print("${PresenterManager.getCurrent()} after")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class EndGamePresenter(
private var otherPlayerLeft = false

init {
println("${Game.isInGame()} init")
Game.subscribeToGameUpdates(this.toString(), ::onGameUpdate)
endGameView.endGameStatus = endGameStatus
endGameView.init()
Expand All @@ -27,6 +28,7 @@ class EndGamePresenter(
}

private fun requestRematch() {
println("${Game.isInGame()} rematch")
endGameView.disableRematchButton()
endGameView.updateRematchText("Rematch request\nsent...")
runBlocking {
Expand All @@ -37,6 +39,7 @@ class EndGamePresenter(
}

private fun returnToMenu() {
println("${Game.isInGame()} returntomenu")
runBlocking {
launch {
val wantsRematch = Game.getWantsRematch()
Expand Down Expand Up @@ -66,18 +69,27 @@ class EndGamePresenter(
}

override fun dispose() {
println("${Game.isInGame()} dispose") // Denne e false.... Noe leavea gamen alt for tidlig
endGameView.dispose()
Game.unsubscribeFromGameUpdates(this.toString())

// Both isInGame and isInLobby must be checked here
// game and lobby is being left somewhere else leading to a fatal error if not checked
runBlocking {
launch {
val wantsRematch = Game.getWantsRematch()
Game.leaveGame()
if (Game.isInGame()) {
Game.leaveGame()
}

// TODO: This logic doesn't work as of yet. In further iterations, this would be priority.
if (wantsRematch && !otherPlayerLeft) {
Lobby.leaveLobbyWithoutUpdating()
// TODO: Need to discern between a player that leaves the rematch-screen, and a player that goes from rematch-screen to a lobby.
// Lobby.leaveLobbyWithoutUpdating()
} else {
Lobby.leaveLobby()
if (Lobby.isInLobby()) {
// Lobby.leaveLobby()
}
}
}
}
Expand All @@ -88,6 +100,7 @@ class EndGamePresenter(
}

fun onGameUpdate(updatedGame: GameDto) {
println("${Game.isInGame()} gameupdate")
if (updatedGame.wantRematch) {
if (!Game.getWantsRematch()) {
otherPlayerHasAskedForRematch = true
Expand All @@ -109,7 +122,7 @@ class EndGamePresenter(
} else {
runBlocking {
launch {
Game.leaveGame()
Game.deleteGame()
Lobby.joinRematchLobbyAsHost()
}
}
Expand Down
Loading