diff --git a/assets/Environment/tree.tscn b/assets/Environment/tree.tscn new file mode 100644 index 0000000..52f6a3b --- /dev/null +++ b/assets/Environment/tree.tscn @@ -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 ) diff --git a/scenes/Game.tscn b/scenes/Game.tscn index 3c6d65b..6bb659c 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -266,7 +266,7 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 ) shape = SubResource( 9 ) [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 fov = 60.0 script = ExtResource( 10 ) diff --git a/scenes/TileWorld.cs b/scenes/TileWorld.cs index 4faa73d..ebce74e 100644 --- a/scenes/TileWorld.cs +++ b/scenes/TileWorld.cs @@ -18,9 +18,9 @@ public class TileWorld : Spatial } // constants - private static readonly Color RockColor = new Color (0.5f, 0.5f, 0.4f, 1f); - private static readonly Color GrassColor = new Color (0, 0.4f, 0, 1f); - + private static readonly Color RockColor = new Color(0.5f, 0.5f, 0.4f, 1f); + private static readonly Color GrassColor = new Color(0, 0.4f, 0, 1f); + private GenerationState _currentGenerationState = GenerationState.Heightmap; // signals @@ -43,6 +43,7 @@ public class TileWorld : Spatial private TextureRect _heightmapOffscreenTextureRect; private Array _rockAssets = new Array(); private Array _grassAssets = new Array(); + private Array _treeAssets = new Array(); private Spatial _environmentNode; // Called when the node enters the scene tree for the first time. @@ -66,7 +67,7 @@ public class TileWorld : Spatial _heightmapOffscreenTextureRect.SetSize(new Vector2(Size, Size)); GetNode("Assets").Visible = false; - + foreach (Spatial asset in GetNode("Assets/Rocks").GetChildren()) { _rockAssets.Add(asset); @@ -77,9 +78,13 @@ public class TileWorld : Spatial _grassAssets.Add(asset); } - + foreach (Spatial asset in GetNode("Assets/Trees").GetChildren()) + { + _treeAssets.Add(asset); + } + _environmentNode = GetNode("Environment"); - + Generate(Size); } @@ -92,7 +97,7 @@ public class TileWorld : Spatial _heightmapOffscreenViewport.Size = new Vector2(size, size); OnMapGenerationStart(); - + GenerateNoiseMap(); // GenerateDebugMap(); @@ -109,23 +114,25 @@ public class TileWorld : Spatial Heightmap.Lock(); Colormap.Lock(); - + foreach (int coord_x 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)); - Heightmap.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, + 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)); } } - - Colormap.SetPixel(Size - 1, Size -1, new Color(1, 1, 1, 1)); + + Colormap.SetPixel(Size - 1, Size - 1, new Color(1, 1, 1, 1)); Colormap.Unlock(); - + OnMapGenerationComplete(); } - + private void GenerateNoiseMap() { Heightmap = new Image(); @@ -140,11 +147,11 @@ public class TileWorld : Spatial noiseGenerator.Lacunarity = 4; ImageTexture heightmapTexture = new ImageTexture(); - heightmapTexture.CreateFromImage(noiseGenerator.GetSeamlessImage((int) Size)); + heightmapTexture.CreateFromImage(noiseGenerator.GetSeamlessImage((int)Size)); heightmapTexture.Flags = 0; _heightmapOffscreenTextureRect.Texture = heightmapTexture; Heightmap.CopyFrom(_heightmapOffscreenViewport.GetTexture().GetData()); - + OnHeightMapChanged(); } @@ -155,7 +162,7 @@ public class TileWorld : Spatial child.QueueFree(); } } - + private void OnHeightMapChanged() { _currentGenerationState = GenerationState.Heightmap; @@ -174,13 +181,14 @@ public class TileWorld : Spatial { return null; } - + int assetIndex = randomGenerator.Next(assets.Count); - Spatial assetInstance = (Spatial) assets[assetIndex].Duplicate(); + Spatial assetInstance = (Spatial)assets[assetIndex].Duplicate(); Transform assetTransform = Transform.Identity; assetTransform.origin = GetTileWorldCenterFromOffset(offsetCoord); 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; return assetInstance; @@ -191,11 +199,11 @@ public class TileWorld : Spatial Vector3 colorDifference = new Vector3(colorA.r - colorB.r, colorA.g - colorB.g, colorA.b - colorB.b); return colorDifference.LengthSquared() < 0.1 * 0.1; } - + private void PopulateEnvironment() { Random environmentRandom = new Random(Seed); - + Colormap.Lock(); foreach (int coord_x in Enumerable.Range(0, Size)) { @@ -211,16 +219,24 @@ public class TileWorld : Spatial { _environmentNode.AddChild(rockAsset); } - } else if (IsColorEqualApprox(colorValue, GrassColor)) + } + else if (IsColorEqualApprox(colorValue, GrassColor)) { Spatial grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.35); if (grassAsset != null) { _environmentNode.AddChild(grassAsset); - } + } + + Spatial treeAsset = SelectAsset(offsetCoord, _treeAssets, environmentRandom, 0.10); + if (treeAsset != null) + { + _environmentNode.AddChild(treeAsset); + } } } } + Colormap.Unlock(); } @@ -243,12 +259,11 @@ public class TileWorld : Spatial Heightmap.Lock(); _currentGenerationState = GenerationState.Objects; - + PopulateEnvironment(); OnMapGenerationComplete(); } - } public bool IsOffsetCoordValid(Vector2 offsetCoord) @@ -290,7 +305,7 @@ public class TileWorld : Spatial { return 0f; } - + Vector2 textureCoord = OffsetToTextureCoord(offsetCoord); float heightmapHeight = Heightmap.GetPixel((int)textureCoord.x, (int)(textureCoord.y)).r; @@ -321,6 +336,6 @@ public class TileWorld : Spatial return new Vector3( tileCenter.x - Mathf.Round(Size * 0.75f * 0.5f), GetHeightAtOffset(offsetCoord), - tileCenter.y + (Mathf.Sqrt(3) / 2) * Size * 0.5f); + tileCenter.y + ((Mathf.Sqrt(3) / 2) * Mathf.Round(Size * 0.5f))); } } \ No newline at end of file diff --git a/scenes/TileWorld.tscn b/scenes/TileWorld.tscn index 58e6ff2..da60060 100644 --- a/scenes/TileWorld.tscn +++ b/scenes/TileWorld.tscn @@ -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://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/rockA.tscn" type="PackedScene" id=7] [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"] script = ExtResource( 1 ) @@ -64,4 +65,8 @@ visible = false [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="."]