File size: 1,864 Bytes
e7fdd7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
extends CharacterBody3D
class_name Player

signal reset_signal

const SPEED := 30.0
const JUMP_VELOCITY := 4.5
const TURN_SENS := 2.0

@onready var ai_controller_3d = $AIController3D
@onready var health_bar = $HealthBar

var score := 0
var health := 3:
	set(value):
		health = value
		health_bar.update_health(health)
		if health == 0:
			game_over()


func _ready():
	ai_controller_3d.init(self)

func game_over():
	ai_controller_3d.done = true
	ai_controller_3d.needs_reset = true

func _physics_process(delta):
	if ai_controller_3d.needs_reset:
		reset()
		ai_controller_3d.reset()
		return
		
	var fb : float
	var turn : float
	
	if ai_controller_3d.heuristic == "human":
		fb = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
		turn = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
	else:
		fb = ai_controller_3d.fb_action
		turn = ai_controller_3d.turn_action
	
	fb = clamp(fb, -1.0, 0.5) # limit reverse speed
	#prints(ai_controller_3d.reward, ai_controller_3d.done, fb)
	rotation.y += deg_to_rad(turn*TURN_SENS)
	var direction = (transform.basis * Vector3(0.0, 0, fb)) # .normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	var collided = move_and_slide()
	if collided:
		obstacle_hit() 

func reset():
	position = Vector3.ZERO
	health = 3
	score = 0
	reset_signal.emit()
	
func chest_collected():
	#print("chest collected")
	score += 1
	ai_controller_3d.reward += 1.0

func mine_hit():
	#print("mine hit")
	score -= 1	
	health -= 1
	ai_controller_3d.reward -= 1.0
	
func obstacle_hit():
	#print("obstacle hit")
	pass
	#ai_controller_3d.reward -= 0.01

func _on_game_area_body_exited(body):
	#print("left game area")
	game_over()