diff options
-rwxr-xr-x | gamestate.lua | 25 | ||||
-rw-r--r-- | ghost.lua | 50 | ||||
-rw-r--r-- | init.lua | 4 |
3 files changed, 64 insertions, 15 deletions
diff --git a/gamestate.lua b/gamestate.lua index 683ce3f..8b642cb 100755 --- a/gamestate.lua +++ b/gamestate.lua @@ -2,6 +2,9 @@ -- Array to hold all the running game states mypacman.games = {} +-- Duration of the power pellet effect (in seconds) +local power_pellet_duration = 10 + --------------------------------------------------------- -- Public functions (these can be called from any other place) @@ -50,6 +53,8 @@ function mypacman.game_reset(id, player) local gamestate = mypacman.games[id] minetest.log("action", "resetting game " .. id) + gamestate.power_pellet = false + -- Position the player local player = player or minetest.get_player_by_name(gamestate.player_name) player:setpos(gamestate.start) @@ -117,7 +122,27 @@ function mypacman.on_player_got_pellet(player) minetest.sound_play("mypacman_beginning", {pos = pos,max_hear_distance = 40,gain = 10.0,}) end) end +end + +-- A player got a power pellet, update the state +function mypacman.on_player_got_power_pellet(player) + local name = player:get_player_name() + local gamestate = mypacman.get_game_by_player(name) + if not gamestate then return end + + minetest.chat_send_player(name, "You got a POWER PELLET") + gamestate.power_pellet = os.time() + power_pellet_duration + + local boardcenter = vector.add(gamestate.pos, {x=13,y=0.5,z=15}) + local powersound = minetest.sound_play("mypacman_beginning", {pos = boardcenter,max_hear_distance = 20, object=player, loop=true}) + minetest.after(power_pellet_duration, function() + if os.time() >= (gamestate.power_pellet or 0) then + gamestate.power_pellet = false + minetest.chat_send_player(name, "POWER PELLET wore off") + minetest.sound_stop(powersound) + end + end) end -- Get the game that the given player is playing @@ -1,4 +1,7 @@ +local ghosts_death_delay = 5 + + local ghosts = { {"pinky","Pinky"}, {"inky","Inky"}, @@ -59,23 +62,38 @@ for i in ipairs(ghosts) do local s = self.object:getpos() -- ghost local p = player:getpos() -- player - + print(dump(gamestate)) -- find distance from ghost to player local distance = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5 if distance < 1.5 then - -- player is so close it got catched!! - gamestate.lives = gamestate.lives - 1 - if gamestate.lives < 1 then - minetest.chat_send_player(gamestate.player_name,"Game Over") - player:moveto(vector.add(gamestate.pos,{x=0.5,y=0.5,z=-1.5})) - mypacman.game_end(self.gameid) + -- player touches ghost!! - elseif gamestate.lives == 1 then - minetest.chat_send_player(gamestate.player_name,"This is your last life") - mypacman.game_reset(self.gameid, player) + if gamestate.power_pellet then + -- Player eats ghost! move it to spawn + local ghost_spawn = vector.add(gamestate.pos, {x=13,y=0.5,z=19}) + self.object:setpos(ghost_spawn) + -- set the timer negative so it'll have to wait extra time + self.timer = -ghosts_death_delay + -- play sound and reward player + minetest.sound_play("mypacman_eatfruit", {pos = p, + max_hear_distance = 6, gain = 10.0, + }) + player:get_inventory():add_item('main', 'mypacman:cherrys') else - minetest.chat_send_player(gamestate.player_name,"You have ".. gamestate.lives .." lives left") - mypacman.game_reset(self.gameid, player) + -- Ghost catches the player! + gamestate.lives = gamestate.lives - 1 + if gamestate.lives < 1 then + minetest.chat_send_player(gamestate.player_name,"Game Over") + player:moveto(vector.add(gamestate.pos,{x=0.5,y=0.5,z=-1.5})) + mypacman.game_end(self.gameid) + + elseif gamestate.lives == 1 then + minetest.chat_send_player(gamestate.player_name,"This is your last life") + mypacman.game_reset(self.gameid, player) + else + minetest.chat_send_player(gamestate.player_name,"You have ".. gamestate.lives .." lives left") + mypacman.game_reset(self.gameid, player) + end end else @@ -84,9 +102,13 @@ for i in ipairs(ghosts) do if p.x > s.x then yaw = yaw + math.pi end - -- face player and move towards him + -- face player and move backwards/forwards self.object:setyaw(yaw) - self.set_velocity(self, gamestate.speed) + if gamestate.power_pellet then + self.set_velocity(self, -gamestate.speed) --negative velocity + else + self.set_velocity(self, gamestate.speed) + end end end, @@ -64,7 +64,9 @@ minetest.register_node("mypacman:pellet_2", { {-0.6875, -0.1875, -0.1875, -0.3125, 0.1875, 0.1875}, } }, - on_destruct = function(pos) + after_dig_node = function(pos, oldnode, oldmetadata, digger) + mypacman.on_player_got_power_pellet(digger) + minetest.sound_play("mypacman_eatfruit", { pos = pos, max_hear_distance = 100, |