123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import GetForwardVector from 'Assets/Library/GetForwardVector';
- import * as RE from 'rogue-engine';
- import * as THREE from 'three'
- import FloorCheckComponent from './FloorCheckComponent.re';
- import RAPIER from '@dimforge/rapier3d-compat';
- import RapierBody from '@RE/RogueEngine/rogue-rapier/Components/RapierBody.re';
- export default class RapierMovementController extends RE.Component {
- @RE.props.num() speed: number = 1
- @RE.props.num() jumpStrength: number = 10
- vectorCalculator: GetForwardVector
- bodyComponent: RapierBody
- private jumpVector: RAPIER.Vector3 = new RAPIER.Vector3(0,1,0)
- private scaledVelocity: THREE.Vector3 = new THREE.Vector3(0,0,0)
- private rapierScaledVelocity: RAPIER.Vector3 = new RAPIER.Vector3(0,0,0)
- awake() {
- this.bodyComponent = RE.getComponent(RapierBody, this.object3d) as RapierBody
- }
- start() {
- this.vectorCalculator = new GetForwardVector(RE.Runtime.camera)
- }
- update() {
- let direction = {x:0, y: 0, z: 0}
- if (RE.Input.keyboard.getKeyPressed("KeyW")) {
- direction.x += 1
- }
- if (RE.Input.keyboard.getKeyPressed("KeyA")) {
- direction.y += -1
- }
- if (RE.Input.keyboard.getKeyPressed("KeyS")) {
- direction.x += -1
- }
- if (RE.Input.keyboard.getKeyPressed("KeyD")) {
- direction.y += 1
- }
- if (RE.Input.keyboard.getKeyPressed("Space")) {
- direction.z = 1
- }
- if(direction.x != 0) {
- this.moveForward(direction.x * this.speed)
- }
- if(direction.y != 0) {
- this.moveRight(direction.y * this.speed)
- }
- if(direction.z != 0) {
- const floorCheckComponent = RE.getComponent(FloorCheckComponent, this.object3d)
- if(floorCheckComponent) {
- if(floorCheckComponent.isOnFloor) {
- this.jumpVector.y = this.jumpStrength
- this.bodyComponent.body.applyImpulse(this.jumpVector, true)
- }
- }
- }
- }
- moveForward(distance) {
- this.scaledVelocity.set(0,0,0)
- this.scaledVelocity.addScaledVector(this.vectorCalculator.getForward(), distance)
- this.rapierScaledVelocity.x = this.scaledVelocity.x
- this.rapierScaledVelocity.y = this.scaledVelocity.y
- this.rapierScaledVelocity.z = this.scaledVelocity.z
-
- this.bodyComponent.body.applyImpulse(this.rapierScaledVelocity, true)
- }
- moveRight(distance) {
- this.scaledVelocity.set(0,0,0)
- this.scaledVelocity.addScaledVector(this.vectorCalculator.getRight(), distance)
- this.rapierScaledVelocity.x = this.scaledVelocity.x
- this.rapierScaledVelocity.y = this.scaledVelocity.y
- this.rapierScaledVelocity.z = this.scaledVelocity.z
- this.bodyComponent.body.applyImpulse(this.rapierScaledVelocity, true)
- }
- }
- RE.registerComponent(RapierMovementController);
|