Refactoring

WorldChunkRefactoring
Martin Felis 2022-10-08 14:14:20 +02:00
parent 4d391ff5b3
commit c4770b395a
10 changed files with 207 additions and 35 deletions

View File

@ -23,7 +23,7 @@ func _ready():
movable_component.pos = self.transform.origin
func _process(delta):
func _process(_delta):
if is_active and color_component:
var color = Color(
(sin(float(OS.get_system_time_msecs()) / 1000.0) + 1.0) / 2.0,
@ -34,7 +34,7 @@ func _process(delta):
pass
func _physics_process(delta):
func _physics_process(_delta):
if not collision_component:
return

View File

@ -9,17 +9,17 @@
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"
@ -54,7 +54,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://utils/SpringDamper.gd"
}, {
"base": "Reference",
"base": "Sprite",
"class": "TintedSpriteComponent",
"language": "GDScript",
"path": "res://components/TintedSpriteComponent.gd"

View File

@ -23,7 +23,7 @@ func _ready():
node.connect("island_tile_hover", self, "on_island_tile_hover")
func _process(delta):
func _process(_delta):
fps_label.text = "FPS: " + str(Performance.get_monitor(Performance.TIME_FPS))
player_pos_label.text = "Pos: " + str(player.transform.origin)
@ -37,5 +37,5 @@ func on_island_tile_selected(island, tile):
print ("clicked on tile " + str(target_coordinate) + " error: " + str(position_error))
func on_island_tile_hover(island, tile):
func on_island_tile_hover(_island, tile):
tile_pos_label.text = "TilePos: " + str(tile.global_transform.origin)

View File

@ -3,7 +3,6 @@
[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/DebugCamera.gd" type="Script" id=3]
[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://components/MovableComponent.tscn" type="PackedScene" id=6]
[ext_resource path="res://scenes/World.gd" type="Script" id=7]
@ -17,6 +16,8 @@ radial_segments = 16
radius = 0.25
height = 0.5
[sub_resource type="HeightMapShape" id=6]
[sub_resource type="PlaneMesh" id=1]
subdivide_width = 24
subdivide_depth = 24
@ -101,8 +102,8 @@ shader = SubResource( 2 )
shader_param/amplitude = Vector2( 0.005, 0.005 )
shader_param/frequency = Vector2( 50, 48 )
shader_param/time_factor = Vector2( 7, 8 )
shader_param/beer_factor = 4.0
shader_param/refraction = 0.01
shader_param/beer_factor = 2.0
shader_param/refraction = 0.001
shader_param/uv_offset_scale = Vector2( 0.2, 0.2 )
shader_param/uv_offset_time_scale = 0.1
shader_param/uv_offset_amplitude = 1.2
@ -180,18 +181,21 @@ max_speed = 3
[node name="World" type="Spatial" parent="."]
script = ExtResource( 7 )
generate_flag = true
[node name="Scene" type="Spatial" parent="World"]
[node name="Navigation" type="Spatial" parent="World"]
[node name="Island" parent="World" instance=ExtResource( 4 )]
[node name="Tiles" type="Spatial" parent="World"]
[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="ClckArea" type="Area" parent="World"]
[node name="CollisionShape" type="CollisionShape" parent="World/ClckArea"]
transform = Transform( 50, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0, 0 )
shape = SubResource( 6 )
[node name="Water" type="Spatial" parent="."]
visible = false
[node name="Water" type="MeshInstance" parent="Water"]
transform = Transform( 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, -0.1, 0 )
@ -210,3 +214,7 @@ 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 )
[connection signal="input_event" from="World/ClckArea" to="World" method="_on_ClckArea_input_event"]
[connection signal="mouse_entered" from="World/ClckArea" to="World" method="_on_ClckArea_mouse_entered"]
[connection signal="mouse_exited" from="World/ClckArea" to="World" method="_on_ClckArea_mouse_exited"]

View File

@ -3,7 +3,8 @@ class_name HexTile3D
onready var is_mouse_over = false
onready var mesh = $Mesh
var old_material_override = Material.new()
onready var old_material_override = preload("res://materials/SandTile.tres")
onready var game_tile = preload("res://utils/GameTile.gd").new()
signal tile_selected
signal tile_mouse_entered
@ -13,14 +14,16 @@ signal tile_mouse_entered
func set_tiletype(type):
var GameTile = load("res://utils/GameTile.gd").new()
var mesh = $Mesh
game_tile.type = type
if type == GameTile.TileType.Sand:
mesh.set_surface_material(0, preload("res://materials/SandTile.tres"))
var _mesh = $Mesh
if type == GameTile.TileType.Sand or type== GameTile.TileType.Water:
_mesh.set_surface_material(0, preload("res://materials/SandTile.tres"))
elif type == GameTile.TileType.Grass:
mesh.set_surface_material(0, preload("res://materials/GrassTile.tres"))
_mesh.set_surface_material(0, preload("res://materials/GrassTile.tres"))
else:
mesh.set_surface_material(0, preload("res://materials/DeepGrassTile.tres"))
_mesh.set_surface_material(0, preload("res://materials/DeepGrassTile.tres"))
func _on_Area_mouse_entered():
@ -39,7 +42,7 @@ func _on_Area_mouse_exited():
mesh.material_override = old_material_override
func _on_Area_input_event(camera, event, position, normal, shape_idx):
func _on_Area_input_event(_camera, event, position, _normal, _shape_idx):
if is_mouse_over and event is InputEventMouseButton:
if event.button_index == 1 and event.pressed:
emit_signal("tile_selected", self)

View File

@ -6,7 +6,7 @@
[sub_resource type="CylinderMesh" id=6]
top_radius = 0.5
bottom_radius = 0.5
height = 1.0
height = 10.0
radial_segments = 6
rings = 1
@ -19,13 +19,14 @@ transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0, 0
script = ExtResource( 1 )
[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, -5, 0 )
mesh = SubResource( 6 )
material/0 = ExtResource( 2 )
[node name="Area" type="Area" parent="Mesh"]
[node name="CollisionShape" type="CollisionShape" parent="Mesh/Area"]
transform = Transform( 1, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0 )
shape = SubResource( 5 )
[connection signal="input_event" from="Mesh/Area" to="." method="_on_Area_input_event"]

View File

@ -51,7 +51,7 @@ func generate():
print ("Generated island with " + str(tiles.get_child_count()) + " nodes")
func do_generate(flag: bool):
func do_generate(_flag: bool):
print ("Generating Level")
generate()

View File

@ -2,17 +2,173 @@ extends Spatial
var GameTile = load("res://utils/GameTile.gd")
var HexCell = preload("res://addons/gdhexgrid/HexCell.gd")
onready var HexGrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var IslandGenerator = preload("res://utils/IslandGenerator.gd").new()
onready var tiles = $Tiles
var has_tiles_initialized = false
export var size = [200, 200]
var tile_map = {}
var active_tile = null
var island_count = 5
export var size = [50, 50]
export var generate_flag: bool = false setget do_generate
func _ready():
assert(get_node("Tiles") != null)
generate()
pass
func create_ocean_tiles():
func _process(_delta):
pass
func add_tile_for_hex(game_tile):
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var islands
pass
var tile = HexTile3D.instance()
var pos = hexgrid.get_hex_center_from_offset(game_tile.offset_coords)
var height = -2.0 # if game_tile.type == GameTile.TileType.Sand else 0.1 + rand_range(0.0, 0.05)
tiles.add_child(tile)
tile_map[game_tile.offset_coords] = tile
tile.set_tiletype(game_tile.type)
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.game_tile = game_tile
return tile
func reset():
var tiles = get_node("Tiles")
print ("clearing old tiles: " + str(tiles.get_child_count()))
for tile in tiles.get_children():
tiles.remove_child(tile)
tile.queue_free()
tile_map = {}
func generate():
reset()
generate_base_tiles()
for i in range (island_count):
generate_island(Vector2(
rand_range(-size[0] / 2, size[0] / 2),
rand_range(-size[1] / 2, size[1] / 2)
))
mark_sand_tiles()
smooth_water_tiles()
smooth_water_tiles()
smooth_water_tiles()
smooth_water_tiles()
func generate_base_tiles():
print ("generating world")
for i in range (-size[0] / 2, size[0] / 2):
for j in range (-size[1] / 2, size[1] / 2):
var tile = GameTile.new()
tile.type = GameTile.TileType.Water
tile.offset_coords = Vector2(i,j)
add_tile_for_hex(tile)
func generate_island(start_pos: Vector2):
var generated_tiles = IslandGenerator.random_walk(20, start_pos)
generated_tiles = IslandGenerator.extrude_tiles(generated_tiles)
# generated_tiles = IslandGenerator.mark_sand_tiles(generated_tiles)
for island_tile in generated_tiles.values():
if not island_tile.offset_coords in tile_map.keys():
continue
var world_tile = tile_map[island_tile.offset_coords]
world_tile.transform.origin[1] = 0
world_tile.set_tiletype(island_tile.type)
print ("Generated island with " + str(tiles.get_child_count()) + " nodes")
func mark_sand_tiles():
for tile in tile_map.values():
if tile.game_tile.type == GameTile.TileType.Water:
continue
tile.set_tiletype(GameTile.TileType.Grass if randi() % 2 else GameTile.TileType.DeepGrass)
var tile_neighbours = tile.game_tile.get_all_adjacent()
for neighbour in tile_neighbours:
var tile_on_map = neighbour.offset_coords in tile_map.keys()
if not tile_on_map:
continue
var neighbour_tile = tile_map[neighbour.offset_coords]
if neighbour_tile.game_tile.type == GameTile.TileType.Water:
tile.set_tiletype(GameTile.TileType.Sand)
func smooth_water_tiles():
# horizontal smoothing
for i in range ((-size[0] / 2) + 1, (size[0] / 2) - 1):
for j in range ((-size[1] / 2) + 1, (size[1] / 2) - 1):
var prev = tile_map[Vector2(i - 1,j)]
var current = tile_map[Vector2(i,j)]
var next = tile_map[Vector2(i + 1,j)]
if current.game_tile.type != GameTile.TileType.Water:
continue
current.transform.origin[1] = (prev.transform.origin[1] + next.transform.origin[1]) * 0.5
# vertical smoothings
for i in range ((-size[0] / 2) + 1, (size[0] / 2) - 1):
for j in range ((-size[1] / 2) + 1, (size[1] / 2) - 1):
var prev = tile_map[Vector2(i,j - 1)]
var current = tile_map[Vector2(i,j)]
var next = tile_map[Vector2(i,j + 1)]
if current.game_tile.type != GameTile.TileType.Water:
continue
current.transform.origin[1] = (prev.transform.origin[1] + next.transform.origin[1]) * 0.5
func do_generate(flag: bool):
print ("Generating Level")
generate_flag = false
# generate()
func _on_ClckArea_input_event(_camera, _event, position, _normal, _shape_idx):
var offset_coords = HexGrid.get_hex_at(position).offset_coords
var world_tile = null
if offset_coords in tile_map.keys():
world_tile = tile_map[offset_coords]
if active_tile != null and world_tile != active_tile:
active_tile._on_Area_mouse_exited()
if active_tile == world_tile:
return
if world_tile == null:
return
active_tile = world_tile
active_tile._on_Area_mouse_entered()
func _on_ClckArea_mouse_entered():
pass # Replace with function body.
func _on_ClckArea_mouse_exited():
pass # Replace with function body.

View File

@ -1,7 +1,7 @@
extends "res://addons/gdhexgrid/HexCell.gd"
class_name GameTile
enum TileType { Sand, Grass, DeepGrass, Rock, Water }
enum TileType { Water, Sand, Grass, DeepGrass, Rock }
var type = TileType.Sand

View File

@ -3,6 +3,11 @@ extends Node
var GameTile = load("res://utils/GameTile.gd")
var HexCell = preload("res://addons/gdhexgrid/HexCell.gd")
var rng = RandomNumberGenerator.new()
func _ready():
rng.randomize()
func hex_cell_to_island_tile (hex: HexCell) -> GameTile:
@ -11,12 +16,11 @@ func hex_cell_to_island_tile (hex: HexCell) -> GameTile:
return tile
func random_walk(walk_size):
func random_walk(walk_size, start_offset = Vector2(0,0)):
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)))
var current_tile = hex_cell_to_island_tile(hexgrid.get_hex_from_offset(start_offset))
tiles[current_tile.offset_coords] = current_tile
for i in range(walk_size - 1):
@ -24,7 +28,7 @@ func random_walk(walk_size):
var tile_exists = true
while tile_exists and len(neighbours) > 0:
var test_index = randi() % len(neighbours)
var test_index = rng.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)
@ -61,7 +65,7 @@ func extrude_tiles(tiles):
func mark_sand_tiles(tiles):
for tile in tiles.values():
tile.type = GameTile.TileType.Grass if randi() % 2 else GameTile.TileType.DeepGrass
tile.type = GameTile.TileType.Grass if rng.randi() % 2 else GameTile.TileType.DeepGrass
var tile_neighbours = tile.get_all_adjacent()
for neighbour in tile_neighbours: