forked from interstellar_development/freeftf
a92202b536
The freezer is now implemented, just as it worked in the original. There are also a few fixes here and there in preparation for Milestonr 4.
110 lines
3.4 KiB
GDScript
110 lines
3.4 KiB
GDScript
## 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/>.
|
|
|
|
extends Node
|
|
|
|
const DEFAULT_SERVER_IP = "127.0.0.1"
|
|
const PORT = 36969
|
|
const MAX_CONNECTIONS = 16
|
|
|
|
var players = {}
|
|
var players_numbered = []
|
|
var label
|
|
var map
|
|
var character = preload("res://objects/player.tscn")
|
|
var is_server = false
|
|
|
|
func reset():
|
|
players = {}
|
|
players_numbered = []
|
|
label = null
|
|
map = null
|
|
is_server = false
|
|
character = preload("res://objects/player.tscn")
|
|
if multiplayer != null:
|
|
multiplayer.multiplayer_peer = null
|
|
|
|
func join_game(ip):
|
|
var peer = ENetMultiplayerPeer.new()
|
|
var error = peer.create_client(ip, PORT)
|
|
if error:
|
|
return error
|
|
multiplayer.multiplayer_peer = peer
|
|
|
|
|
|
func create_game():
|
|
var peer = ENetMultiplayerPeer.new()
|
|
var error = peer.create_server(PORT, MAX_CONNECTIONS)
|
|
if error:
|
|
return error
|
|
multiplayer.multiplayer_peer = peer
|
|
|
|
@rpc("authority", "call_remote", "reliable")
|
|
func sync_playerlist(text):
|
|
get_tree().root.get_node("create/player_list/list").text = text
|
|
|
|
@rpc("any_peer", "call_local", "reliable")
|
|
func send_playerinfo(name, id):
|
|
if Server.is_server:
|
|
var done = 0
|
|
players[id] = name
|
|
label = get_tree().root.get_node("create/player_list/list")
|
|
label.text = ""
|
|
for w in players:
|
|
if done == 1:
|
|
label.text = str(label.text +"\n" + players[w])
|
|
else:
|
|
label.text = players[w]
|
|
done = 1
|
|
|
|
|
|
@rpc("authority", "call_local", "reliable")
|
|
func start_game(server_players):
|
|
players = server_players
|
|
players_numbered = players.keys()
|
|
var success = get_tree().change_scene_to_file("res://maps/base_map.tscn")
|
|
while !get_tree().root.has_node("./map/"):
|
|
await get_tree().create_timer(0.001).timeout
|
|
map = get_tree().root.get_node("./map/")
|
|
var i = 0
|
|
for w in players:
|
|
var player = character.instantiate()
|
|
player.name = "player" + str(i)
|
|
i += 1
|
|
player.position.z = -i*2.5
|
|
map.add_child(player)
|
|
if Server.is_server:
|
|
var random = randi() % Server.players.size()
|
|
sync_beast.rpc(random)
|
|
|
|
@rpc("any_peer", "call_remote", "unreliable")
|
|
func sync_player(node_name, position, rotation):
|
|
var current_character = get_tree().root.get_node("./map/"+node_name)
|
|
current_character.position = position
|
|
current_character.rotation = rotation
|
|
|
|
@rpc("authority", "call_local", "reliable")
|
|
func sync_computers(node_name, current):
|
|
var current_character = get_tree().root.get_node("./map/"+node_name)
|
|
current_character.current = current
|
|
|
|
@rpc("authority", "call_local", "reliable")
|
|
func sync_beast(player):
|
|
while !get_tree().root.has_node("./map/player"+str(player)):
|
|
await get_tree().create_timer(0.001).timeout
|
|
var current_character = get_tree().root.get_node("./map/player"+str(player))
|
|
current_character.beast_init()
|
|
|
|
@rpc("any_peer", "call_local", "reliable")
|
|
func player_hit(target, beast):
|
|
target = get_tree().root.get_node("./map/"+target)
|
|
beast = get_tree().root.get_node("./map/"+beast)
|
|
if !target.is_frozen:
|
|
target.captured(beast)
|
|
beast.got_one(target)
|
|
|