diff options
| -rwxr-xr-x | gamestate.lua | 52 | ||||
| -rw-r--r-- | ghost.lua | 22 | 
2 files changed, 58 insertions, 16 deletions
| diff --git a/gamestate.lua b/gamestate.lua index 8b642cb..476590a 100755 --- a/gamestate.lua +++ b/gamestate.lua @@ -53,7 +53,11 @@ function mypacman.game_reset(id, player)  	local gamestate = mypacman.games[id]  	minetest.log("action", "resetting game " .. id) +	-- Save the time when the game was last resetted (to solve timing issues) +	local last_reset = os.time() +  	gamestate.power_pellet = false +	gamestate.last_reset = last_reset  	-- Position the player  	local player = player or minetest.get_player_by_name(gamestate.player_name) @@ -61,24 +65,32 @@ function mypacman.game_reset(id, player)  	-- Spawn the ghosts and assign the game id to each ghost  	minetest.after(2, function() -		local pos = vector.add(gamestate.pos, {x=13,y=0.5,z=19}) -		local ghost = minetest.add_entity(pos, "mypacman:inky") -		ghost:get_luaentity().gameid = id +		if mypacman.games[id] and last_reset == mypacman.games[id].last_reset then +			local pos = vector.add(gamestate.pos, {x=13,y=0.5,z=19}) +			local ghost = minetest.add_entity(pos, "mypacman:inky") +			ghost:get_luaentity().gameid = id +		end  	end)  	minetest.after(12, function() -		local pos = vector.add(gamestate.pos, {x=15,y=0.5,z=19}) -		local ghost = minetest.add_entity(pos, "mypacman:pinky") -		ghost:get_luaentity().gameid = id +		if mypacman.games[id] and last_reset == mypacman.games[id].last_reset then +			local pos = vector.add(gamestate.pos, {x=15,y=0.5,z=19}) +			local ghost = minetest.add_entity(pos, "mypacman:pinky") +			ghost:get_luaentity().gameid = id +		end  	end)  	minetest.after(22, function() -		local pos = vector.add(gamestate.pos, {x=13,y=0.5,z=18}) -		local ghost = minetest.add_entity(pos, "mypacman:blinky") -		ghost:get_luaentity().gameid = id +		if mypacman.games[id] and last_reset == mypacman.games[id].last_reset then +			local pos = vector.add(gamestate.pos, {x=13,y=0.5,z=18}) +			local ghost = minetest.add_entity(pos, "mypacman:blinky") +			ghost:get_luaentity().gameid = id +		end  	end)  	minetest.after(32, function() -		local pos = vector.add(gamestate.pos, {x=15,y=0.5,z=18}) -		local ghost = minetest.add_entity(pos, "mypacman:clyde") -		ghost:get_luaentity().gameid = id +		if mypacman.games[id] and last_reset == mypacman.games[id].last_reset then +			local pos = vector.add(gamestate.pos, {x=15,y=0.5,z=18}) +			local ghost = minetest.add_entity(pos, "mypacman:clyde") +			ghost:get_luaentity().gameid = id +		end  	end)  end @@ -193,3 +205,19 @@ minetest.register_globalstep(function(dtime)  		gamestate_save()  	end  end) + +-- Chatcommand to end the game for the current player +minetest.register_chatcommand("mypacman_exit", { +	params = "", +	description = "Loads and saves all rooms", +	func = function(name, param) +		local gamestate = mypacman.get_game_by_player(name) +		if gamestate then +			mypacman.game_end(gamestate.id) +			minetest.get_player_by_name(name):moveto(vector.add(gamestate.pos,{x=0.5,y=0.5,z=-1.5})) +			minetest.chat_send_player(name, "You are no longer playing pacman") +		else +			minetest.chat_send_player(name, "You are not currently in a pacman game") +		end +	end +}) @@ -49,10 +49,20 @@ for i in ipairs(ghosts) do  			-- Do we have game state? if not just die  			local gamestate = mypacman.games[self.gameid]  			if not gamestate then -				minetest.log("action", "Removing pacman ghost from finished game " .. (self.gameid or "")) +				minetest.log("action", "Removing pacman ghost without game assigned")  				self.object:remove()  				return  			end +			-- Make sure we are in the right state by keeping track of the reset time +			-- if the reset time changed it's likely the game got resetted while the entity wasn't loaded +			if self.last_reset then +				if self.last_reset ~= gamestate.last_reset then +					minetest.log("action", "Removing pacman ghost remaining after reset ") +					self.object:remove() +				end +			else +				self.last_reset = gamestate.last_reset +			end  			-- Make sure we have a targetted player  			if not self.target then @@ -62,7 +72,7 @@ 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 @@ -114,13 +124,17 @@ for i in ipairs(ghosts) do  		-- This function should return the saved state of the entity in a string  		get_staticdata = function(self) -			return self.gameid or "" +			return (self.gameid or "") .. ";" .. (self.last_reset or "")  		end,  		-- This function should load the saved state of the entity from a string  		on_activate = function(self, staticdata)  			if staticdata and staticdata ~= "" then -				self.gameid = staticdata +				local data = string.split(staticdata, ";") +				if #data == 2 then +					self.gameid = data[1] +					self.last_reset = tonumber(data[2]) +				end  			end  		end  	}) | 
