Spawning and watering is now bound to the cursor position (kinda)
This commit is contained in:
@@ -1337,10 +1337,11 @@ sprite_frames = SubResource("SpriteFrames_4q8ml")
|
|||||||
autoplay = "default"
|
autoplay = "default"
|
||||||
frame_progress = 0.481133
|
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")
|
script = ExtResource("4_q5t2e")
|
||||||
_hoeSprite = NodePath("../CharacterBody3D/Farming/Hoe")
|
_hoeSprite = NodePath("../CharacterBody3D/Farming/Hoe")
|
||||||
_wateringCanSprite = NodePath("../CharacterBody3D/Farming/Watering can")
|
_wateringCanSprite = NodePath("../CharacterBody3D/Farming/Watering can")
|
||||||
_fieldPrefab = ExtResource("2_oq5hi")
|
_fieldPrefab = ExtResource("2_oq5hi")
|
||||||
_movingPlayer = NodePath("../CharacterBody3D")
|
_movingPlayer = NodePath("../CharacterBody3D")
|
||||||
|
_camera = NodePath("../CharacterBody3D/CameraPivot2/SubPivot/Camera3D")
|
||||||
metadata/_custom_type_script = "uid://b1sscdr4ptec8"
|
metadata/_custom_type_script = "uid://b1sscdr4ptec8"
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ public partial class FarmingControls : Node3D
|
|||||||
[Export] private Sprite3D _wateringCanSprite;
|
[Export] private Sprite3D _wateringCanSprite;
|
||||||
[Export] private PackedScene _fieldPrefab;
|
[Export] private PackedScene _fieldPrefab;
|
||||||
[Export] private Node3D _movingPlayer;
|
[Export] private Node3D _movingPlayer;
|
||||||
|
[Export] private Camera3D _camera;
|
||||||
|
|
||||||
public FieldService FieldParent;
|
public FieldService FieldParent;
|
||||||
|
|
||||||
@@ -45,40 +46,44 @@ public partial class FarmingControls : Node3D
|
|||||||
|
|
||||||
public override void _Input(InputEvent @event)
|
public override void _Input(InputEvent @event)
|
||||||
{
|
{
|
||||||
if (@event.IsActionPressed("click") && _hoeInHand)
|
Vector2 mousePosition = GetViewport().GetMousePosition();
|
||||||
{
|
var dropPlane = new Plane(new Vector3(0, 0, 10), 10);
|
||||||
MakeField();
|
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(fieldPosition);
|
||||||
FieldBehaviour field = FieldParent.Get(currentPos);
|
|
||||||
if (field == null)
|
if (field == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
field.Water();
|
field.Water();
|
||||||
Debug.Print("Watered the field.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MakeField()
|
private void MakeField(Vector2I fieldPosition)
|
||||||
{
|
{
|
||||||
if(FieldParent == null || _fieldPrefab == null)
|
if(FieldParent == null || _fieldPrefab == null)
|
||||||
return;
|
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.
|
// only instantiate a field if there isn't one already.
|
||||||
if(FieldParent.Get(intPosition) == null)
|
if(FieldParent.Get(fieldPosition) == null)
|
||||||
{
|
{
|
||||||
Node fieldInstance = _fieldPrefab.Instantiate();
|
Node fieldInstance = _fieldPrefab.Instantiate();
|
||||||
if (fieldInstance is Node3D field3d)
|
if (fieldInstance is Node3D field3d)
|
||||||
@@ -86,10 +91,10 @@ public partial class FarmingControls : Node3D
|
|||||||
// add dictionary entry for the field
|
// add dictionary entry for the field
|
||||||
Array<Node> fields = field3d.FindChildren("*", nameof(FieldBehaviour));
|
Array<Node> fields = field3d.FindChildren("*", nameof(FieldBehaviour));
|
||||||
if (fields.Count > 0)
|
if (fields.Count > 0)
|
||||||
FieldParent.TryAddEntry(intPosition, fields[0] as FieldBehaviour);
|
FieldParent.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour);
|
||||||
|
|
||||||
// reposition and reparent the instance
|
// reposition and reparent the instance
|
||||||
field3d.Position = playerPos;
|
field3d.Position = new Vector3(fieldPosition.X, 0.1f, fieldPosition.Y * -1);;
|
||||||
FieldParent.AddChild(fieldInstance);
|
FieldParent.AddChild(fieldInstance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ namespace Babushka.scripts.CSharp.Common.Farming
|
|||||||
{
|
{
|
||||||
FieldState = FieldState.Watered;
|
FieldState = FieldState.Watered;
|
||||||
Texture = Watered;
|
Texture = Watered;
|
||||||
Debug.Print($"Current Texture: {Texture.ResourceName}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ public partial class FieldService : Node3D
|
|||||||
if (!fields.ContainsKey(key))
|
if (!fields.ContainsKey(key))
|
||||||
{
|
{
|
||||||
fields.Add(key, field);
|
fields.Add(key, field);
|
||||||
Debug.Print("Added entry: " + key);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -24,12 +23,8 @@ public partial class FieldService : Node3D
|
|||||||
// Read
|
// Read
|
||||||
public FieldBehaviour Get(Vector2I key)
|
public FieldBehaviour Get(Vector2I key)
|
||||||
{
|
{
|
||||||
Debug.Print($"Getting field at {key}. Found: {fields.ContainsKey(key)}.");
|
|
||||||
if (fields.TryGetValue(key, out FieldBehaviour field))
|
if (fields.TryGetValue(key, out FieldBehaviour field))
|
||||||
{
|
|
||||||
Debug.Print($"Getting field at {key}, field: {field.Name}.");
|
|
||||||
return field;
|
return field;
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -38,7 +33,6 @@ public partial class FieldService : Node3D
|
|||||||
public void UpdateEntry(Vector2I fieldPosition, FieldBehaviour state)
|
public void UpdateEntry(Vector2I fieldPosition, FieldBehaviour state)
|
||||||
{
|
{
|
||||||
|
|
||||||
Debug.Print("Updating entry: " + fieldPosition);
|
|
||||||
if (fields.ContainsKey(fieldPosition))
|
if (fields.ContainsKey(fieldPosition))
|
||||||
{
|
{
|
||||||
fields[fieldPosition] = state;
|
fields[fieldPosition] = state;
|
||||||
@@ -53,7 +47,6 @@ public partial class FieldService : Node3D
|
|||||||
|
|
||||||
public void RemoveEntry(Vector2I fieldPosition)
|
public void RemoveEntry(Vector2I fieldPosition)
|
||||||
{
|
{
|
||||||
Debug.Print("Removing entry: " + fieldPosition);
|
|
||||||
if (fields.ContainsKey(fieldPosition))
|
if (fields.ContainsKey(fieldPosition))
|
||||||
{
|
{
|
||||||
fields.Remove(fieldPosition);
|
fields.Remove(fieldPosition);
|
||||||
|
|||||||
Reference in New Issue
Block a user