Random progress

WorldChunkRefactoring
Martin Felis 2022-09-21 14:09:31 +02:00
parent 69933d58ae
commit c3bca6b208
18 changed files with 204 additions and 224 deletions

View File

@ -61,6 +61,7 @@
""" """
extends Resource extends Resource
class_name HexCell
#warning-ignore-all:unused_class_variable #warning-ignore-all:unused_class_variable
# We use unit-size flat-topped hexes # We use unit-size flat-topped hexes

View File

@ -2,9 +2,9 @@ extends Node2D
var SpringDamper = preload("res://utils/SpringDamper.gd") var SpringDamper = preload("res://utils/SpringDamper.gd")
export var target = Vector2 (0, 0) export var target = Vector3 (0, 0, 0)
export var pos = Vector2 (0, 0) export var pos = Vector3 (0, 0, 0)
export var vel = Vector2 (0, 0) export var vel = Vector3 (0, 0, 0)
export var max_speed = 100000 export var max_speed = 100000
onready var spring_damper = SpringDamper.new(4, .99, 0.5) onready var spring_damper = SpringDamper.new(4, .99, 0.5)
@ -35,4 +35,5 @@ func _process(delta):
func _draw(): func _draw():
draw_circle (target, 10, Color(0.9, 0.2, 0.2)) #draw_circle (target, 10, Color(0.9, 0.2, 0.2))
pass

View File

@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://components/MovableComponent.gd" type="Script" id=1]
[node name="Movable" type="Node2D"]
script = ExtResource( 1 )
max_speed = 300

View File

@ -1,4 +1,4 @@
extends KinematicBody2D extends KinematicBody
onready var color_component = $Color onready var color_component = $Color
onready var tinted_sprite_component = $TintedSprite onready var tinted_sprite_component = $TintedSprite
@ -54,10 +54,15 @@ func _on_entity_clicked():
func _on_position_updated(new_position): func _on_position_updated(new_position):
if not collision_component: if not collision_component:
transform.origin = new_position transform.origin = new_position
update()
func _unhandled_input(event): func set_movable_target(target):
if not movable_component == null:
movable_component.target = target
func _unhandled_input_disabled(event):
print ("Player unhandled input...")
if event is InputEventMouseButton and event.pressed: if event is InputEventMouseButton and event.pressed:
assert(movable_component) assert(movable_component)

View File

@ -4,7 +4,7 @@
[ext_resource path="res://components/TintedSpriteComponent.gd" type="Script" id=2] [ext_resource path="res://components/TintedSpriteComponent.gd" type="Script" id=2]
[ext_resource path="res://components/ColorComponent.gd" type="Script" id=3] [ext_resource path="res://components/ColorComponent.gd" type="Script" id=3]
[ext_resource path="res://components/ClickableComponent.tscn" type="PackedScene" id=4] [ext_resource path="res://components/ClickableComponent.tscn" type="PackedScene" id=4]
[ext_resource path="res://components/MovableComponent.gd" type="Script" id=5] [ext_resource path="res://components/MovableComponent.tscn" type="PackedScene" id=5]
[ext_resource path="res://assets/pirate.svg" type="Texture" id=6] [ext_resource path="res://assets/pirate.svg" type="Texture" id=6]
[ext_resource path="res://assets/white.png" type="Texture" id=8] [ext_resource path="res://assets/white.png" type="Texture" id=8]
@ -18,11 +18,7 @@ height = 122.0
[node name="PlayerEntity" type="KinematicBody2D"] [node name="PlayerEntity" type="KinematicBody2D"]
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Components" type="Node2D" parent="."] [node name="Movable" parent="." instance=ExtResource( 5 )]
[node name="Movable" type="Node2D" parent="."]
script = ExtResource( 5 )
max_speed = 300
[node name="TintedSprite" type="Sprite" parent="."] [node name="TintedSprite" type="Sprite" parent="."]
position = Vector2( -9, 3 ) position = Vector2( -9, 3 )

View File

@ -0,0 +1,4 @@
[gd_resource type="SpatialMaterial" format=2]
[resource]
albedo_color = Color( 0.0627451, 0.235294, 0.0117647, 1 )

4
materials/GrassTile.tres Normal file
View File

@ -0,0 +1,4 @@
[gd_resource type="SpatialMaterial" format=2]
[resource]
albedo_color = Color( 0.160784, 0.384314, 0.0901961, 1 )

4
materials/SandTile.tres Normal file
View File

@ -0,0 +1,4 @@
[gd_resource type="SpatialMaterial" format=2]
[resource]
albedo_color = Color( 0.937255, 0.756863, 0.282353, 1 )

View File

@ -24,6 +24,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://components/ColorComponent.gd" "path": "res://components/ColorComponent.gd"
}, { }, {
"base": "Resource",
"class": "HexCell",
"language": "GDScript",
"path": "res://addons/gdhexgrid/HexCell.gd"
}, {
"base": "Spatial", "base": "Spatial",
"class": "HexTile3D", "class": "HexTile3D",
"language": "GDScript", "language": "GDScript",
@ -34,6 +39,16 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://scenes/Island.gd" "path": "res://scenes/Island.gd"
}, { }, {
"base": "Node",
"class": "IslandGenerator",
"language": "GDScript",
"path": "res://utils/IslandGenerator.gd"
}, {
"base": "Resource",
"class": "IslandTile",
"language": "GDScript",
"path": "res://utils/IslandTile.gd"
}, {
"base": "Object", "base": "Object",
"class": "SpringDamper", "class": "SpringDamper",
"language": "GDScript", "language": "GDScript",
@ -48,8 +63,11 @@ _global_script_class_icons={
"ClickableComponent": "", "ClickableComponent": "",
"CollisionLine": "", "CollisionLine": "",
"ColorComponent": "", "ColorComponent": "",
"HexCell": "",
"HexTile3D": "", "HexTile3D": "",
"Island": "", "Island": "",
"IslandGenerator": "",
"IslandTile": "",
"SpringDamper": "", "SpringDamper": "",
"TintedSpriteComponent": "" "TintedSpriteComponent": ""
} }

View File

@ -14,6 +14,7 @@ var player_velocity = Vector3.ZERO
var player_speed = 5 var player_speed = 5
var target_coordinate = Vector3.ZERO var target_coordinate = Vector3.ZERO
func _ready(): func _ready():
for node in world.get_children(): for node in world.get_children():
if node is Island: if node is Island:
@ -27,20 +28,9 @@ func _process(delta):
player_pos_label.text = "Pos: " + str(player.transform.origin) player_pos_label.text = "Pos: " + str(player.transform.origin)
func _physics_process(delta):
var position_error = target_coordinate - player.transform.origin
var error_length = position_error.length()
if error_length > 0.05:
player_velocity = position_error / error_length * player_speed
else:
player_velocity = Vector3.ZERO
player.transform.origin += player_velocity * delta
func on_island_tile_selected(island, tile): func on_island_tile_selected(island, tile):
target_coordinate = tile.global_transform.origin target_coordinate = tile.global_transform.origin
player.set_movable_target(tile.global_transform.origin)
var position_error = target_coordinate - player.transform.origin var position_error = target_coordinate - player.transform.origin
print ("clicked on tile " + str(target_coordinate) + " error: " + str(position_error)) print ("clicked on tile " + str(target_coordinate) + " error: " + str(position_error))

View File

@ -1,17 +1,20 @@
[gd_scene load_steps=11 format=2] [gd_scene load_steps=12 format=2]
[ext_resource path="res://scenes/HexTile3D.tscn" type="PackedScene" id=1] [ext_resource path="res://entities/PlayerEntity.gd" type="Script" id=1]
[ext_resource path="res://scenes/HexGrid3DTest.gd" type="Script" id=2] [ext_resource path="res://scenes/HexGrid3DTest.gd" type="Script" id=2]
[ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=3] [ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=3]
[ext_resource path="res://scenes/Island.tscn" type="PackedScene" id=4] [ext_resource path="res://scenes/Island.tscn" type="PackedScene" id=4]
[ext_resource path="res://assets/water_diffuse.png" type="Texture" id=5] [ext_resource path="res://assets/water_diffuse.png" type="Texture" id=5]
[ext_resource path="res://components/MovableComponent.tscn" type="PackedScene" id=6]
[sub_resource type="CapsuleMesh" id=4] [sub_resource type="CapsuleMesh" id=4]
radius = 0.2 radius = 0.2
mid_height = 0.5 mid_height = 0.5
radial_segments = 16 radial_segments = 16
[sub_resource type="NoiseTexture" id=5] [sub_resource type="CapsuleShape" id=5]
radius = 0.25
height = 0.5
[sub_resource type="PlaneMesh" id=1] [sub_resource type="PlaneMesh" id=1]
subdivide_width = 24 subdivide_width = 24
@ -94,10 +97,10 @@ void fragment() {
[sub_resource type="ShaderMaterial" id=3] [sub_resource type="ShaderMaterial" id=3]
shader = SubResource( 2 ) shader = SubResource( 2 )
shader_param/amplitude = Vector2( 0.01, 0.011 ) shader_param/amplitude = Vector2( 0.005, 0.005 )
shader_param/frequency = Vector2( 50, 48 ) shader_param/frequency = Vector2( 50, 48 )
shader_param/time_factor = Vector2( 7, 8 ) shader_param/time_factor = Vector2( 7, 8 )
shader_param/beer_factor = 0.5 shader_param/beer_factor = 4.0
shader_param/refraction = 0.01 shader_param/refraction = 0.01
shader_param/uv_offset_scale = Vector2( 0.2, 0.2 ) shader_param/uv_offset_scale = Vector2( 0.2, 0.2 )
shader_param/uv_offset_time_scale = 0.1 shader_param/uv_offset_time_scale = 0.1
@ -108,9 +111,6 @@ shader_param/texturemap = ExtResource( 5 )
[node name="HexGrid3DTest" type="Spatial"] [node name="HexGrid3DTest" type="Spatial"]
script = ExtResource( 2 ) script = ExtResource( 2 )
[node name="HexTile3D" parent="." instance=ExtResource( 1 )]
visible = false
[node name="DirectionalLight" type="DirectionalLight" parent="."] [node name="DirectionalLight" type="DirectionalLight" parent="."]
transform = Transform( 0.83729, 0.174005, 0.518332, -0.54676, 0.266466, 0.793757, 0, -0.948008, 0.318248, 0, 8.03448, 0 ) transform = Transform( 0.83729, 0.174005, 0.518332, -0.54676, 0.266466, 0.793757, 0, -0.948008, 0.318248, 0, 8.03448, 0 )
shadow_enabled = true shadow_enabled = true
@ -156,7 +156,8 @@ __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="Player" type="Spatial" parent="."] [node name="Player" type="KinematicBody" parent="."]
script = ExtResource( 1 )
[node name="Camera" type="Camera" parent="Player"] [node name="Camera" type="Camera" parent="Player"]
transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, -4.76837e-07, 6.70866, 3.34822 ) transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, -4.76837e-07, 6.70866, 3.34822 )
@ -169,30 +170,40 @@ transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5,
mesh = SubResource( 4 ) mesh = SubResource( 4 )
material/0 = null material/0 = null
[node name="TextureRect" type="TextureRect" parent="."] [node name="CollisionShape" type="CollisionShape" parent="Player"]
margin_right = 40.0 transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 )
margin_bottom = 40.0 shape = SubResource( 5 )
texture = SubResource( 5 )
expand = true [node name="Movable" parent="Player" instance=ExtResource( 6 )]
__meta__ = { max_speed = 3
"_edit_use_anchors_": false
}
[node name="World" type="Spatial" parent="."] [node name="World" type="Spatial" parent="."]
[node name="Scene" type="Spatial" parent="World"]
[node name="Navigation" type="Spatial" parent="World"]
[node name="Island" parent="World" instance=ExtResource( 4 )] [node name="Island" parent="World" instance=ExtResource( 4 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )
[node name="Island2" parent="World" instance=ExtResource( 4 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -12.2668, 0, 0 )
[node name="Water" type="Spatial" parent="."] [node name="Water" type="Spatial" parent="."]
[node name="Water" type="MeshInstance" parent="Water"] [node name="Water" type="MeshInstance" parent="Water"]
transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0 ) transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, -0.1, 0 )
mesh = SubResource( 1 ) mesh = SubResource( 1 )
skeleton = NodePath("../..") skeleton = NodePath("../..")
material/0 = SubResource( 3 ) material/0 = SubResource( 3 )
[node name="Water2" type="MeshInstance" parent="Water"] [node name="Water2" type="MeshInstance" parent="Water"]
transform = Transform( -10, 0, 0, 0, 10, 0, 0, 0, 10, -20, 0, -0.01 ) transform = Transform( -10, 0, 0, 0, 10, 0, 0, 0, 10, -20, -0.1, -0.01 )
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 3 )
[node name="Water3" type="MeshInstance" parent="Water"]
transform = Transform( -10, 0, 0, 0, 10, 0, 0, 0, 10, 20, -0.1, -0.01 )
mesh = SubResource( 1 ) mesh = SubResource( 1 )
skeleton = NodePath("../..") skeleton = NodePath("../..")
material/0 = SubResource( 3 ) material/0 = SubResource( 3 )

View File

@ -4,16 +4,24 @@ class_name HexTile3D
onready var is_mouse_over = false onready var is_mouse_over = false
onready var mesh = $Mesh onready var mesh = $Mesh
var old_material_override = Material.new() var old_material_override = Material.new()
export var hex = null setget set_hex, get_hex
signal tile_selected signal tile_selected
signal tile_mouse_entered signal tile_mouse_entered
func set_hex(_hex): #var cube_coords = Vector3(0, 0, 0) setget set_cube_coords, get_cube_coords
hex = _hex
func set_tiletype(type):
var IslandTile = load("res://utils/IslandTile.gd").new()
var mesh = $Mesh
if type == IslandTile.TileType.Sand:
mesh.set_surface_material(0, preload("res://materials/SandTile.tres"))
elif type == IslandTile.TileType.Grass:
mesh.set_surface_material(0, preload("res://materials/GrassTile.tres"))
else:
mesh.set_surface_material(0, preload("res://materials/DeepGrassTile.tres"))
func get_hex():
return hex
func _on_Area_mouse_entered(): func _on_Area_mouse_entered():
is_mouse_over = true is_mouse_over = true

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=5 format=2] [gd_scene load_steps=5 format=2]
[ext_resource path="res://scenes/HexTile3D.gd" type="Script" id=1] [ext_resource path="res://scenes/HexTile3D.gd" type="Script" id=1]
[ext_resource path="res://materials/DeepGrassTile.tres" type="Material" id=2]
[sub_resource type="CylinderMesh" id=6] [sub_resource type="CylinderMesh" id=6]
top_radius = 0.5 top_radius = 0.5
@ -9,9 +10,6 @@ height = 1.0
radial_segments = 6 radial_segments = 6
rings = 1 rings = 1
[sub_resource type="SpatialMaterial" id=7]
albedo_color = Color( 0.160784, 0.384314, 0.0901961, 1 )
[sub_resource type="CylinderShape" id=5] [sub_resource type="CylinderShape" id=5]
radius = 0.5 radius = 0.5
height = 1.0 height = 1.0
@ -23,7 +21,7 @@ script = ExtResource( 1 )
[node name="Mesh" type="MeshInstance" parent="."] [node name="Mesh" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0 )
mesh = SubResource( 6 ) mesh = SubResource( 6 )
material/0 = SubResource( 7 ) material/0 = ExtResource( 2 )
[node name="Area" type="Area" parent="Mesh"] [node name="Area" type="Area" parent="Mesh"]

View File

@ -2,13 +2,14 @@ tool
extends Spatial extends Spatial
class_name Island class_name Island
onready var IslandTile = preload("res://utils/IslandTile.gd")
export var generate: bool = false setget do_generate export var generate: bool = false setget do_generate
export var level_size: int = 10 export var level_size: int = 10
signal island_tile_selected signal island_tile_selected
signal island_tile_hover signal island_tile_hover
var tiles_by_offset = {}
func reset(): func reset():
var tiles = $Tiles var tiles = $Tiles
@ -16,8 +17,6 @@ func reset():
for node in tiles.get_children(): for node in tiles.get_children():
tiles.remove_child(node) tiles.remove_child(node)
node.queue_free() node.queue_free()
tiles_by_offset = {}
func create_tile_for_hex(hex): func create_tile_for_hex(hex):
@ -26,140 +25,28 @@ func create_tile_for_hex(hex):
var tile = HexTile3D.instance() var tile = HexTile3D.instance()
var pos = hexgrid.get_hex_center_from_offset(hex.offset_coords) 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 if hex.type == IslandTile.TileType.Sand else 0.1 + rand_range(0.0, 0.05)
var height = 0.0
tile.set_tiletype(hex.type)
tile.transform.origin = Vector3(pos.x, height, pos.y) tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.connect("tile_selected", self, "on_tile_selected") tile.connect("tile_selected", self, "on_tile_selected")
tile.connect("tile_mouse_entered", self, "on_tile_hover") tile.connect("tile_mouse_entered", self, "on_tile_hover")
return tile 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(): func generate():
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() var IslandGenerator = preload("res://utils/IslandGenerator.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var tiles = $Tiles var tiles = $Tiles
reset() reset()
generate_random_walk(level_size) var generated_tiles = IslandGenerator.random_walk(level_size)
# extrude_island() generated_tiles = IslandGenerator.extrude_tiles(generated_tiles)
# mark_grass() generated_tiles = IslandGenerator.mark_sand_tiles(generated_tiles)
# mark_deep_grass()
#
# for i in range(-level_size / 2, level_size / 2): for island_tile in generated_tiles.values():
# for j in range (-level_size / 2, level_size / 2): var scene_tile = create_tile_for_hex(island_tile)
# var pos = hexgrid.get_hex_center_from_offset(Vector2(i, j)) tiles.add_child(scene_tile)
# 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") print ("Generated island with " + str(tiles.get_child_count()) + " nodes")

View File

@ -3,7 +3,7 @@
[ext_resource path="res://scenes/Island.gd" type="Script" id=1] [ext_resource path="res://scenes/Island.gd" type="Script" id=1]
[node name="Island" type="Spatial"] [node name="Island" type="Spatial"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 5.43195, 0, 0 )
script = ExtResource( 1 ) script = ExtResource( 1 )
level_size = 200
[node name="Tiles" type="Spatial" parent="."] [node name="Tiles" type="Spatial" parent="."]

70
utils/IslandGenerator.gd Normal file
View File

@ -0,0 +1,70 @@
class_name IslandGenerator
extends Node
var IslandTile = preload("res://utils/IslandTile.gd")
var HexCell = preload("res://addons/gdhexgrid/HexCell.gd")
func hex_cell_to_island_tile (hex: HexCell) -> IslandTile:
var tile = IslandTile.new()
tile.fromHexCell(hex)
return tile
func random_walk(walk_size):
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var tiles = {}
var current_offset_coordinate = Vector2(0, 0)
var current_tile = hex_cell_to_island_tile(hexgrid.get_hex_from_offset(Vector2(0,0)))
tiles[current_tile.offset_coords] = current_tile
for i in range(walk_size - 1):
var neighbours = current_tile.get_all_adjacent()
var tile_exists = true
while tile_exists and len(neighbours) > 0:
var test_index = randi() % len(neighbours)
var test_tile = hex_cell_to_island_tile(neighbours[test_index])
if test_tile.offset_coords in tiles.keys():
neighbours.remove(test_index)
else:
tile_exists = false
current_tile = test_tile
if len(neighbours) == 0:
break
tiles[current_tile.offset_coords] = current_tile
return tiles
func extrude_tiles(tiles):
var old_tiles = tiles
tiles = {}
for tile in old_tiles.values():
if not tile.offset_coords in tiles.keys():
tiles[tile.offset_coords] = tile
var neighbours = tile.get_all_adjacent()
for neighbour in neighbours:
if neighbour.offset_coords in tiles.keys():
continue
neighbour = hex_cell_to_island_tile(neighbour)
tiles[neighbour.offset_coords] = neighbour
return tiles
func mark_sand_tiles(tiles):
for tile in tiles.values():
tile.type = IslandTile.TileType.Grass if randi() % 2 else IslandTile.TileType.DeepGrass
var tile_neighbours = tile.get_all_adjacent()
for neighbour in tile_neighbours:
if not neighbour.offset_coords in tiles.keys():
tile.type = IslandTile.TileType.Sand
break
return tiles

20
utils/IslandTile.gd Normal file
View File

@ -0,0 +1,20 @@
extends "res://addons/gdhexgrid/HexCell.gd"
class_name IslandTile
enum TileType { Sand, Grass, DeepGrass, Rock }
var type = TileType.Sand
func _init(coords=null):
# HexCells can be created with coordinates
if coords:
self.cube_coords = obj_to_coords(coords)
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func fromHexCell (hex_cell: HexCell):
cube_coords = hex_cell.cube_coords
offset_coords = hex_cell.offset_coords
axial_coords = hex_cell.axial_coords

View File

@ -1,44 +0,0 @@
tool
extends Node
export var generate = false setget do_generate
export var level_size = 10
func generate_level(level_node):
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var num_tiles_x = level_size
var num_tiles_z = level_size
for i in range(-num_tiles_x / 2, num_tiles_x / 2):
for j in range (-num_tiles_z / 2, num_tiles_z / 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")
level_node.add_child(tile)
var pos = hexgrid.get_hex_center3(Vector3(0, 0, 0))
print (pos)
var hex = hexgrid.get_hex_at(pos)
print (hex)
print ("Generated level with " + str(level_node.get_child_count()) + " nodes")
func do_generate(flag: bool):
print ("flag: " + str(flag))
var tiles = get_node("../Tiles")
if tiles == null:
print ("Cannot generate: node '../Tiles' not found!")
return
for node in tiles.get_children():
tiles.remove_child(node)
node.queue_free()
generate_level(tiles)