From 2707c8a21c89955ae1aa3012eb9b2ed97c74226b Mon Sep 17 00:00:00 2001 From: Fernando Carmona Varo Date: Thu, 22 Oct 2015 22:21:35 +0200 Subject: Added score HUD --- gamestate.lua | 8 ++++++-- init.lua | 1 + scorehud.lua | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100755 scorehud.lua diff --git a/gamestate.lua b/gamestate.lua index 149c0d3..5af47b8 100755 --- a/gamestate.lua +++ b/gamestate.lua @@ -50,6 +50,7 @@ end -- Finish the game with the given id function mypacman.game_end(id) mypacman.remove_ghosts(id) + mypacman.remove_hud(mypacman.players[id], mypacman.games[id].player_name) -- Clear the data mypacman.games[id] = nil mypacman.players[id] = nil @@ -123,7 +124,8 @@ function mypacman.on_player_got_pellet(player) gamestate.pellet_count = gamestate.pellet_count + 1 gamestate.score = gamestate.score + 10 - minetest.chat_send_player(name, "Your score is "..gamestate.score) + mypacman.update_hud(gamestate.id, player) + if gamestate.pellet_count >= 252 then -- 252 minetest.chat_send_player(name, "You cleared the board!") @@ -154,7 +156,7 @@ function mypacman.on_player_got_power_pellet(player) minetest.chat_send_player(name, "You got a POWER PELLET") gamestate.power_pellet = os.time() + power_pellet_duration gamestate.score = gamestate.score + 50 - minetest.chat_send_player(name, "Your score is "..gamestate.score) + mypacman.update_hud(gamestate.id, player) local boardcenter = vector.add(gamestate.pos, {x=13,y=0.5,z=15}) local powersound = minetest.sound_play("mypacman_powerup", {pos = boardcenter,max_hear_distance = 20, object=player, loop=true}) @@ -256,6 +258,7 @@ minetest.register_on_joinplayer(function(player) for id,game in pairs(mypacman.games) do if game.player_name == name then mypacman.players[id] = player + mypacman.update_hud(id, player) end end end) @@ -265,6 +268,7 @@ minetest.register_on_leaveplayer(function(player) for id,game in pairs(mypacman.games) do if game.player_name == name then mypacman.players[id] = nil + mypacman.remove_hud(player, name) end end end) diff --git a/init.lua b/init.lua index cc45f7e..8e7dbc7 100644 --- a/init.lua +++ b/init.lua @@ -8,6 +8,7 @@ dofile(minetest.get_modpath("mypacman").."/ghost.lua") dofile(minetest.get_modpath("mypacman").."/blocks.lua") dofile(minetest.get_modpath("mypacman").."/portals.lua") dofile(minetest.get_modpath("mypacman").."/gamestate.lua") +dofile(minetest.get_modpath("mypacman").."/scorehud.lua") --Yellow Pellets diff --git a/scorehud.lua b/scorehud.lua new file mode 100755 index 0000000..dbdfd7f --- /dev/null +++ b/scorehud.lua @@ -0,0 +1,35 @@ + + +local hud_table = {} + +function mypacman.update_hud(id, player) + local game = mypacman.games[id] + player = player or minetest.get_player_by_name(game.player_name) + if not player then + return + end + local hudtext = "Score: " .. game.score + local hud = hud_table[game.player_name] + if not hud then + hud = player:hud_add({ + hud_elem_type = "text", + position = {x = 1, y = 0}, + offset = {x=-400, y = 75}, + scale = {x = 100, y = 100}, + number = 0xFFFFFF, --color + text = hudtext + }) + hud_table[game.player_name] = hud + else + player:hud_change(hud, "text", hudtext) + end +end + + +function mypacman.remove_hud(player, playername) + local name = playername or player:get_player_name() + local hud = hud_table[name] + if hud then + player:hud_remove(hud) + end +end -- cgit v1.2.3