import CannonBody from '@RE/BeardScript/rogue-cannon/Components/CannonBody.re'; import GetForwardVector from 'Assets/Library/GetForwardVector'; import * as CANNON from 'cannon-es'; import * as RE from 'rogue-engine'; import * as THREE from 'three' import FloorCheckComponent from './FloorCheckComponent.re'; export default class CannonMovementController extends RE.Component { @RE.props.num() speed: number = 1 @RE.props.num() jumpStrength: number = 3 vectorCalculator: GetForwardVector bodyComponent: CannonBody awake() { this.bodyComponent = RE.getComponent(CannonBody, this.object3d) as CannonBody } 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.bodyComponent.body.applyImpulse(new CANNON.Vec3(0, this.jumpStrength, 0), new CANNON.Vec3(this.object3d.position.x, this.object3d.position.y,this.object3d.position.z)) // this.bodyComponent.body.applyForce(new CANNON.Vec3(0,1,0).scale(this.jumpStrength)) } } } } moveForward(distance) { let scaledVelocity = new THREE.Vector3() scaledVelocity.addScaledVector(this.vectorCalculator.getForward(), distance) this.bodyComponent.body.applyForce(new CANNON.Vec3(scaledVelocity.x, scaledVelocity.y, scaledVelocity.z)) // this.object3d.position.addScaledVector(this.vectorCalculator.getForward(), distance); } moveRight(distance) { let scaledVelocity = new THREE.Vector3() scaledVelocity.addScaledVector(this.vectorCalculator.getRight(), distance) this.bodyComponent.body.applyForce(new CANNON.Vec3(scaledVelocity.x, scaledVelocity.y, scaledVelocity.z)) // this.object3d.position.addScaledVector(this.vectorCalculator.getRight(), distance); } } RE.registerComponent(CannonMovementController);