Added support for editing multiple islands

master
Martin Felis 2021-06-28 22:34:48 +02:00
parent 47519ade75
commit f8d77b4a60
6 changed files with 90 additions and 46 deletions

View File

@ -44,7 +44,3 @@ func ScreenToHex(pos: Vector2, camera: Camera2D):
func WorldToScreen(pos: Vector2, camera: Camera2D): func WorldToScreen(pos: Vector2, camera: Camera2D):
return pos * camera.zoom + camera.offset return pos * camera.zoom + camera.offset
func DrawTile(world_coords: Vector2, tile_type):
HexTileDrawer.draw_tile(world_coords, tile_type)

View File

@ -9,11 +9,12 @@ extends Node2D
onready var EditorUI = get_node("UI/Editor") onready var EditorUI = get_node("UI/Editor")
onready var TileTypes = get_node("UI/TileTypes") onready var TileTypes = get_node("UI/TileTypes")
onready var NoneButton = get_node("UI/Editor/NoneButton") onready var NoneButton = get_node("UI/Editor/NoneButton")
onready var IslandIndex = get_node("UI/Editor/IslandIndex")
# World objects # World objects
onready var world = get_node("../World") onready var world = get_node("../World")
onready var world_camera = get_node("../World/Camera") onready var world_camera = get_node("../World/Camera")
var last_index = -1
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
@ -21,6 +22,8 @@ func _ready():
func handle_editor_event(event): func handle_editor_event(event):
var island = world.current_island
if (Input.get_mouse_button_mask() & BUTTON_LEFT) == BUTTON_LEFT: if (Input.get_mouse_button_mask() & BUTTON_LEFT) == BUTTON_LEFT:
# if event.pressed and event.button_index == BUTTON_LEFT: # if event.pressed and event.button_index == BUTTON_LEFT:
var pressed_button = NoneButton.group.get_pressed_button() var pressed_button = NoneButton.group.get_pressed_button()
@ -29,12 +32,7 @@ func handle_editor_event(event):
tile_type = pressed_button.text tile_type = pressed_button.text
var hex_coord = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position,world.WorldCamera)) var hex_coord = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position,world.WorldCamera))
if tile_type != "None": island.set_tile(hex_coord, tile_type)
world.tile_data[hex_coord] = tile_type
update()
elif hex_coord in world.tile_data.keys():
world.tile_data.erase(hex_coord)
update()
return true return true
@ -56,20 +54,39 @@ func _unhandled_input(event):
update() update()
func get_island_filename_for_index(index):
return "user://pirate_game_island_" + str(index) + ".island"
func get_island_filename():
return get_island_filename_for_index(IslandIndex.value)
func _on_SaveWorldButton_pressed(): func _on_SaveWorldButton_pressed():
world.save_world("user://pirate_game_world.save") var island = world.current_island
island.save_island(get_island_filename())
func _on_ClearWorldButton_pressed(): func _on_ClearWorldButton_pressed():
world.tile_data = {} var island = world.current_island
world.update() island.clear_island()
func _on_LoadWorldButton_pressed(): func _on_LoadWorldButton_pressed():
world.load_world("user://pirate_game_world.save") var island = world.current_island
update() island.load_island(get_island_filename())
last_index = IslandIndex.value
func _on_EditIslandButton_toggled(button_pressed): func _on_EditIslandButton_toggled(button_pressed):
EditorUI.visible = button_pressed EditorUI.visible = button_pressed
print (button_pressed) print (button_pressed)
func _on_IslandIndex_value_changed(value):
var island = world.current_island
if last_index >= 0:
island.save_island(get_island_filename_for_index(last_index))
island.clear_island()
island.load_island(get_island_filename())

View File

