Improved editor UI, added saving loading of world

master
Martin Felis 2021-06-22 22:47:01 +02:00
parent 5fe013f128
commit c96605dadd
3 changed files with 117 additions and 43 deletions

View File

@ -10,6 +10,7 @@ config_version=4
_global_script_classes=[ ]
_global_script_class_icons={
}
[application]

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=12 format=2]
[gd_scene load_steps=10 format=2]
[ext_resource path="res://scenes/World.gd" type="Script" id=1]
[ext_resource path="res://scenes/HexTile.gd" type="Script" id=2]
@ -6,8 +6,6 @@
[ext_resource path="res://scenes/FPSValue.gd" type="Script" id=4]
[ext_resource path="res://scenes/pirate.gd" type="Script" id=5]
[ext_resource path="res://fonts/Roboto/Roboto-Regular.ttf" type="DynamicFontData" id=6]
[ext_resource path="res://scenes/Island.gd" type="Script" id=7]
[ext_resource path="res://scenes/GridHighlight.gd" type="Script" id=8]
[sub_resource type="DynamicFont" id=1]
size = 27
@ -31,7 +29,6 @@ current = true
script = ExtResource( 2 )
[node name="GridHighlight" type="Node2D" parent="World"]
script = ExtResource( 8 )
[node name="UI" type="CanvasLayer" parent="World"]
@ -113,19 +110,49 @@ text = "0"
align = 2
script = ExtResource( 4 )
[node name="TileTypes" type="VBoxContainer" parent="World/UI"]
anchor_left = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = -82.0
[node name="Editor" type="VBoxContainer" parent="World/UI"]
margin_left = 12.0
margin_top = 53.0
margin_right = 82.0
margin_bottom = 600.0
theme = SubResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="NoneButton" type="Button" parent="World/UI/TileTypes"]
[node name="World" type="Label" parent="World/UI/Editor"]
margin_right = 82.0
margin_bottom = 39.0
margin_bottom = 33.0
text = "World"
[node name="ClearWorldButton" type="Button" parent="World/UI/Editor"]
margin_top = 37.0
margin_right = 82.0
margin_bottom = 76.0
text = "Clear"
[node name="LoadWorldButton" type="Button" parent="World/UI/Editor"]
margin_top = 80.0
margin_right = 82.0
margin_bottom = 119.0
text = "Load"
[node name="SaveWorldButton" type="Button" parent="World/UI/Editor"]
margin_top = 123.0
margin_right = 82.0
margin_bottom = 162.0
text = "Save"
[node name="Label" type="Label" parent="World/UI/Editor"]
margin_top = 166.0
margin_right = 82.0
margin_bottom = 199.0
text = "Tiles"
[node name="NoneButton" type="Button" parent="World/UI/Editor"]
margin_top = 203.0
margin_right = 82.0
margin_bottom = 242.0
rect_pivot_offset = Vector2( -1239.87, 282.07 )
toggle_mode = true
group = SubResource( 3 )
@ -134,10 +161,10 @@ __meta__ = {
"_edit_use_anchors_": false
}
[node name="SandButton" type="Button" parent="World/UI/TileTypes"]
margin_top = 43.0
[node name="SandButton" type="Button" parent="World/UI/Editor"]
margin_top = 246.0
margin_right = 82.0
margin_bottom = 82.0
margin_bottom = 285.0
rect_pivot_offset = Vector2( -1239.87, 282.07 )
toggle_mode = true
group = SubResource( 3 )
@ -146,10 +173,10 @@ __meta__ = {
"_edit_use_anchors_": false
}
[node name="GrassButton" type="Button" parent="World/UI/TileTypes"]
margin_top = 86.0
[node name="GrassButton" type="Button" parent="World/UI/Editor"]
margin_top = 289.0
margin_right = 82.0
margin_bottom = 125.0
margin_bottom = 328.0
rect_pivot_offset = Vector2( -1239.87, 282.07 )
toggle_mode = true
group = SubResource( 3 )
@ -165,4 +192,6 @@ texture = ExtResource( 3 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="World/PlayerChar"]
[node name="Island" type="Node" parent="World"]
script = ExtResource( 7 )
[connection signal="pressed" from="World/UI/Editor/ClearWorldButton" to="World" method="_on_ClearWorldButton_pressed"]
[connection signal="pressed" from="World/UI/Editor/LoadWorldButton" to="World" method="_on_LoadWorldButton_pressed"]
[connection signal="pressed" from="World/UI/Editor/SaveWorldButton" to="World" method="_on_SaveWorldButton_pressed"]

View File

