/// <summary> /// In order to keep the class testable, we need to reduce the number of /// dependencies injected through static classes at its minimum. /// Implementors are the place where platform dependencies can be transformed into /// entity components, so that here we can use inputComponent instead of /// the class Input. /// </summary> /// <param name="playerEntityView"></param> /// <param name="entityView"></param> void Movement(ref PlayerInputDataStruct playerEntityView, ref PlayerEntityViewStruct entityView) { // Store the input axes. var input = playerEntityView.input; // Normalise the movement vector and make it proportional to the speed per second. var movement = input.normalized * entityView.speedComponent.movementSpeed * _time.deltaTime; // Move the player to it's current position plus the movement. entityView.transformComponent.position = entityView.positionComponent.position + movement; }
void Turning(ref PlayerInputDataStruct playerEntityView, ref PlayerEntityViewStruct entityView) { // Create a ray from the mouse cursor on screen in the direction of the camera. var camRay = playerEntityView.camRay; // Perform the raycast and if it hits something on the floor layer... Vector3 point; if (_rayCaster.CheckHit(camRay, camRayLength, floorMask, out point)) { // Create a vector from the player to the point on the floor the raycast from the mouse hit. var playerToMouse = point - entityView.positionComponent.position; // Ensure the vector is entirely along the floor plane. playerToMouse.y = 0f; // Create a quaternion (rotation) based on looking down the vector from the player to the mouse. var newRotatation = Quaternion.LookRotation(playerToMouse); // Set the player's rotation to this new rotation. entityView.transformComponent.rotation = newRotatation; } }