|
using Godot; |
|
|
|
public partial class Player : CharacterBody3D |
|
{ |
|
[Export] |
|
public GameSceneManager GameSceneManager { get; set; } |
|
[Export] |
|
public Area3D Goal { get; set; } |
|
[Export] |
|
public Area3D Obstacle { get; set; } |
|
[Export] |
|
public Node3D AIController { get; set; } |
|
[Export] |
|
public float Speed { get => speed; set => speed = value; } |
|
public Vector2 RequestedMovement { get; set; } |
|
|
|
private float speed = 5f; |
|
private Transform3D initialTransform; |
|
|
|
public override void _Ready() |
|
{ |
|
initialTransform = Transform; |
|
} |
|
|
|
public override void _PhysicsProcess(double delta) |
|
{ |
|
if (!IsOnFloor()) |
|
{ |
|
Velocity += GetGravity() * (float)delta; |
|
} |
|
|
|
|
|
|
|
if ((string)AIController.Get("heuristic") == "human") |
|
{ |
|
RequestedMovement = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down"); |
|
} |
|
|
|
RequestedMovement = RequestedMovement.LimitLength(1f) * Speed; |
|
|
|
Velocity = new(RequestedMovement.X, Velocity.Y, RequestedMovement.Y); |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!(bool)AIController.Get("done")) |
|
{ |
|
MoveAndSlide(); |
|
} |
|
|
|
ResetOnPlayerFalling(); |
|
|
|
} |
|
|
|
|
|
private void ResetOnPlayerFalling() |
|
{ |
|
if (GlobalPosition.Y < -1.0) |
|
{ |
|
GameOver(-1.0); |
|
} |
|
} |
|
|
|
|
|
private void GameOver(double reward) |
|
{ |
|
double currentReward = (double)AIController.Get("reward"); |
|
AIController.Set("reward", currentReward + reward); |
|
GameSceneManager.Reset(); |
|
} |
|
|
|
|
|
public void Reset() |
|
{ |
|
AIController.Call("end_episode"); |
|
Transform = initialTransform; |
|
} |
|
|
|
|
|
public void OnGoalBodyEntered(Node3D body) |
|
{ |
|
GameOver(1.0); |
|
} |
|
|
|
|
|
public void OnObstacleBodyEntered(Node3D body) |
|
{ |
|
GameOver(-1.0); |
|
} |
|
} |
|
|