@ -139,39 +139,45 @@ __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="World" type="Label" parent="Editor/UI/Editor"] [node name="Island" type="Label" parent="Editor/UI/Editor"]
margin_right = 82.0 margin_right = 110.0
margin_bottom = 33.0 margin_bottom = 33.0
text = "World" text = "Island"
[node name="IslandIndex" type="SpinBox" parent="Editor/UI/Editor"]
margin_top = 37.0
margin_right = 110.0
margin_bottom = 80.0
max_value = 9.0
[node name="ClearWorldButton" type="Button" parent="Editor/UI/Editor"] [node name="ClearWorldButton" type="Button" parent="Editor/UI/Editor"]
margin_top = 37.0 margin_top = 84.0
margin_right = 82.0 margin_right = 110.0
margin_bottom = 76.0 margin_bottom = 123.0
text = "Clear" text = "Clear"
[node name="LoadWorldButton" type="Button" parent="Editor/UI/Editor"] [node name="LoadWorldButton" type="Button" parent="Editor/UI/Editor"]
margin_top = 80.0 margin_top = 127.0
margin_right = 82.0 margin_right = 110.0
margin_bottom = 119.0 margin_bottom = 166.0
text = "Load" text = "Load"
[node name="SaveWorldButton" type="Button" parent="Editor/UI/Editor"] [node name="SaveWorldButton" type="Button" parent="Editor/UI/Editor"]
margin_top = 123.0 margin_top = 170.0
margin_right = 82.0 margin_right = 110.0
margin_bottom = 162.0 margin_bottom = 209.0
text = "Save" text = "Save"
[node name="Label" type="Label" parent="Editor/UI/Editor"] [node name="Label" type="Label" parent="Editor/UI/Editor"]
margin_top = 166.0 margin_top = 213.0
margin_right = 82.0 margin_right = 110.0
margin_bottom = 199.0 margin_bottom = 246.0
text = "Tiles" text = "Tiles"
[node name="NoneButton" type="Button" parent="Editor/UI/Editor"] [node name="NoneButton" type="Button" parent="Editor/UI/Editor"]
margin_top = 203.0 margin_top = 250.0
margin_right = 82.0 margin_right = 110.0
margin_bottom = 242.0 margin_bottom = 289.0
rect_pivot_offset = Vector2( -1239.87, 282.07 ) rect_pivot_offset = Vector2( -1239.87, 282.07 )
toggle_mode = true toggle_mode = true
group = SubResource( 3 ) group = SubResource( 3 )
@ -181,9 +187,9 @@ __meta__ = {
} }
[node name="SandButton" type="Button" parent="Editor/UI/Editor"] [node name="SandButton" type="Button" parent="Editor/UI/Editor"]
margin_top = 246.0 margin_top = 293.0
margin_right = 82.0 margin_right = 110.0
margin_bottom = 285.0 margin_bottom = 332.0
rect_pivot_offset = Vector2( -1239.87, 282.07 ) rect_pivot_offset = Vector2( -1239.87, 282.07 )
toggle_mode = true toggle_mode = true
group = SubResource( 3 ) group = SubResource( 3 )
@ -193,14 +199,15 @@ __meta__ = {
} }
[node name="GrassButton" type="Button" parent="Editor/UI/Editor"] [node name="GrassButton" type="Button" parent="Editor/UI/Editor"]
margin_top = 289.0 margin_top = 336.0
margin_right = 82.0 margin_right = 110.0
margin_bottom = 328.0 margin_bottom = 375.0
rect_pivot_offset = Vector2( -1239.87, 282.07 ) rect_pivot_offset = Vector2( -1239.87, 282.07 )
toggle_mode = true toggle_mode = true
group = SubResource( 3 ) group = SubResource( 3 )
text = "Grass" text = "Grass"
[connection signal="toggled" from="World/UI/TopContainer/EditIslandButton" to="Editor" method="_on_EditIslandButton_toggled"] [connection signal="toggled" from="World/UI/TopContainer/EditIslandButton" to="Editor" method="_on_EditIslandButton_toggled"]
[connection signal="value_changed" from="Editor/UI/Editor/IslandIndex" to="Editor" method="_on_IslandIndex_value_changed"]
[connection signal="pressed" from="Editor/UI/Editor/ClearWorldButton" to="Editor" method="_on_ClearWorldButton_pressed"] [connection signal="pressed" from="Editor/UI/Editor/ClearWorldButton" to="Editor" method="_on_ClearWorldButton_pressed"]
[connection signal="pressed" from="Editor/UI/Editor/LoadWorldButton" to="Editor" method="_on_LoadWorldButton_pressed"] [connection signal="pressed" from="Editor/UI/Editor/LoadWorldButton" to="Editor" method="_on_LoadWorldButton_pressed"]
[connection signal="pressed" from="Editor/UI/Editor/SaveWorldButton" to="Editor" method="_on_SaveWorldButton_pressed"] [connection signal="pressed" from="Editor/UI/Editor/SaveWorldButton" to="Editor" method="_on_SaveWorldButton_pressed"]

