forked from espenia/LOVEChessLua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.lua
186 lines (157 loc) · 4.97 KB
/
piece.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
Piece = Object:extend()
require "square"
function Piece:new(color, x, y, gridSize, xOffset, yOffset, posX, posY)
self.ratio = 0.9 -- img has 90% width of grid width
self.gridSize = gridSize
self.heightScale = gridSize * self.ratio / self.image:getHeight()
self.widthScale = self.heightScale -- squared
self.width = self.image:getWidth() * self.widthScale
self.height = self.image:getHeight() * self.heightScale
self.xOffset = xOffset
self.yOffset = yOffset
self.x = x + self.xOffset + (self.gridSize - self.width) / 2
self.y = y + self.yOffset + (self.gridSize - self.height) / 2
self.clicked = false
self.color = color
self.drawOffset = 5
self.actualPos = Square()
self.actualPos:set(posX, posY)
self.captured = false
self.firstMove = true
self.castlingInProcess = false
end
function Piece:getColor()
return self.color
end
function Piece:toBeCaptured(toBeCaptured)
self.captured = toBeCaptured
end
function Piece:isCaptured()
return self.captured
end
-- function Piece:setColor(i)
-- if i == 0 then
-- self.color = "w"
-- else
-- self.color = "b"
-- end
-- end
function Piece:move(xd, yd)
self.x = xd * self.gridSize + self.xOffset + (self.gridSize - self.width) / 2
self.y = yd * self.gridSize + self.yOffset + (self.gridSize - self.height) / 2
self.actualPos:set(xd,yd)
end
function Piece:update(dt)
self:isPressed()
self:drag()
end
function Piece:getChessPos()
local x = math.floor((self.x - self.xOffset) / self.gridSize)
local y = math.floor((self.y - self.yOffset) / self.gridSize)
return x, y
end
function Piece:updateOnResize(xOffset, yOffset, gridSize)
local x, y = self.actualPos:get()
local ratio = 0.9
self.xOffset = xOffset
self.yOffset = yOffset
self.gridSize = gridSize
self.heightScale = gridSize * ratio / self.image:getHeight()
self.widthScale = self.heightScale
self.width = self.image:getWidth() * self.widthScale
self.height = self.image:getHeight() * self.heightScale
self.x = x * gridSize + xOffset + (gridSize - self.width) / 2
self.y = y * gridSize + yOffset + (gridSize - self.height) / 2
end
function Piece:getName()
return self.name
end
function Piece:keyboardMove()
if love.keyboard.isDown("left") then
self.x = self.x - self.speed * dt
elseif love.keyboard.isDown("right") then
self.x = self.x + self.speed * dt
end
end
function Piece:draw()
local lift = 0
if self.clicked then lift = self.drawOffset end
love.graphics.draw(self.image, self.x, self.y - lift, 0, self.widthScale, self.heightScale)
end
function Piece:isPressed()
local delta = self.gridSize / 3.3
if love.mouse.isDown(1) then
local x, y = love.mouse.getPosition()
if (x > self.x + self.width / 2 - delta) and
(x < self.x + self.width / 2 + delta) and
(y > self.y + self.height / 2 - delta) and
(y < self.y + self.height / 2 + delta) then
self.clicked = true
end
else
self.clicked = false
end
end
function Piece:drag()
local x,y = 0,0
if self.clicked then
x, y = love.mouse.getPosition()
--if self:mouseOnBoard(x, y) then
x = math.floor((x - self.xOffset) / self.gridSize) * self.gridSize
self.x = x + self.xOffset + (self.gridSize - self.width) / 2
y = math.floor((y - self.yOffset) / self.gridSize) * self.gridSize
self.y = y + self.yOffset + (self.gridSize - self.height) / 2
--end
end
end
function Piece:mouseOnBoard(x, y)
local xAxisOnBoard = x > self.xOffset and x < self.xOffset + 8 * self.gridSize
local yAxisOnBoard = y > self.yOffset and y < self.yOffset + 8 * self.gridSize
return xAxisOnBoard and yAxisOnBoard
end
function Piece:setColor(i)
if i == 0 then
self.color = "w"
else
self.color = "b"
end
end
function Piece:updatePos(lastMove)
xf,yf = lastMove:getEnd()
self.actualPos:set(xf,yf)
self.firstMove = false
end
function Piece:getColor()
return self.color
end
function Piece:checkPos(colorf, xf, yf)
myX, myY = self.actualPos:get();
if myX == xf and myY == yf and self.color == colorf then
return true
else
return false
end
end
function Piece:checkCoordinates(xf, yf)
myX, myY = self.actualPos:get();
if myX == xf and myY == yf then
return true
else
return false
end
end
function Piece:getActualPos()
return self.actualPos:get()
end
function Piece:canCapture(movement)
return self:validateMovement(movement)
end
function Piece:getCastlingInProcess()
return self.castlingInProcess
end
function Piece:setClicked()
self.clicked = true
end
function Piece:checkPossibleCasteling(pieces, pressed, size)
return true
end