Tweaking island generation

WorldChunkRefactoring
Martin Felis 2022-09-15 14:15:23 +02:00
parent e9b282f873
commit 69933d58ae
6 changed files with 170 additions and 19 deletions

View File

@ -212,7 +212,12 @@ func get_hex_center_from_offset(offset):
var hex = HexCell.new()
hex.offset_coords = offset
return hex_transform * hex.axial_coords
func get_hex_from_offset(offset):
var hex = HexCell.new()
hex.offset_coords = offset
return hex
func get_hex_at(coords):
# Returns a HexCell at the given Vector2/3 on the projection plane
# If the given value is a Vector3, its x,z coords will be used

View File

@ -9,22 +9,27 @@
config_version=4
_global_script_classes=[ {
"base": "Reference",
"base": "Node",
"class": "ClickableComponent",
"language": "GDScript",
"path": "res://components/ClickableComponent.gd"
}, {
"base": "Reference",
"base": "KinematicBody2D",
"class": "CollisionLine",
"language": "GDScript",
"path": "res://utils/CollisionLine.gd"
}, {
"base": "Reference",
"base": "Node",
"class": "ColorComponent",
"language": "GDScript",
"path": "res://components/ColorComponent.gd"
}, {
"base": "Spatial",
"class": "HexTile3D",
"language": "GDScript",
"path": "res://scenes/HexTile3D.gd"
}, {
"base": "Spatial",
"class": "Island",
"language": "GDScript",
"path": "res://scenes/Island.gd"
@ -34,7 +39,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://utils/SpringDamper.gd"
}, {
"base": "Reference",
"base": "Sprite",
"class": "TintedSpriteComponent",
"language": "GDScript",
"path": "res://components/TintedSpriteComponent.gd"
@ -43,6 +48,7 @@ _global_script_class_icons={
"ClickableComponent": "",
"CollisionLine": "",
"ColorComponent": "",
"HexTile3D": "",
"Island": "",
"SpringDamper": "",
"TintedSpriteComponent": ""

View File

@ -11,7 +11,7 @@ onready var player = $Player
onready var world = $World
var player_velocity = Vector3.ZERO
var player_speed = 1
var player_speed = 5
var target_coordinate = Vector3.ZERO
func _ready():

View File

@ -179,7 +179,6 @@ __meta__ = {
}
[node name="World" type="Spatial" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )
[node name="Island" parent="World" instance=ExtResource( 4 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )

View File

@ -1,12 +1,19 @@
extends Spatial
class_name HexTile3D
onready var is_mouse_over = false
onready var mesh = $Mesh
var old_material_override = Material.new()
export var hex = null setget set_hex, get_hex
signal tile_selected
signal tile_mouse_entered
func set_hex(_hex):
hex = _hex
func get_hex():
return hex
func _on_Area_mouse_entered():
is_mouse_over = true

View File

@ -8,24 +8,158 @@ export var level_size: int = 10
signal island_tile_selected
signal island_tile_hover
func generate():
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var tiles_by_offset = {}
func reset():
var tiles = $Tiles
for node in tiles.get_children():
tiles.remove_child(node)
node.queue_free()
for i in range(-level_size / 2, level_size / 2):
for j in range (-level_size / 2, level_size / 2):
var pos = hexgrid.get_hex_center_from_offset(Vector2(i, j))
var tile = HexTile3D.instance()
var height = (sin(pos.y * 0.3) * sin(pos.y * 0.8) * 0.8 + cos ((pos.x) * 0.9) * 1.24) * 0.5 + 0.4 + rand_range(-0.3, 0.3)
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.connect("tile_selected", self, "on_tile_selected")
tile.connect("tile_mouse_entered", self, "on_tile_hover")
tiles.add_child(tile)
tiles_by_offset = {}
func create_tile_for_hex(hex):
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var tile = HexTile3D.instance()
var pos = hexgrid.get_hex_center_from_offset(hex.offset_coords)
var hex_pos = hexgrid.get_hex_center_from_offset(hex.offset_coords)
var height = 0.0
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.connect("tile_selected", self, "on_tile_selected")
tile.connect("tile_mouse_entered", self, "on_tile_hover")
return tile
func generate_random_walk (length):
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var tiles = $Tiles
var cur_hex_coord = Vector2(0,0)
var i = length
while (i >= 0):
var current_hex = hexgrid.get_hex_from_offset(cur_hex_coord)
var tile = create_tile_for_hex(current_hex)
tiles.add_child(tile)
tiles_by_offset[cur_hex_coord] = tile
# var pos = hexgrid.get_hex_center_from_offset(cur_hex_coord)
# var tile = HexTile3D.instance()
# var height = 0.0
# tile.transform.origin = Vector3(pos.x, height, pos.y)
# tile.connect("tile_selected", self, "on_tile_selected")
# tile.connect("tile_mouse_entered", self, "on_tile_hover")
# tiles.add_child(tile)
#
# var current_hex_from_axial = hexgrid.get_hex_at(current_hex.axial_coords)
#
var neighbours = current_hex.get_all_adjacent()
cur_hex_coord = neighbours[randi() %6].offset_coords
# for neighbour in neighbours:
# tile = create_tile_for_hex(neighbour)
# tiles.add_child(tile)
# var rand_idx = randi() % 6
# cur_hex_coord = neighbours[rand_idx].offset_coords
i = i - 1
func extrude_island():
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var tiles = $Tiles
var old_tiles = tiles
tiles = {}
var old_tiles_by_offset = tiles_by_offset
# for offset_coord in tiles_by_offset.keys():
# var hex = hexgrid.get_hex_from_offset(offset_coord)
# var neighbours = current_hex.get_all_adjacent()
# cur_hex_coord = neighbours[randi() %6].offset_coords
#
#
#
# for tile in old_tiles:
# var hex_coord = Globals.WorldToHex(tile)
# tiles[tile] = "Sand"
# var neighbours = Globals.HexGrid.get_hex_at(hex_coord).get_all_adjacent()
# for cell in neighbours:
# var world_coord = Globals.HexToWorld(hex_coord + cell.axial_coords)
# tiles[world_coord] = "Sand"
#
#
#func mark_grass():
# var old_tiles = tiles
# tiles = {}
#
# for tile in old_tiles:
# var hex_coord = Globals.WorldToHex(tile)
#
# var neighbours = Globals.HexGrid.get_hex_at(hex_coord).get_all_adjacent()
# var is_center_cell = true
# for cell in neighbours:
# var world_coord = Globals.HexToWorld(hex_coord + cell.axial_coords)
# if not world_coord in old_tiles.keys():
# is_center_cell = false
#
# if is_center_cell:
# tiles[tile] = "Grass"
# else:
# tiles[tile] = "Sand"
#
#
#
#func mark_deep_grass():
# var old_tiles = tiles
# tiles = {}
#
# for tile in old_tiles:
# if old_tiles[tile] != "Grass":
# tiles[tile] = old_tiles[tile]
# continue
#
# var hex_coord = Globals.WorldToHex(tile)
#
# var neighbours = Globals.HexGrid.get_hex_at(hex_coord).get_all_adjacent()
# var is_center_cell = true
# for cell in neighbours:
# var world_coord = Globals.HexToWorld(hex_coord + cell.axial_coords)
# if world_coord in old_tiles.keys() and old_tiles[world_coord] != "Grass":
# is_center_cell = false
# break
#
# if is_center_cell:
# tiles[tile] = "DeepGrass"
# else:
# tiles[tile] = "Grass"
func generate():
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var tiles = $Tiles
reset()
generate_random_walk(level_size)
# extrude_island()
# mark_grass()
# mark_deep_grass()
#
# for i in range(-level_size / 2, level_size / 2):
# for j in range (-level_size / 2, level_size / 2):
# var pos = hexgrid.get_hex_center_from_offset(Vector2(i, j))
# var tile = HexTile3D.instance()
# var height = (sin(pos.y * 0.3) * sin(pos.y * 0.8) * 0.8 + cos ((pos.x) * 0.9) * 1.24) * 0.5 + 0.4 + rand_range(-0.3, 0.3)
# tile.transform.origin = Vector3(pos.x, height, pos.y)
# tile.connect("tile_selected", self, "on_tile_selected")
# tile.connect("tile_mouse_entered", self, "on_tile_hover")
# tiles.add_child(tile)
print ("Generated island with " + str(tiles.get_child_count()) + " nodes")