parent
2a7f39e3fe
commit
366923699c
@ -0,0 +1,24 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://b1d2e7ely6hyw"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://cqc72e4hq6bcd" path="res://prefabs/interaction_area_2d.tscn" id="1_femni"]
|
||||
[ext_resource type="Script" uid="uid://bdffon388rkty" path="res://scripts/CSharp/Common/Farming/FieldBehaviour2D.cs" id="2_femni"]
|
||||
[ext_resource type="Texture2D" uid="uid://c2pirgay3jfnn" path="res://art/farm/tilable grounds/böden/trockene farming erde.png" id="3_lsfck"]
|
||||
[ext_resource type="Texture2D" uid="uid://ctvdxwgmfaj5c" path="res://art/farm/tilable grounds/böden/nasse farming erde.png" id="4_cus02"]
|
||||
[ext_resource type="PackedScene" uid="uid://bjhj1wa5olwcu" path="res://prefabs/farm/base_plant.tscn" id="5_msuq8"]
|
||||
|
||||
[node name="BaseField" type="Node2D"]
|
||||
|
||||
[node name="InteractionArea2" parent="." instance=ExtResource("1_femni")]
|
||||
|
||||
[node name="FieldBehaviour" type="Sprite2D" parent="."]
|
||||
scale = Vector2(0.4, 1e-05)
|
||||
script = ExtResource("2_femni")
|
||||
Tilled = ExtResource("3_lsfck")
|
||||
Watered = ExtResource("4_cus02")
|
||||
|
||||
[node name="BasePlant" parent="FieldBehaviour" node_paths=PackedStringArray("_seeds", "_smallPlants", "_bigPlants", "_readyPlants") instance=ExtResource("5_msuq8")]
|
||||
transform = Transform3D(0.5, 0, 0, 0, -2.18557e-08, 0.5, 0, -0.5, -2.18557e-08, 0, 0, 0)
|
||||
_seeds = [null, null, null]
|
||||
_smallPlants = [null, null, null, null]
|
||||
_bigPlants = [null, null, null, null]
|
||||
_readyPlants = [null, null, null, null]
|
||||
@ -0,0 +1,24 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://cqc72e4hq6bcd"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://ckp413wrub5fm" path="res://scripts/CSharp/Common/CharacterControls/InteractionArea2D.cs" id="1_6svbd"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_npluf"]
|
||||
radius = 100.0
|
||||
|
||||
[node name="InteractionArea" type="Node2D" node_paths=PackedStringArray("_area", "_label")]
|
||||
script = ExtResource("1_6svbd")
|
||||
_area = NodePath("Area3D")
|
||||
_label = NodePath("Label")
|
||||
|
||||
[node name="Area3D" type="Area2D" parent="."]
|
||||
collision_mask = 16
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape2D" parent="Area3D"]
|
||||
shape = SubResource("CircleShape2D_npluf")
|
||||
|
||||
[node name="Label" type="Label" parent="."]
|
||||
visible = false
|
||||
text = "[E]"
|
||||
|
||||
[connection signal="body_entered" from="Area3D" to="." method="OnPlayerEntered"]
|
||||
[connection signal="body_exited" from="Area3D" to="." method="OnPlayerExited"]
|
||||
@ -0,0 +1,33 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
|
||||
public partial class InteractionArea2D : Node2D
|
||||
{
|
||||
[Export] private Area2D _area;
|
||||
[Export] private Label _label;
|
||||
[Export] private bool _showLabel = true;
|
||||
|
||||
[Signal]
|
||||
public delegate void InteractedEventHandler();
|
||||
|
||||
public void OnPlayerEntered(Node2D player)
|
||||
{
|
||||
if(_showLabel)
|
||||
_label.Show();
|
||||
}
|
||||
|
||||
public void OnPlayerExited(Node2D player)
|
||||
{
|
||||
_label.Hide();
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (@event.IsAction("interact") && @event.IsPressed() && _area.HasOverlappingBodies())
|
||||
{
|
||||
_label.Hide();
|
||||
EmitSignal(SignalName.Interacted);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
uid://ckp413wrub5fm
|
||||
@ -0,0 +1,97 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class FarmingControls2D : Node2D
|
||||
{
|
||||
[Export] private Sprite2D _hoeSprite;
|
||||
[Export] private Sprite2D _wateringCanSprite;
|
||||
[Export] private PackedScene _fieldPrefab;
|
||||
[Export] private Node2D _movingPlayer;
|
||||
[Export] private Camera2D _camera;
|
||||
|
||||
public FieldService2D FieldParent;
|
||||
|
||||
private bool _hoeInHand = false;
|
||||
private bool _waterCanInHand = false;
|
||||
|
||||
#region Tools
|
||||
|
||||
public bool ActivateHoe(bool activate)
|
||||
{
|
||||
bool success = ActivateTool(activate, _hoeSprite);
|
||||
_hoeInHand = success;
|
||||
return success;
|
||||
}
|
||||
|
||||
public bool ActivateWateringCan(bool activate)
|
||||
{
|
||||
bool success = ActivateTool(activate, _wateringCanSprite);
|
||||
_waterCanInHand = success;
|
||||
return success;
|
||||
}
|
||||
|
||||
private bool ActivateTool(bool activate, Sprite2D tool)
|
||||
{
|
||||
tool.Visible = !activate;
|
||||
return !activate;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
Vector2 mousePosition = GetViewport().GetMousePosition();
|
||||
|
||||
Vector2I adjustedPosition = new Vector2I(AdjustValue(mousePosition.X), AdjustValue(mousePosition.Y));
|
||||
|
||||
if (@event.IsActionPressed("click") && _hoeInHand)
|
||||
{
|
||||
MakeField(adjustedPosition);
|
||||
}
|
||||
|
||||
if (@event.IsActionPressed("click") && _waterCanInHand)
|
||||
{
|
||||
WaterTheField(adjustedPosition);
|
||||
}
|
||||
}
|
||||
|
||||
private void WaterTheField(Vector2I fieldPosition)
|
||||
{
|
||||
FieldBehaviour2D field = FieldParent.Get(fieldPosition);
|
||||
if (field == null)
|
||||
return;
|
||||
|
||||
field.Water();
|
||||
}
|
||||
|
||||
private void MakeField(Vector2I fieldPosition)
|
||||
{
|
||||
if(FieldParent == null || _fieldPrefab == null)
|
||||
return;
|
||||
|
||||
// only instantiate a field if there isn't one already.
|
||||
if(FieldParent.Get(fieldPosition) == null)
|
||||
{
|
||||
Node fieldInstance = _fieldPrefab.Instantiate();
|
||||
if (fieldInstance is Node2D field2d)
|
||||
{
|
||||
// add dictionary entry for the field
|
||||
Array<Node> fields = field2d.FindChildren("*", nameof(FieldBehaviour2D));
|
||||
if (fields.Count > 0)
|
||||
FieldParent.TryAddEntry(fieldPosition, fields[0] as FieldBehaviour2D);
|
||||
|
||||
// reposition and reparent the instance
|
||||
field2d.Position = new Vector2(fieldPosition.X, fieldPosition.Y);;
|
||||
FieldParent.AddChild(fieldInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int AdjustValue(float value)
|
||||
{
|
||||
return (int) Mathf.Floor(value);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
uid://bcskt5ckh3rqa
|
||||
@ -0,0 +1,47 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class FieldBehaviour2D : Sprite2D
|
||||
{
|
||||
[Export] private Texture2D Tilled;
|
||||
[Export] private Texture2D Watered;
|
||||
[Export] public FieldState FieldState = FieldState.Empty;
|
||||
|
||||
public Vector2 FieldPosition;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Texture = Tilled;
|
||||
base._Ready();
|
||||
}
|
||||
|
||||
public void Water()
|
||||
{
|
||||
FieldState = FieldState.Watered;
|
||||
Texture = Watered;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the player enters the field'S interaction area and presses <E>.
|
||||
/// </summary>
|
||||
public void Farm()
|
||||
{
|
||||
switch (FieldState)
|
||||
{
|
||||
case FieldState.Empty:
|
||||
Texture = Tilled;
|
||||
FieldState = FieldState.Tilled;
|
||||
break;
|
||||
case FieldState.Tilled:
|
||||
FieldState = FieldState.Planted;
|
||||
break;
|
||||
case FieldState.Planted:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
uid://bdffon388rkty
|
||||
@ -0,0 +1,54 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
public partial class FieldService2D : Node2D
|
||||
{
|
||||
[Export] private Dictionary<Vector2I, FieldBehaviour2D> fields = new Dictionary<Vector2I, FieldBehaviour2D>();
|
||||
|
||||
//Create
|
||||
public bool TryAddEntry(Vector2I key, FieldBehaviour2D field)
|
||||
{
|
||||
if (!fields.ContainsKey(key))
|
||||
{
|
||||
fields.Add(key, field);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read
|
||||
public FieldBehaviour2D Get(Vector2I key)
|
||||
{
|
||||
if (fields.TryGetValue(key, out FieldBehaviour2D field))
|
||||
return field;
|
||||
return field;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
//Update
|
||||
public void UpdateEntry(Vector2I fieldPosition, FieldBehaviour2D state)
|
||||
{
|
||||
|
||||
if (fields.ContainsKey(fieldPosition))
|
||||
{
|
||||
fields[fieldPosition] = state;
|
||||
}
|
||||
else
|
||||
{
|
||||
TryAddEntry(fieldPosition, state);
|
||||
}
|
||||
}
|
||||
|
||||
//Delete
|
||||
|
||||
public void RemoveEntry(Vector2I fieldPosition)
|
||||
{
|
||||
if (fields.ContainsKey(fieldPosition))
|
||||
{
|
||||
fields.Remove(fieldPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
uid://dhxtdhfqx3bte
|
||||
@ -0,0 +1,45 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
|
||||
public partial class VesnaBehaviour2D : Node
|
||||
{
|
||||
[ExportGroup("Farming")]
|
||||
[Export] private FieldService _fieldParent;
|
||||
[Export] private FarmingControls _farmingControls;
|
||||
|
||||
[Signal] public delegate void ToolPickupEventHandler(bool success);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_farmingControls.FieldParent = _fieldParent;
|
||||
}
|
||||
|
||||
#region Farming
|
||||
|
||||
public void ActivateHoe(bool activate)
|
||||
{
|
||||
ActivateTool(activate, 0);
|
||||
}
|
||||
|
||||
public void ActivateWateringCan(bool activate)
|
||||
{
|
||||
ActivateTool(activate, 1);
|
||||
}
|
||||
|
||||
private void ActivateTool(bool activate , int toolId)
|
||||
{
|
||||
bool success = false;
|
||||
if (toolId == 0)
|
||||
{
|
||||
success = _farmingControls.ActivateHoe(activate);
|
||||
}
|
||||
else if (toolId == 1)
|
||||
{
|
||||
success = _farmingControls.ActivateWateringCan(activate);
|
||||
}
|
||||
EmitSignal(SignalName.ToolPickup, success);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
uid://b05uyj001ehwi
|
||||
Loading…
Reference in new issue