Browse Source
change to json file to ConfigFile, small bot improvements
change to json file to ConfigFile, small bot improvements
Lurkars
5 years ago
16 changed files with 170 additions and 186 deletions
-
13project.godot
-
30scripts/Util.gd
-
28scripts/game/config.gd
-
62scripts/game/config_apply.gd
-
4scripts/game/game.gd
-
3scripts/game/gamestate.gd
-
118scripts/game/settings.gd
-
2scripts/menus/direct_host_menu.gd
-
14scripts/menus/lobby_menu.gd
-
2scripts/menus/local_game.gd
-
6scripts/menus/server_create_menu.gd
-
4scripts/menus/settings_controls_menu.gd
-
44scripts/menus/settings_menu.gd
-
4scripts/menus/settings_player_menu.gd
-
20scripts/player/bot_controls.gd
-
2scripts/player/player.gd
@ -1,12 +1,36 @@ |
|||||
extends Node |
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()): |
for idx in range(node.get_child_count()): |
||||
node.remove_child(node.get_child(0)) |
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) |
return curve.get_point_position(curve.get_point_count() - 1) |
@ -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) |
@ -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) |
@ -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) |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue