diff options
-rwxr-xr-x | gamestate.lua | 78 | ||||
-rw-r--r-- | ghost.lua | 11 | ||||
-rw-r--r-- | init.lua | 1 | ||||
-rwxr-xr-x | scorehud.lua | 35 |
4 files changed, 114 insertions, 11 deletions
diff --git a/gamestate.lua b/gamestate.lua index fd073e0..5af47b8 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 @@ -45,8 +50,10 @@ 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 end -- Resets the game to the start positions @@ -117,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!") @@ -148,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}) @@ -195,19 +203,73 @@ 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 positions = { + {x=0.5,y=0.5,z=0.5}, + {x=-0.5,y=0.5,z=-0.5}, + } + 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 + ------------------- --- 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 + mypacman.update_hud(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 + mypacman.remove_hud(player, name) + end end end) @@ -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 @@ -71,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 @@ -131,6 +135,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 @@ -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 |