From 2652bbbfa8d1b1e2f2c9408442328a49e82a99ad Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 9 Jul 2021 14:17:25 +0200 Subject: [PATCH] Island placement okayish, added boat --- Globals.gd | 11 ++++- assets/boat.svg | 93 ++++++++++++++++++++++++++++++++++++++++++ assets/boat.svg.import | 34 +++++++++++++++ scenes/Game.tscn | 15 +++++-- scenes/Island.gd | 51 +++++++++++++---------- scenes/World.gd | 84 ++++++++++++++++++++++++++++---------- 6 files changed, 239 insertions(+), 49 deletions(-) create mode 100644 assets/boat.svg create mode 100644 assets/boat.svg.import diff --git a/Globals.gd b/Globals.gd index 8b26432..e92eb83 100644 --- a/Globals.gd +++ b/Globals.gd @@ -8,6 +8,7 @@ const SHOVEL_DURATION=1 var DebugLabel = null var HexGrid = null +var OceanGrid = null var hex_size = 128 @@ -15,8 +16,10 @@ var hex_size = 128 func _ready(): HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new() HexGrid.hex_scale = Vector2(hex_size, hex_size) -# add_child(HexGrid) - + + OceanGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new() + OceanGrid.hex_scale = Vector2(hex_size, hex_size) + HexTileDrawer.hex_scale = Vector2(hex_size, hex_size) @@ -51,5 +54,9 @@ func WorldToHex(pos: Vector2): return Globals.HexGrid.get_hex_at(pos).axial_coords +func WorldToHexCenter(pos: Vector2): + return Globals.HexGrid.get_hex_center(Globals.HexGrid.get_hex_at(pos).axial_coords) + + func WorldToScreen(pos: Vector2, camera: Camera2D): return pos * camera.zoom + camera.offset diff --git a/assets/boat.svg b/assets/boat.svg new file mode 100644 index 0000000..b57f46a --- /dev/null +++ b/assets/boat.svg @@ -0,0 +1,93 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + diff --git a/assets/boat.svg.import b/assets/boat.svg.import new file mode 100644 index 0000000..a70e656 --- /dev/null +++ b/assets/boat.svg.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/boat.svg-5f58629bd5f6beab10c05f8be81c6a0b.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/boat.svg" +dest_files=[ "res://.import/boat.svg-5f58629bd5f6beab10c05f8be81c6a0b.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/scenes/Game.tscn b/scenes/Game.tscn index 1d8a04e..b9619fa 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=12 format=2] +[gd_scene load_steps=13 format=2] [ext_resource path="res://scenes/World.gd" type="Script" id=1] [ext_resource path="res://scenes/Grid.gd" type="Script" id=2] @@ -8,6 +8,7 @@ [ext_resource path="res://fonts/Roboto/Roboto-Regular.ttf" type="DynamicFontData" id=6] [ext_resource path="res://scenes/Editor.gd" type="Script" id=7] [ext_resource path="res://scenes/GridHighlight.gd" type="Script" id=8] +[ext_resource path="res://assets/boat.svg" type="Texture" id=9] [sub_resource type="DynamicFont" id=1] size = 27 @@ -24,9 +25,6 @@ resource_name = "TileTypeGroup" [node name="World" type="Node2D" parent="."] script = ExtResource( 1 ) -[node name="Camera" type="Camera2D" parent="World"] -current = true - [node name="Grid" type="Node2D" parent="World"] script = ExtResource( 2 ) @@ -143,6 +141,15 @@ texture = ExtResource( 3 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="World/PlayerChar"] +[node name="Camera" type="Camera2D" parent="World"] +current = true + +[node name="PlayerBoat" type="Node2D" parent="World"] + +[node name="boat" type="Sprite" parent="World/PlayerBoat"] +scale = Vector2( 0.455, 0.455 ) +texture = ExtResource( 9 ) + [node name="Editor" type="Node2D" parent="."] script = ExtResource( 7 ) diff --git a/scenes/Island.gd b/scenes/Island.gd index 6cdd379..131f1f2 100644 --- a/scenes/Island.gd +++ b/scenes/Island.gd @@ -1,11 +1,11 @@ extends Node2D var tiles = {} -var offset = Vector2.ZERO +var offset_world = Vector2.ZERO var center_coord = Vector2.ZERO var center_world_coord = Vector2.ZERO var tile_local_coords = [] -var rect_world = Rect2() +var rect_local = Rect2() var radius_world = 0.0 # Called when the node enters the scene tree for the first time. @@ -23,8 +23,8 @@ func set_tile(coord: Vector2, value: String): update() -func set_offset(value: Vector2): - offset = value +func set_offset_world(value: Vector2): + offset_world = value func clear_island(): @@ -50,10 +50,12 @@ func calc_bbox(): if local_coords.y > coord_max.y: coord_max.y = local_coords.y - var hex_half_size = Vector2.ONE * Globals.hex_size * 0.5 - rect_world = Rect2(coord_min - hex_half_size, coord_max - coord_min + hex_half_size * 2) + var hex_half_size = Vector2 (Globals.hex_size * 0.5, Globals.hex_size * sqrt(3) * 0.25) + rect_local = Rect2(coord_min - hex_half_size, coord_max - coord_min + hex_half_size * 2) center_world_coord = Globals.HexToWorld(Globals.WorldToHex(coord_min + 0.5 * (coord_max - coord_min))) - radius_world = max(rect_world.size.x, rect_world.size.y) * 0.5 + center_coord = Globals.WorldToHex(center_world_coord) + print ("center coord: " + str(center_coord)) + radius_world = max(rect_local.size.x, rect_local.size.y) * 0.5 func save_island(path: String): @@ -100,28 +102,35 @@ func load_island(path: String): func check_overlap(other): - var tile_offset = Globals.HexToWorld(offset) - var other_offset = Globals.HexToWorld(other.offset) - - var rel_offset = other_offset - tile_offset - var dist = rel_offset.length() - radius_world - other.radius_world + var rect_world = calc_rect_world() + var rect_world_other = other.calc_rect_world() - return dist < 0 + return rect_world.intersects(rect_world_other) +func calc_rect_world(): + var rect_world = Rect2(rect_local) + rect_world.position = transform.origin + rect_world.position + offset_world + return rect_world + + +func draw_bsphere(): + var transform = get_transform() + draw_set_transform(transform.origin + offset_world, transform.get_rotation(), transform.get_scale()) + draw_circle(center_world_coord, radius_world, Color("#80000033")) + draw_circle(center_world_coord, 10, Color.red) + func _draw(): - var tile_offset = Globals.HexToWorld(offset) - print (name, tile_offset) + print (name, offset_world) for coord in tiles.keys(): - draw_set_transform (coord + tile_offset, 0, Vector2.ONE) + draw_set_transform (coord + offset_world, 0, Vector2.ONE) draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.get_tile_color(tiles[coord])) var transform = get_transform() - draw_set_transform(transform.origin + tile_offset, transform.get_rotation(), transform.get_scale()) + draw_set_transform(transform.origin + offset_world, transform.get_rotation(), transform.get_scale()) - draw_rect(rect_world, Color.red, false) - draw_circle(center_world_coord, radius_world, Color.brown) - draw_circle(center_world_coord, 10, Color.red) +# draw_rect(rect_local, Color.red, false) +# draw_bsphere() var default_font = Control.new().get_font("font") - draw_string(default_font, Vector2(0, 0), name) + draw_string(default_font, Vector2(0, 0), name + str(" ") + str(offset_world)) diff --git a/scenes/World.gd b/scenes/World.gd index 42e984a..213302a 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -12,6 +12,7 @@ onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue") onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue") onready var WorldCoordValueLabel = get_node("UI/TopContainer/WorldCoordValue") onready var PlayerChar = get_node("PlayerChar") +onready var PlayerBoat = get_node("PlayerBoat") onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue") var Island = preload("Island.gd") @@ -23,7 +24,6 @@ var is_dragging = false var drag_start = null var target = Vector2() var tile_data = {} -var islands = [] var current_island = null @@ -37,40 +37,77 @@ func _ready(): 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): + WorldCamera.offset = PlayerChar.position + PlayerBoat.transform.origin = PlayerChar.transform.origin + pass + + +func check_island_overlap(new_island): + var islands = Islands.get_children() + + for island in islands: + if island.check_overlap(new_island): + return true + + return false + + +func add_island_at(file_name, offset_world: Vector2): + var island = Island.new() + island.load_island(file_name) + island.offset_world = offset_world + Islands.add_child(island) + + +func populate_ocean_grid(): + Globals.OceanGrid.remove_obstacles(Globals.OceanGrid.get_obstacles()) + + func generate(): var rng = RandomNumberGenerator.new() rng.randomize() - islands.clear() - for island in Islands.get_children(): - Islands.remove_child(island) - island.queue_free() + clear_islands() + + var radius = 1300 + var num_islands = 10 - var radius = 5 - var num_islands = 3 for i in range (num_islands): - - var overlapping = true - var find_free_retries = 10 - var island = Island.new() - var file_name = "user://pirate_game_island_" + str(i) + ".island" + var island_index = i % 4 + var file_name = "user://pirate_game_island_" + str(island_index) + ".island" island.load_island(file_name) - - while overlapping and find_free_retries > 0: - overlapping = false - island.offset = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius)) - for world_island in Islands.get_children(): - overlapping = world_island.check_overlap(island) + var rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius)) + island.offset_world = Globals.WorldToHexCenter(rand_coord) + + var overlapping = check_island_overlap(island) + var overlap_retry_num = 0 + var overlap_retry_max = 10 + while overlapping and overlap_retry_num < overlap_retry_max: + if overlap_retry_num % 4 == 0: + radius = radius + 500 + overlap_retry_num = overlap_retry_num + 1 + + rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius)) + island.offset_world = Globals.WorldToHexCenter(rand_coord) + overlapping = check_island_overlap(island) - find_free_retries = find_free_retries - 1 - if overlapping: - print ("Could not find free spot for island!") + print ("Could not place island!") else: - print ("Retries left: " + str(find_free_retries) ) + print ("Placed after " + str(overlap_retry_num) + " retries.") Islands.add_child(island) + + populate_ocean_grid() + func handle_game_event(event): @@ -105,6 +142,9 @@ func _unhandled_input(event): hex_hover = Globals.ScreenToHex(event.position, WorldCamera) GridHighlight.pos = hex_hover HexCoordValueLabel.text = str(hex_hover) + +# Islands.get_children()[1].offset_world = Globals.HexToWorld(hex_hover) +# Islands.get_children()[1].update() WorldCoordValueLabel.text = str(Globals.ScreenToWorld(event.position, WorldCamera))