Spawning and watering is now bound to the cursor position (kinda)

remotes/checkIfPRContentChanged-1749884192368007736/c_sharp_setup
kziolkowski 9 months ago
parent 339428c312
commit 762a694be3

@ -1337,10 +1337,11 @@ sprite_frames = SubResource("SpriteFrames_4q8ml")
autoplay = "default"
frame_progress = 0.481133
[node name="FarmingControls" type="Node3D" parent="." node_paths=PackedStringArray("_hoeSprite", "_wateringCanSprite", "_movingPlayer")]
[node name="FarmingControls" type="Node3D" parent="." node_paths=PackedStringArray("_hoeSprite", "_wateringCanSprite", "_movingPlayer", "_camera")]
script = ExtResource("4_q5t2e")
_hoeSprite = NodePath("../CharacterBody3D/Farming/Hoe")
_wateringCanSprite = NodePath("../CharacterBody3D/Farming/Watering can")
_fieldPrefab = ExtResource("2_oq5hi")
_movingPlayer = NodePath("../CharacterBody3D")
_camera = NodePath("../CharacterBody3D/CameraPivot2/SubPivot/Camera3D")
metadata/_custom_type_script = "uid://b1sscdr4ptec8"

@ -13,6 +13,7 @@ public partial class FarmingControls : Node3D
[Export] private Sprite3D _wateringCanSprite;
[Export] private PackedScene _fieldPrefab;
[Export] private Node3D _movingPlayer;
[Export] private Camera3D _camera;
public FieldService FieldParent;
@ -45,40 +46,44 @@ public partial class FarmingControls : Node3D
public override void _Input(InputEvent @event)
{
if (@event.IsActionPressed("click") && _hoeInHand)
{
MakeField();
}
Vector2 mousePosition = GetViewport().GetMousePosition();
var dropPlane = new Plane(new Vector3(0, 0, 10), 10);
Vector3? position3D = dropPlane.IntersectsRay(_camera.ProjectRayOrigin(mousePosition),
_camera.ProjectRayNormal(mousePosition));
Debug.Print(position3D.ToString());
if (@event.IsActionPressed("click") && _waterCanInHand)
if (position3D.HasValue)
{
WaterTheField();
Vector2I adjustedPosition = new Vector2I(AdjustValue(position3D.Value.X), AdjustValue(position3D.Value.Y));
if (@event.IsActionPressed("click") && _hoeInHand)
{
MakeField(adjustedPosition);
}
if (@event.IsActionPressed("click") && _waterCanInHand)
{
WaterTheField(adjustedPosition);
}
}
}
private void WaterTheField()
private void WaterTheField(Vector2I fieldPosition)
{
Vector2I currentPos = new Vector2I(AdjustValue(_movingPlayer.GlobalPosition.X), AdjustValue(_movingPlayer.GlobalPosition.Z));
FieldBehaviour field = FieldParent.Get(currentPos);
FieldBehaviour field = FieldParent.Get(fieldPosition);
if (field == null)
return;
field.Water();
Debug.Print("Watered the field.");
}
private void MakeField()
private void MakeField(Vector2I fieldPosition)
{
if(FieldParent == null || _fieldPrefab == null)
return;
// get current player position and adjust it to field / dictionary needs
Vector3 playerPos = _movingPlayer.GlobalPosition;
playerPos = new Vector3(AdjustValue(playerPos.X), 0.1f, AdjustValue(playerPos.Z));
Vector2I intPosition = new Vector2I((int) playerPos.X, (int) playerPos.Z);
// only instantiate a field if there isn't one already.
if(FieldParent.Get(intPosition) == null)
if(FieldParent.Get(fieldPosition) == null)
{
Node fieldInstance = _fieldPrefab.Instantiate();
if (fieldInstance is Node3D field3d)
@ -86,10 +91,10 @@ public partial class FarmingControls : Node3D
// add dictionary entry for the field
Array<Node> fields = field3d.FindChildren("*", nameof(FieldBehaviour));
if (fields.Count > 0)
FieldParent.TryAddEntry(intPosition, fields[0] as FieldBehaviour);
FieldParent.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour);
// reposition and reparent the instance
field3d.Position = playerPos;
field3d.Position = new Vector3(fieldPosition.X, 0.1f, fieldPosition.Y * -1);;
FieldParent.AddChild(fieldInstance);
}
}

@ -31,7 +31,6 @@ namespace Babushka.scripts.CSharp.Common.Farming
{
FieldState = FieldState.Watered;
Texture = Watered;
Debug.Print($"Current Texture: {Texture.ResourceName}");
}
/// <summary>

@ -15,7 +15,6 @@ public partial class FieldService : Node3D
if (!fields.ContainsKey(key))
{
fields.Add(key, field);
Debug.Print("Added entry: " + key);
return true;
}
return false;
@ -24,12 +23,8 @@ public partial class FieldService : Node3D
// Read
public FieldBehaviour Get(Vector2I key)
{
Debug.Print($"Getting field at {key}. Found: {fields.ContainsKey(key)}.");
if (fields.TryGetValue(key, out FieldBehaviour field))
{
Debug.Print($"Getting field at {key}, field: {field.Name}.");
return field;
}
return null;
}
@ -38,7 +33,6 @@ public partial class FieldService : Node3D
public void UpdateEntry(Vector2I fieldPosition, FieldBehaviour state)
{
Debug.Print("Updating entry: " + fieldPosition);
if (fields.ContainsKey(fieldPosition))
{
fields[fieldPosition] = state;
@ -53,7 +47,6 @@ public partial class FieldService : Node3D
public void RemoveEntry(Vector2I fieldPosition)
{
Debug.Print("Removing entry: " + fieldPosition);
if (fields.ContainsKey(fieldPosition))
{
fields.Remove(fieldPosition);

Loading…
Cancel
Save