|
| 1 | +extends CharacterBody3D |
| 2 | + |
| 3 | +var _yaw : float = 0 |
| 4 | +var _pitch = 0 |
| 5 | +var _dir = Vector3(sin(_yaw), 0, cos(_yaw)) |
| 6 | +var _prox = 3 |
| 7 | + |
| 8 | +var _tps_look_from = Vector3() |
| 9 | + |
| 10 | +enum CameraType {CAM_FIXED, CAM_FPS, CAM_TPS} |
| 11 | + |
| 12 | +var _bullet_scene = load("res://bullet.tscn") |
| 13 | +var _cam_type = CameraType.CAM_FIXED |
| 14 | + |
| 15 | +func _ready(): |
| 16 | + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) |
| 17 | + $Rig/Camera_TPS.set_as_top_level(true) |
| 18 | + toggle_camera_type() |
| 19 | + |
| 20 | +func _input(event: InputEvent) -> void: |
| 21 | + if event is InputEventMouseMotion: |
| 22 | + _yaw -= event.relative.x * 0.005 |
| 23 | + _pitch += event.relative.y * 0.01 |
| 24 | + _pitch = clamp(_pitch, -PI, PI) |
| 25 | + $Rig.rotation = Vector3(0, _yaw, 0) |
| 26 | + |
| 27 | +func _update_camera(_delta: float): |
| 28 | + _dir.x = sin(_yaw) |
| 29 | + _dir.z = cos(_yaw) |
| 30 | + |
| 31 | + $Rig/Head.rotation = Vector3(_pitch * -0.5, 0, 0) |
| 32 | + |
| 33 | + match _cam_type: |
| 34 | + CameraType.CAM_TPS: |
| 35 | + var target = $Rig/Head.get_global_transform_interpolated().origin |
| 36 | + var pos = target |
| 37 | + pos.x += _dir.x * _prox |
| 38 | + pos.z += _dir.z * _prox |
| 39 | + pos.y += 2.0 + (_pitch * (0.2 * _prox)) |
| 40 | + |
| 41 | + var offset = pos - _tps_look_from |
| 42 | + var l = offset.length() |
| 43 | + |
| 44 | + var tps_cam_speed = _delta * 8.0 |
| 45 | + if (l > tps_cam_speed): |
| 46 | + offset *= tps_cam_speed / l |
| 47 | + _tps_look_from += offset |
| 48 | + |
| 49 | + $Rig/Camera_TPS.look_at_from_position(_tps_look_from, target, Vector3(0, 1, 0)) |
| 50 | + |
| 51 | + |
| 52 | +func toggle_camera_type(): |
| 53 | + match _cam_type: |
| 54 | + CameraType.CAM_FIXED: |
| 55 | + _cam_type = CameraType.CAM_FPS |
| 56 | + $Rig/Head/Camera_FPS.make_current() |
| 57 | + CameraType.CAM_FPS: |
| 58 | + _cam_type = CameraType.CAM_TPS |
| 59 | + $Rig/Camera_TPS.make_current() |
| 60 | + CameraType.CAM_TPS: |
| 61 | + _cam_type = CameraType.CAM_FIXED |
| 62 | + get_node("../Camera_Fixed").make_current() |
| 63 | + |
| 64 | + # Hide body in FPS view. |
| 65 | + $Rig/Mesh_Body.visible = _cam_type != CameraType.CAM_FPS |
| 66 | + |
| 67 | +func _process(_delta: float) -> void: |
| 68 | + if (Input.is_action_just_pressed("ui_focus_next")): |
| 69 | + toggle_camera_type() |
| 70 | + |
| 71 | + if (Input.is_action_just_pressed("fire")): |
| 72 | + #print("fire") |
| 73 | + var bul = _bullet_scene.instantiate() |
| 74 | + var tr : Transform3D = $Rig/Head/Fire_Origin.get_global_transform_interpolated() |
| 75 | + bul.position = tr.origin |
| 76 | + var bul_dir = tr.basis[2].normalized() |
| 77 | + bul.linear_velocity = bul_dir * -9 |
| 78 | + get_node("..").add_child(bul) |
| 79 | + bul.reset_physics_interpolation() |
| 80 | + |
| 81 | + bul.position -= bul_dir * (1.0 - Engine.get_physics_interpolation_fraction()) |
| 82 | + |
| 83 | + |
| 84 | + # If we pressed reset, or too far from the origin... move back to origin. |
| 85 | + if (Input.is_action_just_pressed("ui_accept") or position.length() > 10): |
| 86 | + position = Vector3(0, 1, 0) |
| 87 | + velocity = Vector3() |
| 88 | + reset_physics_interpolation() |
| 89 | + _yaw = 0 |
| 90 | + _pitch = 0 |
| 91 | + |
| 92 | + if (Input.is_action_just_pressed("jump")) and is_on_floor(): |
| 93 | + velocity += Vector3(0, 12, 0) |
| 94 | + |
| 95 | + |
| 96 | + _update_camera(_delta) |
| 97 | + |
| 98 | + |
| 99 | +func _physics_process(_delta: float) -> void: |
| 100 | + |
| 101 | + var move : Vector3 = Vector3() |
| 102 | + |
| 103 | + if Input.is_action_pressed("ui_up"): |
| 104 | + move.z -= 1 |
| 105 | + if Input.is_action_pressed("ui_down"): |
| 106 | + move.z += 1 |
| 107 | + if Input.is_action_pressed("ui_left"): |
| 108 | + move.x -= 1 |
| 109 | + if Input.is_action_pressed("ui_right"): |
| 110 | + move.x += 1 |
| 111 | + |
| 112 | + move.y -= 0.9 |
| 113 | + |
| 114 | + # Apply mouse rotation to the move. |
| 115 | + move = move.rotated(Vector3(0, 1, 0), _yaw) |
| 116 | + |
| 117 | + velocity += move |
| 118 | + |
| 119 | + move_and_slide() |
| 120 | + |
| 121 | + velocity *= 0.9 |
| 122 | + |
| 123 | + |
0 commit comments