Skip to content

Commit

Permalink
feat:docs and annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ActionScripted committed Jan 25, 2024
1 parent bfcfa0e commit 963f23a
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 31 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ Using lazy.nvim:
opts = {},
}
```

## Configuration

TODO. But right now, nothing to configure. EZPZ.

## Development

Leaning on [Lua Language Server](https://luals.github.io/) for annotations and docs.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO

- [ ] docs: use kdheepak/panvimdoc or equivalent
- [ ] feat(core): add pieces
- [ ] feat(input): add piece movement
- [ ] feat(input): add piece rotation
Expand All @@ -8,9 +9,9 @@
- [ ] feat(ui): add pause screen
- [ ] feat(ui): add score info
- [ ] feat(ui): add start screen
- [ ] feat(ui): hide cursor/input/block
- [ ] feat(ui): namespace for highlights
- [ ] perf(core): decouple game (loop) tick from level/speed
- [ ] perf(core): double buffer for rendering
- [ ] perf(core): should we try virtual text?
- [ ] test(core): add tests
- [ ] feat(ui): hide cursor/input/block
8 changes: 8 additions & 0 deletions doc/tetris.nvim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*lazy.nvim.txt* Last change: 2024 January 25

==============================================================================
Table of Contents *tetris.nvim-table-of-contents*

1. tetris.nvim |tetris.nvim-tetris.nvim|

**tetris.nvim** is a pure Lua Tetris game for Neovim.
6 changes: 5 additions & 1 deletion lua/tetris/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
---local async = require("plenary.async")
require("tetris.math")

local tetris = {
input = require("tetris.input"),
score_max = 99999999,
score_min = 0,
should_close = false,
speed = 60,
ui = require("tetris.ui"),
Expand Down Expand Up @@ -64,7 +67,8 @@ tetris.draw_game = function(buffer)
-- vim.api.nvim_buf_set_lines(buffer, 0, -1, false, lines)
tetris.ui.draw_layout_main()

tetris.score = tetris.score + 10
-- TODO: ew
tetris.score = math.clamp(tetris.score + 10, tetris.score_min, tetris.score_max)

-- TODO: keep positions (row,col) for this in layout, probaly
vim.api.nvim_buf_set_extmark(tetris.ui.window.buffer, tetris.namespace, 4, 31, {
Expand Down
9 changes: 6 additions & 3 deletions lua/tetris/input.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---Input mappings and handlers.
local input = {}

---Mappings from inputs to actions.
---@type table
input.mappings = {
["<Esc>"] = "quit",
["<LeftMouse>"] = "noop",
Expand All @@ -14,9 +17,9 @@ input.mappings = {
q = "quit",
}

--- Setup bindings for our primary buffer.
--- @param buffer any
--- @param handlers any
---Setup bindings for our primary buffer.
---@param buffer any
---@param handlers any
input.setup = function(buffer, handlers)
for key, action in pairs(input.mappings) do
if action == "noop" then
Expand Down
8 changes: 8 additions & 0 deletions lua/tetris/layouts.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
---Layouts (views) for the game.
local layouts = {}

---Convert a string to a table of strings.
---For drawing to buffers we need a table of strings, where each string
---is a line. But it's not fun to define layouts as tables.
---@param layout string
---@return table
local string_to_table = function(layout)
local lines = {}
for s in layout:gmatch("[^\r\n]+") do
Expand All @@ -8,6 +14,8 @@ local string_to_table = function(layout)
return lines
end

---Main layout.
---@return table
layouts.main = function()
local layout = [[
╭───────────────────────────────────╮
Expand Down
13 changes: 13 additions & 0 deletions lua/tetris/math.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--- Math utility functions.
--- Note that we are snapping these directly to the math library. If you hate it,
--- open a PR and refactor and do `local tetris_math = require("tetris.math")` or
--- something and then I will comment about how ugly that is and we can bicker in
--- public. I will concede and look like a jerk and eventually merge your PR. ;)

---Clamp a number between a minimum and maximum value.
---@param x number Value to clamp
---@param min number Minimum value
---@param max number Maximum value
function math.clamp(x, min, max)
return math.max(math.min(x, max), min)
end
59 changes: 33 additions & 26 deletions lua/tetris/ui.lua
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
--- UI module for Tetris.nvim

local layouts = require("tetris.layouts")
local popup = require("plenary.popup")
local M = {}

M.window = {
buffer = nil,
height = 23,
id = nil,
width = 37,
--- UI state, mostly.
local ui = {
window = {
buffer = nil,
height = 23,
id = nil,
width = 37,
},
}

M.create_window = function()
M.window.buffer = vim.api.nvim_create_buf(false, true)
---Create window and window homies like buffer and
---setup autocmds to close them when we leave.
ui.create_window = function()
ui.window.buffer = vim.api.nvim_create_buf(false, true)

local win_id, win = popup.create(M.window.buffer, {
local win_id, win = popup.create(ui.window.buffer, {
border = false,
col = math.floor((vim.o.columns - M.window.width) / 2),
col = math.floor((vim.o.columns - ui.window.width) / 2),
highlight = "TetrisWindow",
line = math.floor(((vim.o.lines - M.window.height) / 2) - 1),
minheight = M.window.height,
minwidth = M.window.width,
line = math.floor(((vim.o.lines - ui.window.height) / 2) - 1),
minheight = ui.window.height,
minwidth = ui.window.width,
noautocmd = true,
})

M.window.id = win_id
ui.window.id = win_id

vim.api.nvim_create_autocmd("BufLeave", {
buffer = M.window.buffer,
buffer = ui.window.buffer,
once = true,
callback = function()
pcall(vim.api.nvim_win_close, win_id, true)
--pcall(vim.api.nvim_win_close, win.border.win_id, true)
-- TODO: require telescope
require("telescope.utils").buf_delete(M.window.buffer)
require("telescope.utils").buf_delete(ui.window.buffer)
end,
})

Expand All @@ -40,19 +46,20 @@ M.create_window = function()
vim.api.nvim_win_set_option(win_id, "winhl", "Cursor:CustomCursor")
end

--- Fill/flood the game window with empty spaces.
-- We cannot write to specific lines/cols in the buffer without there
-- already being text there. So we flood the area to avoid errors.
M.flood_window = function()
---Fill/flood the game window with empty spaces.
---We CANNOT write to specific lines/cols in the buffer without there
---already being text there. So we flood the area to avoid errors.
ui.flood_window = function()
local lines = {}
for _ = 1, M.window.height do
table.insert(lines, string.rep(" ", M.window.width))
for _ = 1, ui.window.height do
table.insert(lines, string.rep(" ", ui.window.width))
end
vim.api.nvim_buf_set_lines(M.window.buffer, 0, -1, false, lines)
vim.api.nvim_buf_set_lines(ui.window.buffer, 0, -1, false, lines)
end

M.draw_layout_main = function()
vim.api.nvim_buf_set_lines(M.window.buffer, 0, -1, false, layouts.main())
---Draw our main layout to our main buffer.
ui.draw_layout_main = function()
vim.api.nvim_buf_set_lines(ui.window.buffer, 0, -1, false, layouts.main())
end

return M
return ui
6 changes: 6 additions & 0 deletions stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
column_width = 120
indent_type = "Spaces"
indent_width = 2

[sort_requires]
enabled = true

0 comments on commit 963f23a

Please sign in to comment.