Added trees

WorldChunkRefactoring
Martin Felis 2023-06-22 18:34:39 +02:00
parent b672d4556a
commit 9896d0896a
4 changed files with 68 additions and 30 deletions

View File

@ -0,0 +1,18 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://assets/KenneySurvivalKit/Models/tree.glb" type="PackedScene" id=1]
[sub_resource type="CapsuleShape" id=1]
radius = 0.329139
height = 0.936801
[node name="tree" instance=ExtResource( 1 )]
[node name="tree" parent="." index="0"]
transform = Transform( 1.5, 0, 0, 0, 1, 0, 0, 0, 1.5, 0, 0, 0 )
[node name="StaticBody" type="StaticBody" parent="." index="1"]
[node name="CollisionShape" type="CollisionShape" parent="StaticBody" index="0"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.519998, 0 )
shape = SubResource( 1 )

View File

@ -266,7 +266,7 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 )
shape = SubResource( 9 ) shape = SubResource( 9 )
[node name="Camera" type="Camera" parent="."] [node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 0.60042, 0.799685, 0, -0.799685, 0.60042, -4.76837e-07, 8.3759, 5.70105 ) transform = Transform( 1, 0, 0, 0, 0.60042, 0.799685, 0, -0.799685, 0.60042, -4.76837e-07, 6.37557, 4.57224 )
current = true current = true
fov = 60.0 fov = 60.0
script = ExtResource( 10 ) script = ExtResource( 10 )

View File

@ -43,6 +43,7 @@ public class TileWorld : Spatial
private TextureRect _heightmapOffscreenTextureRect; private TextureRect _heightmapOffscreenTextureRect;
private Array<Spatial> _rockAssets = new Array<Spatial>(); private Array<Spatial> _rockAssets = new Array<Spatial>();
private Array<Spatial> _grassAssets = new Array<Spatial>(); private Array<Spatial> _grassAssets = new Array<Spatial>();
private Array<Spatial> _treeAssets = new Array<Spatial>();
private Spatial _environmentNode; private Spatial _environmentNode;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
@ -77,6 +78,10 @@ public class TileWorld : Spatial
_grassAssets.Add(asset); _grassAssets.Add(asset);
} }
foreach (Spatial asset in GetNode<Node>("Assets/Trees").GetChildren())
{
_treeAssets.Add(asset);
}
_environmentNode = GetNode<Spatial>("Environment"); _environmentNode = GetNode<Spatial>("Environment");
@ -114,8 +119,10 @@ public class TileWorld : Spatial
{ {
foreach (int coord_y in Enumerable.Range(0, Size)) foreach (int coord_y in Enumerable.Range(0, Size))
{ {
Colormap.SetPixel(coord_x, coord_y, new Color((float) Mathf.Min(coord_x, coord_y) / Size, (float) 0, 0, 1)); Colormap.SetPixel(coord_x, coord_y,
Heightmap.SetPixel(coord_x, coord_y, new Color((float) Mathf.Min(coord_x, coord_y) / Size, (float) 0, 0, 1)); new Color((float)Mathf.Min(coord_x, coord_y) / Size, (float)0, 0, 1));
Heightmap.SetPixel(coord_x, coord_y,
new Color((float)Mathf.Min(coord_x, coord_y) / Size, (float)0, 0, 1));
} }
} }
@ -180,7 +187,8 @@ public class TileWorld : Spatial
Transform assetTransform = Transform.Identity; Transform assetTransform = Transform.Identity;
assetTransform.origin = GetTileWorldCenterFromOffset(offsetCoord); assetTransform.origin = GetTileWorldCenterFromOffset(offsetCoord);
assetTransform.origin.y += 1.2f; assetTransform.origin.y += 1.2f;
assetTransform.basis = assetTransform.basis.Rotated(Vector3.Up, (float)(randomGenerator.NextDouble() * Mathf.Pi * 2)); assetTransform.basis =
assetTransform.basis.Rotated(Vector3.Up, (float)(randomGenerator.NextDouble() * Mathf.Pi * 2));
assetInstance.Transform = assetTransform; assetInstance.Transform = assetTransform;
return assetInstance; return assetInstance;
@ -211,16 +219,24 @@ public class TileWorld : Spatial
{ {
_environmentNode.AddChild(rockAsset); _environmentNode.AddChild(rockAsset);
} }
} else if (IsColorEqualApprox(colorValue, GrassColor)) }
else if (IsColorEqualApprox(colorValue, GrassColor))
{ {
Spatial grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.35); Spatial grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.35);
if (grassAsset != null) if (grassAsset != null)
{ {
_environmentNode.AddChild(grassAsset); _environmentNode.AddChild(grassAsset);
} }
Spatial treeAsset = SelectAsset(offsetCoord, _treeAssets, environmentRandom, 0.10);
if (treeAsset != null)
{
_environmentNode.AddChild(treeAsset);
} }
} }
} }
}
Colormap.Unlock(); Colormap.Unlock();
} }
@ -248,7 +264,6 @@ public class TileWorld : Spatial
OnMapGenerationComplete(); OnMapGenerationComplete();
} }
} }
public bool IsOffsetCoordValid(Vector2 offsetCoord) public bool IsOffsetCoordValid(Vector2 offsetCoord)
@ -321,6 +336,6 @@ public class TileWorld : Spatial
return new Vector3( return new Vector3(
tileCenter.x - Mathf.Round(Size * 0.75f * 0.5f), tileCenter.x - Mathf.Round(Size * 0.75f * 0.5f),
GetHeightAtOffset(offsetCoord), GetHeightAtOffset(offsetCoord),
tileCenter.y + (Mathf.Sqrt(3) / 2) * Size * 0.5f); tileCenter.y + ((Mathf.Sqrt(3) / 2) * Mathf.Round(Size * 0.5f)));
} }
} }

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=2] [gd_scene load_steps=10 format=2]
[ext_resource path="res://scenes/TileWorld.cs" type="Script" id=1] [ext_resource path="res://scenes/TileWorld.cs" type="Script" id=1]
[ext_resource path="res://icon.png" type="Texture" id=2] [ext_resource path="res://icon.png" type="Texture" id=2]
@ -8,6 +8,7 @@
[ext_resource path="res://assets/Environment/rockC.tscn" type="PackedScene" id=6] [ext_resource path="res://assets/Environment/rockC.tscn" type="PackedScene" id=6]
[ext_resource path="res://assets/Environment/rockA.tscn" type="PackedScene" id=7] [ext_resource path="res://assets/Environment/rockA.tscn" type="PackedScene" id=7]
[ext_resource path="res://assets/Environment/grassLarge.tscn" type="PackedScene" id=8] [ext_resource path="res://assets/Environment/grassLarge.tscn" type="PackedScene" id=8]
[ext_resource path="res://assets/Environment/tree.tscn" type="PackedScene" id=9]
[node name="TileWorld" type="Spatial"] [node name="TileWorld" type="Spatial"]
script = ExtResource( 1 ) script = ExtResource( 1 )
@ -64,4 +65,8 @@ visible = false
[node name="grassLarge" parent="Assets/Grass" instance=ExtResource( 8 )] [node name="grassLarge" parent="Assets/Grass" instance=ExtResource( 8 )]
[node name="Trees" type="Spatial" parent="Assets"]
[node name="tree" parent="Assets/Trees" instance=ExtResource( 9 )]
[node name="Environment" type="Spatial" parent="."] [node name="Environment" type="Spatial" parent="."]