// Fill out your copyright notice in the Description page of Project Settings. #include "BasePawn.h" #include "Camera/CameraComponent.h" #include "Components/StaticMeshComponent.h" // Sets default values ABasePawn::ABasePawn(const FObjectInitializer &object_initializer): APawn(object_initializer) { // Create default components. root_component = object_initializer.CreateDefaultSubobject(this, FName("Root")); main_camera = object_initializer.CreateDefaultSubobject(this, FName("MainCamera")); player_mesh = object_initializer.CreateDefaultSubobject(this, FName("PlayerMesh")); movement_component = object_initializer.CreateDefaultSubobject(this, FName("MovementComponent")); // Setup component hierarchy. RootComponent = root_component; main_camera->SetupAttachment(root_component); player_mesh->SetupAttachment(root_component); movement_component->UpdatedComponent = player_mesh; PrimaryActorTick.bCanEverTick = true; // Set defaults. accel_max_time = 0.25; horizontal_speed_max = 100; vertical_speed_max = 100; } // Called when the game starts or when spawned void ABasePawn::BeginPlay() { Super::BeginPlay(); } UPawnMovementComponent* ABasePawn::GetMovementComponent() const { return movement_component; } // Called every frame void ABasePawn::Tick(float DeltaTime) { Super::Tick(DeltaTime); // Move the player. //vertical_speed = vertical_accel.update(DeltaTime); //horizontal_speed = horizontal_accel.update(DeltaTime); //// "X" is up, "Y" is right. //const FVector location = player_mesh->GetComponentLocation(); //const float new_vertical = (DeltaTime * vertical_speed) + location.X; //const float new_horizontal = (DeltaTime * horizontal_speed) + location.Y; //player_mesh->SetWorldLocation(FVector(new_vertical, new_horizontal, location.Z)); } // Called to bind functionality to input void ABasePawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); //PlayerInputComponent->BindAction("MoveUp", EInputEvent::IE_Pressed, this, &ABasePawn::handle_move_input, movement_t::UP, 1); //PlayerInputComponent->BindAction("MoveUp", EInputEvent::IE_Released, this, &ABasePawn::handle_move_input, movement_t::UP, 0); //PlayerInputComponent->BindAction("MoveDown", EInputEvent::IE_Pressed, this, &ABasePawn::handle_move_input, movement_t::DOWN, -1); //PlayerInputComponent->BindAction("MoveDown", EInputEvent::IE_Released, this, &ABasePawn::handle_move_input, movement_t::DOWN, 0); //PlayerInputComponent->BindAction("MoveRight", EInputEvent::IE_Pressed, this, &ABasePawn::handle_move_input, movement_t::RIGHT, 1); //PlayerInputComponent->BindAction("MoveRight", EInputEvent::IE_Released, this, &ABasePawn::handle_move_input, movement_t::RIGHT, 0); //PlayerInputComponent->BindAction("MoveLeft", EInputEvent::IE_Pressed, this, &ABasePawn::handle_move_input, movement_t::LEFT, -1); //PlayerInputComponent->BindAction("MoveLeft", EInputEvent::IE_Released, this, &ABasePawn::handle_move_input, movement_t::LEFT, 0); PlayerInputComponent->BindAxis("MoveForward", this, &ABasePawn::handle_move_forward); PlayerInputComponent->BindAxis("MoveRight", this, &ABasePawn::handle_move_right); } void ABasePawn::handle_move_forward(float axis) { FRotator rotation = Controller->GetControlRotation(); FRotator yaw(0.0f, rotation.Yaw, 0.0f); FVector forward = FRotationMatrix(yaw).GetUnitAxis(EAxis::X); AddMovementInput(forward, axis); } void ABasePawn::handle_move_right(float axis) { FRotator rotation = Controller->GetControlRotation(); FRotator yaw(0.0f, rotation.Yaw, 0.0f); FVector right = FRotationMatrix(yaw).GetUnitAxis(EAxis::Y); AddMovementInput(right, axis); } //void ABasePawn::handle_move_input(const movement_t move_type, const int direction) //{ // if (move_type == movement_t::UP || move_type == movement_t::DOWN) // { // // Prevent two vertical inputs entered at the same time. // if (vertical_accel.end != 0 && direction != 0) // { // return; // } // // const float new_speed = direction * vertical_speed_max; // vertical_accel.set(vertical_speed, new_speed, accel_max_time); // } // else // { // // Prevent two horizontal inputs entered at the same time. // if (horizontal_accel.end != 0 && direction != 0) // { // return; // } // // const float new_speed = direction * horizontal_speed_max; // horizontal_accel.set(horizontal_speed, new_speed, accel_max_time); // } //}