Skip to content

Commit

Permalink
feat(input): use axes for movement on simple controller
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaisthorpe committed Jul 5, 2024
1 parent d11e903 commit 78b1ec8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 141 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-files-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tedengine/ted': minor
---

Add movement using axes to simple controller
53 changes: 6 additions & 47 deletions apps/docs/src/cameras/fixed-axis-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import {
TFixedAxisCameraController,
TOrbitCamera,
} from '@tedengine/ted';
import type { TController, TActorWithOnUpdate } from '@tedengine/ted';
import type { TActorWithOnUpdate } from '@tedengine/ted';

class Cube extends TPawn implements TActorWithOnUpdate {
private speed = 10;
private isDown: { [key: string]: boolean } = {};

constructor(engine: TEngine, x: number, y: number, z: number) {
super();
Expand All @@ -33,57 +32,17 @@ class Cube extends TPawn implements TActorWithOnUpdate {
}

async onUpdate(): Promise<void> {
this.controller?.update();
if (!this.controller) return;

const force = vec3.fromValues(0, 0, 0);

if (this.isDown['left']) {
force[0] -= this.speed;
}

if (this.isDown['right']) {
force[0] += this.speed;
}
this.controller.update();

if (this.isDown['up']) {
force[2] -= this.speed;
}
const force = vec3.fromValues(0, 0, 0);

if (this.isDown['down']) {
force[2] += this.speed;
}
force[0] += this.speed * this.controller.getAxisValue('Horizontal');
force[2] -= this.speed * this.controller.getAxisValue('Vertical');

this.rootComponent.applyCentralForce(force);
}

public setupController(controller: TController): void {
super.setupController(controller);

controller.bindAction('Up', 'pressed', this.pressed('up').bind(this));
controller.bindAction('Up', 'released', this.released('up').bind(this));
controller.bindAction('Left', 'pressed', this.pressed('left').bind(this));
controller.bindAction('Left', 'released', this.released('left').bind(this));
controller.bindAction('Down', 'pressed', this.pressed('down').bind(this));
controller.bindAction('Down', 'released', this.released('down').bind(this));
controller.bindAction('Right', 'pressed', this.pressed('right').bind(this));
controller.bindAction(
'Right',
'released',
this.released('right').bind(this),
);
}

private pressed(key: string) {
return () => {
this.isDown[key] = true;
};
}

private released(key: string) {
return () => {
this.isDown[key] = false;
};
}
}

class Plane extends TActor {
Expand Down
53 changes: 6 additions & 47 deletions apps/docs/src/cameras/follow-component-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import {
TOrbitCamera,
TFollowComponentCameraController,
} from '@tedengine/ted';
import type { TController, TActorWithOnUpdate } from '@tedengine/ted';
import type { TActorWithOnUpdate } from '@tedengine/ted';

class Cube extends TPawn implements TActorWithOnUpdate {
private speed = 10;
private isDown: { [key: string]: boolean } = {};

constructor(engine: TEngine, x: number, y: number, z: number) {
super();
Expand All @@ -33,57 +32,17 @@ class Cube extends TPawn implements TActorWithOnUpdate {
}

async onUpdate(): Promise<void> {
this.controller?.update();
if (!this.controller) return;

const force = vec3.fromValues(0, 0, 0);

if (this.isDown['left']) {
force[0] -= this.speed;
}

if (this.isDown['right']) {
force[0] += this.speed;
}
this.controller.update();

if (this.isDown['up']) {
force[2] -= this.speed;
}
const force = vec3.fromValues(0, 0, 0);

if (this.isDown['down']) {
force[2] += this.speed;
}
force[0] += this.speed * this.controller.getAxisValue('Horizontal');
force[2] -= this.speed * this.controller.getAxisValue('Vertical');

this.rootComponent.applyCentralForce(force);
}

public setupController(controller: TController): void {
super.setupController(controller);

controller.bindAction('Up', 'pressed', this.pressed('up').bind(this));
controller.bindAction('Up', 'released', this.released('up').bind(this));
controller.bindAction('Left', 'pressed', this.pressed('left').bind(this));
controller.bindAction('Left', 'released', this.released('left').bind(this));
controller.bindAction('Down', 'pressed', this.pressed('down').bind(this));
controller.bindAction('Down', 'released', this.released('down').bind(this));
controller.bindAction('Right', 'pressed', this.pressed('right').bind(this));
controller.bindAction(
'Right',
'released',
this.released('right').bind(this),
);
}

private pressed(key: string) {
return () => {
this.isDown[key] = true;
};
}

private released(key: string) {
return () => {
this.isDown[key] = false;
};
}
}

class Plane extends TActor {
Expand Down
53 changes: 6 additions & 47 deletions apps/docs/src/physics/forces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import {
TPawn,
TSimpleController,
} from '@tedengine/ted';
import type { TController, TActorWithOnUpdate } from '@tedengine/ted';
import type { TActorWithOnUpdate } from '@tedengine/ted';

class Cube extends TPawn implements TActorWithOnUpdate {
private speed = 10;
private isDown: { [key: string]: boolean } = {};

constructor(engine: TEngine, x: number, y: number, z: number) {
super();
Expand All @@ -31,57 +30,17 @@ class Cube extends TPawn implements TActorWithOnUpdate {
}

async onUpdate(): Promise<void> {
this.controller?.update();
if (!this.controller) return;

const force = vec3.fromValues(0, 0, 0);

if (this.isDown['left']) {
force[0] -= this.speed;
}

if (this.isDown['right']) {
force[0] += this.speed;
}
this.controller.update();

if (this.isDown['up']) {
force[2] -= this.speed;
}
const force = vec3.fromValues(0, 0, 0);

if (this.isDown['down']) {
force[2] += this.speed;
}
force[0] += this.speed * this.controller.getAxisValue('Horizontal');
force[2] -= this.speed * this.controller.getAxisValue('Vertical');

this.rootComponent.applyCentralForce(force);
}

public setupController(controller: TController): void {
super.setupController(controller);

controller.bindAction('Up', 'pressed', this.pressed('up').bind(this));
controller.bindAction('Up', 'released', this.released('up').bind(this));
controller.bindAction('Left', 'pressed', this.pressed('left').bind(this));
controller.bindAction('Left', 'released', this.released('left').bind(this));
controller.bindAction('Down', 'pressed', this.pressed('down').bind(this));
controller.bindAction('Down', 'released', this.released('down').bind(this));
controller.bindAction('Right', 'pressed', this.pressed('right').bind(this));
controller.bindAction(
'Right',
'released',
this.released('right').bind(this),
);
}

private pressed(key: string) {
return () => {
this.isDown[key] = true;
};
}

private released(key: string) {
return () => {
this.isDown[key] = false;
};
}
}

class Plane extends TActor {
Expand Down
5 changes: 5 additions & 0 deletions packages/ted/src/input/simple-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export default class TSimpleController extends TController {
super(engine.events);

// Movement
this.addAxisFromKeyEvent('Vertical', 'w', 1);
this.addAxisFromKeyEvent('Vertical', 's', -1);
this.addAxisFromKeyEvent('Horizontal', 'a', -1);
this.addAxisFromKeyEvent('Horizontal', 'd', 1);

this.addActionFromKeyEvent('Up', 'w');
this.addActionFromKeyEvent('Left', 'a');
this.addActionFromKeyEvent('Down', 's');
Expand Down

0 comments on commit 78b1ec8

Please sign in to comment.