GUI/AI improvements...

This commit is contained in:
Segey Lapin
2022-01-09 02:40:04 +03:00
parent fedb6a38e1
commit dfbd2f34a5
19 changed files with 662 additions and 130 deletions

32
ui/main_menu.gd Normal file
View File

@@ -0,0 +1,32 @@
extends Control
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
controls.is_gui = true
$v/quit.connect("pressed", self, "quit_pressed")
$v/load_game.connect("pressed", self, "load_game_pressed")
$v/new_game.connect("pressed", self, "new_game_pressed")
$v/edit_ai.connect("pressed", self, "edit_ai_pressed")
func quit_pressed():
get_tree().quit()
func load_game_pressed():
controls.is_gui = true
hide()
# var main = preload("res://world.tscn")
# get_tree().change_scene_to(main)
controls.menu.nosave = true
controls.menu.popup()
func new_game_pressed():
controls.is_gui = false
var main = preload("res://world.tscn")
get_tree().change_scene_to(main)
func edit_ai_pressed():
var ai_graph = preload("res://ui/ai_graph.tscn")
get_tree().change_scene_to(ai_graph)

60
ui/main_menu.tscn Normal file
View File

@@ -0,0 +1,60 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://ui/theme.tres" type="Theme" id=1]
[ext_resource path="res://ui/main_menu.gd" type="Script" id=2]
[node name="main_menu" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="ColorRect" type="ColorRect" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0.137255, 0.235294, 0.34902, 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="v" type="VBoxContainer" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -72.0
margin_top = -92.0
margin_right = 72.0
margin_bottom = 92.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="load_game" type="Button" parent="v"]
margin_right = 144.0
margin_bottom = 43.0
theme = ExtResource( 1 )
text = "Load game"
[node name="new_game" type="Button" parent="v"]
margin_top = 47.0
margin_right = 144.0
margin_bottom = 90.0
theme = ExtResource( 1 )
text = "New Game"
[node name="edit_ai" type="Button" parent="v"]
margin_top = 94.0
margin_right = 144.0
margin_bottom = 137.0
theme = ExtResource( 1 )
text = "Edit AI"
[node name="quit" type="Button" parent="v"]
margin_top = 141.0
margin_right = 144.0
margin_bottom = 184.0
theme = ExtResource( 1 )
text = "Quit"

97
ui/save_game.gd Normal file
View File

@@ -0,0 +1,97 @@
extends WindowDialog
var save_path = "user://saves"
var next_save_id = 0
var nosave = false
func _ready():
print("save/load ready")
$v/quit.connect("pressed", self, "exit_game")
$v/return.connect("pressed", self, "close")
$v/save/new_save.connect("pressed", self, "save_game")
connect("about_to_show", self, "about_to_show")
connect("popup_hide", self, "popup_hide")
func exit_game():
controls.is_gui = false
get_tree().quit()
func close():
controls.is_gui = false
hide()
func save_game():
scenario.prepare_save_data()
print("next_save_id: ", next_save_id)
var filename = save_path + "/" + str(next_save_id) + ".sav"
var f = File.new()
if f.open(filename, File.WRITE) == OK:
print("saving to ", filename)
f.store_string(JSON.print(scenario.save_data, "\t", false))
f.close()
print("userdata = ", OS.get_user_data_dir())
controls.is_gui = false
hide()
func load_game(file_name):
controls.is_gui = false
nosave = false
var f = File.new()
if f.open(save_path + "/" + file_name, File.READ) == OK:
var s = f.get_as_text()
var result = JSON.parse(s)
scenario.save_data = result.result
scenario.restart_scene()
else:
print("Can't open ", file_name)
hide()
func popup_hide():
yield(get_tree().create_timer(0.5), "timeout")
controls.is_gui = false
hide()
func about_to_show():
print("save/load about to show")
if nosave:
if $v/save/new_save.visible:
$v/save/new_save.hide()
else:
if !$v/save/new_save.visible:
$v/save/new_save.show()
var d: = Directory.new()
var f: = File.new()
var theme = Theme.new()
var font: DynamicFont = preload("res://fonts/DefaultFont.tres")
theme.default_font = font
for e in $v/save/s/v.get_children():
e.queue_free()
if d.dir_exists(save_path):
if d.open(save_path) == OK:
d.list_dir_begin()
var file_name = d.get_next()
while file_name != "":
if file_name in [".", ".."]:
file_name = d.get_next()
continue
var fid = file_name.replace(".sav", "")
if fid.is_valid_integer():
next_save_id = max(next_save_id, int(fid) + 1)
print(file_name)
var t = f.get_modified_time(save_path + "/" + file_name)
var ts = OS.get_datetime_from_unix_time(t)
var b = Button.new()
b.theme = theme
b.text = "Load " + str(ts.year) + "." + str(ts.month) + "." + str(ts.day)
b.text += " " + str(ts.hour) + ":" + str(ts.minute) + ":" + str(ts.second)
print(b.text)
$v/save/s/v.add_child(b)
b.connect("pressed", self, "load_game", [file_name])
$v/save/s/v.update()
$v/save/s.update()
$v/save.update()
$v.update()
update()
$v/quit.update()
file_name = d.get_next()
d.list_dir_end()
else:
d.make_dir_recursive(save_path)
print("next_save_id: ", next_save_id)
update()

66
ui/save_game.tscn Normal file
View File

@@ -0,0 +1,66 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://fonts/DefaultFont.tres" type="DynamicFont" id=1]
[ext_resource path="res://ui/save_game.gd" type="Script" id=2]
[node name="save_game" type="WindowDialog"]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 10.0
margin_top = 30.0
margin_right = -10.0
margin_bottom = -30.0
script = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="v" type="VBoxContainer" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
size_flags_horizontal = 3
size_flags_vertical = 3
__meta__ = {
"_edit_use_anchors_": false
}
[node name="return" type="Button" parent="v"]
margin_right = 1004.0
margin_bottom = 43.0
custom_fonts/font = ExtResource( 1 )
text = "Return to game"
[node name="save" type="VBoxContainer" parent="v"]
margin_top = 47.0
margin_right = 1004.0
margin_bottom = 493.0
size_flags_horizontal = 3
size_flags_vertical = 3
__meta__ = {
"_edit_use_anchors_": false
}
[node name="new_save" type="Button" parent="v/save"]
margin_right = 1004.0
margin_bottom = 43.0
custom_fonts/font = ExtResource( 1 )
text = "New save"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="s" type="ScrollContainer" parent="v/save"]
margin_top = 47.0
margin_right = 1004.0
margin_bottom = 446.0
size_flags_horizontal = 3
size_flags_vertical = 3
[node name="v" type="VBoxContainer" parent="v/save/s"]
[node name="quit" type="Button" parent="v"]
margin_top = 497.0
margin_right = 1004.0
margin_bottom = 540.0
custom_fonts/font = ExtResource( 1 )
text = "Quit game"

6
ui/theme.tres Normal file
View File

@@ -0,0 +1,6 @@
[gd_resource type="Theme" load_steps=2 format=2]
[ext_resource path="res://fonts/DefaultFont.tres" type="DynamicFont" id=1]
[resource]
default_font = ExtResource( 1 )

25
ui/town_menu.gd Normal file
View File

@@ -0,0 +1,25 @@
extends Control
func _ready():
$buttons/return.connect("pressed", self, "finish")
$buttons/upgrade.connect("pressed", self, "upgrade")
$buttons/teleport_to_town.connect("pressed", self, "teleport")
set_physics_process(false)
func finish():
controls.is_gui = false
hide()
func upgrade():
controls.is_gui = false
hide()
scenario.camp_level += 1
scenario.camp.queue_free()
scenario.camp = streaming.setup_bandit_camp(scenario.camp_site)
func teleport():
set_physics_process(true)
func _physics_process(delta):
var cam = get_viewport().get_camera()
if cam:
var player = cam.get_meta("player")
player.global_transform.origin = scenario.camp.global_transform.origin + Vector3(0, 10, 0)
set_physics_process(false)

45
ui/town_menu.tscn Normal file
View File

@@ -0,0 +1,45 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://ui/theme.tres" type="Theme" id=1]
[ext_resource path="res://ui/town_menu.gd" type="Script" id=2]
[node name="town_menu" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="buttons" type="VBoxContainer" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -20.0
margin_top = -20.0
margin_right = 20.0
margin_bottom = 20.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="upgrade" type="Button" parent="buttons"]
margin_right = 217.0
margin_bottom = 43.0
theme = ExtResource( 1 )
text = "Upgrade town"
[node name="teleport_to_town" type="Button" parent="buttons"]
margin_top = 47.0
margin_right = 217.0
margin_bottom = 90.0
theme = ExtResource( 1 )
text = "Teleport to town"
[node name="return" type="Button" parent="buttons"]
margin_top = 94.0
margin_right = 217.0
margin_bottom = 137.0
theme = ExtResource( 1 )
text = "Return"