@ -2,7 +2,7 @@ extends Node2D
onready var EditIslandButton = get_node("UI/TopContainer/EditIslandButton")
onready var TileTypes = get_node("UI/TileTypes")
onready var NoneButton = get_node("UI/TileTypes/NoneButton")
onready var NoneButton = get_node("UI/Editor/NoneButton")
onready var GridDrawer = get_node("GridDrawer")
onready var WorldCamera = get_node("Camera")
onready var OffsetValueLabel = get_node("UI/TopContainer/OffsetValue")
@ -24,6 +24,7 @@ var tile_data = {}
# Called when the node enters the scene tree for the first time.
func _ready():
_on_LoadWorldButton_pressed()
HexGrid.hex_scale = Vector2(hex_size, hex_size)
# Set player starting position
@ -31,8 +32,9 @@ func _ready():
GridDrawer.hex_scale = Vector2(hex_size, hex_size)
func _draw():
draw_grid()
draw_tiles()
draw_grid()
func draw_grid():
var view_grid_corners = calc_grid_view_rect()
@ -42,8 +44,6 @@ func draw_grid():
var bl_tile_coords = HexGrid.get_hex_center(view_grid_corners[2])
var br_tile_coords = HexGrid.get_hex_center(view_grid_corners[3])
print (tl_tile_coords, bl_tile_coords)
var nc = ceil ((tr_tile_coords[0] - tl_tile_coords[0]) * 1.5 / hex_size)
var nr = ceil ((bl_tile_coords[1] - tr_tile_coords[1]) / hex_size) + 2
@ -89,25 +89,28 @@ func handle_game_event(event):
if event.pressed and event.button_index == BUTTON_LEFT:
PlayerChar.target = HexGrid.get_hex_center(screen_to_hex(event.position))
return true
return false
func handle_editor_event(event):
if event is InputEventMouseButton:
# Move main character
if event.pressed and event.button_index == BUTTON_LEFT:
var pressed_button = NoneButton.group.get_pressed_button()
var tile_type = "None"
if pressed_button:
tile_type = pressed_button.text
var hex_coord = HexGrid.get_hex_center(screen_to_hex(event.position))
if tile_type != "None":
tile_data[hex_coord] = tile_type
elif hex_coord in tile_data.keys():
tile_data.erase(hex_coord)
return true
if (Input.get_mouse_button_mask() & BUTTON_LEFT) == BUTTON_LEFT:
# if event.pressed and event.button_index == BUTTON_LEFT:
var pressed_button = NoneButton.group.get_pressed_button()
var tile_type = "None"
if pressed_button:
tile_type = pressed_button.text
var hex_coord = HexGrid.get_hex_center(screen_to_hex(event.position))
if tile_type != "None":
tile_data[hex_coord] = tile_type
update()
elif hex_coord in tile_data.keys():
tile_data.erase(hex_coord)
update()
return true
return false
func _unhandled_input(event):
if event is InputEventMouseButton:
@ -131,14 +134,55 @@ func _unhandled_input(event):
WorldCamera.zoom = WorldCamera.zoom * 0.8
if 'position' in event:
hex_hover = screen_to_hex(event.position)
HexCoordValueLabel.text = str(hex_hover)
if is_dragging:
WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x
else:
hex_hover = screen_to_hex(event.position)
HexCoordValueLabel.text = str(hex_hover)
elif EditIslandButton.pressed:
if handle_editor_event (event):
return
update()
OffsetValueLabel.text = str(WorldCamera.offset)
ZoomValueLabel.text = str(WorldCamera.zoom)
func _on_SaveWorldButton_pressed():
var world_save_data = File.new()
world_save_data.open("user://pirate_game_world.save", File.WRITE)
world_save_data.store_line (to_json(tile_data))
world_save_data.close()
func _on_ClearWorldButton_pressed():
tile_data = {}
update()
func _on_LoadWorldButton_pressed():
var world_save_data = File.new()
world_save_data.open("user://pirate_game_world.save", File.READ)
var data = parse_json(world_save_data.get_line())
world_save_data.close()
var data_keys = data.keys()
tile_data = {}
var coord_regex = RegEx.new()
coord_regex.compile("\\((?<tile_x>-?\\d+(.?\\d+)?), (?<tile_y>-?\\d+(.?\\d+)?)\\)")
var reg_test_result = coord_regex.search("(0, 0)")
assert(reg_test_result)
reg_test_result = coord_regex.search("(123.124, 552.0)")
assert(reg_test_result)
for k in data_keys:
var coords = Vector2.ZERO
var regresult = coord_regex.search(k)
if regresult:
print(regresult.get_string("tile_x"), " - ", regresult.get_string("tile_y"))
coords = Vector2(float(regresult.get_string("tile_x")), float(regresult.get_string("tile_y")))
tile_data[coords] = data[k]
update()