Island placement okayish, added boat

master
Martin Felis 2021-07-09 14:17:25 +02:00
parent db97e9d66d
commit 2652bbbfa8
6 changed files with 239 additions and 49 deletions

View File

@ -8,6 +8,7 @@ const SHOVEL_DURATION=1
var DebugLabel = null var DebugLabel = null
var HexGrid = null var HexGrid = null
var OceanGrid = null
var hex_size = 128 var hex_size = 128
@ -15,8 +16,10 @@ var hex_size = 128
func _ready(): func _ready():
HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new() HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new()
HexGrid.hex_scale = Vector2(hex_size, hex_size) 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) 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 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): func WorldToScreen(pos: Vector2, camera: Camera2D):
return pos * camera.zoom + camera.offset return pos * camera.zoom + camera.offset

93
assets/boat.svg Normal file
View File

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="67.73333mm"
height="67.73333mm"
viewBox="0 0 67.73333 67.73333"
version="1.1"
id="svg1420"
inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)"
sodipodi:docname="boat.svg">
<defs
id="defs1414" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.98994949"
inkscape:cx="118.12073"
inkscape:cy="-21.57981"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="1916"
inkscape:window-height="1041"
inkscape:window-x="0"
inkscape:window-y="37"
inkscape:window-maximized="0"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
showguides="false" />
<metadata
id="metadata1417">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-22.735567,-21.843752)">
<rect
style="fill:none;fill-opacity:1;stroke:none;stroke-width:1.16499996;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect2086"
width="67.73333"
height="67.73333"
x="22.735567"
y="21.843752" />
<path
style="opacity:1;fill:#aa4400;fill-opacity:1;stroke:#2b1100;stroke-width:0.52916664;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 23.071131,55.664689 14.890256,-0.395664 6.023877,11.301298 30.159844,-0.25963 3.997071,-4.343995 11.792181,-0.808583 -6.566376,19.510488 -13.026317,8.152523 -32.203568,0.09449 -10.826748,-3.990267 z"
id="rect904"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccccccccccc" />
<path
style="opacity:1;fill:#2b1100;fill-opacity:1;stroke:#2b1100;stroke-width:0.52916664;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 25.288535,67.942695 61.907369,0.801808 -1.80407,5.865346 -59.201264,-0.968852 z"
id="rect907"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccccc" />
<path
style="opacity:1;fill:#2b1100;fill-opacity:1;stroke:#2b1100;stroke-width:0.52916664;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 26.509194,78.481637 56.85879,2.186966 -5.580848,3.288939 -50.612142,-0.202825 z"
id="rect907-4"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccccc" />
<path
style="opacity:1;fill:#2b1100;fill-opacity:1;stroke:#2b1100;stroke-width:0.52916664;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 23.40113,58.180225 40.23631,59.699017 42.139007,63.65613 24.06693,63.453305 Z"
id="rect907-4-4"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccccc" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.6 KiB

34
assets/boat.svg.import Normal file
View File

@ -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

View File

