Skip to content

Instantly share code, notes, and snippets.

@netpro2k
Last active February 14, 2018 01:01
Show Gist options
  • Save netpro2k/7f91598d32476f97d24285c6d77f17d6 to your computer and use it in GitHub Desktop.
Save netpro2k/7f91598d32476f97d24285c6d77f17d6 to your computer and use it in GitHub Desktop.
Mock actions API
class Game {
init() {
Action.registerActions(actionDefinition);
Action.registerBindings("oculus", bindingDefinition);
// This maybe happens in the lib somewhere? Is the user prompted to select one somehow?
Action.activateBindings("oculus");
Action.activateActionSet("gameplay");
// Generate these automatically from the binding definition?
// ex: Action.pathIds.gameplay.in.jump;
this.jumpPathId = Action.idForPath("/action/gameplay/in/jump");
this.movePathId = Action.idForPath("/action/gameplay/in/move");
this.jumpVibratePath = Action.idForPath("/action/gameplay/out/jump_landing");
this.moveDir = { x: 0, y: 0 };
}
tick(dt) {
Action.tick(dt);
if(this.isGrounded && Action.getBoolOn(this.jumpPathId)) {
this.jump();
}
Action.fillVector2(this.movePathId, this.moveDir);
this.moveCharacter(this.moveDir, dt);
// The fillVector2 function fills an existing object, getVector2 returns a new object
// const moveDir = Action.getVector2(this.movePathId);
if(!this.wasGrounded && this.isGrounded) {
const pulseMilliseconds = 200;
Action.haptic_pulse(this.jumpVibratePath, pulseMilliseconds);
}
this.wasGrounded = this.isGrounded
/// When showing the menu, you would also switch action sets
// Action.activateActionSet("menu");
}
}
const actionDefinition = {
"actions" : [
{
"name" : "/gameplay/in/jump",
"type" : "boolean"
},
{
"name" : "/gameplay/in/move",
"type" : "vector2"
},
{
"name" : "/gameplay/in/cursor_origin",
"type" : "pose"
},
{
"name" : "/gameplay/in/cursor_select",
"type" : "boolean"
},
{
"name" : "/menu/in/next_item",
"type" : "boolean"
},
{
"name" : "/menu/in/prev_item",
"type" : "boolean"
},
{
"name" : "/menu/in/select_item",
"type" : "boolean"
},
{
"name" : "/gameplay/out/object_hovered",
"type" : "vibration"
},
{
"name" : "/gameplay/out/object_selected",
"type" : "vibration"
},
{
"name" : "/gameplay/out/jump_landing",
"type" : "vibration"
},
{
"name" : "/menu/out/select_item",
"type" : "vibration"
}
],
"localization" : [
{
"language_tag" : "en",
"/gameplay/in/jump" : "Jump",
"/gameplay/in/move" : "Move",
"/gameplay/in/cursor_origin" : "Cursor Origin",
"/gameplay/in/cursor_select" : "Cursor Select",
"/menu/in/next_item" : "Next Menu Item",
"/menu/in/prev_item" : "Previous Menu Item",
"/menu/in/select_item" : "Select Menu Item",
"/gameplay/out/cursor_select" : "Cursor selection",
"/gameplay/out/jump_landing" : "Jump Landing",
"/menu/out/select_item" : "Select Menu Item"
}
]
}
const bindingDefinition = {
filters: [
{
name: "inverted_move",
type: "invert_vector2",
properties: {
y: true
},
sources: {
vec: "/devices/oculus_touch/left/thumbstick",
},
},
{
name: "cursor_select_trigger",
type: "simple_threshold",
properties: {
threshold: 0.3
},
sources: {
axis: "/devices/oculus_touch/right/trigger",
},
},
{
name: "menu_dpad",
type: "virtual_dpad",
properties: {
angle: 0.5236
},
sources: {
axis: "/devices/oculus_touch/right/thumbstick",
},
},
// {
// name: "menu_dpad",
// type: "virtual_dpad",
// properties: {
// requires_press: true
// },
// sources: {
// axis: "/devices/vive/right/touchpad",
// pressed: "/devices/vive/right/touchpad_pressed"
// },
// },
],
bindings: [
{
source: "/devices/oculus_touch/left/x_pressed",
destination: "/gameplay/in/jump"
},
{
source: "/filters/invert_vector2/inverted_move",
destination: "/gameplay/in/move",
},
{
source: "/filters/simple_threshold/cursor_select_trigger",
destination: "/gameplay/in/cursor_select",
},
{
source: "/devices/oculus_touch/right/point_pose",
destination: "/gameplay/in/cursor_origin",
},
{
source: "/filters/virtual_dpad/menu_dpad/south",
destination: "/menu/in/next_item",
},
{
source: "/filters/virtual_dpad/menu_dpad/north",
destination: "/menu/in/prev_item",
},
{
source: "/devices/oculus_touch/right/a_pressed",
destination: "/menu/in/select_item",
},
{
source: "/gameplay/out/object_hovered",
destination: "/devices/oculus_touch/right/rumble",
},
{
source: "/gameplay/out/object_selected",
destination: "/devices/oculus_touch/right/rumble",
},
{
source: "/gameplay/out/jump_landing",
destination: "/devices/oculus_touch/left/rumble",
},
{
source: "/menu/out/select_item",
destination: "/devices/oculus_touch/right/rumble",
}
]
}
Action.registerFilter("invert_vector2", {
properties: {
x: { type: "bool", default: false },
y: { type: "bool", default: false }
},
sources: {
vec: { type: "vector2" }
},
destination: { type: "vector2" },
init() { },
tick() { }
});
Action.registerFilter("simple_threshold", {
properties: {
threshold: { type: "float", default: 1 },
},
sources: {
axis: { type: "float" }
},
destination: { type: "bool" },
init() { },
tick() { }
});
Action.registerFilter("virtual_dpad", {
properties: {
deadzone: { type: "float", default: 0.3 },
angle: { type: "float", default: 0 },
requires_press: { type: "bool", default: false },
},
sources: {
vec: { type: "vector2" },
pressed: { type: "bool" }
},
destinations: {
north: { type: "bool" },
south: { type: "bool" },
east: { type: "bool" },
west: { type: "bool" },
center: { type: "bool" },
},
init() { },
tick() { }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment