mirror of
https://github.com/LDDestroier/CC/
synced 2025-01-09 17:00:28 +00:00
Improved player input
Now it turns using the last-inputted direction, meaning that holding multiple directions down shouldn't confuse the direction setting function!
This commit is contained in:
parent
cafe3c3005
commit
d3464eedef
108
tron
108
tron
@ -122,6 +122,8 @@ local nou = 2
|
|||||||
|
|
||||||
local keysDown = {}
|
local keysDown = {}
|
||||||
local netKeysDown = {}
|
local netKeysDown = {}
|
||||||
|
local lastDirectionPressed
|
||||||
|
local netLastDirectionPressed
|
||||||
|
|
||||||
-- the scrolling of the screen
|
-- the scrolling of the screen
|
||||||
local scrollX = 0
|
local scrollX = 0
|
||||||
@ -330,7 +332,7 @@ local ageTrails = function()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local control = {
|
local control, revControl = {
|
||||||
up = keys.up,
|
up = keys.up,
|
||||||
down = keys.down,
|
down = keys.down,
|
||||||
left = keys.left,
|
left = keys.left,
|
||||||
@ -340,7 +342,10 @@ local control = {
|
|||||||
lookLeft = keys.a,
|
lookLeft = keys.a,
|
||||||
lookRight = keys.d,
|
lookRight = keys.d,
|
||||||
release = keys.space
|
release = keys.space
|
||||||
}
|
}, {}
|
||||||
|
for k,v in pairs(control) do
|
||||||
|
revControl[v] = k
|
||||||
|
end
|
||||||
|
|
||||||
-- keeps track of where you are
|
-- keeps track of where you are
|
||||||
local gamemode = ""
|
local gamemode = ""
|
||||||
@ -481,7 +486,6 @@ local render = function()
|
|||||||
drawGrid(scrollX + scrollAdjX, scrollY + scrollAdjY)
|
drawGrid(scrollX + scrollAdjX, scrollY + scrollAdjY)
|
||||||
termsetCursorPos(1,1)
|
termsetCursorPos(1,1)
|
||||||
termsetTextColor(player[you].color[1])
|
termsetTextColor(player[you].color[1])
|
||||||
termwrite("P" .. you)
|
|
||||||
tsv(true)
|
tsv(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -639,6 +643,14 @@ local getInput = function()
|
|||||||
keysDown = {}
|
keysDown = {}
|
||||||
else
|
else
|
||||||
if evt[1] == "key" then
|
if evt[1] == "key" then
|
||||||
|
if (not keysDown[evt[2]]) and (
|
||||||
|
evt[2] == control.up or
|
||||||
|
evt[2] == control.down or
|
||||||
|
evt[2] == control.left or
|
||||||
|
evt[2] == control.right
|
||||||
|
) then
|
||||||
|
lastDirectionPressed = revControl[evt[2]]
|
||||||
|
end
|
||||||
keysDown[evt[2]] = true
|
keysDown[evt[2]] = true
|
||||||
elseif evt[1] == "key_up" then
|
elseif evt[1] == "key_up" then
|
||||||
keysDown[evt[2]] = false
|
keysDown[evt[2]] = false
|
||||||
@ -687,6 +699,7 @@ local sendInfo = function(gameID)
|
|||||||
keysDown = isHost and nil or keysDown,
|
keysDown = isHost and nil or keysDown,
|
||||||
trail = isHost and lastTrails or nil,
|
trail = isHost and lastTrails or nil,
|
||||||
deadGuys = isHost and deadGuys or nil,
|
deadGuys = isHost and deadGuys or nil,
|
||||||
|
lastDir = lastDirectionPressed
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -763,15 +776,14 @@ local moveTick = function(doSend)
|
|||||||
return deadAnimation(doSend)
|
return deadAnimation(doSend)
|
||||||
end
|
end
|
||||||
|
|
||||||
local setDirection = function(keylist, p, checkDir)
|
local setDirection = function(p, checkDir, lastDir)
|
||||||
p.putTrail = not keylist[control.release]
|
if (lastDir == control.left) and (checkDir or p.direction) ~= 0 then
|
||||||
if keylist[control.left] and (checkDir or p.direction) ~= 0 then
|
|
||||||
p.direction = 2
|
p.direction = 2
|
||||||
elseif keylist[control.right] and (checkDir or p.direction) ~= 2 then
|
elseif (lastDir == control.right) and (checkDir or p.direction) ~= 2 then
|
||||||
p.direction = 0
|
p.direction = 0
|
||||||
elseif keylist[control.up] and (checkDir or p.direction) ~= 1 then
|
elseif (lastDir == control.up) and (checkDir or p.direction) ~= 1 then
|
||||||
p.direction = -1
|
p.direction = -1
|
||||||
elseif keylist[control.down] and (checkDir or p.direction) ~= -1 then
|
elseif (lastDir == control.down) and (checkDir or p.direction) ~= -1 then
|
||||||
p.direction = 1
|
p.direction = 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -787,11 +799,12 @@ local game = function()
|
|||||||
np = player[nou]
|
np = player[nou]
|
||||||
|
|
||||||
if isHost then
|
if isHost then
|
||||||
setDirection(keysDown, p)
|
setDirection(p, nil, control[lastDirectionPressed])
|
||||||
setDirection(netKeysDown, np)
|
setDirection(np, nil, control[netLastDirectionPressed])
|
||||||
else
|
else
|
||||||
setDirection(keysDown, p)
|
setDirection(p, nil, control[lastDirectionPressed])
|
||||||
end
|
end
|
||||||
|
p.putTrail = not keysDown[control.release]
|
||||||
|
|
||||||
if keysDown[control.lookLeft] then
|
if keysDown[control.lookLeft] then
|
||||||
scrollAdjX = scrollAdjX - 2
|
scrollAdjX = scrollAdjX - 2
|
||||||
@ -871,6 +884,7 @@ local networking = function()
|
|||||||
end
|
end
|
||||||
elseif type(msg.keysDown) == "table" then
|
elseif type(msg.keysDown) == "table" then
|
||||||
netKeysDown = msg.keysDown
|
netKeysDown = msg.keysDown
|
||||||
|
netLastDirectionPressed = msg.lastDir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -895,37 +909,45 @@ local helpScreen = function()
|
|||||||
waitForKey(0.25)
|
waitForKey(0.25)
|
||||||
end
|
end
|
||||||
|
|
||||||
while true do
|
local main = function()
|
||||||
decision = titleScreen()
|
while true do
|
||||||
lockInput = false
|
decision = titleScreen()
|
||||||
if decision == "start" then
|
lockInput = false
|
||||||
trail = {}
|
if decision == "start" then
|
||||||
deadGuys = {}
|
-- reset all info between games
|
||||||
gameDelay = gameDelayInit
|
trail = {}
|
||||||
grid = deepCopy(initGrid)
|
deadGuys = {}
|
||||||
player = resetPlayers()
|
lastDirectionPressed = nil
|
||||||
you, nou = 1, 2
|
netLastDirectionPressed = nil
|
||||||
gamename = ""
|
gameDelay = gameDelayInit
|
||||||
for i = 1, 32 do
|
grid = deepCopy(initGrid)
|
||||||
gamename = gamename .. string.char(math.random(1,126))
|
player = resetPlayers()
|
||||||
|
you, nou = 1, 2
|
||||||
|
gamename = ""
|
||||||
|
for i = 1, 32 do
|
||||||
|
gamename = gamename .. string.char(math.random(1,126))
|
||||||
|
end
|
||||||
|
|
||||||
|
waitingForGame = true
|
||||||
|
transmit(port, {
|
||||||
|
player = player,
|
||||||
|
gameID = gamename,
|
||||||
|
new = os.time(),
|
||||||
|
gameDelay = gameDelayInit,
|
||||||
|
grid = initGrid
|
||||||
|
})
|
||||||
|
parallel.waitForAny(pleaseWait, networking)
|
||||||
|
sleep(0.1)
|
||||||
|
startCountdown()
|
||||||
|
parallel.waitForAny(getInput, game, networking)
|
||||||
|
elseif decision == "help" then
|
||||||
|
helpScreen()
|
||||||
|
elseif decision == "demo" then
|
||||||
|
parallel.waitForAny(getInput, gridDemo)
|
||||||
|
elseif decision == "exit" then
|
||||||
|
return cleanExit()
|
||||||
end
|
end
|
||||||
waitingForGame = true
|
|
||||||
transmit(port, {
|
|
||||||
player = player,
|
|
||||||
gameID = gamename,
|
|
||||||
new = os.time(),
|
|
||||||
gameDelay = gameDelayInit,
|
|
||||||
grid = initGrid
|
|
||||||
})
|
|
||||||
parallel.waitForAny(pleaseWait, networking)
|
|
||||||
sleep(0.1)
|
|
||||||
startCountdown()
|
|
||||||
parallel.waitForAny(getInput, game, networking)
|
|
||||||
elseif decision == "help" then
|
|
||||||
helpScreen()
|
|
||||||
elseif decision == "demo" then
|
|
||||||
parallel.waitForAny(getInput, gridDemo)
|
|
||||||
elseif decision == "exit" then
|
|
||||||
return cleanExit()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user