Skip to content

Commit

Permalink
Physics: Refactoring: extract Game/lord_physics. Closes #1666
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 authored and Doloment committed Sep 16, 2024
1 parent 3f2208b commit 2e3caf4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 39 deletions.
2 changes: 1 addition & 1 deletion mods/lord/Core/physics/mod.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name = physics
depends = dev_helpers, builtin, lord_equipment
depends = dev_helpers, builtin
38 changes: 0 additions & 38 deletions mods/lord/Core/physics/src/physics.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
local ipairs
= ipairs

local PlayerPhysics = require('physics.PlayerPhysics')
local Event = require('physics.Event')


--- @type table<number,string>|string[]
local PHYSICS_TYPES = { "jump", "speed", "gravity" }

--- @type physics.PlayerPhysics[]|table<string,physics.PlayerPhysics>
local player_physics = {}


physics = {} -- luacheck: ignore unused global variable physics

local function register_api()
Expand All @@ -37,43 +30,12 @@ local function register_api()
}
end

--- @param player Player
local function collect_physics(player)
local physics = { speed = 1, gravity = 1, jump = 1 }
for _, stack in equipment.for_player(player):items(equipment.Kind.ARMOR) do
if stack:get_count() == 1 then
local item_groups = stack:get_definition().groups
for _, type in ipairs(PHYSICS_TYPES) do
local value = item_groups["physics_" .. type]
if value then
physics[type] = (physics[type] or 1) + value
end
end
end
end

return physics
end

--- @param player Player
local function set_player_physics(player)
local physics_o = collect_physics(player)

physics.for_player(player):set(physics_o)
end


return {
--- @param mod minetest.Mod
init = function(mod)
register_api()

equipment.on_load(equipment.Kind.ARMOR, function(player)
set_player_physics(player)
end)
equipment.on_change(equipment.Kind.ARMOR, function(player)
set_player_physics(player)
end)
minetest.register_on_leaveplayer(function(player)
player_physics[player:get_player_name()] = nil
end)
Expand Down
5 changes: 5 additions & 0 deletions mods/lord/Game/lord_physics/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


minetest.mod(function(mod)
require("lord_physics").init(mod)
end)
2 changes: 2 additions & 0 deletions mods/lord/Game/lord_physics/mod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = lord_physics
depends = builtin, physics, lord_equipment
42 changes: 42 additions & 0 deletions mods/lord/Game/lord_physics/src/lord_physics.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local ipairs
= ipairs

--- @type table<number,string>|string[]
local PHYSICS_TYPES = { "jump", "speed", "gravity" }

--- @param player Player
local function collect_physics_from_armor(player)
local physics = { speed = 1, gravity = 1, jump = 1 }
for _, stack in equipment.for_player(player):items(equipment.Kind.ARMOR) do
if stack:get_count() == 1 then
local item_groups = stack:get_definition().groups
for _, type in ipairs(PHYSICS_TYPES) do
local value = item_groups["physics_" .. type]
if value then
physics[type] = (physics[type] or 1) + value
end
end
end
end

return physics
end

--- @param player Player
local function set_player_physics(player)
local physics_o = collect_physics_from_armor(player)

physics.for_player(player):set(physics_o)
end


return {
init = function()
equipment.on_load(equipment.Kind.ARMOR, function(player)
set_player_physics(player)
end)
equipment.on_change(equipment.Kind.ARMOR, function(player)
set_player_physics(player)
end)
end,
}

0 comments on commit 2e3caf4

Please sign in to comment.