Minor cleanup World.gd

master
Martin Felis 2021-07-10 11:24:04 +02:00
parent 1e85465873
commit cf7a62d89b
2 changed files with 46 additions and 70 deletions

View File

@ -253,7 +253,6 @@ 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="pressed" from="World/UI/TopContainer/Button" to="World" method="_on_generate_button_pressed"] [connection signal="pressed" from="World/UI/TopContainer/Button" to="World" method="_on_generate_button_pressed"]
[connection signal="value_changed" from="Editor/UI/Editor/IslandIndex" to="Editor" method="_on_IslandIndex_value_changed"] [connection signal="value_changed" from="Editor/UI/Editor/IslandIndex" to="Editor" method="_on_IslandIndex_value_changed"]

View File

@ -29,6 +29,9 @@ var current_island = null
var player_navigation_path = [] var player_navigation_path = []
#
# Godot Functions
#
func _ready(): func _ready():
Grid.view_camera = WorldCamera Grid.view_camera = WorldCamera
GridHighlight.view_camera = WorldCamera GridHighlight.view_camera = WorldCamera
@ -39,12 +42,6 @@ func _ready():
PlayerChar.position = Globals.HexGrid.get_hex_center(Vector2(0,0)) PlayerChar.position = Globals.HexGrid.get_hex_center(Vector2(0,0))
func clear_islands():
for island in Islands.get_children():
Islands.remove_child(island)
island.queue_free()
func _process(_delta): func _process(_delta):
WorldCamera.offset = PlayerChar.position WorldCamera.offset = PlayerChar.position
PlayerBoat.transform.origin = PlayerChar.transform.origin PlayerBoat.transform.origin = PlayerChar.transform.origin
@ -58,6 +55,27 @@ func _process(_delta):
PlayerChar.target = player_navigation_path[0] PlayerChar.target = player_navigation_path[0]
func _draw():
var nav_path_len = len(player_navigation_path)
if nav_path_len > 0:
var last_point = player_navigation_path[0]
draw_circle(last_point, 5, "#f200f2")
for i in range (1, nav_path_len):
var cur_point = player_navigation_path[i]
draw_line (last_point, cur_point, "#f200f2")
draw_circle(cur_point, 5, "#f200f2")
last_point = cur_point
#
# World Modification/Query
#
func clear_islands():
for island in Islands.get_children():
Islands.remove_child(island)
island.queue_free()
func get_tile_type(world_coord: Vector2): func get_tile_type(world_coord: Vector2):
for island in Islands.get_children(): for island in Islands.get_children():
var tile = island.get_tile_by_world_coord(world_coord) var tile = island.get_tile_by_world_coord(world_coord)
@ -74,17 +92,6 @@ func add_island_at(file_name, offset_world: Vector2):
Islands.add_child(island) Islands.add_child(island)
func populate_ocean_grid():
var obstacles = Globals.OceanGrid.get_obstacles()
Globals.OceanGrid.remove_obstacles(obstacles.keys())
Globals.OceanGrid.set_bounds(Vector2.ONE * -500, Vector2.ONE * 500)
for island in Islands.get_children():
for tile in island.tiles.keys():
var grid_coords = Globals.WorldToHex(tile + island.offset_world)
Globals.OceanGrid.add_obstacles(grid_coords, 5)
func check_island_location_valid(new_island): func check_island_location_valid(new_island):
var grid_origin_world_coord = Globals.HexToWorld(Vector2(0,0)) var grid_origin_world_coord = Globals.HexToWorld(Vector2(0,0))
if new_island.get_tile_by_world_coord(grid_origin_world_coord) != null: if new_island.get_tile_by_world_coord(grid_origin_world_coord) != null:
@ -112,7 +119,7 @@ func generate():
for i in range (num_islands): for i in range (num_islands):
var island = Island.new() var island = Island.new()
var island_index = i % 2 var island_index = i % 4
var file_name = "user://pirate_game_island_" + str(island_index) + ".island" var file_name = "user://pirate_game_island_" + str(island_index) + ".island"
island.load_island(file_name) island.load_island(file_name)
@ -139,6 +146,19 @@ func generate():
populate_ocean_grid() populate_ocean_grid()
#
# Navigation
#
func populate_ocean_grid():
var obstacles = Globals.OceanGrid.get_obstacles()
Globals.OceanGrid.remove_obstacles(obstacles.keys())
Globals.OceanGrid.set_bounds(Vector2.ONE * -500, Vector2.ONE * 500)
for island in Islands.get_children():
for tile in island.tiles.keys():
var grid_coords = Globals.WorldToHex(tile + island.offset_world)
Globals.OceanGrid.add_obstacles(grid_coords, 15)
func update_player_navigation_target(target_world: Vector2): func update_player_navigation_target(target_world: Vector2):
var start_coord = Globals.WorldToHex(PlayerChar.transform.origin) var start_coord = Globals.WorldToHex(PlayerChar.transform.origin)
@ -160,6 +180,13 @@ func update_player_navigation_target(target_world: Vector2):
update() update()
#
# Input & Events
#
func _on_generate_button_pressed():
generate()
func handle_game_event(event): func handle_game_event(event):
if event is InputEventMouseButton: if event is InputEventMouseButton:
# Move main character # Move main character
@ -206,53 +233,3 @@ func _unhandled_input(event):
OffsetValueLabel.text = str(WorldCamera.offset) OffsetValueLabel.text = str(WorldCamera.offset)
ZoomValueLabel.text = str(WorldCamera.zoom) ZoomValueLabel.text = str(WorldCamera.zoom)
func save_world(path: String):
var world_save_data = File.new()
world_save_data.open(path, File.WRITE)
world_save_data.store_line (to_json(tile_data))
world_save_data.close()
func load_world(path: String):
var world_save_data = File.new()
world_save_data.open(path, 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()
func _on_generate_button_pressed():
generate()
func _draw():
var nav_path_len = len(player_navigation_path)
if nav_path_len > 0:
var last_point = player_navigation_path[0]
draw_circle(last_point, 5, "#f200f2")
for i in range (1, nav_path_len):
var cur_point = player_navigation_path[i]
draw_line (last_point, cur_point, "#f200f2")
draw_circle(cur_point, 5, "#f200f2")
last_point = cur_point