File size: 4,525 Bytes
bd23f64 |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
extends CharacterBody3D
class_name Robot
@export var level_manager: LevelManager
@export var wheels: Array[Node3D]
@export var acceleration: float = 25.0
@export var rotation_speed: float = 15.0
@export var friction: float = 15.0
@export var max_horizontal_speed: float = 10.0
@export var gravity := Vector3.DOWN * 16.0
@onready var robot_visual: Node3D = $"robot"
@onready var ai_controller: RobotAIController = $AIController3D
var current_level: int
var next_level
var requested_movement: Vector3
var max_level_reached: int = 0
var current_goal_transform: Transform3D
var previous_distance_to_goal: float
var conveyor_belt_areas_entered: int
var conveyor_belt_direction: int
var conveyor_belt_speed: float = 15.0
func _ready():
reset()
func reset():
velocity = Vector3.ZERO
global_position = level_manager.get_spawn_position(current_level)
current_goal_transform = level_manager.randomize_goal(current_level)
previous_distance_to_goal = global_position.distance_to(current_goal_transform.origin)
func _physics_process(delta):
reset_on_needs_reset()
handle_movement(delta)
func reset_on_needs_reset():
if ai_controller.needs_reset:
level_manager.reset_coins(current_level)
if next_level == null:
if randi_range(1, 6) == 1:
current_level = randi_range(0, current_level)
else:
current_level = next_level
next_level = null
level_manager.reset_coins(current_level)
reset()
ai_controller.reset()
func handle_movement(delta):
var movement := Vector3()
if ai_controller.heuristic == "human":
if Input.is_action_pressed("ui_up"):
movement.x = -1
if Input.is_action_pressed("ui_down"):
movement.x = 1
if Input.is_action_pressed("ui_left"):
movement.z = 1
if Input.is_action_pressed("ui_right"):
movement.z = -1
movement = movement.normalized()
else:
movement = requested_movement
apply_acceleration(movement, delta)
apply_gravity(delta)
apply_friction(delta)
apply_conveyor_belt_velocity(delta)
limit_horizontal_speed()
move_and_slide()
rotate_toward_movement(delta)
update_wheels_and_visual_rotation(delta)
func apply_conveyor_belt_velocity(delta):
if conveyor_belt_areas_entered > 0:
velocity += Vector3.LEFT * conveyor_belt_direction * conveyor_belt_speed * delta
func limit_horizontal_speed():
var horizontal_velocity := Vector2(velocity.x, velocity.z).limit_length(max_horizontal_speed)
velocity = Vector3(horizontal_velocity.x, velocity.y, horizontal_velocity.y)
func apply_acceleration(direction, delta):
velocity += direction * acceleration * delta
func apply_friction(delta):
velocity = velocity.move_toward(Vector3(0, velocity.y, 0), friction * delta)
func apply_gravity(delta):
velocity += gravity * delta
func rotate_toward_movement(delta):
var movement = Vector3(velocity.x, 0, velocity.z).normalized()
var look_at_target: Vector3 = global_position + movement
if look_at_target.distance_to(global_position) > 0:
robot_visual.global_transform = (
robot_visual
. global_transform
. interpolate_with(global_transform.looking_at(look_at_target), rotation_speed * delta)
. orthonormalized()
)
func update_wheels_and_visual_rotation(delta):
var movement := Vector2(velocity.x, velocity.z).length()
for wheel in wheels:
wheel.rotate_object_local(Vector3.LEFT, movement * delta)
robot_visual.rotation.x = -0.01 * movement
func _on_area_3d_area_entered(area):
if area.get_collision_layer_value(1):
#print("Level goal reached")
if not level_manager.check_all_coins_collected(current_level):
return
if current_level > max_level_reached:
max_level_reached = current_level
print("max level passed: ", max_level_reached)
next_level = (current_level + 1) % level_manager.levels. size()
end_episode(1.0)
if area.get_collision_layer_value(2):
#print("Coin picked up")
level_manager.deactivate_coin(area, current_level)
ai_controller.reward += 1
if area.get_collision_layer_value(3):
#print("On conveyor belt")
conveyor_belt_direction = 1 if randi_range(0, 1) == 0 else -1
conveyor_belt_areas_entered += 1
pass
if area.get_collision_layer_value(4):
#print("Enemy collision")
end_episode(-1.0)
func _on_area_3d_body_entered(body):
if body.get_collision_layer_value(10):
#print("Robot fell down")
end_episode(-1.0)
func end_episode(reward: float):
ai_controller.reward += reward
ai_controller.done = true
ai_controller.needs_reset = true
func _on_area_3d_area_exited(area):
if area.get_collision_layer_value(3):
conveyor_belt_areas_entered -= 1
|