Browse Source

change to json file to ConfigFile, small bot improvements

Lurkars 5 years ago
parent
commit
cafd838dfc
  1. 13
      project.godot
  2. 30
      scripts/Util.gd
  3. 28
      scripts/game/config.gd
  4. 62
      scripts/game/config_apply.gd
  5. 4
      scripts/game/game.gd
  6. 3
      scripts/game/gamestate.gd
  7. 118
      scripts/game/settings.gd
  8. 2
      scripts/menus/direct_host_menu.gd
  9. 14
      scripts/menus/lobby_menu.gd
  10. 2
      scripts/menus/local_game.gd
  11. 6
      scripts/menus/server_create_menu.gd
  12. 4
      scripts/menus/settings_controls_menu.gd
  13. 44
      scripts/menus/settings_menu.gd
  14. 4
      scripts/menus/settings_player_menu.gd
  15. 20
      scripts/player/bot_controls.gd
  16. 2
      scripts/player/player.gd

13
project.godot

@ -53,11 +53,6 @@ _global_script_classes=[ {
"class": "TracksFactory", "class": "TracksFactory",
"language": "GDScript", "language": "GDScript",
"path": "res://scripts/road/roads_factory.gd" "path": "res://scripts/road/roads_factory.gd"
}, {
"base": "Node",
"class": "Util",
"language": "GDScript",
"path": "res://scripts/Util.gd"
} ] } ]
_global_script_class_icons={ _global_script_class_icons={
"BaseInventory": "", "BaseInventory": "",
@ -68,8 +63,7 @@ _global_script_class_icons={
"Preview": "", "Preview": "",
"Road": "", "Road": "",
"Route": "", "Route": "",
"TracksFactory": "",
"Util": ""
"TracksFactory": ""
} }
[application] [application]
@ -86,8 +80,9 @@ client="*res://scripts/networking/client.gd"
server="*res://scripts/networking/server.gd" server="*res://scripts/networking/server.gd"
game_server="*res://scripts/networking/game_server.gd" game_server="*res://scripts/networking/game_server.gd"
roads_factory="*res://scripts/road/roads_factory.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] [display]

30
scripts/Util.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)

28
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)

62
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)

4
scripts/game/game.gd

@ -12,9 +12,9 @@ const REMOVE_LAST = 2
func _ready(): func _ready():
route = get_node("route") 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(): 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(): if is_network_master():
route.rpc("add_road",roads_factory.START, -1) route.rpc("add_road",roads_factory.START, -1)

3
scripts/game/gamestate.gd

@ -90,8 +90,7 @@ remotesync func prepare_game():
player_scene.set_start(player_scene.get_index()) player_scene.set_start(player_scene.get_index())
if bots: 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 bot_index = players.size() + index
var player_scene = Player.instance() var player_scene = Player.instance()
player_scene.set_name("bot_player" + str(bot_index)) player_scene.set_name("bot_player" + str(bot_index))

118
scripts/game/settings.gd

@ -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)

2
scripts/menus/direct_host_menu.gd

@ -4,7 +4,7 @@ var LobbyMenu = preload("res://scenes/menus/LobbyMenu.tscn")
func _ready(): 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(): func _draw():

14
scripts/menus/lobby_menu.gd

@ -10,21 +10,21 @@ func _ready():
get_tree().get_root().get_node("multiplayer_menu").hide() get_tree().get_root().get_node("multiplayer_menu").hide()
if get_tree().get_root().has_node("main_menu"): if get_tree().get_root().has_node("main_menu"):
get_tree().get_root().get_node("main_menu").hide() get_tree().get_root().get_node("main_menu").hide()
get_node("menu/back").connect("pressed",self,"_on_back_pressed") get_node("menu/back").connect("pressed",self,"_on_back_pressed")
gamestate.connect("players_changed", self, "_players_changed") gamestate.connect("players_changed", self, "_players_changed")
gamestate.connect("game_ready", self, "_game_ready") gamestate.connect("game_ready", self, "_game_ready")
gamestate.connect("game_started", self, "_game_started") gamestate.connect("game_started", self, "_game_started")
client.connect("server_disconnected", self, "_server_disconnected") client.connect("server_disconnected", self, "_server_disconnected")
player_settings.get_name_node().connect("text_changed",self,"_on_name_text_changed") 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("color_changed",self,"_on_color_color_changed")
player_settings.get_color_node().connect("draw",self,"_on_player_changed") player_settings.get_color_node().connect("draw",self,"_on_player_changed")
countdown_timer.set_wait_time(server.LOBBY_READY_WAIT_TIME) 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_text(gamestate.game_name)
find_node("server_name").set_tooltip(gamestate.game_name) find_node("server_name").set_tooltip(gamestate.game_name)
@ -61,7 +61,7 @@ func _game_ready(all_ready):
else: else:
countdown_timer.stop() countdown_timer.stop()
get_node("countdown").hide() get_node("countdown").hide()
func _game_started(): func _game_started():
queue_free() queue_free()

2
scripts/menus/local_game.gd

@ -4,7 +4,7 @@ onready var player_settings = find_node("player_settings")
func _ready(): 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(): func _draw():

