File size: 2,693 Bytes
2952f21 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
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 controlled by human, takes the keyboard arrows as input
// otherwise, requested_movement will be set by the AIController based on RL agent's output actions
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);
// We only move when the episode is not marked as done
// to prevent repeating a possibly wrong action from the previous episode.
// This is related to the sync node Action Repeat functionality,
// we don't get a new action for every physics step. This check may
// not be required for every env, and is not used in all examples.
if (!(bool)AIController.Get("done"))
{
MoveAndSlide();
}
ResetOnPlayerFalling();
}
// Resets the game if the player has fallen down
private void ResetOnPlayerFalling()
{
if (GlobalPosition.Y < -1.0)
{
GameOver(-1.0);
}
}
// Ends the game, setting an optional reward
private void GameOver(double reward)
{
double currentReward = (double)AIController.Get("reward");
AIController.Set("reward", currentReward + reward);
GameSceneManager.Reset();
}
// Resets the player and AIController
public void Reset()
{
AIController.Call("end_episode");
Transform = initialTransform;
}
// When the goal is entered, we restart the game with a positive reward
public void OnGoalBodyEntered(Node3D body)
{
GameOver(1.0);
}
// When the obstacle is entered, we restart the game with a negative reward
public void OnObstacleBodyEntered(Node3D body)
{
GameOver(-1.0);
}
}
|