-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Physics: Refactoring: add API; ability to
:set()|:get()
for player;…
… store localy & merge new velues in `:set()`. Relates to #1666
- Loading branch information
Showing
3 changed files
with
119 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- @alias physics.callback fun(player:Player,physics:physics.PlayerPhysics) | ||
|
||
--- @class physics.Event: base_classes.Event | ||
--- @field on fun(self:self,event:string|physics.Event.Type): fun(callback:physics.callback) | ||
--- @field trigger fun(self:self,event:string|physics.Event.Type, ...): void | ||
local Event = base_classes.Event:extended() | ||
|
||
--- @class physics.Event.Type | ||
Event.Type = { | ||
on_init = 'on_init', | ||
on_change = 'on_change', | ||
} | ||
--- @type table<string,physics.callback[]> | ||
Event.subscribers = { | ||
--- @type physics.callback[] | ||
on_init = {}, | ||
--- @type physics.callback[] | ||
on_change = {}, | ||
} | ||
|
||
|
||
return Event |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
local table_merge, setmetatable | ||
= table.merge, setmetatable | ||
|
||
local Event = require('physics.Event') | ||
|
||
|
||
--- @class physics.PlayerPhysics | ||
local PlayerPhysics = { | ||
--- @type Player | ||
player = nil, | ||
|
||
--- @private | ||
--- @type table<string,number> | ||
physics = nil, | ||
} | ||
|
||
--- Constructor | ||
--- @public | ||
--- @param player Player | ||
--- @param physics table<string,number> | ||
--- @return physics.PlayerPhysics | ||
function PlayerPhysics:new(player, physics) | ||
local class = self | ||
|
||
self = {} | ||
self.player = player | ||
self.physics = physics or {} | ||
|
||
return setmetatable(self, { __index = class }) | ||
end | ||
|
||
--- @param player Player | ||
--- @return physics.PlayerPhysics | ||
function PlayerPhysics:refresh_player(player) | ||
self.player = player | ||
|
||
return self | ||
end | ||
|
||
--- @param physics table | ||
function PlayerPhysics:set(physics) | ||
self.physics = table_merge(self.physics, physics) | ||
|
||
self.player:set_physics_override(table_merge( | ||
self.player:get_physics_override(), self.physics | ||
)) | ||
|
||
Event:trigger(Event.Type.on_change, self.player, self) | ||
end | ||
|
||
--- @overload fun() | ||
--- @param name string | ||
--- @return number|table<string,number> | ||
function PlayerPhysics:get(name) | ||
return name | ||
and (self.physics[name] or nil) | ||
or self.physics | ||
end | ||
|
||
|
||
return PlayerPhysics |