From c94209ba7b01cbceae11f055a18ec3d49cda9db8 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 16 Jul 2021 10:38:57 +0200 Subject: [PATCH] drawing treasure on map --- scenes/Island.gd | 8 +++++-- scenes/World.gd | 56 +++++++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/scenes/Island.gd b/scenes/Island.gd index cd4ffdd..c6a6033 100644 --- a/scenes/Island.gd +++ b/scenes/Island.gd @@ -4,7 +4,6 @@ var tiles = {} var offset_world = Vector2.ZERO var center_coord = Vector2.ZERO var center_world_coord = Vector2.ZERO -var tile_local_coords = [] var rect_local = Rect2() var radius_world = 0.0 var is_active = false @@ -13,6 +12,8 @@ var landing_site_world = null var ship_anchor_world = null var obstacles_local_coords = {} +var treasure_local_coords = null +var highlight_treasure = false # Called when the node enters the scene tree for the first time. func _ready(): @@ -45,7 +46,6 @@ func calc_bbox(): var coords = tiles.keys() for coord in coords: var local_coords = (coord) - tile_local_coords.append(local_coords) if local_coords.x < coord_min.x: coord_min.x = local_coords.x if local_coords.y < coord_min.y: @@ -163,6 +163,10 @@ func _draw(): draw_set_transform (coord + offset_world, 0, Vector2.ONE) draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.get_tile_color(tiles[coord])) + if treasure_local_coords and highlight_treasure: + draw_set_transform (treasure_local_coords + offset_world, 0, Vector2.ONE) + draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.create_color_array("#922")) + draw_set_transform(transform.origin + offset_world, transform.get_rotation(), transform.get_scale()) if Globals.debug_nav and is_active: diff --git a/scenes/World.gd b/scenes/World.gd index d9bd312..528a668 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -153,28 +153,39 @@ func check_island_location_valid(new_island): return true -func render_treasure_map(): - if treasure_map_rendered == false: - treasure_map_rendered = true - - IslandMapRenderer.render_target_update_mode = Viewport.UPDATE_ONCE - IslandMapRenderer.render_target_clear_mode = Viewport.CLEAR_MODE_ONLY_NEXT_FRAME - var camera = IslandMapRenderer.get_node("Camera2D") - camera.current = true - camera.zoom = Vector2.ONE * 10.0 - camera.offset = treasure_island.center_world_coord - var island = Island.new() - island.tiles = treasure_island.tiles.duplicate() - island.name = "island" - - for child in IslandMapRenderer.get_children(): - if child.name == "island": - print ("removing ", child) - IslandMapRenderer.remove_child(child) - child.queue_free() - break +func place_treasure(): + assert (treasure_island != null) + var island_tile_coords = treasure_island.tiles.keys() + var grass_tiles = [] + for coord in island_tile_coords: + if treasure_island.tiles[coord] == "Grass": + grass_tiles.append(coord) + + treasure_island.treasure_local_coords = grass_tiles[randi() % len(grass_tiles)] - IslandMapRenderer.add_child(island) + treasure_island.update() + +func render_treasure_map(): + IslandMapRenderer.render_target_update_mode = Viewport.UPDATE_ONCE + IslandMapRenderer.render_target_clear_mode = Viewport.CLEAR_MODE_ONLY_NEXT_FRAME + var camera = IslandMapRenderer.get_node("Camera2D") + camera.current = true + camera.zoom = Vector2.ONE * 10.0 + camera.offset = treasure_island.center_world_coord + var island = Island.new() + island.tiles = treasure_island.tiles.duplicate() + island.treasure_local_coords = treasure_island.treasure_local_coords + island.highlight_treasure = true + island.name = "island" + + for child in IslandMapRenderer.get_children(): + if child.name == "island": + print ("removing ", child) + IslandMapRenderer.remove_child(child) + child.queue_free() + break + + IslandMapRenderer.add_child(island) IslandMap.texture = IslandMapRenderer.get_texture() @@ -185,7 +196,6 @@ func generate(): PlayerChar.target = Vector2.ZERO treasure_island = null - treasure_map_rendered = false var rng = RandomNumberGenerator.new() rng.randomize() @@ -226,6 +236,8 @@ func generate(): num_islands = Islands.get_child_count() treasure_island = Islands.get_child(randi() % num_islands) + place_treasure() + render_treasure_map() populate_ocean_nav_grid()