6
scripts/menus/server_create_menu.gd

@ -2,12 +2,12 @@ extends Control
func _ready(): 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" var game_title = "New Game"
if player_name: if player_name:
game_title += " by " + player_name game_title += " by " + player_name
find_node("name").set_text(game_title) 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(): func _draw():
@ -21,7 +21,7 @@ func _on_create_pressed():
var bots = 0 var bots = 0
if find_node("bots").is_pressed(): if find_node("bots").is_pressed():
bots = 1 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): func _on_game_created(result, response_code, headers, body):

4
scripts/menus/settings_controls_menu.gd

@ -18,7 +18,7 @@ func _ready():
func init_mapping(): func init_mapping():
new_inputs = {} new_inputs = {}
for action in settings.CONTROL_ACTIONS:
for action in config.CONTROL_ACTIONS:
var action_label = Label.new() var action_label = Label.new()
action_label.set_name(action + "_label") action_label.set_name(action + "_label")
action_label.set_text(action) action_label.set_text(action)
@ -77,7 +77,7 @@ func _unhandled_input(event):
if check_usage: if check_usage:
var has_event = "" var has_event = ""
for action in settings.CONTROL_ACTIONS:
for action in config.CONTROL_ACTIONS:
if InputMap.event_is_action(event, action): if InputMap.event_is_action(event, action):
has_event = action has_event = action

44
scripts/menus/settings_menu.gd

@ -13,18 +13,18 @@ func _ready():
get_node("menu/back").connect("pressed",self,"_on_back_pressed") get_node("menu/back").connect("pressed",self,"_on_back_pressed")
# game # game
find_node("bots").set_pressed(settings.read_value("bots", true))
find_node("bots").set_pressed(config.get_value("game","bots", true))
# graphics # 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()) 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").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 # system
find_node("server_addr").set_text(game_server.get_server_addr()) find_node("server_addr").set_text(game_server.get_server_addr())
@ -61,7 +61,7 @@ func _init_locales():
locales.clear() locales.clear()
locales.add_item(tr("LOCALE_EN")) locales.add_item(tr("LOCALE_EN"))
locales.add_item(tr("LOCALE_DE")) locales.add_item(tr("LOCALE_DE"))
locale = settings.read_value("locale","en")
locale = config.get_value("system","locale","en")
match locale: match locale:
"en": "en":
locales.select(0) locales.select(0)
@ -72,7 +72,7 @@ func _init_locales():
func _on_locales_tree_exiting(): func _on_locales_tree_exiting():
settings.apply_locale()
config_apply.apply_locale()
func _on_save_pressed(): func _on_save_pressed():
@ -81,37 +81,35 @@ func _on_save_pressed():
func _on_apply_pressed(): func _on_apply_pressed():
var values = {}
# game # 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 # 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 # controls
values['controls'] = find_node("settings_controls").new_inputs
config.set_value("controls","mapping",find_node("settings_controls").new_inputs)
# system # system
config.set_value("system","locale",locale)
var server_addr = find_node("server_addr").get_text() var server_addr = find_node("server_addr").get_text()
if server_addr.empty(): if server_addr.empty():
server_addr = game_server.SERVER_ADDR 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() var api_addr = find_node("api_addr").get_text()
if api_addr.empty(): if api_addr.empty():
api_addr = game_server.API_ADDR 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): func _on_light_toggled(button_pressed):

4
scripts/menus/settings_player_menu.gd

@ -2,8 +2,8 @@ extends Control
func _ready(): 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(): func get_name_node():

20
scripts/player/bot_controls.gd

@ -7,12 +7,8 @@ const OFFSET_STEPS = 0.01
var break_constrain = 0.0 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): func _physics_process(delta):
@ -23,7 +19,7 @@ func _physics_process(delta):
else: else:
if player.get_road() != null: if player.get_road() != null:
var road = player.get_road() var road = player.get_road()
var rate = randf()
var rate = util.randf()
var speed = player.current_speed var speed = player.current_speed
var offset = 0 var offset = 0
var distance = player.follow.get_offset() var distance = player.follow.get_offset()
@ -46,11 +42,11 @@ func _physics_process(delta):
distance = 0 distance = 0
road = gamestate.game.route.get_road(road.get_index()+1) 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 player.thrust = -1
elif speed - 0.1 < break_constrain:
elif speed - 0.08 < break_constrain && rate > underspeed_rate:
player.thrust = 1 player.thrust = 1
elif break_constrain == 0:
elif break_constrain == 0 && rate > underspeed_rate:
player.thrust = 1 player.thrust = 1
else: else:
player.thrust = 0 player.thrust = 0
@ -61,8 +57,8 @@ func set_player(path:String):
get_node("inventory").set_player(player) 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): func set_underspeed_rate(new_underspeed_rate:float):

2
scripts/player/player.gd

@ -146,7 +146,7 @@ func _on_raceCar_road_entered(road):
buffered_rotation = last_rotation buffered_rotation = last_rotation
if path.curve.get_point_count() > 0: 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: if road.get_end_rotation().length() != 0:
last_rotation += road.get_end_rotation() last_rotation += road.get_end_rotation()

Loading…
Cancel
Save