Added game logic to the mini board

master
Fernando Carmona Varo 2015-10-26 20:20:11 +01:00
parent 23814830b0
commit a593cc5432
4 changed files with 57 additions and 37 deletions

View File

@ -15,7 +15,7 @@ local score_for_life_award = 5000
-- Public functions (these can be called from any other place)
-- Start the game from the spawn block at position "pos" activated by "player"
function pacmine.game_start(pos, player)
function pacmine.game_start(pos, player, gamedef)
-- create an id unique for the given position
local id = minetest.pos_to_string(pos)
local player_name = player:get_player_name()
@ -28,17 +28,21 @@ function pacmine.game_start(pos, player)
end
-- Create a new game state with that id and add it to the game list
gamedef = gamedef or {}
gamestate = {
id = id,
player_name = player_name,
pos = pos,
start = {x=pos.x+14,y=pos.y+0.5,z=pos.z+16},
pellet_count = 0,
player_start = vector.add(pos, (gamedef.player_start or {x=14,y=0.5,z=16})),
ghost_start = vector.add(pos, (gamedef.ghost_start or {x=13,y=0.5,z=19})),
pellet_total = gamedef.pellet_total or 252,
speed = gamedef.speed or 2,
lives = gamedef.lives or 3,
scorename = gamedef.scorename,
level = 1,
speed = 2,
lives = 3,
score = 0,
awarded_lives = 0,
pellet_count = 0,
}
pacmine.games[id] = gamestate
pacmine.players[id] = player
@ -46,8 +50,8 @@ function pacmine.game_start(pos, player)
minetest.log("action","New pacmine game started at " .. id .. " by " .. gamestate.player_name)
-- place schematic
local schem = minetest.get_modpath("pacmine").."/schems/pacmine.mts"
minetest.place_schematic({x=pos.x,y=pos.y-1,z=pos.z-2},schem,0, "air", true)
local schem = gamedef
minetest.place_schematic({x=pos.x,y=pos.y-1,z=pos.z-2},gamedef.schematic,0, "air", true)
-- Set start positions
pacmine.game_reset(id, player)
@ -65,12 +69,14 @@ function pacmine.game_end(id)
player:moveto(vector.add(gamestate.pos,{x=0.5,y=0.5,z=-1.5}))
end
-- Save score
local ranking = myhighscore.save_score("pacmine", {
player = gamestate.player_name,
score = gamestate.score
})
if ranking then
minetest.chat_send_player(gamestate.player_name, "You made it to the highscores! Your Ranking: " .. ranking)
if gamestate.scorename then
local ranking = myhighscore.save_score(gamestate.scorename, {
player = gamestate.player_name,
score = gamestate.score
})
if ranking then
minetest.chat_send_player(gamestate.player_name, "You made it to the highscores! Your Ranking: " .. ranking)
end
end
-- Clear the data
pacmine.games[id] = nil
@ -90,34 +96,30 @@ function pacmine.game_reset(id, player)
-- Position the player
local player = player or minetest.get_player_by_name(gamestate.player_name)
player:setpos(gamestate.start)
player:setpos(gamestate.player_start)
-- Spawn the ghosts and assign the game id to each ghost
minetest.after(2, function()
if pacmine.games[id] and last_reset == pacmine.games[id].last_reset then
local pos = vector.add(gamestate.pos, {x=13,y=0.5,z=19})
local ghost = minetest.add_entity(pos, "pacmine:inky")
local ghost = minetest.add_entity(gamestate.ghost_start, "pacmine:inky")
ghost:get_luaentity().gameid = id
end
end)
minetest.after(12, function()
if pacmine.games[id] and last_reset == pacmine.games[id].last_reset then
local pos = vector.add(gamestate.pos, {x=15,y=0.5,z=19})
local ghost = minetest.add_entity(pos, "pacmine:pinky")
local ghost = minetest.add_entity(gamestate.ghost_start, "pacmine:pinky")
ghost:get_luaentity().gameid = id
end
end)
minetest.after(22, function()
if pacmine.games[id] and last_reset == pacmine.games[id].last_reset then
local pos = vector.add(gamestate.pos, {x=13,y=0.5,z=18})
local ghost = minetest.add_entity(pos, "pacmine:blinky")
local ghost = minetest.add_entity(gamestate.ghost_start, "pacmine:blinky")
ghost:get_luaentity().gameid = id
end
end)
minetest.after(32, function()
if pacmine.games[id] and last_reset == pacmine.games[id].last_reset then
local pos = vector.add(gamestate.pos, {x=15,y=0.5,z=18})
local ghost = minetest.add_entity(pos, "pacmine:clyde")
local ghost = minetest.add_entity(gamestate.ghost_start, "pacmine:clyde")
ghost:get_luaentity().gameid = id
end
end)
@ -156,7 +158,7 @@ function pacmine.add_fruit(id)
node.name = "pacmine:apple"
node.param2 = 3
end
local pos = vector.add(gamestate.start,{x=0,y=-1,z=0})
local pos = vector.add(gamestate.player_start,{x=0,y=-1,z=0})
minetest.set_node(pos, node)
print(node.param2)
-- Set the timer for the fruit to disappear
@ -176,7 +178,7 @@ function pacmine.on_player_got_pellet(player)
if gamestate.pellet_count == 70 or gamestate.pellet_count == 180 then
pacmine.add_fruit(gamestate.id)
elseif gamestate.pellet_count >= 252 then -- 252
elseif gamestate.pellet_count >= gamestate.pellet_total then
minetest.chat_send_player(name, "You cleared the board!")
pacmine.remove_ghosts(gamestate.id)
@ -327,9 +329,3 @@ minetest.register_chatcommand("pacmine_exit", {
end
end
})
-- Register with the myhighscore mod
myhighscore.register_game("pacmine", {
description = "Pacmine",
icon = "pacmine_1.png",
})

