-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
79 lines (69 loc) · 2.46 KB
/
Copy pathmain.lua
File metadata and controls
79 lines (69 loc) · 2.46 KB
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
local draw_utils = require("draw_utils")
local board = require("board")
local piece_positions = {}
local game = {
white_turn = true,
piece_selected = false
}
-- 0 == no piece selected
local selected_piece = {row = 0, col = 0}
function love.load()
pieces = board.pieces
sprites = board.piece_sprites
board_specs = board.specifications
piece_specs = board.piece_specs
bitboards = board.bitboards
board_state = board.board_state
find_nearest_piece = board.find_nearest_piece
piece_can_be_selected = board.piece_can_be_selected
switch_turn = board.switch_turn
move_is_possible = board.move_is_possible
-- Load piece sprites
for i = 1, #sprites do
sprites[i] = love.graphics.newImage(sprites[i])
end
end
function love.draw()
draw_utils.draw_board(love.graphics.newImage(board_specs.sprite), board_specs.x, board_specs.y)
draw_utils.draw_state(board_state, board_specs.x, board_specs.y, piece_specs.offset, board_specs.length, sprites, piece_positions)
-- Draw selection box
if game.piece_selected then
local box_x, box_y = draw_utils.select_box_pos(selected_piece, board_specs.length)
-- Black
love.graphics.setColor(0, 0, 0)
love.graphics.setLineWidth(3)
love.graphics.rectangle("line", box_x, box_y, board_specs.length / 8, board_specs.length / 8)
love.graphics.reset()
end
end
function love.mousepressed(x, y, button)
if button == 2 then
game.piece_selected = false
return
end
local piece_x, piece_y = find_nearest_piece(piece_positions, x, y)
local rowcol = piece_positions[piece_x][piece_y]
-- Selecting a piece
if not game.piece_selected then
local piece = board_state[rowcol.row][rowcol.col]
if not piece_can_be_selected(board_state, game.white_turn, piece) then
return
end
game.piece_selected = true
selected_piece.row = rowcol.row
selected_piece.col = rowcol.col
return
end
-- Deciding where to place the piece
if game.piece_selected then
local piece = board_state[selected_piece.row][selected_piece.col]
if not move_is_possible(board_state, selected_piece, rowcol) then
return
end
board_state[selected_piece.row][selected_piece.col] = 0
board_state[rowcol.row][rowcol.col] = piece
game.piece_selected = false
game.white_turn = switch_turn(game.white_turn)
return
end
end