-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameController.lua
129 lines (99 loc) · 3.08 KB
/
GameController.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
local math = require "math"
local RuleBook = require "RuleBook"
local TurnPanel = require "TurnPanel"
local WinnerPanel = require "WinnerPanel"
local GameController = {
boardView = nil,
selectedToken = nil,
gameState = nil,
turnPanel = nil,
winnerPanel = nil
}
local GameState = { GAME_IN_PROGRESS = "game_in_progress", GAME_OVER = "game_over"}
function GameController:new(boardView)
local newObj = {}
setmetatable( newObj, self )
self.__index = self
newObj.boardView = boardView
local RedPlayer = {
owner = "red",
firstMasterPromotionPlace = 322,
color = {1, 0, 0, 1}
}
local BluePlayer = {
owner = "blue",
firstMasterPromotionPlace = 322,
color = {0, 0, 1, 1}
}
RedPlayer.nextPlayer = BluePlayer
BluePlayer.nextPlayer = RedPlayer
newObj.gameState = {
playersList = {["red"] = RedPlayer, ["blue"] = BluePlayer},
currentPlayer = RedPlayer,
board = newObj.boardView.board,
promotionPlace = 1, -- To resolve tiebreaker
state = GameState.GAME_IN_PROGRESS
}
newObj.turnPanel = TurnPanel:new(newObj.gameState)
newObj.turnPanel:select(newObj.gameState.currentPlayer)
newObj.winnerPanel = WinnerPanel:new()
Runtime:addEventListener( "tap", newObj )
return self
end
function GameController:tap( event )
local localX, localY = self.boardView.group:contentToLocal(event.x, event.y)
cellX = math.floor(localX/cellWidth) + 1
cellY = math.floor(localY/cellHeight) + 1
local board = self.boardView.board
if self.gameState.state == GameState.GAME_IN_PROGRESS then
if cellX >= 1 and cellX <= board.width and
cellY >= 1 and cellY <= board.height then
if self.selectedToken ~= nil then
if RuleBook.isValidMove(self.gameState, self.selectedToken, cellX, cellY) then
RuleBook.performMove(self.gameState, self.selectedToken, cellX, cellY)
self:deselect()
if RuleBook.isGameOver(self.gameState) then
print("Game Over")
self.gameState.state = GameState.GAME_OVER
local winner = RuleBook.getWinner(self.gameState)
self.winnerPanel:show(self.gameState.playersList[winner])
else
self:nextPlayer()
end
return true
end
end
local token = board:getToken(cellX, cellY)
if token and token.owner == self.gameState.currentPlayer.owner then
if token ~= self.selectedToken then
self:deselect()
self:selectToken(token)
else
self:deselect()
end
end
return true
end
elseif self.gameState.state == GameState.GAME_OVER then
-- Do nothing
end
end
function GameController:nextPlayer()
self.gameState.currentPlayer = self.gameState.currentPlayer.nextPlayer
self.turnPanel:select(self.gameState.currentPlayer)
end
function GameController:selectToken(token)
self.selectedToken = token
token.isSelected = true
local listOfMoves = RuleBook.listValidMoves(self.boardView.board, token)
for _, pos in ipairs(listOfMoves) do
self.boardView:setHighlight(pos.x, pos.y, true)
end
end
function GameController:deselect()
if self.selectedToken == nil then return end
self.selectedToken.isSelected = false
self.selectedToken = nil
self.boardView:hideAllHighlights()
end
return GameController