-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.gd
132 lines (101 loc) · 4.57 KB
/
player.gd
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
extends CharacterBody3D
@export var jump: float
@export var walkSpeed: float
@export var sensitivity: float
@export var head: Node3D
@export var cam: Camera3D
@export var RayCast: RayCast3D
@export var highlighter: MeshInstance3D
@export var coords_label: Label
@export var fps_label: Label
@export var gamemode_label: Label
@export var targeted_label: Label
@export var facing_label: Label
@export var chunk_label: Label
@export var is_survival: bool
var lookAngles = Vector2.ZERO
var targetedBlock: String
var looking_at: Vector2
var directions := [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]
var distances : Array[float] = [0, 0, 0, 0]
var directions_string := ["east [+X]", "west [-X]", "south [+N]", "north [-N]"]
var sprintSpeed: float
var playerSpeed: float
var camXRotation: float = 0
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var blockManager = get_node(^"/root/Level/BlockManager")
@onready var chunkManager = get_node(^"/root/Level/ChunkManager")
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
sprintSpeed = walkSpeed + (walkSpeed * .3)
playerSpeed = walkSpeed
func _physics_process(delta):
if !is_on_floor() && is_survival:
velocity.y -= gravity * delta
if Input.is_action_just_pressed("Up") && is_on_floor() && is_survival:
velocity.y = jump
var horizontal = Input.get_vector("Left", "Right", "Backward", "Forward").normalized()
var direction = Vector3.ZERO
direction += horizontal.x * head.global_basis.x
direction += horizontal.y * -head.global_basis.z
if !is_survival:
var vertical = Input.get_axis("Up", "Down") * -1
velocity.y = vertical * gravity
velocity.x = direction.x * playerSpeed
velocity.z = direction.z * playerSpeed
if Input.is_action_pressed("Sprint") and direction != Vector3.ZERO:
playerSpeed = sprintSpeed
var speedTween = get_tree().create_tween()
speedTween.tween_property(cam, "fov", 90, .25)
if Input.is_action_just_released("Sprint"):
playerSpeed = walkSpeed
var speedTween = get_tree().create_tween()
speedTween.tween_property(cam, "fov", 75, .25)
move_and_slide()
func _input(event):
if Input.is_physical_key_pressed(KEY_F4):
is_survival = false if is_survival else true
if Input.is_physical_key_pressed(KEY_F1):
coords_label.visible = false if coords_label.visible else true
fps_label.visible = false if fps_label.visible else true
gamemode_label.visible = false if gamemode_label.visible else true
if event is InputEventMouseMotion:
var mouseMotion = event as InputEventMouseMotion
lookAngles.x = mouseMotion.relative.y * sensitivity
lookAngles.y = -mouseMotion.relative.x * sensitivity
head.rotate_y(deg_to_rad(lookAngles.y))
if camXRotation + lookAngles.x > -90 && camXRotation + lookAngles.x < 90:
cam.rotate_x(deg_to_rad(-lookAngles.x))
camXRotation += lookAngles.x
looking_at = Vector2.RIGHT.rotated(round(head.rotation.y / TAU * 8) * TAU / 8).snapped(Vector2.ONE)
for i in len(directions):
distances[i] = directions[i].distance_to(looking_at)
facing_label.text = "Facing: %s" % str(directions_string[distances.find(distances.min())])
func _process(delta):
highlighter.visible = RayCast.is_colliding()
if RayCast.is_colliding() && RayCast.get_collider() is Chunk:
var chunk = RayCast.get_collider()
highlighter.visible = true
var blockPosition = RayCast.get_collision_point() - 0.5 * RayCast.get_collision_normal()
var intBlockPosition = Vector3i(floori(blockPosition.x), floori(blockPosition.y), floori(blockPosition.z))
targetedBlock = chunk.getBlock(Vector3i(intBlockPosition - Vector3i(chunk.global_position)))
targeted_label.text = "Targeted block: " + targetedBlock + " (" +str(intBlockPosition.x) + " / " + str(intBlockPosition.y) + " / " + str(intBlockPosition.z) + ")"
highlighter.global_position = Vector3(intBlockPosition) + Vector3(0.5, 0.5, 0.5)
if Input.is_action_just_pressed("Break"):
chunk.setBlock(Vector3i(intBlockPosition - Vector3i(chunk.global_position)), blockManager.air)
if Input.is_action_just_pressed("Place"):
chunkManager.setBlock(Vector3i(intBlockPosition + Vector3i(RayCast.get_collision_normal())), blockManager.stone)
else:
highlighter.visible = false
targeted_label.text = "Targeted block: none"
coords_label.text = (
"XYZ: "
+ ("%.2f" % global_position.x)
+ " / "
+ ("%.2f" % global_position.y)
+ " / "
+ ("%.2f" % global_position.z)
)
fps_label.text = str(Engine.get_frames_per_second()) + " fps_label"
gamemode_label.text = "is_survival" if is_survival else "creative"
chunk_label.text = "Chunk: " + str(floori(global_position.x / 32)) + " / " + str(floori(global_position.z / 32))