diff options
author | heav <hheav3@gmail.com> | 2023-03-27 03:39:21 +0000 |
---|---|---|
committer | heav <hheav3@gmail.com> | 2023-03-27 03:39:21 +0000 |
commit | 91b55d2d983bd46edc09b8d91a982122104006db (patch) | |
tree | be2adb7a4e3a76f6822025b62dae7fbee6b7088f | |
parent | e6839a9e313be5e904550308f40ac6a3ae9a500c (diff) | |
parent | a966f45abf25dd486071a9552686e579de717417 (diff) |
Merge branch 'master' of citrons:polyflux
-rw-r--r-- | game/init.lua | 16 | ||||
-rw-r--r-- | game/polyomino.lua | 20 |
2 files changed, 26 insertions, 10 deletions
diff --git a/game/init.lua b/game/init.lua index 689da31..d6d9144 100644 --- a/game/init.lua +++ b/game/init.lua @@ -55,8 +55,9 @@ function M:input_loop() while self.piece:move(-1, 0) do did_move = true end if did_move then evloop.queue "game.lock_cancel" end elseif key == "up" then - self.piece:rotate() - evloop.queue "game.lock_cancel" + if self.piece:rotate() then + evloop.queue "game.lock_cancel" + end elseif key == "space" then local dropped = false while self.piece:move(-1, 0) do @@ -86,9 +87,14 @@ function M:input_loop() end function M:place_piece() - self.piece:place() - self.piece = nil - evloop.queue "game.lock_cancel" + if not self.piece:can_move(-1, 0) then + self.piece:place() + self.piece = nil + evloop.queue "game.lock_cancel" + return true + else + return false + end end function M:next_piece() diff --git a/game/polyomino.lua b/game/polyomino.lua index eb726b3..ba6da92 100644 --- a/game/polyomino.lua +++ b/game/polyomino.lua @@ -70,7 +70,7 @@ function M:drop(field) local new = setmetatable({poly = self}, piece) new.field = field new.line = field.lines - (self.bottom - 1) - new.column = math.floor(field.columns / 2 - self.size / 2 + 0.5) + new.column = math.floor(field.columns / 2 - self.size / 2 + 1) new.rotation = 1 if not new:can_occupy() then return @@ -96,6 +96,17 @@ function piece:can_occupy(line, column, rotation) return true end +function piece:can_move(lines, columns, rotation) + local rotation = self.rotation + (rotation or 0) + if rotation > 4 then + rotation = 1 + elseif rotation < 1 then + rotation = 4 + end + local line, column = self.line + lines or 0, self.column + columns or 0 + return self:can_occupy(line, column, rotation) +end + function piece:place() if self.placed then return @@ -126,10 +137,9 @@ function piece:rotate(ccw) end function piece:move(lines, columns) - local line, column = self.line + lines, self.column + columns - if self:can_occupy(line, column) then - self.line = line - self.column = column + if self:can_move(lines, columns) then + self.line = self.line + lines or 0 + self.column = self.column + columns or 0 return true end return false |