From cafd838dfccbc6f0a3de27c4b0eec0aadaaabee8 Mon Sep 17 00:00:00 2001 From: Lurkars Date: Sun, 28 Jul 2019 13:26:30 +0200 Subject: [PATCH] change to json file to ConfigFile, small bot improvements --- project.godot | 13 +-- scripts/Util.gd | 30 +++++- scripts/game/config.gd | 28 ++++++ scripts/game/config_apply.gd | 62 +++++++++++++ scripts/game/game.gd | 4 +- scripts/game/gamestate.gd | 3 +- scripts/game/settings.gd | 118 ------------------------ scripts/menus/direct_host_menu.gd | 2 +- scripts/menus/lobby_menu.gd | 14 +-- scripts/menus/local_game.gd | 2 +- scripts/menus/server_create_menu.gd | 6 +- scripts/menus/settings_controls_menu.gd | 4 +- scripts/menus/settings_menu.gd | 44 +++++---- scripts/menus/settings_player_menu.gd | 4 +- scripts/player/bot_controls.gd | 20 ++-- scripts/player/player.gd | 2 +- 16 files changed, 170 insertions(+), 186 deletions(-) create mode 100644 scripts/game/config.gd create mode 100644 scripts/game/config_apply.gd delete mode 100644 scripts/game/settings.gd diff --git a/project.godot b/project.godot index 8ffa563..c002776 100644 --- a/project.godot +++ b/project.godot @@ -53,11 +53,6 @@ _global_script_classes=[ { "class": "TracksFactory", "language": "GDScript", "path": "res://scripts/road/roads_factory.gd" -}, { -"base": "Node", -"class": "Util", -"language": "GDScript", -"path": "res://scripts/Util.gd" } ] _global_script_class_icons={ "BaseInventory": "", @@ -68,8 +63,7 @@ _global_script_class_icons={ "Preview": "", "Road": "", "Route": "", -"TracksFactory": "", -"Util": "" +"TracksFactory": "" } [application] @@ -86,8 +80,9 @@ client="*res://scripts/networking/client.gd" server="*res://scripts/networking/server.gd" game_server="*res://scripts/networking/game_server.gd" roads_factory="*res://scripts/road/roads_factory.gd" -settings="*res://scripts/game/settings.gd" -Util="*res://scripts/Util.gd" +config="*res://scripts/game/config.gd" +config_apply="*res://scripts/game/config_apply.gd" +util="*res://scripts/Util.gd" [display] diff --git a/scripts/Util.gd b/scripts/Util.gd index eb9dd4a..b70c211 100644 --- a/scripts/Util.gd +++ b/scripts/Util.gd @@ -1,12 +1,36 @@ extends Node -class_name Util +var random_number_generator:RandomNumberGenerator = RandomNumberGenerator.new() -static func clear_node(node:Node): +func _ready(): + random_number_generator.randomize() + + +func randf(): + return random_number_generator.randf() + + +func randf_range (from:float, to:float ): + return random_number_generator.randf_range(from,to) + + +func randfn (mean:float=0.0, deviation:float=1.0 ): + return random_number_generator.randfn(mean, deviation) + + +func randi(): + return random_number_generator.randi() + + +func randi_range ( from:int, to:int ): + return random_number_generator.randi_range( from, to) + + +func clear_node(node:Node): for idx in range(node.get_child_count()): node.remove_child(node.get_child(0)) -static func curve_get_last_point(curve:Curve3D): +func curve_get_last_point(curve:Curve3D): return curve.get_point_position(curve.get_point_count() - 1) diff --git a/scripts/game/config.gd b/scripts/game/config.gd new file mode 100644 index 0000000..ba57ada --- /dev/null +++ b/scripts/game/config.gd @@ -0,0 +1,28 @@ +extends Node + +const FILE_PATH = "user://local_storage" + +const RESOLUTIONS = [Vector2(1920,1080),Vector2(1600,900),Vector2(1366,758),Vector2(1280,720),Vector2(1136,640),Vector2(1024,576)] + +const CONTROL_ACTIONS = ["controls_thrust", "controls_break", "controls_add_road", "controls_next_road_type", "controls_prev_road_type", "controls_next_road_variant", "controls_prev_road_variant", "controls_reset", "controls_menu"] + +const INPUT_UI_MAPPING = {"ui_accept" : "controls_add_road", "ui_select" : "controls_add_road", "ui_up" : "controls_next_road_variant", "ui_down" : "controls_prev_road_variant", "ui_left" : "controls_prev_road_type", "ui_right" : "controls_next_road_type"} + +var config_file:ConfigFile = ConfigFile.new() + + + +func _ready(): + config_file.load(FILE_PATH) + + +func save(): + config_file.save(FILE_PATH) + + +func get_value(section:String, key:String, default = null): + return config_file.get_value(section,key,default) + + +func set_value(section:String, key:String, value): + config_file.set_value(section,key,value) \ No newline at end of file diff --git a/scripts/game/config_apply.gd b/scripts/game/config_apply.gd new file mode 100644 index 0000000..7176629 --- /dev/null +++ b/scripts/game/config_apply.gd @@ -0,0 +1,62 @@ +extends Node + +const FILE_PATH = "user://local_storage" + + + +func _ready(): + apply_settings() + + +func apply_settings(): + apply_locale() + apply_game_server() + apply_resolution() + apply_controls() + + +func apply_locale(): + TranslationServer.set_locale(config.get_value("system","locale","en")) + + +func apply_game_server(): + var server_addr = config.get_value("game_server", "server_addr") + if server_addr != null && not server_addr.empty(): + game_server.set_server_addr(server_addr) + var api_addr = config.get_value("game_server","api_addr") + if api_addr != null && not api_addr.empty(): + game_server.set_api_addr(api_addr) + + +func apply_resolution(): + var view_port = get_tree().get_root() + OS.set_window_fullscreen(config.get_value("graphics","fullscreen", true)) + + var resolution = config.get_value("graphics","resolution",config.RESOLUTIONS[0]) + if OS.is_window_fullscreen(): + var base_size = Vector2(1920, 1080) + var scale= base_size.x / resolution.x + get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_2D,SceneTree.STRETCH_ASPECT_EXPAND,base_size,scale) + else: + OS.set_window_size(resolution) + get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,SceneTree.STRETCH_ASPECT_IGNORE,OS.get_window_size(),1) + + +func apply_controls(): + InputMap.load_from_globals() + var control_map = config.get_value("controls","mapping",{}) + for action in InputMap.get_actions(): + if control_map.has(action): + for event in InputMap.get_action_list(action): + if event is InputEventKey && control_map[action].has("key"): + InputMap.action_erase_event(action, event) + event.set_scancode(OS.find_scancode_from_string(control_map[action]["key"])) + InputMap.action_add_event(action,event) + elif event is InputEventJoypadButton && control_map[action].has("joypad"): + InputMap.action_erase_event(action, event) + event.set_button_index(control_map[action]["joypad"]) + InputMap.action_add_event(action,event) + + for ui_mapping in config.INPUT_UI_MAPPING: + for event in InputMap.get_action_list(config.INPUT_UI_MAPPING[ui_mapping]): + InputMap.action_add_event(ui_mapping, event) \ No newline at end of file diff --git a/scripts/game/game.gd b/scripts/game/game.gd index 1ea8df3..5a452c3 100644 --- a/scripts/game/game.gd +++ b/scripts/game/game.gd @@ -12,9 +12,9 @@ const REMOVE_LAST = 2 func _ready(): route = get_node("route") - find_node("sun").set_visible(settings.read_value("light", false)) + find_node("sun").set_visible(config.get_value("graphics","light", false)) if find_node("sun").is_visible_in_tree(): - find_node("sun").set_shadow(settings.read_value("shadows", false)) + find_node("sun").set_shadow(config.get_value("graphics","shadows", false)) if is_network_master(): route.rpc("add_road",roads_factory.START, -1) diff --git a/scripts/game/gamestate.gd b/scripts/game/gamestate.gd index d8177ae..f90729b 100644 --- a/scripts/game/gamestate.gd +++ b/scripts/game/gamestate.gd @@ -90,8 +90,7 @@ remotesync func prepare_game(): player_scene.set_start(player_scene.get_index()) if bots: - # for index in range(MAX_PEERS-players.size()): # DEBUG - for index in range(1): + for index in range(MAX_PEERS-players.size()): var bot_index = players.size() + index var player_scene = Player.instance() player_scene.set_name("bot_player" + str(bot_index)) diff --git a/scripts/game/settings.gd b/scripts/game/settings.gd deleted file mode 100644 index f3deaba..0000000 --- a/scripts/game/settings.gd +++ /dev/null @@ -1,118 +0,0 @@ -extends Node - -const FILE_PATH = "user://local_storage" - -const RESOLUTIONS = [Vector2(1920,1080),Vector2(1600,900),Vector2(1366,758),Vector2(1280,720),Vector2(1136,640),Vector2(1024,576)] - -const CONTROL_ACTIONS = ["controls_thrust", "controls_break", "controls_add_road", "controls_next_road_type", "controls_prev_road_type", "controls_next_road_variant", "controls_prev_road_variant", "controls_reset", "controls_menu"] - -const INPUT_UI_MAPPING = {"ui_accept" : "controls_add_road", "ui_select" : "controls_add_road", "ui_up" : "controls_next_road_variant", "ui_down" : "controls_prev_road_variant", "ui_left" : "controls_prev_road_type", "ui_right" : "controls_next_road_type"} - -onready var view_port = get_tree().get_root() - - -func _ready(): - apply_settings() - - -func read_config(): - var f = File.new() - var err = f.open(FILE_PATH, File.READ) - var config = {} - if err == OK: - config = parse_json(f.get_as_text()) - f.close() - if config == null: - config = {} - return config - - -func write_config(config:Dictionary): - var f = File.new() - var err = f.open(FILE_PATH, File.WRITE) - #var err = f.open_encrypted_with_pass(FILE_PATH, File.WRITE, OS.get_unique_id()) - f.store_string(to_json(config)) - f.close() - - -func write_value(key:String, value): - var config = read_config() - config[key] = value - write_config(config) - - -func read_value(key:String, default = null): - var config = read_config() - if config.has(key) && config.get(key) != null: - return config.get(key) - else: - return default - - -func delete_value(key:String): - var config = read_config() - config.erase(key) - write_config(config) - - -func write_values(new_config:Dictionary): - var config = read_config() - for key in new_config: - config[key] = new_config[key] - write_config(config) - - -func read_values(): - return read_config() - - -func apply_settings(): - apply_locale() - apply_game_server() - apply_resolution() - apply_controls() - - -func apply_locale(): - TranslationServer.set_locale(read_value("locale","en")) - - -func apply_game_server(): - var server_addr = read_value("server_addr") - if server_addr != null && not server_addr.empty(): - game_server.set_server_addr(server_addr) - var api_addr = read_value("api_addr") - if api_addr != null && not api_addr.empty(): - game_server.set_api_addr(api_addr) - - -func apply_resolution(): - OS.set_window_fullscreen(read_value("fullscreen", true)) - - if OS.is_window_fullscreen(): - var base_size = Vector2(1920, 1080) - var scale= base_size.x / RESOLUTIONS[read_value("resolution",0)].x - get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_2D,SceneTree.STRETCH_ASPECT_EXPAND,base_size,scale) - else: - OS.set_window_size(RESOLUTIONS[read_value("resolution",0)]) - get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,SceneTree.STRETCH_ASPECT_IGNORE,OS.get_window_size(),1) - - -func apply_controls(): - InputMap.load_from_globals() - var control_map = read_value("controls",{}) - for action in InputMap.get_actions(): - if control_map.has(action): - for event in InputMap.get_action_list(action): - if event is InputEventKey && control_map[action].has("key"): - InputMap.action_erase_event(action, event) - event.set_scancode(OS.find_scancode_from_string(control_map[action]["key"])) - InputMap.action_add_event(action,event) - elif event is InputEventJoypadButton && control_map[action].has("joypad"): - InputMap.action_erase_event(action, event) - event.set_button_index(control_map[action]["joypad"]) - InputMap.action_add_event(action,event) - - for ui_mapping in INPUT_UI_MAPPING: - for event in InputMap.get_action_list(INPUT_UI_MAPPING[ui_mapping]): - InputMap.action_add_event(ui_mapping, event) \ No newline at end of file diff --git a/scripts/menus/direct_host_menu.gd b/scripts/menus/direct_host_menu.gd index 1b6b73d..e97dbb3 100644 --- a/scripts/menus/direct_host_menu.gd +++ b/scripts/menus/direct_host_menu.gd @@ -4,7 +4,7 @@ var LobbyMenu = preload("res://scenes/menus/LobbyMenu.tscn") func _ready(): - find_node("bots").set_pressed(settings.read_value("bots", true)) + find_node("bots").set_pressed(config.get_value("game","bots", true)) func _draw(): diff --git a/scripts/menus/lobby_menu.gd b/scripts/menus/lobby_menu.gd index f701385..a228450 100644 --- a/scripts/menus/lobby_menu.gd +++ b/scripts/menus/lobby_menu.gd @@ -10,21 +10,21 @@ func _ready(): get_tree().get_root().get_node("multiplayer_menu").hide() if get_tree().get_root().has_node("main_menu"): get_tree().get_root().get_node("main_menu").hide() - + get_node("menu/back").connect("pressed",self,"_on_back_pressed") - + gamestate.connect("players_changed", self, "_players_changed") gamestate.connect("game_ready", self, "_game_ready") gamestate.connect("game_started", self, "_game_started") client.connect("server_disconnected", self, "_server_disconnected") - + player_settings.get_name_node().connect("text_changed",self,"_on_name_text_changed") player_settings.get_color_node().connect("color_changed",self,"_on_color_color_changed") player_settings.get_color_node().connect("draw",self,"_on_player_changed") - + countdown_timer.set_wait_time(server.LOBBY_READY_WAIT_TIME) - - if gamestate.game_name: + + if gamestate.game_name: find_node("server_name").set_text(gamestate.game_name) find_node("server_name").set_tooltip(gamestate.game_name) @@ -61,7 +61,7 @@ func _game_ready(all_ready): else: countdown_timer.stop() get_node("countdown").hide() - + func _game_started(): queue_free() diff --git a/scripts/menus/local_game.gd b/scripts/menus/local_game.gd index bfc2b2f..fa5b4d6 100644 --- a/scripts/menus/local_game.gd +++ b/scripts/menus/local_game.gd @@ -4,7 +4,7 @@ onready var player_settings = find_node("player_settings") func _ready(): - find_node("bots").set_pressed(settings.read_value("bots", true)) + find_node("bots").set_pressed(config.get_value("game","bots", true)) func _draw(): diff --git a/scripts/menus/server_create_menu.gd b/scripts/menus/server_create_menu.gd index 9dc4d7c..6c836e4 100644 --- a/scripts/menus/server_create_menu.gd +++ b/scripts/menus/server_create_menu.gd @@ -2,12 +2,12 @@ extends Control func _ready(): - var player_name = settings.read_value("player_name", false) + var player_name = config.get_value("game","player_name", false) var game_title = "New Game" if player_name: game_title += " by " + player_name find_node("name").set_text(game_title) - find_node("bots").set_pressed(settings.read_value("bots", true)) + find_node("bots").set_pressed(config.get_value("game","bots", true)) func _draw(): @@ -21,7 +21,7 @@ func _on_create_pressed(): var bots = 0 if find_node("bots").is_pressed(): bots = 1 - req.post_request('client/game/create', to_json({'name' : server_name, 'bots' : bots})) + req.post_request('client/game/create', to_json({'name' : server_name, 'bots' : bots})) func _on_game_created(result, response_code, headers, body): diff --git a/scripts/menus/settings_controls_menu.gd b/scripts/menus/settings_controls_menu.gd index 35298cb..6f883f0 100644 --- a/scripts/menus/settings_controls_menu.gd +++ b/scripts/menus/settings_controls_menu.gd @@ -18,7 +18,7 @@ func _ready(): func init_mapping(): new_inputs = {} - for action in settings.CONTROL_ACTIONS: + for action in config.CONTROL_ACTIONS: var action_label = Label.new() action_label.set_name(action + "_label") action_label.set_text(action) @@ -77,7 +77,7 @@ func _unhandled_input(event): if check_usage: var has_event = "" - for action in settings.CONTROL_ACTIONS: + for action in config.CONTROL_ACTIONS: if InputMap.event_is_action(event, action): has_event = action diff --git a/scripts/menus/settings_menu.gd b/scripts/menus/settings_menu.gd index 5770aca..0981f27 100644 --- a/scripts/menus/settings_menu.gd +++ b/scripts/menus/settings_menu.gd @@ -13,18 +13,18 @@ func _ready(): get_node("menu/back").connect("pressed",self,"_on_back_pressed") # game - find_node("bots").set_pressed(settings.read_value("bots", true)) + find_node("bots").set_pressed(config.get_value("game","bots", true)) # graphics - find_node("fullscreen").set_pressed(settings.read_value("fullscreen", true)) - find_node("light").set_pressed(settings.read_value("light", false)) - find_node("shadows").set_pressed(settings.read_value("shadows", false)) + find_node("fullscreen").set_pressed(config.get_value("graphics","fullscreen", true)) + find_node("light").set_pressed(config.get_value("graphics","light", false)) + find_node("shadows").set_pressed(config.get_value("graphics","shadows", false)) find_node("shadows").set_disabled(not find_node("light").is_pressed()) - for resolution in settings.RESOLUTIONS: + for resolution in config.RESOLUTIONS: find_node("resolution").add_item(str(resolution.x) + " * " + str(resolution.y)) - find_node("resolution").select(settings.read_value("resolution", 0)) + find_node("resolution").select(config.RESOLUTIONS.find(config.get_value("graphics","resolution", config.RESOLUTIONS[0]))) # system find_node("server_addr").set_text(game_server.get_server_addr()) @@ -61,7 +61,7 @@ func _init_locales(): locales.clear() locales.add_item(tr("LOCALE_EN")) locales.add_item(tr("LOCALE_DE")) - locale = settings.read_value("locale","en") + locale = config.get_value("system","locale","en") match locale: "en": locales.select(0) @@ -72,7 +72,7 @@ func _init_locales(): func _on_locales_tree_exiting(): - settings.apply_locale() + config_apply.apply_locale() func _on_save_pressed(): @@ -81,37 +81,35 @@ func _on_save_pressed(): func _on_apply_pressed(): - var values = {} - # game - values['player_name'] = player_settings.get_name_node().get_text() - values['player_color'] = player_settings.get_color_node().color.to_html() - values['bots'] = find_node("bots").is_pressed() - values['locale'] = locale + config.set_value("game","player_name",player_settings.get_name_node().get_text()) + config.set_value("game","player_color",player_settings.get_color_node().color) + config.set_value("game","bots",find_node("bots").is_pressed()) # graphics - values['resolution'] = find_node("resolution").get_selected() - values['fullscreen'] = find_node("fullscreen").is_pressed() - values['light'] = find_node("light").is_pressed() - values['shadows'] = find_node("shadows").is_pressed() + config.set_value("graphics","resolution",config.RESOLUTIONS[find_node("resolution").get_selected()]) + config.set_value("graphics","fullscreen",find_node("fullscreen").is_pressed()) + config.set_value("graphics","light",find_node("light").is_pressed()) + config.set_value("graphics","shadows",find_node("shadows").is_pressed()) # controls - values['controls'] = find_node("settings_controls").new_inputs + config.set_value("controls","mapping",find_node("settings_controls").new_inputs) # system + config.set_value("system","locale",locale) var server_addr = find_node("server_addr").get_text() if server_addr.empty(): server_addr = game_server.SERVER_ADDR - values['server_addr'] = server_addr + config.set_value("system","server_addr",server_addr) var api_addr = find_node("api_addr").get_text() if api_addr.empty(): api_addr = game_server.API_ADDR - values['api_addr'] = api_addr + config.set_value("system","api_addr",api_addr) - settings.write_values(values) + config.save() - settings.apply_settings() + config_apply.apply_settings() func _on_light_toggled(button_pressed): diff --git a/scripts/menus/settings_player_menu.gd b/scripts/menus/settings_player_menu.gd index a04f3f9..57c1cb0 100644 --- a/scripts/menus/settings_player_menu.gd +++ b/scripts/menus/settings_player_menu.gd @@ -2,8 +2,8 @@ extends Control func _ready(): - get_name_node().set_text(settings.read_value("player_name", "Player")) - get_color_node().set_pick_color(Color(settings.read_value("player_color", "#000000"))) + get_name_node().set_text(config.get_value("game","player_name", "Player")) + get_color_node().set_pick_color(config.get_value("game","player_color", Color.black)) func get_name_node(): diff --git a/scripts/player/bot_controls.gd b/scripts/player/bot_controls.gd index 5c68321..050a066 100644 --- a/scripts/player/bot_controls.gd +++ b/scripts/player/bot_controls.gd @@ -7,12 +7,8 @@ const OFFSET_STEPS = 0.01 var break_constrain = 0.0 -export var error_rate:float = 0.00 -export var underspeed_rate:float = 0.00 - - -func _ready(): - randomize() +export var overspeed_rate:float = 0.3 +export var underspeed_rate:float = 0.1 func _physics_process(delta): @@ -23,7 +19,7 @@ func _physics_process(delta): else: if player.get_road() != null: var road = player.get_road() - var rate = randf() + var rate = util.randf() var speed = player.current_speed var offset = 0 var distance = player.follow.get_offset() @@ -46,11 +42,11 @@ func _physics_process(delta): distance = 0 road = gamestate.game.route.get_road(road.get_index()+1) - if break_constrain > 0 && speed > break_constrain - 0.05: + if break_constrain > 0 && speed > break_constrain - 0.04 && rate > overspeed_rate: player.thrust = -1 - elif speed - 0.1 < break_constrain: + elif speed - 0.08 < break_constrain && rate > underspeed_rate: player.thrust = 1 - elif break_constrain == 0: + elif break_constrain == 0 && rate > underspeed_rate: player.thrust = 1 else: player.thrust = 0 @@ -61,8 +57,8 @@ func set_player(path:String): get_node("inventory").set_player(player) -func set_error_rate(new_error_rate:float): - error_rate = new_error_rate +func set_overspeed_rate(new_overspeed_rate:float): + overspeed_rate = new_overspeed_rate func set_underspeed_rate(new_underspeed_rate:float): diff --git a/scripts/player/player.gd b/scripts/player/player.gd index d83087c..c6a40da 100644 --- a/scripts/player/player.gd +++ b/scripts/player/player.gd @@ -146,7 +146,7 @@ func _on_raceCar_road_entered(road): buffered_rotation = last_rotation if path.curve.get_point_count() > 0: - buffered_translate = Util.curve_get_last_point(path.curve) + buffered_translate = util.curve_get_last_point(path.curve) if road.get_end_rotation().length() != 0: last_rotation += road.get_end_rotation()