Skip to content

Commit

Permalink
working waypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
naturefreshmilk committed May 7, 2018
1 parent 63bc14d commit df41e89
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 7 deletions.
30 changes: 24 additions & 6 deletions functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ missions.start_mission = function(player, mission)

for i,m in pairs(playermissions) do
if m.title == mission.title then
minetest.chat_send_player(playername, "Mission alread running: " .. mission.title)
minetest.chat_send_player(playername, "Mission already running: " .. mission.title)
return
end
end
Expand All @@ -32,10 +32,27 @@ missions.start_mission = function(player, mission)
missions.save_missions()
end

missions.remove_mission = function(player, mission)
local playername = player:get_player_name()
local playermissions = missions.list[playername]
if playermissions == nil then playermissions = {} end

for i,m in pairs(playermissions) do
if m.title == mission.title then
table.remove(playermissions, i)
return
end
end
end


local update_player_mission = function(player, mission, time)
local update_player_mission = function(player, mission, remaining)
if remaining <= 0 then
-- mission timed-out
missions.hud_remove_mission(player, mission)
missions.remove_mission(player, mission)
minetest.chat_send_player(player:get_player_name(), "Mission timed out!: " .. mission.title)
end
end

local timer = 0
Expand All @@ -49,14 +66,15 @@ minetest.register_globalstep(function(dtime)
local playermissions = missions.list[playername]
if playermissions ~= nil then
for j,mission in pairs(playermissions) do
local diff = now - mission.start
print(playername .. ": " .. mission.title .. " == " .. diff .. " seconds")
local remaining = mission.time - (now - mission.start)

update_player_mission(player, mission, diff)
update_player_mission(player, mission, remaining)
end
end
missions.hud_update(player, playermissions)
end

timer = 0
end
end)
end)

138 changes: 138 additions & 0 deletions hud.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

local HUD_POSITION = {x = 0.5, y = 0.2}
local HUD_ALIGNMENT = {x = 1, y = 0}

local hud = {} -- playerName -> {}

minetest.register_on_joinplayer(function(player)
local playername = player:get_player_name()

local data = {}

data.title = player:hud_add({
hud_elem_type = "text",
position = HUD_POSITION,
offset = {x = 0, y = 0},
text = "",
alignment = HUD_ALIGNMENT,
scale = {x = 100, y = 100},
number = 0xFFFFFF
})

data.mission = player:hud_add({
hud_elem_type = "text",
position = HUD_POSITION,
offset = {x = 0, y = 35},
text = "",
alignment = HUD_ALIGNMENT,
scale = {x = 100, y = 100},
number = 0x00FF00
})

data.time = player:hud_add({
hud_elem_type = "text",
position = HUD_POSITION,
offset = {x = 0, y = 70},
text = "",
alignment = HUD_ALIGNMENT,
scale = {x = 100, y = 100},
number = 0x00FF00
})

hud[playername] = data
end)

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

local format_time = function(seconds)
local minutes = math.floor(seconds / 60)
local secs = seconds - (minutes * 60)
if secs < 10 then
return minutes .. ":0" .. secs
else
return minutes .. ":" ..secs
end
end

missions.hud_remove_mission = function(player, mission)
-- remove waypoints from elapsed mission
if mission.hud ~= nil and mission.hud.source ~= nil then
player:hud_remove(mission.hud.source)
end

if mission.hud ~= nil and mission.hud.target ~= nil then
player:hud_remove(mission.hud.target)
end
end

missions.hud_update = function(player, playermissions)
local playername = player:get_player_name()

local now = os.time(os.date("!*t"))
local data = hud[playername]
local topMission = nil

if data ~= nil and playermissions ~= nil then
for i,mission in pairs(playermissions) do

if mission.hud == nil then
-- add waypoint markers if new mission
mission.hud = {}
if mission.source ~= nil then
mission.hud.source = player:hud_add({
hud_elem_type = "waypoint",
name = mission.source.title .. "(Source)",
text = "m",
number = 0x0000FF,
world_pos = {x=mission.source.x, y=mission.source.y, z=mission.source.z}
})
end
mission.hud.target = player:hud_add({
hud_elem_type = "waypoint",
name = mission.target.title .. "(Destination)",
text = "m",
number = 0x0000FF,
world_pos = {x=mission.target.x, y=mission.target.y, z=mission.target.z}
})
end


-- top mission check
if topMission == nil then
topMission = mission
else
local remainingTime = mission.time - (now - mission.start)
local topRemainingTime = topMission.time - (now - topMission.start)

if remainingTime < topRemainingTime then
topMission = mission
end
end
end
end

if topMission ~= nil then
-- show the first mission to time out
local remainingTime = topMission.time - (now - topMission.start)
player:hud_change(data.title, "text", "Missions: (1/" .. table.getn(playermissions) .. ")")
player:hud_change(data.mission, "text", topMission.title)
player:hud_change(data.time, "text", "" .. format_time(remainingTime))
if remainingTime > 60 then
player:hud_change(data.time, "number", 0x00FF00)
player:hud_change(data.mission, "number", 0x00FF00)
else
player:hud_change(data.time, "number", 0xFF0000)
player:hud_change(data.mission, "number", 0xFF0000)
end
else
-- no missions running
player:hud_change(data.title, "text", "")
player:hud_change(data.mission, "text", "")
player:hud_change(data.time, "text", "")
end

end

1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ missions = {
}

dofile(MP.."/functions.lua")
dofile(MP.."/hud.lua")
dofile(MP.."/missionblock.lua")
dofile(MP.."/missionchest.lua")

Expand Down
3 changes: 2 additions & 1 deletion missionblock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,5 @@ minetest.register_node("missions:missionblock", {
update_formspec(meta)
end

})
})

0 comments on commit df41e89

Please sign in to comment.