Skip to content

Commit

Permalink
Control panel formatting, working input modifiers, elevator and arm b…
Browse files Browse the repository at this point in the history
…ehavior
  • Loading branch information
LucaHaverty committed Jun 18, 2024
1 parent bbece64 commit ffc546f
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 119 deletions.
2 changes: 1 addition & 1 deletion fission/src/mirabuf/MirabufSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { LayerReserve } from "@/systems/physics/PhysicsSystem";
import Mechanism from "@/systems/physics/Mechanism";
import SynthesisBrain from "@/systems/simulation/synthesis_brain/SynthesisBrain";

const DEBUG_BODIES = true;
const DEBUG_BODIES = false;

interface RnDebugMeshes {
colliderMesh: THREE.Mesh;
Expand Down
22 changes: 11 additions & 11 deletions fission/src/modals/configuring/ChangeInputsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import InputSystem from "@/systems/input/InputSystem"

// capitalize first letter
const transformKeyName = (control: Input) => {
let suffix = ""
let prefix = ""
if (control.modifiers) {
if (control.modifiers.meta) suffix += " + Meta"
if (control.modifiers.shift) suffix += " + Shift"
if (control.modifiers.ctrl) suffix += " + Ctrl"
if (control.modifiers.alt) suffix += " + Alt"
if (control.modifiers.meta) prefix += "Meta + "
if (control.modifiers.shift) prefix += "Shift + "
if (control.modifiers.ctrl) prefix += "Ctrl + "
if (control.modifiers.alt) prefix += "Alt + "
}
return control.keybind[0].toUpperCase() + control.keybind.substring(1) + suffix
return prefix + control.keybind[0].toUpperCase() + control.keybind.substring(1)
}

const ChangeInputsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
const [loadedRobot, setLoadedRobot] = useState<string>("")
const [selectedInput, setSelectedInput] = useState<string>("")
const [chosenKey, setChosenKey] = useState<string>("")
const [modifierState, setModifierState] = useState<ModifierState>({})
const [modifierState, setModifierState] = useState<ModifierState>({ctrl: false, alt: false, shift: false, meta: false})

useEffect(() => {
setTimeout(() => setLoadedRobot("Dozer v9"), 2_000)
setTimeout(() => setLoadedRobot("Dozer v9"), 1)
})

if (selectedInput && chosenKey) {
Expand All @@ -34,7 +34,7 @@ const ChangeInputsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
selected.modifiers = modifierState
setChosenKey("")
setSelectedInput("")
setModifierState({})
setModifierState({ctrl: false, alt: false, shift: false, meta: false})
}

return (
Expand All @@ -58,7 +58,7 @@ const ChangeInputsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
{Object.values(InputSystem.robotInputs).map(c => (
<LabeledButton
key={c.name}
label={c.name}
label={InputSystem.toTitleCase(c.name)}
placement={LabelPlacement.Left}
value={
c.name == selectedInput
Expand Down Expand Up @@ -91,7 +91,7 @@ const ChangeInputsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
{Object.values(InputSystem.globalInputs).map(c => (
<LabeledButton
key={c.name}
label={c.name}
label={InputSystem.toTitleCase(c.name)}
placement={LabelPlacement.Left}
value={
c.name == selectedInput
Expand Down
64 changes: 43 additions & 21 deletions fission/src/systems/input/InputSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@ import WorldSystem from "../WorldSystem";

declare global {
type ModifierState = {
alt?: boolean
ctrl?: boolean
shift?: boolean
meta?: boolean
alt: boolean
ctrl: boolean
shift: boolean
meta: boolean
}

type Input = {
name: string
keybind: string
isGlobal: boolean
modifiers?: ModifierState
modifiers: ModifierState
}
}

// When a robot is loaded, default inputs replace any unassigned inputs
const defaultInputs: { [key: string]: Input } = {
"intake": { name: "intake", keybind: "e", isGlobal: true },
"shootGamepiece": { name: "shootGamepiece", keybind: "q", isGlobal: true },
"enableGodMode": { name: "enableGodMode", keybind: "g", isGlobal: true },

"arcadeForward": { name: "arcadeForward", keybind: "w", isGlobal: false },
"arcadeBackward": { name: "arcadeBackward", keybind: "s", isGlobal: false },
"arcadeLeft": { name: "arcadeLeft", keybind: "a", isGlobal: false },
"arcadeRight": { name: "arcadeRight", keybind: "d", isGlobal: false },
"intake": { name: "intake", keybind: "e", isGlobal: true, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
"shootGamepiece": { name: "shootGamepiece", keybind: "q", isGlobal: true, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
"enableGodMode": { name: "enableGodMode", keybind: "g", isGlobal: true, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },

"arcadeForward": { name: "arcadeForward", keybind: "w", isGlobal: false, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
"arcadeBackward": { name: "arcadeBackward", keybind: "s", isGlobal: false, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
"arcadeLeft": { name: "arcadeLeft", keybind: "a", isGlobal: false, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
"arcadeRight": { name: "arcadeRight", keybind: "d", isGlobal: false, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
"armPositive": { name: "armPositive", keybind: "1", isGlobal: false, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
"armNegative": { name: "armNegative", keybind: "2", isGlobal: false, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
"elevatorNegative": { name: "elevatorNegative", keybind: "4", isGlobal: false, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
"elevatorPositive": { name: "elevatorPositive", keybind: "3", isGlobal: false, modifiers: { ctrl: false, alt: false, shift: false, meta: false } },
}

class InputSystem extends WorldSystem {
Expand All @@ -44,7 +48,7 @@ class InputSystem extends WorldSystem {
}

// A list of keys currently being pressed
private static keysPressed: { [key: string]: boolean } = {};
private static _keysPressed: { [key: string]: boolean } = {};

constructor() {
super();
Expand All @@ -67,8 +71,10 @@ class InputSystem extends WorldSystem {
}

// #region WorldSystem Functions

public Update(_: number): void { }
static _currentModifierState: ModifierState;
public Update(_: number): void {InputSystem
InputSystem._currentModifierState = { ctrl: InputSystem.isKeyPressed("Control"), alt: InputSystem.isKeyPressed("Alt"), shift: InputSystem.isKeyPressed("Shift"), meta: InputSystem.isKeyPressed("Meta") }
}

public Destroy(): void {
document.removeEventListener('keydown', this.handleKeyDown);
Expand All @@ -79,25 +85,31 @@ class InputSystem extends WorldSystem {
// #region Input Events

handleKeyDown(event: KeyboardEvent) {
InputSystem.keysPressed[event.key] = true;
InputSystem._keysPressed[event.key] = true;
}

handleKeyUp(event: KeyboardEvent) {
InputSystem.keysPressed[event.key] = false;
InputSystem._keysPressed[event.key] = false;
}

// #endregion
// #region Get Inputs

private static isKeyPressed(key: string): boolean {
return !!InputSystem.keysPressed[key];
return !!InputSystem._keysPressed[key];
}

public static getInput(inputName: string) : boolean {
// Checks if there is a global control for this action
if (inputName in this.allInputs) {
// TODO: support for control modifiers
return this.isKeyPressed(this.allInputs[inputName].keybind);
let targetInput = this.allInputs[inputName];

Check failure on line 105 in fission/src/systems/input/InputSystem.ts

View workflow job for this annotation

GitHub Actions / ESLint Format Validation

'targetInput' is never reassigned. Use 'const' instead

// Check for input modifiers

if (!this.compareModifiers(InputSystem._currentModifierState, targetInput.modifiers))
return false;

return this.isKeyPressed(targetInput.keybind);
}

// If the input does not exist, returns false
Expand All @@ -108,7 +120,17 @@ class InputSystem extends WorldSystem {
return (this.getInput(positive) ? 1 : 0) - (this.getInput(negative) ? 1 : 0);
}

public static toTitleCase(camelCase: string) : string {
const result = camelCase.replace(/([A-Z])/g, " $1");
const finalResult = result.charAt(0).toUpperCase() + result.slice(1);
return finalResult;
}

// #endregion

private static compareModifiers(state1: ModifierState, state2: ModifierState) : boolean {
return state1.alt == state2.alt && state1.ctrl == state2.ctrl && state1.meta == state2.meta && state1.shift == state2.shift;
}
}

export default InputSystem;
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class ArcadeDriveBehavior extends Behavior {
leftWheels: WheelDriver[];
rightWheels: WheelDriver[];

private driveSpeed = 30;
private turnSpeed = 30;
private _driveSpeed = 30;
private _turnSpeed = 30;

constructor(leftWheels: WheelDriver[], rightWheels: WheelDriver[], leftStimuli: WheelRotationStimulus[], rightStimuli: WheelRotationStimulus[]) {
super(leftWheels.concat(rightWheels), leftStimuli.concat(rightStimuli));
Expand All @@ -25,8 +25,8 @@ class ArcadeDriveBehavior extends Behavior {
}

public Update(_: number): void {
this.driveSpeeds(InputSystem.getAxis("arcadeForward", "arcadeBackward")*this.driveSpeed,
InputSystem.getAxis("arcadeRight", "arcadeLeft")*this.turnSpeed);
this.driveSpeeds(InputSystem.getAxis("arcadeForward", "arcadeBackward")*this._driveSpeed,
InputSystem.getAxis("arcadeRight", "arcadeLeft")*this._turnSpeed);
}
}

Expand Down
47 changes: 19 additions & 28 deletions fission/src/systems/simulation/behavior/GenericArmBehavior.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
// import HingeDriver from "../driver/HingeDriver";
// import WheelDriver from "../driver/WheelDriver";
// import HingeStimulus from "../stimulus/HingeStimulus";
// import WheelRotationStimulus from "../stimulus/WheelStimulus";
// import Behavior from "./Behavior";
// import InputSystem from "@/systems/input/InputSystem";
import HingeDriver from "../driver/HingeDriver";
import HingeStimulus from "../stimulus/HingeStimulus";
import Behavior from "./Behavior";
import InputSystem from "@/systems/input/InputSystem";

// class ArcadeDriveBehavior extends Behavior {
// private _hingeDriver: HingeDriver;
class GenericArmBehavior extends Behavior {
private _hingeDriver: HingeDriver;

// private driveSpeed = 30;
// private turnSpeed = 30;
private _rotationalSpeed = 30;

// constructor(hingeDriver: HingeDriver, hingeStimulus: HingeStimulus) {
// super(leftWheels.concat(rightWheels), leftStimuli.concat(rightStimuli));
// this.leftWheels = leftWheels;
// this.rightWheels = rightWheels;
// }
constructor(hingeDriver: HingeDriver, hingeStimulus: HingeStimulus) {
super([hingeDriver], [hingeStimulus]);
this._hingeDriver = hingeDriver;
}

// driveSpeeds(linearVelocity: number, rotationVelocity: number) {
// let leftSpeed = linearVelocity + rotationVelocity;
// let rightSpeed = linearVelocity - rotationVelocity;

// this.leftWheels.forEach((wheel) => wheel.targetWheelSpeed = leftSpeed);
// this.rightWheels.forEach((wheel) => wheel.targetWheelSpeed = rightSpeed);
// }
rotateArm(rotationalVelocity: number) {
this._hingeDriver.targetVelocity = rotationalVelocity;
}

// public Update(_: number): void {
// this.driveSpeeds(InputSystem.getAxis("arcadeForward", "arcadeBackward")*this.driveSpeed,
// InputSystem.getAxis("arcadeRight", "arcadeLeft")*this.turnSpeed);
// }
// }
public Update(_: number): void {
this.rotateArm(InputSystem.getAxis("armPositive", "armNegative")*this._rotationalSpeed);
}
}

// export default ArcadeDriveBehavior;
export default GenericArmBehavior;
28 changes: 28 additions & 0 deletions fission/src/systems/simulation/behavior/GenericElevatorBehavior.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import SliderDriver from "../driver/SliderDriver";
import SliderStimulus from "../stimulus/SliderStimulus";
import Behavior from "./Behavior";
import InputSystem from "@/systems/input/InputSystem";

class GenericElevatorBehavior extends Behavior {
private _sliderDriver: SliderDriver;

private _linearSpeed = 1;

private _inverted: boolean;

constructor(sliderDriver: SliderDriver, sliderStimulus: SliderStimulus, inverted: boolean) {
super([sliderDriver], [sliderStimulus]);
this._sliderDriver = sliderDriver;
this._inverted = inverted;
}

moveElevator(positionDelta: number) {
this._sliderDriver.targetPosition += positionDelta * (this._inverted ? -1 : 1);
}

public Update(deltaT: number): void {
this.moveElevator(InputSystem.getAxis("elevatorPositive", "elevatorNegative")*this._linearSpeed*deltaT);
}
}

export default GenericElevatorBehavior;
Loading

0 comments on commit ffc546f

Please sign in to comment.