Spawn chests randomly and also mark according tiles as non-walkable.

WorldChunkRefactoring
Martin Felis 2023-08-12 12:55:17 +02:00
parent e759d6d3d5
commit 5b6efaebe9
2 changed files with 25 additions and 24 deletions

View File

@ -1,11 +1,10 @@
[gd_scene load_steps=14 format=2]
[gd_scene load_steps=13 format=2]
[ext_resource path="res://scenes/StreamContainer.tscn" type="PackedScene" id=1]
[ext_resource path="res://entities/Player.tscn" type="PackedScene" id=2]
[ext_resource path="res://scenes/Camera.tscn" type="PackedScene" id=3]
[ext_resource path="res://ui/EditorUI.tscn" type="PackedScene" id=4]
[ext_resource path="res://utils/TileHighlight.tscn" type="PackedScene" id=5]
[ext_resource path="res://entities/Chest.tscn" type="PackedScene" id=7]
[ext_resource path="res://scenes/TileWorld.tscn" type="PackedScene" id=8]
[ext_resource path="res://scenes/Game.cs" type="Script" id=9]
[ext_resource path="res://ui/WorldGeneratorUI.gd" type="Script" id=12]
@ -42,7 +41,6 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 0 )
[node name="TileWorld" parent="." instance=ExtResource( 8 )]
GenerationMapType = 0
Size = 64
[node name="GameUI" type="HBoxContainer" parent="."]
anchor_left = 1.0
@ -260,7 +258,7 @@ text = "Textures"
[node name="WorldTextureRect" type="TextureRect" parent="Generator Container/WorldGeneratorContainer"]
margin_top = 88.0
margin_right = 100.0
margin_right = 135.0
margin_bottom = 188.0
rect_min_size = Vector2( 100, 100 )
expand = true
@ -269,7 +267,7 @@ flip_v = true
[node name="HeightTextureRect" type="TextureRect" parent="Generator Container/WorldGeneratorContainer"]
margin_top = 192.0
margin_right = 100.0
margin_right = 135.0
margin_bottom = 292.0
rect_min_size = Vector2( 100, 100 )
expand = true
@ -298,15 +296,6 @@ parameters/playback = SubResource( 26 )
[node name="Entities" type="Spatial" parent="."]
[node name="Chest" parent="Entities" instance=ExtResource( 7 )]
transform = Transform( -0.534471, 0, -0.845187, 0, 1, 0, 0.845187, 0, -0.534471, 2.68689, 0, 2.60865 )
[node name="Chest3" parent="Entities" instance=ExtResource( 7 )]
transform = Transform( 0.550568, 0, -0.83479, 0, 1, 0, 0.83479, 0, 0.550568, 4.88275, 0, -1.70504 )
[node name="Chest2" parent="Entities" instance=ExtResource( 7 )]
transform = Transform( 0.793576, 0, -0.608471, 0, 1, 0, 0.608471, 0, 0.793576, 2.79265, 0, -5.36551 )
[node name="Axe" parent="Entities" instance=ExtResource( 14 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.79762, 0, 0 )
input_ray_pickable = false
@ -317,4 +306,3 @@ input_ray_pickable = false
[editable path="TileWorld"]
[editable path="Player"]
[editable path="Player/Geometry"]
[editable path="Entities/Chest"]

View File

@ -57,6 +57,7 @@ public class TileWorld : Spatial
private Array<Spatial> _rockAssets = new();
private Array<Spatial> _grassAssets = new();
private Array<Spatial> _treeAssets = new();
private PackedScene _chestScene = GD.Load<PackedScene>("res://entities/Chest.tscn");
private Spatial _environmentNode;
private bool _resizeTriggered;
private int _resizeExtraFrameCount = 0;
@ -98,6 +99,8 @@ public class TileWorld : Spatial
_treeAssets.Add(asset);
}
_chestScene = GD.Load<PackedScene>("res://entities/Chest.tscn");
_environmentNode = GetNode<Spatial>("Environment");
Entities = GetNode<Spatial>("Entities");
@ -109,7 +112,7 @@ public class TileWorld : Spatial
GD.Print("Resetting World Images to size " + size);
Vector2 sizeVector = new Vector2(size, size);
ColormapImage = new Image();
ColormapImage.Create(size, size, false, Image.Format.Rgba8);
ColormapImage.FillRect(new Rect2(0, 0, size, size), Colors.Black);
@ -134,14 +137,14 @@ public class TileWorld : Spatial
public void Generate(int size)
{
GD.Print("Triggering generation for size: " + size);
if (Size != size)
{
ResetWorldImages(size);
_resizeTriggered = true;
_resizeExtraFrameCount = 1;
}
Size = size;
_worldOffscreenViewport.Size = new Vector2(size, size);
@ -276,7 +279,7 @@ public class TileWorld : Spatial
_worldOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Disabled;
_heightmapOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Disabled;
_resizeTriggered = false;
HeightmapImage.Lock();
EmitSignal("WorldGenerated");
}
@ -317,7 +320,7 @@ public class TileWorld : Spatial
ColormapImage.Lock();
HeightmapImage.Lock();
foreach (int textureCoordU in Enumerable.Range(0, Size))
{
foreach (int textureCoordV in Enumerable.Range(0, Size))
@ -350,6 +353,16 @@ public class TileWorld : Spatial
Entities.AddChild(treeAsset);
HexGrid.AddObstacle(cell);
}
else if (environmentRandom.NextDouble() < 0.01)
{
Chest chestAsset = (Chest)_chestScene.Instance();
Transform assetTransform = Transform.Identity;
assetTransform.origin = GetHexCenterFromOffset(offsetCoord);
assetTransform.origin.y += 1.2f;
chestAsset.Transform = assetTransform;
Entities.AddChild(chestAsset);
HexGrid.AddObstacle(cell);
}
}
else if (IsColorWater(colorValue))
{
@ -368,12 +381,12 @@ public class TileWorld : Spatial
{
_resizeExtraFrameCount--;
return;
}
}
if (_currentGenerationState == GenerationState.Heightmap)
{
HeightmapImage.CopyFrom(_heightmapOffscreenViewport.GetTexture().GetData());
_currentGenerationState = GenerationState.Color;
ImageTexture imageTexture = new ImageTexture();
imageTexture.CreateFromImage(HeightmapImage);
@ -443,7 +456,7 @@ public class TileWorld : Spatial
Vector2 textureCoord = OffsetToTextureCoord(offsetCoord);
float heightmapHeight = HeightmapImage.GetPixel((int)textureCoord.x, (int)(textureCoord.y)).r;
if (heightmapHeight > 0.5)
{
heightmapHeight = 0.6f;