@quenty/characterutils 5.0.0 → 5.0.1-canary.269.5b9ce8a.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/package.json +6 -6
- package/src/Shared/RxCharacterUtils.lua +103 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [5.0.1-canary.269.5b9ce8a.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/characterutils@5.0.0...@quenty/characterutils@5.0.1-canary.269.5b9ce8a.0) (2022-06-24)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Add RxCharacterUtils ([#262](https://github.com/Quenty/NevermoreEngine/issues/262)) ([760a912](https://github.com/Quenty/NevermoreEngine/commit/760a9121368f17eedc5bfca2decec66444811dfe))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [5.0.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/characterutils@4.2.0...@quenty/characterutils@5.0.0) (2022-05-21)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @quenty/characterutils
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/characterutils",
|
|
3
|
-
"version": "5.0.0",
|
|
3
|
+
"version": "5.0.1-canary.269.5b9ce8a.0",
|
|
4
4
|
"description": "CharacterUtils",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -25,13 +25,13 @@
|
|
|
25
25
|
"Quenty"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@quenty/deferred": "
|
|
29
|
-
"@quenty/loader": "
|
|
30
|
-
"@quenty/maid": "
|
|
31
|
-
"@quenty/promise": "
|
|
28
|
+
"@quenty/deferred": "2.1.0",
|
|
29
|
+
"@quenty/loader": "5.0.0",
|
|
30
|
+
"@quenty/maid": "2.3.0",
|
|
31
|
+
"@quenty/promise": "5.0.0"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "5b9ce8a8da36bff12861cddfe093808c6f0a8b66"
|
|
37
37
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
--[=[
|
|
2
|
+
Utilities for observing characters and their humanoids.
|
|
3
|
+
@class RxCharacterUtils
|
|
4
|
+
]=]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local Brio = require("Brio")
|
|
9
|
+
local Maid = require("Maid")
|
|
10
|
+
local Observable = require("Observable")
|
|
11
|
+
local RxBrioUtils = require("RxBrioUtils")
|
|
12
|
+
local RxInstanceUtils = require("RxInstanceUtils")
|
|
13
|
+
|
|
14
|
+
local RxCharacterUtils = {}
|
|
15
|
+
|
|
16
|
+
--[=[
|
|
17
|
+
Observe a player's last character.
|
|
18
|
+
@param player Player
|
|
19
|
+
@return Observable<Brio<Model>>
|
|
20
|
+
]=]
|
|
21
|
+
function RxCharacterUtils.observeLastCharacterBrio(player: Player)
|
|
22
|
+
-- This assumes a player's 'Character' field is set to nil when
|
|
23
|
+
-- their character is destroyed, or when they leave the game.
|
|
24
|
+
return RxInstanceUtils.observePropertyBrio(player, "Character", function(character)
|
|
25
|
+
return character ~= nil
|
|
26
|
+
end)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
--[=[
|
|
30
|
+
Observe a player's last humanoid. Note that it may not be alive!
|
|
31
|
+
@param player Player
|
|
32
|
+
@return Observable<Brio<Humanoid>>
|
|
33
|
+
]=]
|
|
34
|
+
function RxCharacterUtils.observeLastHumanoidBrio(player: Player)
|
|
35
|
+
return RxCharacterUtils.observeLastCharacterBrio(player):Pipe({
|
|
36
|
+
RxBrioUtils.switchMapBrio(function(character)
|
|
37
|
+
return RxInstanceUtils.observeLastNamedChildBrio(character, "Humanoid", "Humanoid")
|
|
38
|
+
end);
|
|
39
|
+
})
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
--[[
|
|
43
|
+
Returns an observable that emits a single brio with the value of the given humanoid.
|
|
44
|
+
When the humanoid dies, the brio is killed and the subscription completes.
|
|
45
|
+
If the humanoid is dead on subscription, the observable immediately completes with nothing emitted.
|
|
46
|
+
@param humanoid Humanoid
|
|
47
|
+
@return Observable<Brio<Humanoid>>
|
|
48
|
+
]]
|
|
49
|
+
local function observeHumanoidLifetimeAsBrio(humanoid: Humanoid)
|
|
50
|
+
return Observable.new(function(sub)
|
|
51
|
+
local function onDeath()
|
|
52
|
+
sub:Complete()
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
if humanoid.Health > 0 then
|
|
56
|
+
local maid = Maid.new()
|
|
57
|
+
|
|
58
|
+
maid._brio = Brio.new(humanoid)
|
|
59
|
+
sub:Fire(maid._brio)
|
|
60
|
+
|
|
61
|
+
-- Died can fire multiple times, but it's ok as we disconnect immediately.
|
|
62
|
+
maid:GiveTask(humanoid.Died:Connect(onDeath))
|
|
63
|
+
|
|
64
|
+
return maid
|
|
65
|
+
else
|
|
66
|
+
onDeath()
|
|
67
|
+
end
|
|
68
|
+
end)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
--[=[
|
|
72
|
+
Observes a player's last living humanoid.
|
|
73
|
+
|
|
74
|
+
```lua
|
|
75
|
+
local Players = game:GetService("Players")
|
|
76
|
+
|
|
77
|
+
maid:GiveTask(
|
|
78
|
+
RxCharacterUtils.observeLastAliveHumanoidBrio(Players.LocalPlayer)
|
|
79
|
+
:Subscribe(function(humanoidBrio)
|
|
80
|
+
local humanoid: Humanoid = humanoidBrio:GetValue()
|
|
81
|
+
local humanoidMaid = humanoidBrio:ToMaid()
|
|
82
|
+
|
|
83
|
+
print("Humanoid:", humanoid)
|
|
84
|
+
|
|
85
|
+
humanoidMaid:GiveTask(function()
|
|
86
|
+
-- The maid cleans up on humanoid death, or when given player leaves the game.
|
|
87
|
+
print("Humanoid has been killed or destroyed!")
|
|
88
|
+
end)
|
|
89
|
+
end)
|
|
90
|
+
)
|
|
91
|
+
```
|
|
92
|
+
@param player Player
|
|
93
|
+
@return Observable<Brio<Humanoid>>
|
|
94
|
+
]=]
|
|
95
|
+
function RxCharacterUtils.observeLastAliveHumanoidBrio(player: Player)
|
|
96
|
+
return RxCharacterUtils.observeLastHumanoidBrio(player):Pipe({
|
|
97
|
+
RxBrioUtils.switchMapBrio(function(humanoid)
|
|
98
|
+
return observeHumanoidLifetimeAsBrio(humanoid)
|
|
99
|
+
end);
|
|
100
|
+
})
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
return RxCharacterUtils
|