Game.kt 1.42 KB
Newer Older
morkolai's avatar
morkolai committed
1
2
package com.mygdx.game.model

3
4
import com.badlogic.gdx.math.Vector3

morkolai's avatar
morkolai committed
5
6
//Logic of the game, holds a GameState, operations
//(((Controller???))) Model can change its own data, but has to be told by controller
morkolai's avatar
morkolai committed
7
8
9
10
11
12
13
14

class Game(gameState: GameState) {

    private var gameState: GameState

    init {
        this.gameState = gameState
    }
15
16
17
18

    fun getStartFieldCoordinates(): List<List<Vector3>>? {
        return this.gameState.getRules()?.generateStartFields()
    }
19
20
21
22
23
24
25
26
27
28
29
30
31
32

    fun fieldExists(cubeCoordinates: Vector3): Boolean {
        return this.gameState.getBoardState()?.fieldExists(cubeCoordinates) ?: false
    }

    fun fieldHasPiece(cubeCoordinates: Vector3): Boolean {
        return this.gameState.getBoardState()?.fields?.get(cubeCoordinates)?.hasPiece() == true
    }

    fun movePiece(fromCoordinates: Vector3, toCoordinates: Vector3): Boolean {
        // TODO: Check if valid move
        return this.gameState.getBoardState()?.movePiece(fromCoordinates, toCoordinates) ?: false
    }

33
34
35
36
37
38
39
40
41
42
43
44
    fun isPlayerFinished(player: Player): Boolean {
        val targetFields = this.gameState.getRules()?.getPlayerTargetFields(player.id)

        if (targetFields != null) {
            for (targetField: Vector3 in targetFields) {
                if (this.gameState.getBoardState()?.fields?.get(targetField)?.getPiece()?.getOwner()?.id != player.id) {
                    return false
                }
            }
        }
        return true
    }
morkolai's avatar
morkolai committed
45
}