freeftf/scripts/game.gd

161 lines
4.8 KiB
GDScript3
Raw Normal View History

2024-08-31 14:58:41 +02:00
## FreeFTF
## Copyright (C) 2024 Interstellar Development
2024-08-02 22:38:08 +02:00
##
2024-08-31 14:58:41 +02:00
## 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.
2024-08-02 22:38:08 +02:00
##
2024-08-31 14:58:41 +02:00
## 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.
2024-08-02 22:38:08 +02:00
##
2024-08-31 14:58:41 +02:00
## 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 22:38:08 +02:00
2024-08-02 00:55:56 +02:00
extends Node
2024-08-31 14:58:41 +02:00
var settings = {"save_version" = 5, "fps_counter" = 1, "port" = 35000, "ip" = "interstellardevelopment.org", "username" = "DefaultName", "sensitivity" = 0.5}
2024-08-02 00:55:56 +02:00
var computers = 0
2024-08-02 20:13:42 +02:00
var players = 0
2024-08-31 14:58:41 +02:00
var map_name = "test"
var is_running = false
var frozen = 0
var dead = 0
var escaped = 0
2024-08-19 18:06:35 +02:00
var result = "You Lose"
var player_escaped = false
var is_beast = false
2024-08-31 14:58:41 +02:00
var prison = preload("res://objects/freezer.tscn")
var objective = preload("res://objects/computer.tscn")
var character = preload("res://objects/player.tscn")
# Server Variables
var player_name
var room_name
var is_server = false
var player_list = {}
func _ready():
if FileAccess.file_exists("user://settings.json"):
var temp = Load.loadJSON("user://settings.json")
if !(temp is Dictionary) or settings["save_version"] != temp["save_version"]:
Save.saveJSON("user://settings.json", settings)
else:
settings = temp
else:
Save.saveJSON("user://settings.json", settings)
if OS.is_debug_build():
settings["fps_counter"] = 1
2024-08-02 00:55:56 +02:00
func freeze():
players -= 1
frozen += 1
2024-08-03 22:24:08 +02:00
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:
2024-08-19 18:06:35 +02:00
if player_escaped or (is_beast and escaped == 0):
result = "You Win"
else:
result = "You Lose"
Game.reset()
get_tree().change_scene_to_file("res://menus/result.tscn")
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
is_running = false
2024-08-03 22:24:08 +02:00
func reset():
computers = 0
players = 0
is_running = false
frozen = 0
dead = 0
escaped = 0
player_escaped = false
is_beast = false
player_name = null
player_list = {}
room_name = null
is_server = false
2024-08-31 14:58:41 +02:00
map_name = "test"
multiplayer.multiplayer_peer = Client.offline
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
2024-08-02 20:13:42 +02:00
func save_setting(setting_name, value):
settings[setting_name] = value
Save.saveJSON("user://settings.json", settings)
func apply_settings():
players = player_list.size() - 1
is_running = true
2024-08-02 20:13:42 +02:00
func start_game():
get_tree().change_scene_to_file("res://maps/"+map_name+".tscn")
while !get_tree().root.has_node("./"+map_name+"/"):
await get_tree().create_timer(0.001).timeout
var map = get_tree().root.get_node("./"+map_name+"/")
2024-08-31 14:58:41 +02:00
for i in range(player_list.size()):
var obj = objective.instantiate()
2024-09-12 17:06:16 +02:00
obj.name = "objective" + str(i)
2024-08-31 14:58:41 +02:00
obj.position = get_tree().root.get_node("./"+map_name+"/objective_spawns/"+str(i)).position
map.add_child(obj)
for i in range(player_list.size()):
var obj = prison.instantiate()
2024-09-12 17:06:16 +02:00
obj.name = "prison" + str(i)
2024-08-31 14:58:41 +02:00
obj.position = get_tree().root.get_node("./"+map_name+"/prison_spawns/"+str(i)).position
map.add_child(obj)
var i = 0
for w in player_list:
var player = character.instantiate()
player.name = "player" + str(i)
2024-08-31 14:58:41 +02:00
player.position = get_tree().root.get_node("./"+map_name+"/player_spawns/"+str(i)).position
i += 1
map.add_child(player)
if is_server:
var random = randi() % player_list.size()
for id in player_list:
Client.rpc_id(id,"sync_beast",random)
apply_settings()
func player_hit(target, beast):
2024-08-13 22:12:37 +02:00
target = get_tree().root.get_node(target)
beast = get_tree().root.get_node(beast)
if !target.is_frozen:
target.captured(beast)
beast.got_one(target)
2024-08-31 14:58:41 +02:00
func sync_player(node_path, position, rotation, rotation2):
2024-08-13 22:12:37 +02:00
if get_tree().root.has_node(node_path):
var current_character = get_tree().root.get_node(node_path)
2024-08-13 20:43:54 +02:00
current_character.position = position
current_character.rotation = rotation
2024-08-31 14:58:41 +02:00
get_tree().root.get_node(node_path).find_child("collision").rotation = rotation2
2024-08-13 22:12:37 +02:00
func sync_hammer(node_path, rotation):
2024-09-12 17:06:16 +02:00
if get_tree().root.has_node(node_path):
var current_character = get_tree().root.get_node(node_path)
current_character.rotation = rotation
2024-08-13 22:12:37 +02:00
func sync_computers(node_path, current):
2024-09-12 17:06:16 +02:00
if get_tree().root.has_node(node_path):
var current_character = get_tree().root.get_node(node_path)
current_character.current = current
func sync_beast(player):
while !get_tree().root.has_node("./"+map_name+"/player"+str(player)):
await get_tree().create_timer(0.001).timeout
var current_character = get_tree().root.get_node("./"+map_name+"/player"+str(player))
current_character.beast_init()