View File

@ -85,7 +85,7 @@ for i in ipairs(ghosts) do
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})
local ghost_spawn = gamestate.ghost_start
self.object:setpos(ghost_spawn)
-- set the timer negative so it'll have to wait extra time
self.timer = -ghosts_death_delay

View File

@ -11,7 +11,7 @@ function pacmine.update_hud(id, player)
pacmine.remove_hud(player)
return
end
local pellets_left = 252 - game.pellet_count
local pellets_left = game.pellet_total - game.pellet_count
local hudtext = "Score " .. game.score
.. "\nLevel " .. game.level
.. "\nLives " .. game.lives

View File

@ -71,7 +71,7 @@ minetest.register_node("pacmine:pellet_2", {
})
--The placer block for pacmine
minetest.register_node("pacmine:block2",{
minetest.register_node("pacmine:classic_board",{
description = "Pacman",
inventory_image = "pacmine_1.png",
tiles = {
@ -88,11 +88,14 @@ minetest.register_node("pacmine:block2",{
light_source = 8,
groups = {cracky = 1},
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
pacmine.game_start(pos, player)
pacmine.game_start(pos, player, {
schematic = minetest.get_modpath("pacmine").."/schems/pacmine.mts",
scorename = "pacmine:classic_board",
})
end,
})
--The placer block for pacmine mini
minetest.register_node("pacmine:block",{
minetest.register_node("pacmine:mini_board",{
description = "Pacman Mini",
inventory_image = "pacmine_1.png^pacmine_mini.png",
tiles = {
@ -109,7 +112,28 @@ minetest.register_node("pacmine:block",{
light_source = 8,
groups = {cracky = 1},
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
local schem = minetest.get_modpath("pacmine").."/schems/pacmini.mts"
minetest.place_schematic({x=pos.x,y=pos.y-1,z=pos.z-2},schem,0, "air", true)
pacmine.game_start(pos, player, {
schematic = minetest.get_modpath("pacmine").."/schems/pacmini.mts",
player_start = {x=13,y=0.5,z=2},
ghost_start = {x=13,y=0.5,z=10},
speed = 1,
pellet_total = 91,
scorename = "pacmine:mini_board",
})
end,
})
minetest.register_alias("pacmine:block", "pacmine:mini_board")
minetest.register_alias("pacmine:block2", "pacmine:normal_board")
-- Register with the myhighscore mod
myhighscore.register_game("pacmine:classic_board", {
description = "Pacmine",
icon = "pacmine_1.png",
})
-- Register with the myhighscore mod
myhighscore.register_game("pacmine:mini_board", {
description = "Pacmine Mini",
icon = "pacmine_1.png^pacmine_mini.png",
})