Spawning and watering is now bound to the cursor position (kinda)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user