2024-08-02 22:38:08 +02:00
## Copyright (C) 2024 Patrick_Pluto
##
## This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
##
## This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-08-02 00:55:56 +02:00
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
var zoom = 0
2024-08-02 20:13:42 +02:00
var player_no
var enabled = false
2024-08-02 00:55:56 +02:00
var gravity = ProjectSettings . get_setting ( " physics/3d/default_gravity " )
2024-08-02 20:13:42 +02:00
func _ready ( ) :
player_no = Game . players
Game . players += 1
print ( Server . players )
print ( Server . players_numbered [ player_no ] )
print ( multiplayer . get_unique_id ( ) )
if Server . players_numbered [ player_no ] == multiplayer . get_unique_id ( ) :
enabled = true
$ cam_y / Camera3D . current = true
2024-08-02 00:55:56 +02:00
func _physics_process ( delta ) :
2024-08-02 20:13:42 +02:00
if enabled :
if not is_on_floor ( ) :
velocity . y -= gravity * delta
2024-08-02 00:55:56 +02:00
2024-08-02 20:13:42 +02:00
if Input . is_action_just_pressed ( " jump " ) and is_on_floor ( ) :
velocity . y = JUMP_VELOCITY
2024-08-02 00:55:56 +02:00
2024-08-02 20:13:42 +02:00
var input_dir = Input . get_vector ( " left " , " right " , " forwards " , " backwards " )
var direction = ( transform . basis * Vector3 ( input_dir . x , 0 , input_dir . y ) ) . 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 )
move_and_slide ( )
Server . sync_player . rpc ( name , position , rotation )
2024-08-02 00:55:56 +02:00
func _input ( event ) :
2024-08-02 20:13:42 +02:00
if enabled :
if event is InputEventMouseMotion and ( Input . is_action_pressed ( " cam_look " ) or $ cam_y / Camera3D . position . z == 0 ) :
var camera_rotation = event . relative * 0.01
if $ cam_y . rotation . x < = 1.6 && camera_rotation . y < = 0 :
$ cam_y . rotate ( Vector3 . RIGHT , - camera_rotation . y )
elif $ cam_y . rotation . x > = - 1.6 && camera_rotation . y > = 0 :
$ cam_y . rotate ( Vector3 . RIGHT , - camera_rotation . y )
if $ cam_y . rotation . x > = 1.6 :
$ cam_y . rotation . x = 1.6
elif $ cam_y . rotation . x < = - 1.6 :
$ cam_y . rotation . x = - 1.6
rotate ( Vector3 . DOWN , camera_rotation . x )
2024-08-02 00:55:56 +02:00
func _unhandled_input ( event ) :
2024-08-02 20:13:42 +02:00
if enabled :
if event . is_action_pressed ( " zoom_in " ) && $ cam_y / Camera3D . position . z > 0 :
zoom = - 1
$ cam_y / Camera3D . position . z += zoom
elif event . is_action_pressed ( " zoom_out " ) && $ cam_y / Camera3D . position . z < = 20 :
zoom = 1
$ cam_y / Camera3D . position . z += zoom
else :
zoom = 0