aboutsummaryrefslogtreecommitdiff
path: root/trainhud.lua
blob: b64c81cdd350ac010af0ebf6476c6fd118cb34e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
advtrains.hud = {}

minetest.register_on_leaveplayer(function(player)
advtrains.hud[player:get_player_name()] = nil
end)

function advtrains.set_trainhud(name, text)
	local hud = advtrains.hud[name]
	local player=minetest.get_player_by_name(name)
	if not hud then
		hud = {}
		advtrains.hud[name] = hud
		hud.id = player:hud_add({
			hud_elem_type = "text",
			name = "ADVTRAINS",
			number = 0xFFFFFF,
			position = {x=0.5, y=0.7},
			offset = {x=0, y=0},
			text = text,
			scale = {x=200, y=60},
			alignment = {x=0, y=0},
		})
		hud.oldText=text
		return
	elseif hud.oldText ~= text then
		player:hud_change(hud.id, "text", text)
		hud.oldText=text
	end
end
function advtrains.hud_train_format(train, flip)
	local fct=1
	if flip then fct=-1 end
	if not train then return "" end
	local max=advtrains.all_traintypes[train.traintype].max_speed or 10
	local vel=math.ceil(train.velocity)*fct
	local tvel=math.ceil(train.tarvelocity)*fct
	local firstLine, secondLine
	if vel<0 then
		firstLine="Speed: <"..string.rep("_", vel+max)..string.rep("+", -vel).."|"..string.rep("_", max)..">"
	else
		firstLine="Speed: <"..string.rep("_", max).."|"..string.rep("+", vel)..string.rep("_", max-vel)..">"
	end
	if tvel<0 then
		secondLine="Target: <"..string.rep("_", tvel+max)..string.rep("+", -tvel).."|"..string.rep("_", max)..">"
	else
		secondLine="Target: <"..string.rep("_", max).."|"..string.rep("+", tvel)..string.rep("_", max-tvel)..">"
	end
	if vel==0 then
		return firstLine.."\n"..secondLine.."\nup for forward, down for backward, use to get off train.  "
	elseif vel<0 then
		return firstLine.."\n"..secondLine.."\nPress up to decelerate, down to accelerate, sneak to stop."
	elseif vel>0 then
		return firstLine.."\n"..secondLine.."\nPress up to accelerate, down to decelerate, sneak to stop."
	end
end