diff --git a/Globals.gd b/Globals.gd index 0716f4d..9f50f60 100644 --- a/Globals.gd +++ b/Globals.gd @@ -7,6 +7,7 @@ const GRID_COLOR="#000000" const SHOVEL_DURATION=1 var DebugLabel = null +var HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new() # Called when the node enters the scene tree for the first time. func _ready(): diff --git a/scenes/Editor.gd b/scenes/Editor.gd new file mode 100644 index 0000000..4608cc7 --- /dev/null +++ b/scenes/Editor.gd @@ -0,0 +1,75 @@ +extends Node2D + + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + +# UI elements +onready var EditorUI = get_node("UI/Editor") +onready var TileTypes = get_node("UI/TileTypes") +onready var NoneButton = get_node("UI/Editor/NoneButton") + +# World objects +onready var world = get_node("../World") +onready var world_camera = get_node("../World/Camera") + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + + +func handle_editor_event(event): + 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 = Globals.HexGrid.get_hex_center(world.screen_to_hex(event.position)) + if tile_type != "None": + 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 false + + +func _unhandled_input(event): + if not EditorUI.visible: + return + + if event is InputEventMouseButton: + if handle_editor_event (event): + return + + if 'position' in event: + if handle_editor_event (event): + return + + update() + + +func _on_SaveWorldButton_pressed(): + world.save_world("user://pirate_game_world.save") + + +func _on_ClearWorldButton_pressed(): + world.tile_data = {} + world.update() + + +func _on_LoadWorldButton_pressed(): + world.load_world("user://pirate_game_world.save") + update() + + +func _on_EditIslandButton_toggled(button_pressed): + EditorUI.visible = button_pressed + print (button_pressed) diff --git a/scenes/Game.tscn b/scenes/Game.tscn index b5a7d84..05980d0 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=2] +[gd_scene load_steps=11 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,6 +6,7 @@ [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/Editor.gd" type="Script" id=7] [sub_resource type="DynamicFont" id=1] size = 27 @@ -40,22 +41,22 @@ __meta__ = { } [node name="EditIslandButton" type="CheckButton" parent="World/UI/TopContainer"] -margin_right = 147.0 +margin_right = 117.0 margin_bottom = 40.0 pressed = true -text = "Edit Island" +text = "Editor" [node name="Offset" type="Label" parent="World/UI/TopContainer"] -margin_left = 151.0 +margin_left = 121.0 margin_top = 13.0 -margin_right = 198.0 +margin_right = 168.0 margin_bottom = 27.0 text = "Offset: " [node name="OffsetValue" type="Label" parent="World/UI/TopContainer"] -margin_left = 202.0 +margin_left = 172.0 margin_top = 13.0 -margin_right = 222.0 +margin_right = 192.0 margin_bottom = 27.0 text = "0,0" __meta__ = { @@ -63,16 +64,16 @@ __meta__ = { } [node name="Zoom" type="Label" parent="World/UI/TopContainer"] -margin_left = 226.0 +margin_left = 196.0 margin_top = 13.0 -margin_right = 275.0 +margin_right = 245.0 margin_bottom = 27.0 text = " Zoom: " [node name="ZoomValue" type="Label" parent="World/UI/TopContainer"] -margin_left = 279.0 +margin_left = 249.0 margin_top = 13.0 -margin_right = 299.0 +margin_right = 269.0 margin_bottom = 27.0 text = "0.0" __meta__ = { @@ -80,108 +81,36 @@ __meta__ = { } [node name="HexCoord" type="Label" parent="World/UI/TopContainer"] -margin_left = 303.0 +margin_left = 273.0 margin_top = 13.0 -margin_right = 377.0 +margin_right = 347.0 margin_bottom = 27.0 text = " HexCoord: " [node name="HexCoordValue" type="Label" parent="World/UI/TopContainer"] -margin_left = 381.0 +margin_left = 351.0 margin_top = 13.0 -margin_right = 409.0 +margin_right = 379.0 margin_bottom = 27.0 text = "(0,0)" [node name="VSeparator" type="VSeparator" parent="World/UI/TopContainer"] -margin_left = 413.0 -margin_right = 417.0 +margin_left = 383.0 +margin_right = 387.0 margin_bottom = 40.0 __meta__ = { "_edit_use_anchors_": false } [node name="FPSValue" type="Label" parent="World/UI/TopContainer"] -margin_left = 421.0 +margin_left = 391.0 margin_top = 13.0 -margin_right = 429.0 +margin_right = 399.0 margin_bottom = 27.0 text = "0" align = 2 script = ExtResource( 4 ) -[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="World" type="Label" parent="World/UI/Editor"] -margin_right = 82.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 ) -text = "None" -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="SandButton" type="Button" parent="World/UI/Editor"] -margin_top = 246.0 -margin_right = 82.0 -margin_bottom = 285.0 -rect_pivot_offset = Vector2( -1239.87, 282.07 ) -toggle_mode = true -group = SubResource( 3 ) -text = "Sand" -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="GrassButton" type="Button" parent="World/UI/Editor"] -margin_top = 289.0 -margin_right = 82.0 -margin_bottom = 328.0 -rect_pivot_offset = Vector2( -1239.87, 282.07 ) -toggle_mode = true -group = SubResource( 3 ) -text = "Grass" - [node name="PlayerChar" type="KinematicBody2D" parent="World"] script = ExtResource( 5 ) @@ -192,6 +121,84 @@ texture = ExtResource( 3 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="World/PlayerChar"] [node name="Island" type="Node" parent="World"] -[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"] + +[node name="Editor" type="Node2D" parent="."] +script = ExtResource( 7 ) + +[node name="UI" type="CanvasLayer" parent="Editor"] + +[node name="Editor" type="VBoxContainer" parent="Editor/UI"] +margin_left = 12.0 +margin_top = 53.0 +margin_right = 94.0 +margin_bottom = 600.0 +theme = SubResource( 2 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="World" type="Label" parent="Editor/UI/Editor"] +margin_right = 82.0 +margin_bottom = 33.0 +text = "World" + +[node name="ClearWorldButton" type="Button" parent="Editor/UI/Editor"] +margin_top = 37.0 +margin_right = 82.0 +margin_bottom = 76.0 +text = "Clear" + +[node name="LoadWorldButton" type="Button" parent="Editor/UI/Editor"] +margin_top = 80.0 +margin_right = 82.0 +margin_bottom = 119.0 +text = "Load" + +[node name="SaveWorldButton" type="Button" parent="Editor/UI/Editor"] +margin_top = 123.0 +margin_right = 82.0 +margin_bottom = 162.0 +text = "Save" + +[node name="Label" type="Label" parent="Editor/UI/Editor"] +margin_top = 166.0 +margin_right = 82.0 +margin_bottom = 199.0 +text = "Tiles" + +[node name="NoneButton" type="Button" parent="Editor/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 ) +text = "None" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="SandButton" type="Button" parent="Editor/UI/Editor"] +margin_top = 246.0 +margin_right = 82.0 +margin_bottom = 285.0 +rect_pivot_offset = Vector2( -1239.87, 282.07 ) +toggle_mode = true +group = SubResource( 3 ) +text = "Sand" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="GrassButton" type="Button" parent="Editor/UI/Editor"] +margin_top = 289.0 +margin_right = 82.0 +margin_bottom = 328.0 +rect_pivot_offset = Vector2( -1239.87, 282.07 ) +toggle_mode = true +group = SubResource( 3 ) +text = "Grass" +[connection signal="toggled" from="World/UI/TopContainer/EditIslandButton" to="Editor" method="_on_EditIslandButton_toggled"] +[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/SaveWorldButton" to="Editor" method="_on_SaveWorldButton_pressed"] diff --git a/scenes/Island.gd b/scenes/Island.gd new file mode 100644 index 0000000..1eccaec --- /dev/null +++ b/scenes/Island.gd @@ -0,0 +1,16 @@ +extends Node + + +# 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(): + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass diff --git a/scenes/World.gd b/scenes/World.gd index 089130d..54bb1b3 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -1,8 +1,6 @@ extends Node2D onready var EditIslandButton = get_node("UI/TopContainer/EditIslandButton") -onready var TileTypes = get_node("UI/TileTypes") -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") @@ -11,7 +9,7 @@ onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue") onready var PlayerChar = get_node("PlayerChar") onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue") -var HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new() +var Island = preload("Island.gd") var hex_size = 128 var hex_grid_bbox = [[0,0], [10,10]] @@ -21,14 +19,15 @@ var is_dragging = false var drag_start = null var target = Vector2() var tile_data = {} +var islands = [] # 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) + load_world("user://pirate_game_world.save") + Globals.HexGrid.hex_scale = Vector2(hex_size, hex_size) # Set player starting position - PlayerChar.position = HexGrid.get_hex_center(Vector2(0,0)) + PlayerChar.position = Globals.HexGrid.get_hex_center(Vector2(0,0)) GridDrawer.hex_scale = Vector2(hex_size, hex_size) func _draw(): @@ -39,10 +38,10 @@ func _draw(): func draw_grid(): var view_grid_corners = calc_grid_view_rect() - var tl_tile_coords = HexGrid.get_hex_center(view_grid_corners[0]) - var tr_tile_coords = HexGrid.get_hex_center(view_grid_corners[1]) - var bl_tile_coords = HexGrid.get_hex_center(view_grid_corners[2]) - var br_tile_coords = HexGrid.get_hex_center(view_grid_corners[3]) + var tl_tile_coords = Globals.HexGrid.get_hex_center(view_grid_corners[0]) + var tr_tile_coords = Globals.HexGrid.get_hex_center(view_grid_corners[1]) + var bl_tile_coords = Globals.HexGrid.get_hex_center(view_grid_corners[2]) + var br_tile_coords = Globals.HexGrid.get_hex_center(view_grid_corners[3]) 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 @@ -54,7 +53,7 @@ func draw_grid(): draw_set_transform(tl_tile_coords + Vector2(dx, r * hex_size * sqrt(3) * 0.5 + dy), 0, Vector2.ONE) draw_polyline(GridDrawer.HexPoints, "#888", 1 * WorldCamera.zoom.x) - var coords = HexGrid.get_hex_center (hex_hover) + var coords = Globals.HexGrid.get_hex_center (hex_hover) draw_set_transform(coords, 0, Vector2.ONE) draw_polyline(GridDrawer.HexPoints, "#f00", 2 * WorldCamera.zoom.x) @@ -68,7 +67,7 @@ func screen_to_world(pos: Vector2): return WorldCamera.offset + pos * WorldCamera.zoom func screen_to_hex(pos: Vector2): - return HexGrid.get_hex_at(screen_to_world(pos)).axial_coords + return Globals.HexGrid.get_hex_at(screen_to_world(pos)).axial_coords func world_to_screen(pos: Vector2): return pos * WorldCamera.zoom + WorldCamera.offset @@ -87,37 +86,14 @@ func handle_game_event(event): if event is InputEventMouseButton: # Move main character if event.pressed and event.button_index == BUTTON_LEFT: - PlayerChar.target = HexGrid.get_hex_center(screen_to_hex(event.position)) + PlayerChar.target = Globals.HexGrid.get_hex_center(screen_to_hex(event.position)) return true return false -func handle_editor_event(event): - 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: - if EditIslandButton.pressed: - if handle_editor_event (event): - return - elif handle_game_event(event): + if handle_game_event(event): return # Move camera @@ -139,9 +115,6 @@ func _unhandled_input(event): if is_dragging: WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x - elif EditIslandButton.pressed: - if handle_editor_event (event): - return update() @@ -149,20 +122,16 @@ func _unhandled_input(event): ZoomValueLabel.text = str(WorldCamera.zoom) -func _on_SaveWorldButton_pressed(): +func save_world(path: String): var world_save_data = File.new() - world_save_data.open("user://pirate_game_world.save", File.WRITE) + world_save_data.open(path, 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(): +func load_world(path: String): var world_save_data = File.new() - world_save_data.open("user://pirate_game_world.save", File.READ) + world_save_data.open(path, File.READ) var data = parse_json(world_save_data.get_line()) world_save_data.close() @@ -184,5 +153,5 @@ func _on_LoadWorldButton_pressed(): 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() diff --git a/thirdparty/gdhexgrid/addons/gut/icon.png.import b/thirdparty/gdhexgrid/addons/gut/icon.png.import index 848473e..77ca840 100644 --- a/thirdparty/gdhexgrid/addons/gut/icon.png.import +++ b/thirdparty/gdhexgrid/addons/gut/icon.png.import @@ -2,15 +2,15 @@ importer="texture" type="StreamTexture" -path="res://.import/icon.png-91b084043b8aaf2f1c906e7b9fa92969.stex" +path="res://.import/icon.png-8ef70a3ee536a318ef1cf6a9c072251e.stex" metadata={ "vram_texture": false } [deps] -source_file="res://addons/gut/icon.png" -dest_files=[ "res://.import/icon.png-91b084043b8aaf2f1c906e7b9fa92969.stex" ] +source_file="res://thirdparty/gdhexgrid/addons/gut/icon.png" +dest_files=[ "res://.import/icon.png-8ef70a3ee536a318ef1cf6a9c072251e.stex" ] [params] diff --git a/thirdparty/gdhexgrid/icon.png.import b/thirdparty/gdhexgrid/icon.png.import index 96cbf46..5f1cd4f 100644 --- a/thirdparty/gdhexgrid/icon.png.import +++ b/thirdparty/gdhexgrid/icon.png.import @@ -2,15 +2,15 @@ importer="texture" type="StreamTexture" -path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" +path="res://.import/icon.png-55a505cd80fd03b4057438c68aa35b70.stex" metadata={ "vram_texture": false } [deps] -source_file="res://icon.png" -dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] +source_file="res://thirdparty/gdhexgrid/icon.png" +dest_files=[ "res://.import/icon.png-55a505cd80fd03b4057438c68aa35b70.stex" ] [params]