@ -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/World.gd" type="Script" id=1]
[ext_resource path="res://scenes/Grid.gd" type="Script" id=2] [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://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/Editor.gd" type="Script" id=7]
[ext_resource path="res://scenes/GridHighlight.gd" type="Script" id=8] [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] [sub_resource type="DynamicFont" id=1]
size = 27 size = 27
@ -24,9 +25,6 @@ resource_name = "TileTypeGroup"
[node name="World" type="Node2D" parent="."] [node name="World" type="Node2D" parent="."]
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Camera" type="Camera2D" parent="World"]
current = true
[node name="Grid" type="Node2D" parent="World"] [node name="Grid" type="Node2D" parent="World"]
script = ExtResource( 2 ) script = ExtResource( 2 )
@ -143,6 +141,15 @@ texture = ExtResource( 3 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="World/PlayerChar"] [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="."] [node name="Editor" type="Node2D" parent="."]
script = ExtResource( 7 ) script = ExtResource( 7 )

View File

@ -1,11 +1,11 @@
extends Node2D extends Node2D
var tiles = {} var tiles = {}
var offset = Vector2.ZERO var offset_world = Vector2.ZERO
var center_coord = Vector2.ZERO var center_coord = Vector2.ZERO
var center_world_coord = Vector2.ZERO var center_world_coord = Vector2.ZERO
var tile_local_coords = [] var tile_local_coords = []
var rect_world = Rect2() var rect_local = Rect2()
var radius_world = 0.0 var radius_world = 0.0
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
@ -23,8 +23,8 @@ func set_tile(coord: Vector2, value: String):
update() update()
func set_offset(value: Vector2): func set_offset_world(value: Vector2):
offset = value offset_world = value
func clear_island(): func clear_island():
@ -50,10 +50,12 @@ func calc_bbox():
if local_coords.y > coord_max.y: if local_coords.y > coord_max.y:
coord_max.y = local_coords.y coord_max.y = local_coords.y
var hex_half_size = Vector2.ONE * Globals.hex_size * 0.5 var hex_half_size = Vector2 (Globals.hex_size * 0.5, Globals.hex_size * sqrt(3) * 0.25)
rect_world = Rect2(coord_min - hex_half_size, coord_max - coord_min + hex_half_size * 2) 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))) 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): func save_island(path: String):
@ -100,28 +102,35 @@ func load_island(path: String):
func check_overlap(other): func check_overlap(other):
var tile_offset = Globals.HexToWorld(offset) var rect_world = calc_rect_world()
var other_offset = Globals.HexToWorld(other.offset) var rect_world_other = other.calc_rect_world()
var rel_offset = other_offset - tile_offset
var dist = rel_offset.length() - radius_world - other.radius_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(): func _draw():
var tile_offset = Globals.HexToWorld(offset) print (name, offset_world)
print (name, tile_offset)
for coord in tiles.keys(): 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])) draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.get_tile_color(tiles[coord]))
var transform = get_transform() 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_rect(rect_local, Color.red, false)
draw_circle(center_world_coord, radius_world, Color.brown) # draw_bsphere()
draw_circle(center_world_coord, 10, Color.red)
var default_font = Control.new().get_font("font") 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))

View File

@ -12,6 +12,7 @@ onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue")
onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue") onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue")
onready var WorldCoordValueLabel = get_node("UI/TopContainer/WorldCoordValue") onready var WorldCoordValueLabel = get_node("UI/TopContainer/WorldCoordValue")
onready var PlayerChar = get_node("PlayerChar") onready var PlayerChar = get_node("PlayerChar")
onready var PlayerBoat = get_node("PlayerBoat")
onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue") onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue")
var Island = preload("Island.gd") var Island = preload("Island.gd")
@ -23,7 +24,6 @@ var is_dragging = false
var drag_start = null var drag_start = null
var target = Vector2() var target = Vector2()
var tile_data = {} var tile_data = {}
var islands = []
var current_island = null var current_island = null
@ -37,40 +37,77 @@ 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):
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(): func generate():
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
rng.randomize() rng.randomize()
islands.clear() clear_islands()
for island in Islands.get_children():
Islands.remove_child(island) var radius = 1300
island.queue_free() var num_islands = 10
var radius = 5
var num_islands = 3
for i in range (num_islands): for i in range (num_islands):
var overlapping = true
var find_free_retries = 10
var island = Island.new() 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) 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(): var rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius))
overlapping = world_island.check_overlap(island) 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: if overlapping:
print ("Could not find free spot for island!") print ("Could not place island!")
else: else:
print ("Retries left: " + str(find_free_retries) ) print ("Placed after " + str(overlap_retry_num) + " retries.")
Islands.add_child(island) Islands.add_child(island)
populate_ocean_grid()
func handle_game_event(event): func handle_game_event(event):
@ -105,6 +142,9 @@ func _unhandled_input(event):
hex_hover = Globals.ScreenToHex(event.position, WorldCamera) hex_hover = Globals.ScreenToHex(event.position, WorldCamera)
GridHighlight.pos = hex_hover GridHighlight.pos = hex_hover
HexCoordValueLabel.text = str(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)) WorldCoordValueLabel.text = str(Globals.ScreenToWorld(event.position, WorldCamera))