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
2024-08-04 13:41:19 +02:00
var jump_velocity = 6
var speed = 10.0
2024-08-02 00:55:56 +02:00
var zoom = 0
2024-08-02 20:13:42 +02:00
var player_no
2024-08-03 16:10:14 +02:00
var npc = false
2024-08-02 20:13:42 +02:00
var enabled = false
2024-08-03 16:10:14 +02:00
var caught = false
var captured_by
var position_pre
var got_person = false
2024-08-04 13:41:19 +02:00
var caught_body
var beast = false
var mouse_locked = true
var is_frozen = false
var hp = 100
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
if Server . players_numbered [ player_no ] == multiplayer . get_unique_id ( ) :
enabled = true
$ cam_y / Camera3D . current = true
2024-08-03 16:10:14 +02:00
else :
npc = 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-03 22:24:08 +02:00
2024-08-02 20:13:42 +02:00
if Input . is_action_just_pressed ( " jump " ) and is_on_floor ( ) :
2024-08-04 13:41:19 +02:00
if beast :
speed -= 6
$ jump_timeout . start ( )
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 :
2024-08-04 13:41:19 +02:00
velocity . x = direction . x * speed
velocity . z = direction . z * speed
2024-08-02 20:13:42 +02:00
else :
2024-08-04 13:41:19 +02:00
velocity . x = move_toward ( velocity . x , 0 , speed )
velocity . z = move_toward ( velocity . z , 0 , speed )
2024-08-02 20:13:42 +02:00
move_and_slide ( )
2024-08-02 00:55:56 +02:00
2024-08-04 13:41:19 +02:00
if ! npc :
print ( hp )
2024-08-03 16:10:14 +02:00
Server . sync_player . rpc ( name , position , rotation )
2024-08-04 13:41:19 +02:00
if $ cam_y / Camera3D . position . z == 0 :
$ cam_y . position . y = 1
if mouse_locked :
Input . set_mouse_mode ( Input . MOUSE_MODE_CAPTURED )
else :
Input . set_mouse_mode ( Input . MOUSE_MODE_VISIBLE )
else :
Input . set_mouse_mode ( Input . MOUSE_MODE_VISIBLE )
$ cam_y . position . y = 0
if Input . is_action_just_pressed ( " escape " ) :
get_tree ( ) . change_scene_to_file ( " res://menus/main_menu.tscn " )
Input . set_mouse_mode ( Input . MOUSE_MODE_VISIBLE )
Game . reset ( )
Server . reset ( )
if Input . is_action_just_pressed ( " mouse_lock " ) :
mouse_locked = ! mouse_locked
2024-08-03 16:10:14 +02:00
2024-08-02 00:55:56 +02:00
func _input ( event ) :
2024-08-03 16:10:14 +02:00
if ! npc :
2024-08-02 20:13:42 +02:00
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-04 13:41:19 +02:00
if ! npc and ! beast :
2024-08-02 20:13:42 +02:00
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
2024-08-03 16:10:14 +02:00
func beast_init ( ) :
2024-08-04 13:41:19 +02:00
beast = true
speed += 1
2024-08-03 16:10:14 +02:00
position . y += 10
2024-08-04 13:41:19 +02:00
$ cam_y / Camera3D . position . z = 0
2024-08-03 16:10:14 +02:00
$ detect_hit . monitoring = true
$ detect_hit . monitorable = true
$ detect_hit / CollisionShape3D . disabled = false
$ hammer_bag . visible = true
func _on_detect_hit_body_entered ( body ) :
if enabled and body is CharacterBody3D and ! got_person :
Server . player_hit . rpc ( body . name , name )
func captured ( beast ) :
if ! npc :
$ in_bag . visible = true
$ time_in_bag . start ( )
visible = false
enabled = false
caught = true
captured_by = beast
position_pre = position
position = Vector3 ( 10000 , 10000 , 10000 )
func got_one ( target ) :
2024-08-04 13:41:19 +02:00
caught_body = target
2024-08-03 16:10:14 +02:00
got_person = true
$ hammer_bag / CSGCylinder3D / CSGSphere3D . visible = true
func lost_one ( ) :
got_person = false
$ hammer_bag / CSGCylinder3D / CSGSphere3D . visible = false
2024-08-04 13:41:19 +02:00
func frozen ( ) :
$ in_bag . visible = false
is_frozen = true
func unfreeze ( ) :
is_frozen = false
2024-08-03 16:10:14 +02:00
func _on_time_in_bag_timeout ( ) :
visible = true
if ! npc :
enabled = true
caught = false
captured_by . lost_one ( )
position = position_pre
$ in_bag . visible = false
2024-08-03 23:08:03 +02:00
func _on_show_fps_timeout ( ) :
2024-08-04 13:41:19 +02:00
print ( " FPS: " + str ( Engine . get_frames_per_second ( ) ) )
func _on_jump_timeout_timeout ( ) :
speed += 6