From: Eduardo Date: Fri, 26 Jan 2024 18:35:33 +0000 (+0100) Subject: moved player to own scene and organice its scripts X-Git-Url: http://git.edufdez.es/?a=commitdiff_plain;h=74e9d9a5255bb147b5a141aa6c707c5eac2d3801;p=ScaryGame.git moved player to own scene and organice its scripts --- diff --git a/Camera3D.gd b/Camera3D.gd deleted file mode 100644 index e5347aa..0000000 --- a/Camera3D.gd +++ /dev/null @@ -1,52 +0,0 @@ -extends Camera3D - -@export var movEnabled := true -@export var mouseSensitivity := 0.5 -@export var flyspeed := 100.0 -@export var debug := false -@export var player: Node3D - -var yaw : float = 0.0 -var pitch : float = 0.0 - -# Called when the node enters the scene tree for the first time. -func _ready(): - yaw = 0.0 - pitch = 0.0 - - Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) - -func _input(event): - # release the mouse on esc - if event.is_action_pressed("ui_cancel"): - Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) - - # capture mouse again on click - if event.is_action_pressed("left_click"): - if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE: - Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) - get_viewport().set_input_as_handled() - - # if mouse is not captured dont move the camera - if not Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: - return - - # get mouse movement and move the camera - if event is InputEventMouseMotion and movEnabled: - var mouseVec: Vector2 = event.get_relative() - - yaw = fmod(yaw - mouseVec.x * mouseSensitivity, 360) - pitch = max(min(pitch - mouseVec.y * mouseSensitivity, 90), -90) - player.rotation = Vector3(deg_to_rad(pitch), deg_to_rad(yaw), 0) - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - if(Input.is_action_pressed("ui_up")): - translate(position - global_basis * Vector3(0, 0, 1) * delta * flyspeed * .01) - if(Input.is_action_pressed("ui_down")): - translate(position - global_basis * Vector3(0, 0, 1) * delta * flyspeed * -.01) - if(Input.is_action_pressed("ui_left")): - translate(position - global_basis * Vector3(1, 0, 0) * delta * flyspeed * .01) - if(Input.is_action_pressed("ui_right")): - translate(position - global_basis * Vector3(1, 0, 0) * delta * flyspeed * -.01) - diff --git a/player.gd b/player.gd deleted file mode 100644 index 47c5b64..0000000 --- a/player.gd +++ /dev/null @@ -1,35 +0,0 @@ -extends CharacterBody3D - -# How fast the player moves in meters per second. -@export var speed = 5 -# The downward acceleration when in the air, in meters per second squared. -@export var fall_acceleration = 10 -@export var pivot: Node3D - -var target_velocity = Vector3.ZERO - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _physics_process(_delta): - var direction = Vector3.ZERO - - if Input.is_action_pressed("move_right"): - direction += transform.basis.x - if Input.is_action_pressed("move_left"): - direction -= transform.basis.x - if Input.is_action_pressed("move_back"): - direction += transform.basis.z - if Input.is_action_pressed("move_forward"): - direction -= transform.basis.z - - if direction != Vector3.ZERO: - direction = direction.normalized() - pivot.look_at(position + direction, Vector3.UP) - - target_velocity.x = direction.x * speed - target_velocity.z = direction.z * speed - - # if not is_on_floor(): - # target_velocity.y = target_velocity.y - (fall_acceleration * delta) - - velocity = target_velocity - move_and_slide() diff --git a/player/Camera3D.gd b/player/Camera3D.gd new file mode 100644 index 0000000..e5347aa --- /dev/null +++ b/player/Camera3D.gd @@ -0,0 +1,52 @@ +extends Camera3D + +@export var movEnabled := true +@export var mouseSensitivity := 0.5 +@export var flyspeed := 100.0 +@export var debug := false +@export var player: Node3D + +var yaw : float = 0.0 +var pitch : float = 0.0 + +# Called when the node enters the scene tree for the first time. +func _ready(): + yaw = 0.0 + pitch = 0.0 + + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + +func _input(event): + # release the mouse on esc + if event.is_action_pressed("ui_cancel"): + Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) + + # capture mouse again on click + if event.is_action_pressed("left_click"): + if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE: + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + get_viewport().set_input_as_handled() + + # if mouse is not captured dont move the camera + if not Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: + return + + # get mouse movement and move the camera + if event is InputEventMouseMotion and movEnabled: + var mouseVec: Vector2 = event.get_relative() + + yaw = fmod(yaw - mouseVec.x * mouseSensitivity, 360) + pitch = max(min(pitch - mouseVec.y * mouseSensitivity, 90), -90) + player.rotation = Vector3(deg_to_rad(pitch), deg_to_rad(yaw), 0) + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + if(Input.is_action_pressed("ui_up")): + translate(position - global_basis * Vector3(0, 0, 1) * delta * flyspeed * .01) + if(Input.is_action_pressed("ui_down")): + translate(position - global_basis * Vector3(0, 0, 1) * delta * flyspeed * -.01) + if(Input.is_action_pressed("ui_left")): + translate(position - global_basis * Vector3(1, 0, 0) * delta * flyspeed * .01) + if(Input.is_action_pressed("ui_right")): + translate(position - global_basis * Vector3(1, 0, 0) * delta * flyspeed * -.01) + diff --git a/player/Player.gd b/player/Player.gd new file mode 100644 index 0000000..47c5b64 --- /dev/null +++ b/player/Player.gd @@ -0,0 +1,35 @@ +extends CharacterBody3D + +# How fast the player moves in meters per second. +@export var speed = 5 +# The downward acceleration when in the air, in meters per second squared. +@export var fall_acceleration = 10 +@export var pivot: Node3D + +var target_velocity = Vector3.ZERO + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _physics_process(_delta): + var direction = Vector3.ZERO + + if Input.is_action_pressed("move_right"): + direction += transform.basis.x + if Input.is_action_pressed("move_left"): + direction -= transform.basis.x + if Input.is_action_pressed("move_back"): + direction += transform.basis.z + if Input.is_action_pressed("move_forward"): + direction -= transform.basis.z + + if direction != Vector3.ZERO: + direction = direction.normalized() + pivot.look_at(position + direction, Vector3.UP) + + target_velocity.x = direction.x * speed + target_velocity.z = direction.z * speed + + # if not is_on_floor(): + # target_velocity.y = target_velocity.y - (fall_acceleration * delta) + + velocity = target_velocity + move_and_slide() diff --git a/player/Player.tscn b/player/Player.tscn new file mode 100644 index 0000000..6c76691 --- /dev/null +++ b/player/Player.tscn @@ -0,0 +1,30 @@ +[gd_scene load_steps=5 format=3 uid="uid://1mmjp4ugn7kk"] + +[ext_resource type="Script" path="res://player/Player.gd" id="1_x0wva"] +[ext_resource type="PackedScene" uid="uid://bpuiyd81j8177" path="res://assets/Farola.glb" id="2_oms0u"] +[ext_resource type="Script" path="res://player/Camera3D.gd" id="3_7uxif"] + +[sub_resource type="Environment" id="Environment_bd0rd"] +sdfgi_enabled = true +volumetric_fog_enabled = true +volumetric_fog_density = 0.1977 +volumetric_fog_albedo = Color(0.144063, 0.144063, 0.144063, 1) + +[node name="CharacterBody3D" type="CharacterBody3D" node_paths=PackedStringArray("pivot")] +top_level = true +script = ExtResource("1_x0wva") +pivot = NodePath("Pivot") + +[node name="Pivot" type="Node3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.68645, 0) + +[node name="Farola" parent="." instance=ExtResource("2_oms0u")] +transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0) + +[node name="Camera3D" type="Camera3D" parent="." node_paths=PackedStringArray("player")] +transform = Transform3D(1, 0, -7.45058e-09, 0, 1, 0, 7.45058e-09, 0, 1, 0, 1.7, -0.106033) +environment = SubResource("Environment_bd0rd") +current = true +script = ExtResource("3_7uxif") +mouseSensitivity = 0.2 +player = NodePath("..")