Skip to content

Commit e4f5c49

Browse files
committed
doom lua: adding tracked entities
1 parent be92eeb commit e4f5c49

File tree

1 file changed

+76
-49
lines changed

1 file changed

+76
-49
lines changed

Assets/Lua/Doom/doom.lua

Lines changed: 76 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,16 @@ local LastMouse = {
8484
local TrackedThings = {}
8585
local TrackedLines = {}
8686
local TrackedSectors = {}
87+
local ThingIDs = {}
88+
local LineIDs = {}
89+
local SectorIDs = {}
8790
local LastFramecount = -1
8891
local ScreenWidth = client.screenwidth()
8992
local ScreenHeight = client.screenheight()
9093

9194
-- forward declarations
9295

96+
local Input
9397
local LastEpisode
9498
local LastMap
9599
local LastInput
@@ -381,6 +385,7 @@ local function init_cache()
381385
-- selectively cache certain properties. by assigning them manually the read function won't be called again
382386

383387
local lineId = line.iLineID
388+
LineIDs[lineId] = true
384389

385390
-- assumption: lines can't become special, except for script command CmdSetLineSpecial
386391
-- exclude lines that have a line id set (and therefore can be targeted by scripts)
@@ -399,6 +404,10 @@ local function init_cache()
399404

400405
table.insert(Lines, line)
401406
end
407+
408+
for _, sector in pairs(Globals.sectors) do
409+
SectorIDs[sector.iSectorID] = true
410+
end
402411
end
403412

404413
local function cached_line_coords(line)
@@ -582,6 +591,10 @@ local function iterate()
582591
local type = mobj.type
583592
local index = mobj.index
584593
local radius_color, text_color = get_mobj_color(mobj, type)
594+
595+
if index >= 0 then
596+
ThingIDs[index] = true
597+
end
585598

586599
if #TrackedThings > 0 then
587600
local id = TrackedThings[#TrackedThings]
@@ -748,7 +761,10 @@ local function reset_view()
748761
end
749762

750763
local function clear_cache()
751-
Lines = nil
764+
Lines = nil
765+
ThingIDs = {}
766+
LineIDs = {}
767+
SectorIDs = {}
752768
reset_view()
753769
end
754770

@@ -809,88 +825,105 @@ local function make_button(x, y, name, func)
809825
text(textX, textY, name, colors[colorIndex] | 0xff000000) -- full alpha
810826
end
811827

828+
local function check_press(key)
829+
return Input[key] and not LastInput[key]
830+
end
831+
812832
local function input_prompt()
813-
local input = input.get()
833+
Input = input.get()
814834
local value = tostring(CurrentPrompt.value or "")
815835

816-
if input.Escape and not LastInput.Escape then
836+
if check_press("Escape") then
817837
CurrentPrompt = nil
818838
return
819-
elseif input.Backspace and not LastInput.Backspace and value ~= "" then
839+
elseif check_press("Backspace") then
820840
value = value:sub(1, -2)
821-
elseif input.Enter and not LastInput.Enter and value ~= "" then
841+
elseif (check_press("Enter") or check_press("KeypadEnter")) and value ~= "" then
822842
CurrentPrompt.fun(tonumber(value))
823843
CurrentPrompt = nil
824844
return
825-
elseif input["Number0"] and not LastInput["Number0"] and value ~= "" then value = value .. "0"
826-
elseif input["Number1"] and not LastInput["Number1"] then value = value .. "1"
827-
elseif input["Number2"] and not LastInput["Number2"] then value = value .. "2"
828-
elseif input["Number3"] and not LastInput["Number3"] then value = value .. "3"
829-
elseif input["Number4"] and not LastInput["Number4"] then value = value .. "4"
830-
elseif input["Number5"] and not LastInput["Number5"] then value = value .. "5"
831-
elseif input["Number6"] and not LastInput["Number6"] then value = value .. "6"
832-
elseif input["Number7"] and not LastInput["Number7"] then value = value .. "7"
833-
elseif input["Number8"] and not LastInput["Number8"] then value = value .. "8"
834-
elseif input["Number9"] and not LastInput["Number9"] then value = value .. "9"
845+
else
846+
for i = 0, 9 do
847+
local digit = tostring(i)
848+
local number = "Number" .. digit
849+
local keypad = "Keypad" .. digit
850+
if (check_press(number)
851+
or check_press(keypad))
852+
then value = value .. digit
853+
end
854+
end
835855
end
836856

837857
local boxWidth = CHAR_WIDTH
838858
local boxHeight = CHAR_HEIGHT
839-
local message = CurrentPrompt.msg .. "\n\n" .. value .. "_"
859+
local message = string.format(
860+
"Enter %s ID from\nlevel editor.\n\n" ..
861+
"Hit \"Enter\" to send,\n" ..
862+
"\"Backspace\" to erase,\n" ..
863+
"or \"Escape\" to cancel.\n\n%s_",
864+
CurrentPrompt.msg, value)
840865
local lineCount, longest = get_line_count(message)
841866
local textWidth = longest *CHAR_WIDTH
842867
local textHeight = lineCount*CHAR_HEIGHT
843-
local colors = { 0x66bbddff, 0xaabbddff, 0xaa88aaff }
844-
local colorIndex = 2
845868
local padding = 50
846869

847870
if textWidth + padding > boxWidth then boxWidth = textWidth + padding end
848871
if textHeight + padding > boxHeight then boxHeight = textHeight + padding end
849872

850-
local x = ScreenWidth /2 - textWidth /2
851-
local y = ScreenHeight/2 - textHeight/2
852-
local textX = x + boxWidth /2 - textWidth /2
853-
local textY = y + boxHeight/2 - textHeight/2 - boxHeight
873+
local x = ScreenWidth /2 - textWidth /2
874+
local y = ScreenHeight/2 - textHeight/2
875+
local textX = x + boxWidth /2 - textWidth /2
876+
local textY = y + boxHeight/2 - textHeight/2
854877

855-
box(x, y, x+boxWidth, y-boxHeight, 0xaaffffff, colors[colorIndex])
878+
box(x, y, x+boxWidth, y+boxHeight, 0xaaffffff, 0xaabbddff)
856879
text(textX, textY, message, 0xffffffff)
857880

858881
if value ~= "" then
859882
CurrentPrompt.value = tonumber(value)
883+
else
884+
CurrentPrompt.value = nil
860885
end
861886

862-
LastInput = input
887+
LastInput = Input
863888
end
864889

865-
local function add_thing()
890+
local function add_entity(type)
866891
if CurrentPrompt then return end
867892

893+
local lookup, array
894+
895+
if type == "thing" then
896+
lookup = ThingIDs
897+
array = TrackedThings
898+
elseif type == "line" then
899+
lookup = LineIDs
900+
array = TrackedLines
901+
elseif type == "sector" then
902+
lookup = SectorIDs
903+
array = TrackedSectors
904+
else print("ERROR: Wrong entity type: " .. type) return
905+
end
906+
868907
CurrentPrompt = {
869-
msg =
870-
"Enter thing ID from\nlevel editor.\n\n" ..
871-
"Hit \"Enter\" to send,\n\"Backspace\" to erase,\nor \"Escape\" to cancel.",
908+
msg = type,
872909
fun = function(id)
873-
table.insert(TrackedThings, id)
874-
print(TrackedThings)
910+
if not lookup[id] then
911+
print(string.format("\nERROR: Can't add %s %d because it doesn't exist!\n", type, id))
912+
return
913+
end
914+
table.insert(array, id)
915+
print(string.format("Added %s %d", type, id))
875916
end,
876917
value = nil
877918
}
878919
end
879920

880-
local function add_line()
881-
882-
end
883-
884-
local function add_sector()
885-
886-
end
887-
888921
local function make_buttons()
889-
make_button(ScreenWidth-315, 30, "Add Thing", add_thing )
890-
make_button(ScreenWidth-210, 30, "Add Line", add_line )
891-
make_button(ScreenWidth-115, 30, "Add Sector", add_sector )
892-
make_button( 10, ScreenHeight-40, "+", function() zoom( 1) end)
893-
make_button( 10, ScreenHeight-10, "-", function() zoom(-1) end)
922+
make_button(ScreenWidth-315, 30, "Add Thing", function() add_entity("thing" ) end)
923+
make_button(ScreenWidth-210, 30, "Add Line", function() add_entity("line" ) end)
924+
make_button(ScreenWidth-115, 30, "Add Sector", function() add_entity("sector") end)
925+
make_button( 10, ScreenHeight-40, "+", function() zoom ( 1 ) end)
926+
make_button( 10, ScreenHeight-10, "-", function() zoom (-1 ) end)
894927
make_button( 40, ScreenHeight-24, "<", pan_left )
895928
make_button( 64, ScreenHeight-40, "^", pan_up )
896929
make_button( 64, ScreenHeight-10, "v", pan_down )
@@ -1028,12 +1061,6 @@ while true do
10281061
LastMouse.left = Mouse.Left
10291062
end
10301063

1031-
--[[--
1032-
text(10, ScreenHeight-170, string.format(
1033-
"Zoom: %.4f\nPanX: %s\nPanY: %s",
1034-
Zoom, Pan.x, Pan.y), 0xffbbddff)
1035-
--]]--
1036-
10371064
LastScreenSize.w = ScreenWidth
10381065
LastScreenSize.h = ScreenHeight
10391066
LastFramecount = framecount

0 commit comments

Comments
 (0)