basic tool handling
This commit is contained in:
parent
3b327ea260
commit
29541f3dc5
6 changed files with 90 additions and 2 deletions
|
@ -0,0 +1,37 @@
|
|||
import { None, useEvent, World } from "@rbxts/matter"
|
||||
import { PlayerCharacter } from "ReplicatedStorage/ecs/components"
|
||||
|
||||
/**
|
||||
* Equipped tool is replicated into the ECS.
|
||||
*/
|
||||
function playerCharacterToolIsTool(world: World): void {
|
||||
for (const [id, character] of world.query(PlayerCharacter)) {
|
||||
for (const [_, toolInstance] of useEvent(character.character, "ChildAdded")) {
|
||||
if (!classIs(toolInstance, "Tool")) continue
|
||||
|
||||
world.insert(
|
||||
id,
|
||||
character.patch({
|
||||
equippedTool: toolInstance
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
for (const [_, toolInstance] of useEvent(character.character, "ChildRemoved")) {
|
||||
// classIs more like classWas
|
||||
if (!classIs(toolInstance, "Tool")) continue
|
||||
|
||||
// could also be swapping tools
|
||||
const swappedTool = character.character.FindFirstChildOfClass("Tool")
|
||||
|
||||
world.insert(
|
||||
id,
|
||||
character.patch({
|
||||
equippedTool: swappedTool ? swappedTool : None
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export = playerCharacterToolIsTool
|
|
@ -9,7 +9,8 @@ type ComponentConstructor = (typeof Components)[ComponentName]
|
|||
|
||||
const REPLICATED_COMPONENT_NAMES: readonly ComponentName[] = [
|
||||
"Model",
|
||||
"Transform"
|
||||
"Transform",
|
||||
"PlayerCharacter"
|
||||
]
|
||||
|
||||
const replicatedComponents: ReadonlySet<ComponentConstructor> = REPLICATED_COMPONENT_NAMES.reduce(
|
||||
|
|
23
src/ServerScriptService/ecs/systems/server/toolHandler.ts
Normal file
23
src/ServerScriptService/ecs/systems/server/toolHandler.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { World, useEvent } from "@rbxts/matter"
|
||||
import { PlayerCharacter } from "ReplicatedStorage/ecs/components"
|
||||
import { ServerState } from "ReplicatedStorage/ecs/state"
|
||||
import { getEvent } from "ReplicatedStorage/remotes"
|
||||
|
||||
function toolHandler(world: World, state: ServerState): void {
|
||||
const activateToolEvent = getEvent("activateToolEvent")
|
||||
const equipToolEvent = getEvent("equipToolEvent")
|
||||
|
||||
for (const [_, character] of world.query(PlayerCharacter)) {
|
||||
for (const [_, player] of useEvent(activateToolEvent, "OnServerEvent")) {
|
||||
state.logger.Info("{@player} activated the tool {@tool}", player, character.equippedTool)
|
||||
if (character.player !== player) continue
|
||||
}
|
||||
|
||||
for (const [_, player] of useEvent(equipToolEvent, "OnServerEvent")) {
|
||||
state.logger.Info("{@player} equipped the tool {@tool}", player, character.equippedTool)
|
||||
if (character.player !== player) continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export = toolHandler
|
Loading…
Add table
Add a link
Reference in a new issue