Skip to content

Instantly share code, notes, and snippets.

@blam23
Last active May 1, 2017 14:52
Show Gist options
  • Save blam23/5802092 to your computer and use it in GitHub Desktop.
Save blam23/5802092 to your computer and use it in GitHub Desktop.
Graphs in Toribash using Lua.
--dofile("dbg.lua")
-- start of graph class
graph = {
LINE = 0, BAR = 1,
Colors = { {1,0,0,0.4}, {0,0,1,0.7}, {0,1,1,0.7}, {1, 0, 1,0.7}, {1, 1, 0, 0.7}, {0, 0.7, 0, 0.7}, {1, 0.5, 0, 0.7}}
}
graph.__index = graph
function graph.new(gtype, x, y, width, height, max)
if(max == nil) then max = 0 end
local n = { }
n.visible = true
n.type = gtype
n.x = x
n.y = y
n.width = width
n.height = height
n.max = max
n.xscale = 10
n.yscale = 1
n.maxx = 0
n.maxy = 0
n.data = { }
n.offset = math.random(1, #graph.Colors)
n.highestdata = 1
setmetatable(n, graph)
return n
end
function graph:insert_data(field, score)
if(self.data[field] == nil) then self.data[field] = { } end
table.insert(self.data[field], score)
if(self.max > 0 and #self.data[field] > self.max) then
table.remove(self.data[field], 1)
end
if(score > self.highestdata) then
self.highestdata = score
self.yscale = self.height / score
end
end
function graph:clear_data()
self.data = { }
self.highestdata = 0
end
function graph:draw()
if(self.visible and #self.data > 0) then
set_color(0.1,0.1,0.1,0.4)
draw_quad(self.x, self.y, self.width, self.height)
self.xscale = self.width/#self.data[1]
local oldy1, oldy2 = 0, 0
for k = 1, #self.data do
local color = ((k + self.offset) % #graph.Colors) + 1
set_color(unpack(graph.Colors[color]))
for i = 1, #self.data[k] do
if(self.type == graph.LINE) then
draw_line(self.x + ((i-1) * self.xscale), self.y + self.height - oldy1, self.x+((i) * self.xscale), self.y + self.height - (self.data[k][i]*self.yscale), 1)
oldy1 = (self.data[k][i]*self.yscale)
else
draw_quad(self.x + ((i-1) * self.xscale), self.y + self.height - (self.data[k][i] * self.yscale), self.xscale, self.data[k][i] * self.yscale)
end
end
end
end
end
-- end of graph class
-- THIS IS EXAMPLE USAGE BELOW
-- NOT NEEDED!
dmg_graph = graph.new(graph.LINE, 5,140,200,200)
xy_graph = graph.new(graph.BAR, 5,345,200,200, 100)
rand_graph = graph.new(graph.LINE, 345,405,800,100, 70)
add_hook("mouse_move", "xy",
function(x, y)
xy_graph:insert_data(1, x)
xy_graph:insert_data(2, y)
end
)
add_hook("command", "commands",
function(cmd)
echo(cmd)
if(cmd == "graph clear") then
echo("Clearing data.")
dmg_graph:clear_data()
--return 1 to stop it being processed.
return 1
elseif(cmd == "graph hide") then
dmg_graph.visible = false
return 1
elseif(cmd == "graph show") then
dmg_graph.visible = true
return 1
end
end
)
add_hook("new_game", "data_clear",
function()
if(not singleplayer) then
remove_hook("enter_freeze", "mp_data_entry")
add_hook("enter_frame", "sp_data_entry", frameEntry)
singleplayer = true
end
dmg_graph:clear_data()
end
)
add_hook("new_mp_game", "data_clear",
function()
if(singleplayer) then
remove_hook("enter_frame", "sp_data_entry")
add_hook("enter_freeze", "mp_data_entry", frameEntry)
singleplayer = false
end
dmg_graph:clear_data()
end
)
add_hook("match_begin", "data_clear", function() dmg_graph:clear_data() end)
local oldinj, oldinj2 = 0, 0
function frameEntry()
if(get_world_state().match_frame == 0) then
dmg_graph:clear_data()
end
currframeoff = 0
dmg_graph:insert_data(2, get_player_info(0).injury-oldinj)
dmg_graph:insert_data(1, get_player_info(1).injury-oldinj2)
oldinj = get_player_info(0).injury
oldinj2 = get_player_info(1).injury
end
add_hook("enter_frame", "sp_data_entry", frameEntry)
local sinny = -1
add_hook("draw2d", "draw",
function()
dmg_graph:draw()
xy_graph:draw()
sinny = sinny + 0.1
if sinny > math.pi then sinny = -math.pi end
rand_graph:insert_data(1, math.sin(sinny) + 1 + math.random() * 0.5)
rand_graph:insert_data(2, math.cos(sinny) + 1 + math.random() * 0.5)
rand_graph:draw()
end
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment