Created
July 20, 2020 15:39
-
-
Save kohbo/f7af1468c4663a6b1638d56ce4a8abbb 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 turnList = {} | |
function onLoad(save_state) | |
print("Kohbo's Lancer Tools Added") | |
startLuaCoroutine(self, "addUIElements") | |
end | |
--This toggles showing or hiding the roll buttons | |
function toggleRollerButtons() | |
if rollerToggle then | |
--UI.show("rollerButtons") | |
UI.setAttribute("rollerButtons", "active", true) | |
UI.setAttribute("rollerLayout", "height", 140) | |
else | |
--UI.hide("rollerButtons") | |
UI.setAttribute("rollerButtons", "active", false) | |
UI.setAttribute("rollerLayout", "height", 40) | |
end | |
--This flips between true/false for show/hide | |
rollerToggle = not rollerToggle | |
end | |
--Activated by roll buttons, this gets a random value and prints it | |
function rollDice(player, _, idValue) | |
--idValue is the "id" value of the XML button | |
roll = math.random(idValue) | |
str = player.steam_name .. " rolled a " .. roll .. " (d" .. idValue .. ")" | |
broadcastToAll(str, {1,1,1}) | |
end | |
function addDieRoller() | |
local globalXmlTable = Global.UI.getXmlTable() | |
local dieRoller = { | |
tag = "VerticalLayout", | |
attributes = { | |
id = "rollerLayout", | |
height = "140", | |
width = "80", | |
allowDragging = "true", | |
returnToOriginalPositionWhenReleased="false", | |
rectAlignment="UpperLeft", | |
offsetXY="300 -80" | |
}, | |
children = { | |
{ | |
tag = "Button", | |
attributes = { | |
height = "40", | |
color = "black", | |
textColor = "white", | |
onClick = self.guid .. "/toggleRollerButtons", | |
text = "Dice Roller" | |
} | |
}, | |
{ | |
tag = "VerticalLayout", | |
attributes = { | |
id = "rollerButtons" | |
}, | |
children = { | |
{ | |
tag = "Button", | |
attributes = { | |
text = "d3", | |
id = "3", | |
onclick = self.guid .. "/rollDice", | |
color = "black", | |
textColor = "white" | |
} | |
}, | |
{ | |
tag = "Button", | |
attributes = { | |
text = "d6", | |
id = "6", | |
onclick = self.guid .. "/rollDice", | |
color = "black", | |
textColor = "white" | |
} | |
}, | |
{ | |
tag = "Button", | |
attributes = { | |
text = "d20", | |
id = "20", | |
onclick = self.guid .. "/rollDice", | |
color = "black", | |
textColor = "white" | |
} | |
} | |
} | |
} | |
} | |
} | |
table.insert(globalXmlTable, dieRoller) | |
Global.UI.setXmlTable(globalXmlTable) | |
end | |
function addTurnTracker() | |
local globalXmlTable = Global.UI.getXmlTable() | |
local turnTracker = { | |
tag = "Panel", | |
attributes = { | |
width = 200, | |
height = 50, | |
allowDragging = true, | |
returnToOriginalPositionWhenReleased = false, | |
id = "kohboTurnTracker" | |
}, | |
children = { | |
{ | |
tag = "VerticalLayout", | |
children = { | |
{ | |
tag = "Button", | |
attributes = { | |
color = "black", | |
textColor = "white", | |
text = "Turn Order" | |
} | |
}, | |
{ | |
tag = "VerticalLayout", | |
attributes = { | |
id = "kohboTurnList" | |
}, | |
children = {} | |
} | |
} | |
} | |
} | |
} | |
table.insert(globalXmlTable, turnTracker) | |
Global.UI.setXmlTable(globalXmlTable) | |
end | |
function updateTurnTracker() | |
print("Updating turn order") | |
local tempTurnTable = {} | |
local globalXmlTable = Global.UI.getXmlTable() | |
-- Find the UI table item index that corresponds with the turn tracker | |
local turnTrackerIndex = 1 | |
for k, v in ipairs(globalXmlTable) do | |
if v.attributes.id == "kohboTurnTracker" then | |
turnTrackerIndex = k | |
end | |
end | |
-- Clear old turns | |
for k=1, #globalXmlTable[turnTrackerIndex].children[1].children[2].children do | |
table.remove(globalXmlTable[turnTrackerIndex].children[1].children[2].children) | |
end | |
-- Insert the turns back into the turn tracker and global UI xml | |
globalXmlTable[turnTrackerIndex].children[1].children[2].children = tempTurnTable | |
Wait.frames(function() Global.UI.setXmlTable(globalXmlTable) end, 1) | |
Wait.frames(function() Global.UI.setAttribute("kohboTurnTracker", "height", 30 + #tempTurnTable * 30) end, 1) | |
end | |
function getTurnTable() | |
local tempTurnTable = {} | |
-- Get the vector that points straight out from the front of the chest | |
local forwardVector = self.getTransformForward():rotateOver('y', -90):normalize() | |
--print(forwardVector) | |
--print(Vector(7,1,0):rotateOver('y', Vector(0,0,-1):angle(forwardVector))) | |
local hitlist = Physics.cast({ | |
origin = self.getPosition():add(forwardVector * 0.4), | |
direction = forwardVector, | |
type = 3, | |
size = Vector(5,1,0):rotateOver('y', Vector(0,0,-1):angle(forwardVector)), | |
max_distance = 40, | |
debug = true | |
}) | |
-- Iterate over hit objects and pick out the turn tokens | |
-- add the found tokens to the turn list | |
for _,obj in ipairs(hitlist) do | |
local ins = obj.hit_object.getInputs() | |
if ins ~= nil then | |
if isTurnToken(ins[1].label) and #(ins[1].value) ~= 0 then | |
print(ins[1].value) | |
table.insert(tempTurnTable, makeTurnButton(ins[1].value)) | |
end | |
end | |
end | |
return tempTurnTable | |
end | |
function makeTurnButton(name) | |
local tempButtonTable = { | |
tag = "Button", | |
attributes = { | |
color = "black", | |
textColor = "white", | |
}, | |
value = name | |
} | |
return tempButtonTable | |
end | |
function isTurnToken(label) | |
local turnTokenTypes = { | |
"Player", | |
"Enemy", | |
"Ally", | |
"Neutral" | |
} | |
for _,v in ipairs(turnTokenTypes) do | |
if v == label then | |
return true | |
end | |
end | |
return false | |
end | |
function addUIElements() | |
-- Wait until object is done spawning | |
while self.spawning do | |
coroutine.yield(0) | |
end | |
-- Die Roller | |
addDieRoller() | |
-- Turn Tracker | |
Wait.frames(addTurnTracker, 1) | |
Timer.create({ | |
identifier = self.guid, | |
function_name = "updateTurnTracker", | |
delay = 5, | |
repetitions = 0 | |
}) | |
-- Must return one for coroutine | |
return 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment