diff --git a/GridMap.tscn b/GridMap.tscn new file mode 100644 index 0000000..f4095f4 --- /dev/null +++ b/GridMap.tscn @@ -0,0 +1,40 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://ProceduralGenerator.gd" type="Script" id=1] +[ext_resource path="res://meshes.meshlib" type="MeshLibrary" id=2] +[ext_resource path="res://Player.gd" type="Script" id=3] + +[sub_resource type="CapsuleMesh" id=1] + +[node name="Spatial" type="Spatial"] + +[node name="GridMap" type="GridMap" parent="."] +mesh_library = ExtResource( 2 ) +cell_size = Vector3( 1, 1, 1 ) +cell_octant_size = 512 +cell_center_x = false +cell_center_y = false +cell_center_z = false +script = ExtResource( 1 ) +__meta__ = { +"_editor_clip_": 0 +} +ChunkWidth = 64 +ChunkHeight = 64 + +[node name="Player" type="Spatial" parent="GridMap"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 20, 0 ) +script = ExtResource( 3 ) + +[node name="MeshInstance" type="MeshInstance" parent="GridMap/Player"] +transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0 ) +mesh = SubResource( 1 ) + +[node name="Camera" type="Camera" parent="GridMap/Player"] +transform = Transform( 1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 10, 0 ) + +[node name="MapUpdateTimer" type="Timer" parent="GridMap"] +wait_time = 0.1 +autostart = true + +[connection signal="timeout" from="GridMap/MapUpdateTimer" to="GridMap" method="_on_MapUpdateTimer_timeout"] diff --git a/MeshLib.tscn b/MeshLib.tscn new file mode 100644 index 0000000..5b0094f --- /dev/null +++ b/MeshLib.tscn @@ -0,0 +1,46 @@ +[gd_scene load_steps=8 format=2] + +[sub_resource type="CubeMesh" id=1] + +[sub_resource type="SpatialMaterial" id=2] +albedo_color = Color( 0.266667, 0.211765, 0.74902, 1 ) + +[sub_resource type="SpatialMaterial" id=3] +albedo_color = Color( 0.054902, 0.588235, 0.843137, 1 ) + +[sub_resource type="SpatialMaterial" id=4] +albedo_color = Color( 0.882353, 0.811765, 0.0588235, 1 ) + +[sub_resource type="SpatialMaterial" id=5] +albedo_color = Color( 0.121569, 0.756863, 0.32549, 1 ) + +[sub_resource type="SpatialMaterial" id=6] +albedo_color = Color( 0.611765, 0.611765, 0.611765, 1 ) + +[sub_resource type="SpatialMaterial" id=7] + +[node name="MeshLib" type="Spatial"] + +[node name="Tile_DeepWaterBlock" type="MeshInstance" parent="."] +mesh = SubResource( 1 ) +material/0 = SubResource( 2 ) + +[node name="Tile_WaterBlock" type="MeshInstance" parent="."] +mesh = SubResource( 1 ) +material/0 = SubResource( 3 ) + +[node name="Tile_SandBlock" type="MeshInstance" parent="."] +mesh = SubResource( 1 ) +material/0 = SubResource( 4 ) + +[node name="Tile_GrassBlock" type="MeshInstance" parent="."] +mesh = SubResource( 1 ) +material/0 = SubResource( 5 ) + +[node name="Tile_StoneBlock" type="MeshInstance" parent="."] +mesh = SubResource( 1 ) +material/0 = SubResource( 6 ) + +[node name="Tile_SnowBlock" type="MeshInstance" parent="."] +mesh = SubResource( 1 ) +material/0 = SubResource( 7 ) diff --git a/Player.gd b/Player.gd new file mode 100644 index 0000000..fb99e61 --- /dev/null +++ b/Player.gd @@ -0,0 +1,20 @@ +extends Spatial + + +const SPEED = 0.25 + + +func _physics_process(delta): + # Movement + var motion = Vector3() + + if Input.is_action_pressed("ui_up"): + motion.z += -1.0 + if Input.is_action_pressed("ui_down"): + motion.z += 1.0 + if Input.is_action_pressed("ui_left"): + motion.x += -1.0 + if Input.is_action_pressed("ui_right"): + motion.x += 1.0 + + global_transform.origin += motion * SPEED diff --git a/ProceduralGenerator.gd b/ProceduralGenerator.gd new file mode 100644 index 0000000..54af773 --- /dev/null +++ b/ProceduralGenerator.gd @@ -0,0 +1,52 @@ +extends GridMap + +export(int) var ChunkWidth: int = 0 +export(int) var ChunkHeight: int = 0 + +var heightMapTexture = NoiseTexture.new() + +onready var player = $Player + +func _ready(): + randomize() + heightMapTexture.width = 512 + heightMapTexture.height = 512 + heightMapTexture.noise = OpenSimplexNoise.new() + heightMapTexture.noise.seed = randi() + + print("Seed: " + str(heightMapTexture.noise.seed)) + + setGridChunk(0, ChunkWidth, 0, ChunkHeight) + +func generateGridChunk(playerPos: Vector3) -> void: + clear() + var chunkCenter = world_to_map(playerPos) + var rowStart: float = chunkCenter.z - (ChunkHeight / 2) + var rowEnd: float = chunkCenter.z + (ChunkHeight / 2) + var columnStart: float = chunkCenter.x - (ChunkWidth / 2) + var columnEnd: float = chunkCenter.x + (ChunkWidth / 2) + setGridChunk(columnStart, columnEnd, rowStart, rowEnd) + +func setGridChunk(columnStart: float, columnEnd: float, rowStart: float, rowEnd: float) -> void: + if columnStart < 0 : + columnStart = 0 + if rowStart < 0 : + rowStart = 0 + if columnEnd > heightMapTexture.width : + columnEnd = heightMapTexture.width - 1 + if rowEnd > heightMapTexture.height : + rowEnd = heightMapTexture.height - 1 + + for mz in range(rowStart, rowEnd): + for mx in range(columnStart, columnEnd): + var noiseValue: float = heightMapTexture.noise.get_noise_2d(mx, mz) + var weight: float = (noiseValue + 1) / 2 + var height: float = floor(lerp(0, 8, weight)) * 2 + for my in range(0, height): + var meshID = floor(my / 2) + set_cell_item(mx, my * 2, mz, meshID, 0) + + + +func _on_MapUpdateTimer_timeout(): + generateGridChunk(player.global_transform.origin) diff --git a/default_env.tres b/default_env.tres new file mode 100644 index 0000000..20207a4 --- /dev/null +++ b/default_env.tres @@ -0,0 +1,7 @@ +[gd_resource type="Environment" load_steps=2 format=2] + +[sub_resource type="ProceduralSky" id=1] + +[resource] +background_mode = 2 +background_sky = SubResource( 1 ) diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..c98fbb6 Binary files /dev/null and b/icon.png differ diff --git a/icon.png.import b/icon.png.import new file mode 100644 index 0000000..a4c02e6 --- /dev/null +++ b/icon.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.png" +dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.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 +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/meshes.meshlib b/meshes.meshlib new file mode 100644 index 0000000..e65f9db Binary files /dev/null and b/meshes.meshlib differ diff --git a/project.godot b/project.godot new file mode 100644 index 0000000..8942dd9 --- /dev/null +++ b/project.godot @@ -0,0 +1,26 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=4 + +[application] + +config/name="gridmap" +config/icon="res://icon.png" + +[gui] + +common/drop_mouse_on_gui_input_disabled=true + +[physics] + +common/enable_pause_aware_picking=true + +[rendering] + +environment/default_environment="res://default_env.tres"