Event Name | Description |
---|---|
OnStartFocusMode | Used to enter Focus mode. |
OnExitFocusMode | Used to exit Focus mode and to perform cleanup. |
OnFocusedInteractionInput events | Used to receive the player’s inputs and process them. |
OnEntityTapped | Used during implementation of the keypad (later in the module). |
// Focused Interaction events
OnStartFocusMode: new hz.NetworkEvent<{exampleController: hz.Entity, cameraPosition: hz.Vec3, cameraRotation: hz.Quaternion}>("OnStartFocusMode"),
OnExitFocusMode: new hz.NetworkEvent<{player: hz.Player}>("OnPlayerExitedExample"),
OnPlayerExitedFocusMode: new hz.NetworkEvent<{player: hz.Player}>("OnPlayerExitedFocusMode"),
OnFocusedInteractionInputStarted: new hz.NetworkEvent<{interactionInfo: hz.InteractionInfo}>("OnFocusedInteractionInputStarted"),
OnFocusedInteractionInputMoved: new hz.NetworkEvent<{interactionInfo: hz.InteractionInfo}>("OnFocusedInteractionInputMoved"),
OnFocusedInteractionInputEnded: new hz.NetworkEvent<{interactionInfo: hz.InteractionInfo}>("OnFocusedInteractionInputEnded"),
OnEntityTapped: new hz.NetworkEvent("OnEntityTapped"),
import {sysEvents} from 'sysEvents';
// TODO: Send the OnPlayerExitedFocusedInteraction event to the local managers to notify that a player exited Focused Interaction and perform any cleanup code (for example, resetting the player's camera)
this.connectCodeBlockEvent(
this.entity,
hz.CodeBlockEvents.OnPlayerExitedFocusedInteraction,
(player: hz.Player) => {
this.sendNetworkBroadcastEvent(sysEvents.OnPlayerExitedFocusMode, {
player: player,
});
},
);
import * as hz from 'horizon/core';
import {sysEvents} from 'sysEvents';
class sysFocusedInteractionManagerServer extends hz.Component<
typeof sysFocusedInteractionManagerServer
> {
static propsDefinition = {};
start() {
// Send the OnPlayerExitedFocusedInteraction event to the local managers to notify that a player exited Focused Interaction and perform any cleanup code (for example, resetting the player's camera)
this.connectCodeBlockEvent(
this.entity,
hz.CodeBlockEvents.OnPlayerExitedFocusedInteraction,
(player: hz.Player) => {
this.sendNetworkBroadcastEvent(sysEvents.OnPlayerExitedFocusMode, {
player: player,
});
},
);
}
}
hz.Component.register(sysFocusedInteractionManagerServer);
// TODO: When the `OnStartFocusMode` event is received, the player will enter Focused Interaction mode and start using an example controller to send inputs to
this.connectNetworkEvent(
this.owningPlayer,
sysEvents.OnStartFocusMode,
data => {
this.activeFocusedInteractionExample = data.exampleController;
this.owningPlayer.enterFocusedInteractionMode();
this.sendNetworkEvent(this.owningPlayer, sysEvents.OnSetCameraModeFixed, {
position: data.cameraPosition,
rotation: data.cameraRotation,
});
},
);
// TODO: When the player exits Focused Interaction mode, reset camera to third person and notify the example controller
this.connectNetworkBroadcastEvent(sysEvents.OnPlayerExitedFocusMode, data => {
if (data.player !== this.owningPlayer) return;
this.sendNetworkEvent(
this.owningPlayer,
sysEvents.OnSetCameraModeThirdPerson,
null,
);
if (this.activeFocusedInteractionExample) {
this.sendNetworkEvent(
this.activeFocusedInteractionExample,
sysEvents.OnExitFocusMode,
{player: this.owningPlayer},
);
this.activeFocusedInteractionExample = undefined;
}
});
// TODO: Tracking Focused Interaction inputs and sending the interaction data to the active example controller
this.connectLocalBroadcastEvent(
hz.PlayerControls.onFocusedInteractionInputStarted,
data => {
const firstInteraction = data.interactionInfo[0];
if (firstInteraction.interactionIndex !== 0) return;
if (this.activeFocusedInteractionExample?.exists()) {
this.sendNetworkEvent(
this.activeFocusedInteractionExample,
sysEvents.OnFocusedInteractionInputStarted,
{interactionInfo: firstInteraction},
);
}
},
);
this.connectLocalBroadcastEvent(
hz.PlayerControls.onFocusedInteractionInputMoved,
data => {
const firstInteraction = data.interactionInfo[0];
if (firstInteraction.interactionIndex !== 0) return;
if (this.activeFocusedInteractionExample?.exists()) {
this.sendNetworkEvent(
this.activeFocusedInteractionExample,
sysEvents.OnFocusedInteractionInputMoved,
{interactionInfo: firstInteraction},
);
}
},
);
this.connectLocalBroadcastEvent(
hz.PlayerControls.onFocusedInteractionInputEnded,
data => {
const firstInteraction = data.interactionInfo[0];
if (firstInteraction.interactionIndex !== 0) return;
if (this.activeFocusedInteractionExample?.exists()) {
this.sendNetworkEvent(
this.activeFocusedInteractionExample,
sysEvents.OnFocusedInteractionInputEnded,
{interactionInfo: firstInteraction},
);
}
},
);
Note: The interactionInfo parameter in these events is an array of interactions. Currently, this parameter contains a single item, as multi-touch is not supported at this time. To future proof our code for multi-touch support, we must check the interactionIndex to ensure we are only handling the touches that are applicable.
import * as hz from 'horizon/core';
import {sysEvents} from 'sysEvents';
class sysFocusedInteractionManagerLocal extends hz.Component<
typeof sysFocusedInteractionManagerLocal
> {
static propsDefinition = {};
private ownedByServer: boolean = true;
private owningPlayer!: hz.Player;
private activeFocusedInteractionExample?: hz.Entity;
private currentTapOptions: hz.FocusedInteractionTapOptions =
hz.DefaultFocusedInteractionTapOptions;
private currentTrailOptions: hz.FocusedInteractionTrailOptions =
hz.DefaultFocusedInteractionTrailOptions;
start() {
this.owningPlayer = this.entity.owner.get();
this.ownedByServer = this.owningPlayer === this.world.getServerPlayer();
// Only the local clients can use Focused Interaction
if (this.ownedByServer) return;
// When the `OnStartFocusMode` event is received, the player will enter Focused Interaction mode and start using an example controller to send inputs to
this.connectNetworkEvent(
this.owningPlayer,
sysEvents.OnStartFocusMode,
data => {
this.activeFocusedInteractionExample = data.exampleController;
this.owningPlayer.enterFocusedInteractionMode();
this.sendNetworkEvent(
this.owningPlayer,
sysEvents.OnSetCameraModeFixed,
{position: data.cameraPosition, rotation: data.cameraRotation},
);
},
);
// When the player exits Focused Interaction mode, reset camera to third person and notify the example controller
this.connectNetworkBroadcastEvent(
sysEvents.OnPlayerExitedFocusMode,
data => {
if (data.player !== this.owningPlayer) return;
this.sendNetworkEvent(
this.owningPlayer,
sysEvents.OnSetCameraModeThirdPerson,
null,
);
if (this.activeFocusedInteractionExample) {
this.sendNetworkEvent(
this.activeFocusedInteractionExample,
sysEvents.OnExitFocusMode,
{player: this.owningPlayer},
);
this.activeFocusedInteractionExample = undefined;
}
},
);
// Tracking Focused Interaction inputs and sending the interaction data to the active example controller
this.connectLocalBroadcastEvent(
hz.PlayerControls.onFocusedInteractionInputStarted,
data => {
const firstInteraction = data.interactionInfo[0];
if (firstInteraction.interactionIndex !== 0) return;
if (this.activeFocusedInteractionExample) {
this.sendNetworkEvent(
this.activeFocusedInteractionExample,
sysEvents.OnFocusedInteractionInputStarted,
{interactionInfo: firstInteraction},
);
}
},
);
this.connectLocalBroadcastEvent(
hz.PlayerControls.onFocusedInteractionInputMoved,
data => {
const firstInteraction = data.interactionInfo[0];
if (firstInteraction.interactionIndex !== 0) return;
if (this.activeFocusedInteractionExample) {
this.sendNetworkEvent(
this.activeFocusedInteractionExample,
sysEvents.OnFocusedInteractionInputMoved,
{interactionInfo: firstInteraction},
);
}
},
);
this.connectLocalBroadcastEvent(
hz.PlayerControls.onFocusedInteractionInputEnded,
data => {
const firstInteraction = data.interactionInfo[0];
if (firstInteraction.interactionIndex !== 0) return;
if (this.activeFocusedInteractionExample) {
this.sendNetworkEvent(
this.activeFocusedInteractionExample,
sysEvents.OnFocusedInteractionInputEnded,
{interactionInfo: firstInteraction},
);
}
},
);
// Customize taps when the `OnSetFocusedInteractionTapOptions` is received
this.connectNetworkEvent(
this.owningPlayer,
sysEvents.OnSetFocusedInteractionTapOptions,
data => {
this.currentTapOptions = {
...this.currentTapOptions,
...data.tapOptions,
};
this.owningPlayer.focusedInteraction.setTapOptions(
data.enabled,
this.currentTapOptions,
);
},
);
// Customize trails when the `OnSetFocusedInteractionTrailOptions` is received
this.connectNetworkEvent(
this.owningPlayer,
sysEvents.OnSetFocusedInteractionTrailOptions,
data => {
this.currentTrailOptions = {
...this.currentTrailOptions,
...data.trailOptions,
};
this.owningPlayer.focusedInteraction.setTrailOptions(
data.enabled,
this.currentTrailOptions,
);
},
);
}
}
hz.Component.register(sysFocusedInteractionManagerLocal);
// TODO: Get all Focused Interaction Managers
this.focusedInteractionManagers = this.world.getEntitiesWithTags(["FIManager"]);
// TODO: Assign a Focused Interaction Manager to the player
if (playerIndex < this.focusedInteractionManagers.length) {
this.focusedInteractionManagers[playerIndex].owner.set(player);
} else {
console.error("Not enough Focused Interaction Managers in the world");
}
// TODO: Release the Focused Interaction Manager from the player
if (playerIndex < this.focusedInteractionManagers.length) {
this.focusedInteractionManagers[playerIndex].owner.set(
this.world.getServerPlayer(),
);
}
import * as hz from 'horizon/core';
class sysPlayerManager extends hz.Component<typeof sysPlayerManager> {
static propsDefinition = {};
private cameraManagers: hz.Entity[] = [];
private focusedInteractionManagers: hz.Entity[] = [];
preStart() {
// Get all camera managers
this.cameraManagers = this.world.getEntitiesWithTags(["CameraManager"]);
// Get all Focused Interaction Managers
this.focusedInteractionManagers = this.world.getEntitiesWithTags(["FIManager"]);
}
start() {
// When a player enters the world assign them a Camera Manager and a Focused Interaction Manager
this.connectCodeBlockEvent(
this.entity,
hz.CodeBlockEvents.OnPlayerEnterWorld,
(player: hz.Player) => {
this.RegisterPlayer(player);
},
);
// When a player exits the world release their Camera Manager and Focused Interaction Manager
this.connectCodeBlockEvent(
this.entity,
hz.CodeBlockEvents.OnPlayerExitWorld,
(player: hz.Player) => {
this.DeregisterPlayer(player);
},
);
}
private RegisterPlayer(player: hz.Player) {
let playerIndex = player.index.get();
// Assign a Camera Manager to the player
if (playerIndex < this.cameraManagers.length) {
this.cameraManagers[playerIndex].owner.set(player);
} else {
console.error("Not enough Camera Managers in the world");
}
// Assign a Focused Interaction Manager to the player
if (playerIndex < this.focusedInteractionManagers.length) {
this.focusedInteractionManagers[playerIndex].owner.set(player);
} else {
console.error("Not enough Focused Interaction Managers in the world");
}
}
private DeregisterPlayer(player: hz.Player) {
let playerIndex = player.index.get();
// Release the Camera Manager from the player
if (playerIndex < this.cameraManagers.length) {
this.cameraManagers[playerIndex].owner.set(this.world.getServerPlayer());
}
// Release the Focused Interaction Manager from the player
if (playerIndex < this.focusedInteractionManagers.length) {
this.focusedInteractionManagers[playerIndex].owner.set(
this.world.getServerPlayer(),
);
}
}
}
hz.Component.register(sysPlayerManager);