diff options
Diffstat (limited to 'mario')
-rwxr-xr-x | mario/gamestate.lua | 60 | ||||
-rwxr-xr-x | mario/hud.lua | 44 | ||||
-rw-r--r-- | mario/init.lua | 19 | ||||
-rw-r--r-- | mario/sounds/mario-coin.ogg (renamed from mario/sounds/mario_coin.ogg) | bin | 4814 -> 4814 bytes | |||
-rw-r--r-- | mario/turtle.lua | 42 |
5 files changed, 123 insertions, 42 deletions
diff --git a/mario/gamestate.lua b/mario/gamestate.lua index c58b5e4..59e535b 100755 --- a/mario/gamestate.lua +++ b/mario/gamestate.lua @@ -20,7 +20,7 @@ function mario.game_start(pos, player, gamedef) -- make sure any previous game with the same id has ended local gamestate = mario.games[id] if gamestate then - minetest.chat_send_player(name, "A game is already in progress for player " .. gamestate.player_name) + minetest.chat_send_player(player_name, "A game is already in progress for player " .. gamestate.player_name) return end @@ -31,8 +31,8 @@ function mario.game_start(pos, player, gamedef) player_name = player_name, pos = pos, player_start = vector.add(pos, (gamedef.player_start or {x=16,y=0,z=1})), - turtle1_start = vector.add(pos, (gamedef.turtle_start or {x=3,y=0.5,z=12})), - turtle2_start = vector.add(pos, (gamedef.turtle_start or {x=30,y=0.5,z=12})), + turtle1_start = vector.add(pos, (gamedef.turtle1_start or {x=3,y=12,z=1})), + turtle2_start = vector.add(pos, (gamedef.turtle1_start or {x=30,y=12,z=1})), coin_total = gamedef.coin_total or 84, speed = gamedef.speed or 2, lives = gamedef.lives or 3, @@ -84,6 +84,7 @@ end -- Resets the game to the start positions function mario.game_reset(id, player) local gamestate = mario.games[id] + if not gamestate then return end minetest.log("action", "resetting game " .. id) -- Save the time when the game was last resetted (to solve timing issues) @@ -98,26 +99,33 @@ function mario.game_reset(id, player) -- Spawn the turtles and assign the game id to each turtle minetest.after(2, function() if mario.games[id] and last_reset == mario.games[id].last_reset then - local turtle = minetest.add_entity(gamestate.turtle_start, "mario:turtle") - turtle:get_luaentity().gameid = id + local turtle = minetest.add_entity(gamestate.turtle1_start, "mario:turtle1") + turtle = turtle:get_luaentity() + turtle.gameid = id + turtle.direction = {x=1,y=0,z=0} end end) minetest.after(2, function() if mario.games[id] and last_reset == mario.games[id].last_reset then - local turtle = minetest.add_entity(gamestate.turtle_start, "mario:turtle2") - turtle:get_luaentity().gameid = id + local turtle = minetest.add_entity(gamestate.turtle2_start, "mario:turtle2") + turtle = turtle:get_luaentity() + turtle.gameid = id + turtle.direction = {x=1,y=0,z=0} end end) minetest.after(45, function() if mario.games[id] and last_reset == mario.games[id].last_reset then - local turtle = minetest.add_entity(gamestate.turtle_start, "mario:turtle3") - turtle:get_luaentity().gameid = id + local turtle = minetest.add_entity(gamestate.turtle1_start, "mario:turtle3") + turtle = turtle:get_luaentity() + turtle.gameid = id + turtle.direction = {x=-1,y=0,z=0} end end) minetest.after(45, function() if mario.games[id] and last_reset == mario.games[id].last_reset then - local turtle = minetest.add_entity(gamestate.turtle_start, "mario:turtle4") - turtle:get_luaentity().gameid = id + local turtle = minetest.add_entity(gamestate.turtle2_start, "mario:turtle4"):get_luaentity() + turtle.gameid = id + turtle.direction = {x=-1,y=0,z=0} end end) end @@ -202,6 +210,27 @@ function mario.on_player_got_mushroom(player, points) minetest.sound_play("mario-bonus", {pos = pos, max_hear_distance = 6}) end +-- The player died! +function mario.on_player_death(id, player) + local gamestate = mario.games[id] + if not gamestate then return end + + gamestate.lives = gamestate.lives - 1 + if gamestate.lives < 1 then + minetest.chat_send_player(gamestate.player_name,"Game Over") + mario.game_end(id) + minetest.sound_play("mario-game-over", {max_hear_distance = 20, object=player}) + elseif gamestate.lives == 1 then + minetest.chat_send_player(gamestate.player_name,"This is your last life") + mario.game_reset(id, player) + minetest.sound_play("mario-continue", {max_hear_distance = 10, object=player}) + else + minetest.chat_send_player(gamestate.player_name,"You have ".. gamestate.lives .." lives left") + mario.game_reset(id, player) + minetest.sound_play("mario-continue", {max_hear_distance = 10, object=player}) + end +end + -- Get the game that the given player is playing function mario.get_game_by_player(player_name) for _,gamestate in pairs(mario.games) do @@ -219,18 +248,15 @@ end 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}, + {x=0.5,y=0.5,z=0.25}, + {x=-0.5,y=0.5,z=-0.25}, } for _,pos in pairs(positions) do pos = vector.add(player_pos, pos) local node = minetest.get_node(pos) - if node.name == "mario:coin_1" then + if node.name == "mario:coin" then minetest.remove_node(pos) mario.on_player_got_coin(player) - elseif node.name == "mario:coin_2" then - minetest.remove_node(pos) - mario.on_player_got_power_coin(player) elseif node.name == "mario:cherrys" then minetest.remove_node(pos) mario.on_player_got_mushroom(player, 100) diff --git a/mario/hud.lua b/mario/hud.lua new file mode 100755 index 0000000..d9bf346 --- /dev/null +++ b/mario/hud.lua @@ -0,0 +1,44 @@ + + +local hud_table = {} + +function mario.update_hud(id, player) + local game = mario.games[id] + player = player or minetest.get_player_by_name(game.player_name) + if not player then + return + elseif not game then + mario.remove_hud(player) + return + end + local coins_left = game.coin_total - game.coin_count + local hudtext = "Score " .. game.score + .. "\nLevel " .. game.level + .. "\nLives " .. game.lives + .. "\nCoins " .. coins_left + + local hud = hud_table[game.player_name] + if not hud then + hud = player:hud_add({ + hud_elem_type = "text", + position = {x = 0, y = 1}, + offset = {x=100, y = -100}, + scale = {x = 100, y = 100}, + number = 0xff2227, --color + text = hudtext + }) + hud_table[game.player_name] = hud + else + player:hud_change(hud, "text", hudtext) + end +end + + +function mario.remove_hud(player, playername) + local name = playername or player:get_player_name() + local hud = hud_table[name] + if hud then + player:hud_remove(hud) + hud_table[name] = nil + end +end diff --git a/mario/init.lua b/mario/init.lua index c435bf7..283492c 100644 --- a/mario/init.lua +++ b/mario/init.lua @@ -5,7 +5,7 @@ dofile(minetest.get_modpath("mario").."/blocks.lua") dofile(minetest.get_modpath("mario").."/portal.lua") dofile(minetest.get_modpath("mario").."/turtle.lua") dofile(minetest.get_modpath("mario").."/gamestate.lua") - +dofile(minetest.get_modpath("mario").."/hud.lua") minetest.register_node("mario:placer",{ description = "Reset", @@ -24,14 +24,12 @@ minetest.register_node("mario:placer",{ local schem = minetest.get_modpath("mario").."/schems/mario.mts" minetest.place_schematic({x=pos.x-1,y=pos.y-2,z=pos.z-2},schem,0, "air", true) player:setpos({x=pos.x+16,y=pos.y+0.1,z=pos.z+1}) - print(name) player:set_physics_override(1,1,0.3,true,false) - -- Left Turtle - minetest.add_entity({x=pos.x+3,y=pos.y+12,z=pos.z+1}, "mario:turtle1") - -- Right Turtle - local turtler = minetest.add_entity({x=pos.x+30,y=pos.y+12,z=pos.z+1}, "mario:turtle1"):get_luaentity() - turtler.direction = {x=-1,y=0,z=0} + mario.game_start(pos, player, { + schematic = minetest.get_modpath("mario").."/schems/mario.mts", + scorename = "mario:classic_board", + }) minetest.sound_play("mario-game-start", {pos = pos,max_hear_distance = 40,gain = 10.0,}) end, @@ -54,6 +52,7 @@ minetest.register_node("mario:placer2",{ minetest.place_schematic({x=pos.x-1,y=pos.y-1,z=pos.z-2},schem,0, "air", true) end, }) + minetest.register_node("mario:exit",{ description = "Exit", tiles = { @@ -73,3 +72,9 @@ minetest.register_node("mario:exit",{ player:set_physics_override(1,1,1,true,false) end, }) + +-- Register with the myhighscore mod +myhighscore.register_game("mario:classic_board", { + description = "Mario", + icon = "mario_border.png^mario_m.png", +}) diff --git a/mario/sounds/mario_coin.ogg b/mario/sounds/mario-coin.ogg Binary files differindex aa789b0..aa789b0 100644 --- a/mario/sounds/mario_coin.ogg +++ b/mario/sounds/mario-coin.ogg diff --git a/mario/turtle.lua b/mario/turtle.lua index 7896f2e..ef9a624 100644 --- a/mario/turtle.lua +++ b/mario/turtle.lua @@ -47,30 +47,19 @@ for i in ipairs(turtles) do if self.timer < 1 then return end self.timer = 0 - local velocity = self.object:getvelocity() - - -- if our velocity is close to zero, turn around (we are in collision) - if math.abs(velocity.x) < 0.25 then - self.direction.x = -self.direction.x - if(self.direction.x == 0) then - self.direction.x = 1 - end - end - self:update_velocity() - end, ---[[ -- Do we have game state? if not just die local gamestate = mario.games[self.gameid] if not gamestate then - minetest.log("action", "Removing pacman ghost without game assigned") + minetest.log("action", "Removing turtle 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 ") + minetest.log("action", "Removing turtle remaining after reset ") self.object:remove() end else @@ -83,6 +72,24 @@ for i in ipairs(turtles) do end local player = self.target + -- find distance to the player + local dist = vector.distance(self.object:getpos(), player:getpos()) + if dist < 1 then + mario.on_player_death(self.gameid, player) + end + + local velocity = self.object:getvelocity() + + -- if our velocity is close to zero, turn around (we are in collision) + if math.abs(velocity.x) < 0.25 then + self.direction.x = -self.direction.x + if(self.direction.x == 0) then + self.direction.x = 1 + end + end + self:update_velocity() + end, +--[[ -- If there's no player just stop if not player then self.set_velocity(self, 0) @@ -139,18 +146,18 @@ for i in ipairs(turtles) do end end end, + --]] -- This function should return the saved state of the entity in a string get_staticdata = function(self) 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) self:update_velocity() self.object:setacceleration(self.acceleration) - --self.object:set_armor_groups({immortal=1}) - --[[ + self.object:set_armor_groups({immortal=1}) if staticdata and staticdata ~= "" then local data = string.split(staticdata, ";") if #data == 2 then @@ -158,7 +165,6 @@ for i in ipairs(turtles) do self.last_reset = tonumber(data[2]) end end - --]] end }) end |