Files
ShootahGame3D/player/player.gd

84 lines
2.3 KiB
GDScript

extends CharacterBody3D
const SPEED: float = 7
const HEALTH_MAX: float = 100.0
const MOUSE_SENSE: float = 0.35
var health: float = 50.0
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _unhandled_input(event: InputEvent) -> void:
if (Input.mouse_mode == Input.MOUSE_MODE_CAPTURED):
if (event is InputEventMouseMotion):
rotation_degrees.y -= event.relative.x * MOUSE_SENSE
$Camera3D.rotation_degrees.x -= event.relative.y * MOUSE_SENSE
$Camera3D.rotation_degrees.x = clamp(
$Camera3D.rotation_degrees.x, -85.0, 85.0
)
elif (event is InputEventMouseButton):
match event.button_index:
MOUSE_BUTTON_LEFT:
var timer: Timer = $Camera3D/gun_model/ShootTimer
if (event.is_pressed()):
print("left MB pressed")
$Camera3D/gun_model.recoil()
shoot()
timer.start()
else:
print("left MB released")
timer.stop()
_:
# unhandled mouse events
pass
else:
# other unhandled input
pass
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_cancel"):
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
else:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _physics_process(delta: float) -> void:
%HealthBar.set("value", health)
# Player model camera is -Z aligned
var input_direction_2D = Input.get_vector(
"move_left", "move_right", "move_forward", "move_back"
)
var direction = transform.basis * Vector3(
input_direction_2D.x, 0.0, input_direction_2D.y
)
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
velocity.y -= 9.80665 * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = 3.5
move_and_slide()
func shoot() -> void:
const BULLET_3D = preload("res://player/bullet_3d.tscn")
var new_bullet = BULLET_3D.instantiate()
%Marker3D.add_child(new_bullet)
new_bullet.global_transform = %Marker3D.global_transform
%AudioStreamPlayer.play()
func _on_shoot_timer_timeout() -> void:
$Camera3D/gun_model.recoil()
shoot()
func heal(amount: float = 35.0) -> void:
# TODO: add values and other fancy healing stuff, for now this works
health = clampf((health + amount), 0.0, HEALTH_MAX)
func hurt(amount: float) -> void:
health = clampf((health - amount), 0.0, HEALTH_MAX)