View File

@ -6,6 +6,7 @@ enum TileType {
Grass Grass
} }
var TileTypeFromString = { "None": TileType.None, "Sand": TileType.Sand, "Grass": TileType.Grass }
var HexPoints = null var HexPoints = null
var HexColors = [] var HexColors = []
@ -29,7 +30,7 @@ func set_hex_scale(scale):
for i in range (7): for i in range (7):
var angle = (60 * i) * PI / 180 var angle = (60 * i) * PI / 180
HexPoints.append(Vector2(cos(angle), sin(angle)) * hex_scale.x / 2) HexPoints.append(Vector2(cos(angle), sin(angle)) * hex_scale.x / 2)
NoneColors.append("#555555") NoneColors.append("#885555")
SandColors.append("#ffa106") SandColors.append("#ffa106")
GrassColors.append("#4b9635") GrassColors.append("#4b9635")

View File

@ -7,6 +7,21 @@ func _ready():
pass # Replace with function body. pass # Replace with function body.
func set_tile(coord: Vector2, value: String):
if HexTileDrawer.TileTypeFromString[value] == HexTileDrawer.TileType.None:
if coord in tiles.keys():
tiles.erase(coord)
else:
tiles[coord] = value
update()
func clear_island():
tiles = {}
update()
func save_island(path: String): func save_island(path: String):
var island_save_data = File.new() var island_save_data = File.new()
island_save_data.open(path, File.WRITE) island_save_data.open(path, File.WRITE)
@ -16,7 +31,13 @@ func save_island(path: String):
func load_island(path: String): func load_island(path: String):
var island_save_data = File.new() var island_save_data = File.new()
if !island_save_data.file_exists(path):
clear_island()
return
island_save_data.open(path, File.READ) island_save_data.open(path, File.READ)
var data = parse_json(island_save_data.get_line()) var data = parse_json(island_save_data.get_line())
island_save_data.close() island_save_data.close()
@ -35,10 +56,12 @@ func load_island(path: String):
var coords = Vector2.ZERO var coords = Vector2.ZERO
var regresult = coord_regex.search(k) var regresult = coord_regex.search(k)
if regresult: if regresult:
print(regresult.get_string("tile_x"), " - ", regresult.get_string("tile_y")) # 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"))) coords = Vector2(float(regresult.get_string("tile_x")), float(regresult.get_string("tile_y")))
tiles[coords] = data[k] tiles[coords] = data[k]
update()
func _draw(): func _draw():
for coord in tiles.keys(): for coord in tiles.keys():

View File

@ -23,17 +23,17 @@ var drag_start = null
var target = Vector2() var target = Vector2()
var tile_data = {} var tile_data = {}
var islands = [] var islands = []
var island = null var current_island = null
func _ready(): func _ready():
Grid.view_camera = WorldCamera Grid.view_camera = WorldCamera
GridHighlight.view_camera = WorldCamera GridHighlight.view_camera = WorldCamera
island = Island.new() current_island = Island.new()
Islands.add_child(island) Islands.add_child(current_island)
island.load_island("user://pirate_game_world.save") current_island.load_island("user://pirate_game_world.save")
# Set player starting position # Set player starting position
PlayerChar.position = Globals.HexGrid.get_hex_center(Vector2(0,0)) PlayerChar.position = Globals.HexGrid.get_hex_center(Vector2(0,0))