Created
May 6, 2023 15:11
-
-
Save EncodeTheCode/93145ad89538a6449a05cfb968bbd3f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local socket = require("socket") | |
-- create a TCP server and bind it to port 12345 | |
local server = socket.tcp() | |
server:bind("*", 12345) | |
server:listen() | |
-- set the server to non-blocking mode | |
server:settimeout(0) | |
-- initialize variables for the turret angle and cursor position | |
local turretAngle = 0 | |
local cursorX, cursorY = 0, 0 | |
-- loop indefinitely | |
while true do | |
-- accept any incoming client connections | |
local client = server:accept() | |
-- if a client connected, read its input | |
if client then | |
local input = client:receive() | |
-- if the input is the escape key, exit the program | |
if input == "\27" then | |
break | |
end | |
-- otherwise, update the turret angle based on the cursor position | |
if input == "up" then | |
turretAngle = math.atan2(cursorY - 1, cursorX) * 180 / math.pi | |
elseif input == "down" then | |
turretAngle = math.atan2(cursorY + 1, cursorX) * 180 / math.pi | |
elseif input == "left" then | |
turretAngle = math.atan2(cursorY, cursorX - 1) * 180 / math.pi | |
elseif input == "right" then | |
turretAngle = math.atan2(cursorY, cursorX + 1) * 180 / math.pi | |
end | |
-- send the updated turret angle back to the client | |
client:send(tostring(turretAngle) .. "\n") | |
-- close the client connection | |
client:close() | |
end | |
-- update the cursor position (in this example, cursorX and cursorY are updated externally) | |
cursorX, cursorY = getCursorPosition() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment