Milestone 6 Beta 2

Reworked multiplayer, like a lot.
This commit is contained in:
patrick_pluto 2024-08-06 23:28:40 +02:00
parent 17ece77b7c
commit 9de67986dd
17 changed files with 309 additions and 89 deletions

View file

@ -12,6 +12,7 @@ extends Control
var elevated = false
func _ready():
multiplayer.connected_to_server.connect(_on_connected_ok)
if Server.is_server:
$player_customization/ip.hide()
$player_customization/ip.text = "localhost"
@ -33,17 +34,21 @@ func _on_start_pressed():
func _input(event):
if Input.is_action_just_pressed("escape"):
Game.reset()
Server.reset()
get_tree().change_scene_to_file("res://menus/main_menu.tscn")
func _on_join_pressed():
if $player_customization/name.text != "" and $player_customization/ip.text != "":
if $player_customization/name.text != "" and $player_customization/name.text.length() <= 20 and $player_customization/ip.text != "":
if !Server.is_server:
Server.join_game($player_customization/ip.text)
$player_customization/join.hide()
$player_customization/join.disabled = true
$player_customization/name.editable = false
$player_customization/ip.editable = false
await get_tree().create_timer(1).timeout
Server.send_playerinfo.rpc($player_customization/name.text, multiplayer.get_unique_id())
func _on_connected_ok():
$player_customization/join.hide()
$player_customization/join.disabled = true
$player_customization/name.editable = false
$player_customization/ip.editable = false
await get_tree().create_timer(1).timeout
Server.send_playerinfo.rpc($player_customization/name.text, multiplayer.get_unique_id())

View file

@ -16,7 +16,8 @@ var living = true
func _process(delta):
if occupied:
trapped_body.position = global_position
if occupied and trapped_body.hp == 0:
if occupied and trapped_body.hp == 0 and living:
Game.died()
living = false
$dead.show()
@ -28,8 +29,10 @@ func _on_area_3d_body_entered(body):
living = true
body.lost_one()
trapped_body.frozen()
Game.freeze()
elif !body.beast and occupied and living and body != trapped_body:
trapped_body.unfreeze()
Game.unfreeze()
occupied = false
trapped_body.position = $spawn.global_position
trapped_body = null

View file

@ -13,6 +13,12 @@ var settings = {"save_version" = 2, "fps_counter" = 1}
var computers = 0
var players = 0
var map_name = "mansion"
var is_running = false
var frozen = 0
var dead = 0
var escaped = 0
var player_escaped = false
var is_beast = false
func _ready():
if FileAccess.file_exists("user://settings.json"):
@ -26,17 +32,43 @@ func _ready():
if OS.is_debug_build():
settings["fps_counter"] = 1
func _process(delta):
pass
func freeze():
players -= 1
frozen += 1
func unfreeze():
players += 1
frozen -= 1
func died():
frozen -= 1
dead += 1
func has_escaped():
players -= 1
escaped += 1
func _process(delta):
if is_running and players <= 0:
get_tree().change_scene_to_file("res://menus/result.tscn")
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
is_running = false
func reset():
computers = 0
players = 0
is_running = false
frozen = 0
dead = 0
escaped = 0
player_escaped = false
is_beast = false
func save_setting(setting_name, value):
settings[setting_name] = value
Save.saveJSON("user://settings.json", settings)
func apply_settings():
pass
players = Server.players_numbered.size() - 1
is_running = true

19
scripts/game_menu.gd Normal file
View file

@ -0,0 +1,19 @@
## freeftf
## 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, either version 3 of the License, or (at your option) any later version.
##
## 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/>.
extends Control
func _on_button_pressed():
Game.reset()
Server.reset()
get_tree().change_scene_to_file("res://menus/main_menu.tscn")
queue_free()

View file

@ -13,8 +13,13 @@ func matchmaking():
get_tree().root.get_node("./main_menu").hide()
func _ready():
Game.reset()
Server.reset()
if !OS.is_debug_build():
$main_content/matchmaking.hide()
$main_content/matchmaking.disabled = true
if DisplayServer.get_name() == "headless":
_on_matchmaking_pressed()
func _on_create_pressed():
get_tree().change_scene_to_file("res://menus/create.tscn")

View file

@ -25,6 +25,7 @@ var beast = false
var mouse_locked = true
var is_frozen = false
var hp = 100
var menu = preload("res://menus/game_menu.tscn")
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@ -65,7 +66,13 @@ func _physics_process(delta):
speed += 3
$cam_y.position.y = 0.5
position.y += 1
if Input.is_action_pressed("click") and $hammer.rotation_degrees.x > -90:
$hammer.rotation_degrees.x -= 5
Server.sync_hammer.rpc(name, $hammer.rotation)
elif $hammer.rotation_degrees.x < 0 and !Input.is_action_pressed("click"):
$hammer.rotation_degrees.x += 5
Server.sync_hammer.rpc(name, $hammer.rotation)
var input_dir = Input.get_vector("left", "right", "forwards", "backwards")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
@ -80,15 +87,18 @@ func _physics_process(delta):
if !npc:
Server.sync_player.rpc(name, position, rotation)
if mouse_locked:
if mouse_locked and !has_node("./game_menu"):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
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 has_node("./game_menu"):
get_node("./game_menu").free()
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
var instance = menu.instantiate()
add_child(instance)
if Input.is_action_just_pressed("mouse_lock"):
mouse_locked = !mouse_locked
@ -96,7 +106,7 @@ func _physics_process(delta):
func _input(event):
if !npc:
if event is InputEventMouseMotion and (Input.is_action_pressed("cam_look") or $cam_y/Camera3D.position.z == 0):
if event is InputEventMouseMotion and (Input.is_action_pressed("cam_look") or $cam_y/Camera3D.position.z == 0) and !has_node("./game_menu"):
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)
@ -110,13 +120,16 @@ func _input(event):
func beast_init():
if !npc:
Game.is_beast = true
beast = true
speed += 2
position.y += 10
$detect_hit.monitoring = true
$detect_hit.monitorable = true
$detect_hit/CollisionShape3D.disabled = false
$hammer_bag.visible = true
$hammer/hammer2/CSGBox3D/detect_hit.monitoring = true
$hammer/hammer2/CSGBox3D/detect_hit.monitorable = true
$hammer/hammer2/CSGBox3D/detect_hit/CollisionShape3D.disabled = false
$hammer.show()
$bag.show()
func _on_detect_hit_body_entered(body):
if enabled and body is CharacterBody3D and !got_person:
@ -137,11 +150,11 @@ func captured(beast2):
func got_one(target):
caught_body = target
got_person = true
$hammer_bag/CSGCylinder3D/CSGSphere3D.visible = true
$bag/CSGSphere3D.show()
func lost_one():
got_person = false
$hammer_bag/CSGCylinder3D/CSGSphere3D.visible = false
$bag/CSGSphere3D.hide()
func frozen():
visible = true

21
scripts/result.gd Normal file
View file

@ -0,0 +1,21 @@
## freeftf
## 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, either version 3 of the License, or (at your option) any later version.
##
## 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/>.
extends Control
func _ready():
if Game.player_escaped or (Game.is_beast and Game.escaped == 0):
$VBoxContainer/Label.text = "You Win"
else:
$VBoxContainer/Label.text = "You Lose"
func _process(delta):
if Input.is_action_just_pressed("escape"):
get_tree().change_scene_to_file("res://menus/main_menu.tscn")

View file

@ -22,6 +22,38 @@ var is_server = false
var first_joined = true
var game_master = "debug"
var map_name = "mansion"
var offline
func _ready():
offline = multiplayer.multiplayer_peer
multiplayer.peer_disconnected.connect(_on_player_disconnected)
multiplayer.server_disconnected.connect(_on_server_disconnected)
func _on_player_disconnected(id):
players.erase(id)
if (players.is_empty() or id == game_master) and multiplayer.is_server():
get_tree().quit()
var x = 0
if !multiplayer.is_server():
for i in players_numbered:
if i == id:
var current_character = get_tree().root.get_node("./"+map_name+"/player"+str(x))
if current_character.hp == 0:
Game.dead -= 1
elif current_character.is_frozen:
Game.frozen -= 1
elif current_character.beast:
Game.player_escaped = true
Game.players = 0
else:
Game.players -= 1
current_character.free()
x += 1
func _on_server_disconnected():
Server.reset()
Game.reset()
get_tree().change_scene_to_file("res://menus/main_menu.tscn")
func reset():
players = {}
@ -29,11 +61,11 @@ func reset():
label = null
map = null
is_server = false
first_joined = false
first_joined = true
game_master = "debug"
character = preload("res://objects/player.tscn")
if multiplayer != null:
multiplayer.multiplayer_peer = null
multiplayer.multiplayer_peer = offline
func join_game(ip):
var peer = ENetMultiplayerPeer.new()
@ -51,9 +83,10 @@ func create_game():
multiplayer.multiplayer_peer = peer
func _process(delta):
if multiplayer != null:
if multiplayer.is_server():
make_host.rpc(game_master)
if multiplayer.is_server():
make_host.rpc(game_master)
@rpc("authority","call_remote","reliable")
func make_host(id):
@ -84,6 +117,7 @@ func send_playerinfo(player_name, id):
label.text = players[w]
done = 1
else:
players[id] = player_name
if first_joined:
first_joined = false
game_master = id
@ -118,6 +152,12 @@ func sync_player(node_name, position, rotation):
var current_character = get_tree().root.get_node("./"+map_name+"/"+node_name)
current_character.position = position
current_character.rotation = rotation
@rpc("any_peer", "call_remote", "unreliable")
func sync_hammer(node_name, rotation):
if !multiplayer.is_server():
var current_character = get_tree().root.get_node("./"+map_name+"/"+node_name+"/hammer")
current_character.rotation = rotation
@rpc("any_peer", "call_local", "reliable")
func sync_computers(node_name, current):
@ -141,4 +181,5 @@ func player_hit(target, beast):
if !target.is_frozen:
target.captured(beast)
beast.got_one(target)

View file

@ -11,8 +11,11 @@ extends Area3D
func _on_body_entered(body):
if body is CharacterBody3D and !body.beast and Game.computers == 0:
if !body.npc:
Game.player_escaped = true
body.free()
$mapcam.active
Game.has_escaped()
func _process(delta):
if Game.computers == 0: