From 1698cd05405af7d96242ed43c0bfb570a9e73dd5 Mon Sep 17 00:00:00 2001 From: Fernando Carmona Varo Date: Thu, 22 Oct 2015 20:28:56 +0200 Subject: Ghosts should be immortal now, even with bigger collisionbox --- ghost.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ghost.lua b/ghost.lua index 228de59..4138e8f 100644 --- a/ghost.lua +++ b/ghost.lua @@ -26,10 +26,8 @@ for i in ipairs(ghosts) do "mypacman_"..itm.."f.png", "mypacman_"..itm.."s.png", }, - groups = {immortal = 1}, velocity = {x=math.random(-1,1), y=0, z=math.random(-1,1)}, - collisionbox = {-0.01, -0.5, -0.01, 0.01, -0.49, 0.01}, - --collisionbox = {-0.25, -1.0, -0.25, 0.25, 0.48, 0.25}, + collisionbox = {-0.25, -1.0, -0.25, 0.25, 0.48, 0.25}, is_visible = true, automatic_rotate = true, automatic_face_movement_dir = -90, -- set yaw direction in degrees, false to disable @@ -131,6 +129,7 @@ for i in ipairs(ghosts) do -- This function should load the saved state of the entity from a string on_activate = function(self, staticdata) + self.object:set_armor_groups({immortal=1}) if staticdata and staticdata ~= "" then local data = string.split(staticdata, ";") if #data == 2 then -- cgit v1.2.3 From c942b65e33df4cc51202f605d037a2a40883c49e Mon Sep 17 00:00:00 2001 From: Fernando Carmona Varo Date: Thu, 22 Oct 2015 20:29:35 +0200 Subject: Let the player eat pellets on touch --- gamestate.lua | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/gamestate.lua b/gamestate.lua index fd073e0..2368eae 100755 --- a/gamestate.lua +++ b/gamestate.lua @@ -2,6 +2,9 @@ -- Array to hold all the running game states mypacman.games = {} +-- Store all the currently playing players +mypacman.players = {} + -- Duration of the power pellet effect (in seconds) local power_pellet_duration = 10 @@ -31,6 +34,8 @@ function mypacman.game_start(pos, player) score = 0, } mypacman.games[id] = gamestate + mypacman.players[id] = player + minetest.log("action","New pacman game started at " .. id .. " by " .. gamestate.player_name) -- place schematic @@ -47,6 +52,7 @@ function mypacman.game_end(id) mypacman.remove_ghosts(id) -- Clear the data mypacman.games[id] = nil + mypacman.players[id] = nil end -- Resets the game to the start positions @@ -195,19 +201,69 @@ local function gamestate_load() end end +-- Called every 0.5 seconds for each player that is currently playing pacman +local function on_player_gamestep(player, gameid) + local player_pos = player:getpos() + local pos = { + x = math.floor(player_pos.x + 0.5), + y = math.floor(player_pos.y + 0.5), + z = math.floor(player_pos.z + 0.5), + } + local node = minetest.get_node(pos) + if node.name == "mypacman:pellet_1" then + minetest.remove_node(pos) + mypacman.on_player_got_pellet(player) + elseif node.name == "mypacman:pellet_2" then + minetest.remove_node(pos) + mypacman.on_player_got_power_pellet(player) + + minetest.sound_play("mypacman_eatfruit", { + pos = pos, + max_hear_distance = 100, + gain = 10.0, + }) + end +end + ------------------- --- Execution code -- load the gamestate from disk mypacman.games = gamestate_load() or {} -local tmr = 0 ---Save Table every 10 seconds +-- Time counters +local tmr_gamestep = 0 +local tmr_savestep = 0 minetest.register_globalstep(function(dtime) - tmr = tmr + dtime; - if tmr >= 10 then - tmr = 0 - gamestate_save() + tmr_gamestep = tmr_gamestep + dtime; + if tmr_gamestep > 0.2 then + for id,player in pairs(mypacman.players) do + on_player_gamestep(player, id) + end + tmr_savestep = tmr_savestep + tmr_gamestep + if tmr_savestep > 10 then + gamestate_save() + tmr_savestep = 0 + end + tmr_gamestep = 0 + end +end) + +minetest.register_on_joinplayer(function(player) + local name = player:get_player_name() + for id,game in pairs(mypacman.games) do + if game.player_name == name then + mypacman.players[id] = player + end + end +end) + +minetest.register_on_leaveplayer(function(player) + local name = player:get_player_name() + for id,game in pairs(mypacman.games) do + if game.player_name == name then + mypacman.players[id] = nil + end end end) -- cgit v1.2.3 From 1a89f9a8e8e2dd95a1dccb70c10892b604ab434c Mon Sep 17 00:00:00 2001 From: Fernando Carmona Varo Date: Thu, 22 Oct 2015 20:44:07 +0200 Subject: Stop ghosts when the player leaves --- ghost.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ghost.lua b/ghost.lua index 4138e8f..a4bd46d 100644 --- a/ghost.lua +++ b/ghost.lua @@ -69,6 +69,12 @@ for i in ipairs(ghosts) do end local player = self.target + -- If there's no player just stop + if not player then + self.set_velocity(self, 0) + return + end + local s = self.object:getpos() -- ghost local p = player:getpos() -- player -- cgit v1.2.3 From 9fc6dc36feac4888ee10a8ee9aeb4fffc2f6e80a Mon Sep 17 00:00:00 2001 From: Fernando Carmona Varo Date: Thu, 22 Oct 2015 20:58:05 +0200 Subject: improved pellet detection --- gamestate.lua | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/gamestate.lua b/gamestate.lua index 2368eae..149c0d3 100755 --- a/gamestate.lua +++ b/gamestate.lua @@ -204,24 +204,26 @@ end -- Called every 0.5 seconds for each player that is currently playing pacman local function on_player_gamestep(player, gameid) local player_pos = player:getpos() - local pos = { - x = math.floor(player_pos.x + 0.5), - y = math.floor(player_pos.y + 0.5), - z = math.floor(player_pos.z + 0.5), + local positions = { + {x=0.5,y=0.5,z=0.5}, + {x=-0.5,y=0.5,z=-0.5}, } - local node = minetest.get_node(pos) - if node.name == "mypacman:pellet_1" then - minetest.remove_node(pos) - mypacman.on_player_got_pellet(player) - elseif node.name == "mypacman:pellet_2" then - minetest.remove_node(pos) - mypacman.on_player_got_power_pellet(player) - - minetest.sound_play("mypacman_eatfruit", { - pos = pos, - max_hear_distance = 100, - gain = 10.0, - }) + for _,pos in pairs(positions) do + pos = vector.add(player_pos, pos) + local node = minetest.get_node(pos) + if node.name == "mypacman:pellet_1" then + minetest.remove_node(pos) + mypacman.on_player_got_pellet(player) + elseif node.name == "mypacman:pellet_2" then + minetest.remove_node(pos) + mypacman.on_player_got_power_pellet(player) + + minetest.sound_play("mypacman_eatfruit", { + pos = pos, + max_hear_distance = 100, + gain = 10.0, + }) + end end end -- cgit v1.2.3 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