WIP 2D rework

remotes/checkIfPRContentChanged-1749880758504311652/farming_mechanic
kziolkowski 8 months ago
parent 2a7f39e3fe
commit 366923699c

@ -1,6 +1,7 @@
[gd_scene load_steps=292 format=3 uid="uid://c25udixd5m6l0"] [gd_scene load_steps=297 format=3 uid="uid://c25udixd5m6l0"]
[ext_resource type="Script" uid="uid://cjbclkxesh3hc" path="res://scripts/CSharp/Common/CharacterControls/Player2D.cs" id="1_ssqtd"] [ext_resource type="Script" uid="uid://cjbclkxesh3hc" path="res://scripts/CSharp/Common/CharacterControls/Player2D.cs" id="1_ssqtd"]
[ext_resource type="Script" uid="uid://b05uyj001ehwi" path="res://scripts/CSharp/Common/Farming/VesnaBehaviour2D.cs" id="1_yl84k"]
[ext_resource type="Texture2D" uid="uid://dygkh117flcou" path="res://art/animation/vesna/Front/F01-Idle/0001.png" id="6_kes6b"] [ext_resource type="Texture2D" uid="uid://dygkh117flcou" path="res://art/animation/vesna/Front/F01-Idle/0001.png" id="6_kes6b"]
[ext_resource type="Texture2D" uid="uid://rwqptk0hk53" path="res://art/animation/vesna/Front/F01-Idle/0002.png" id="7_wukwp"] [ext_resource type="Texture2D" uid="uid://rwqptk0hk53" path="res://art/animation/vesna/Front/F01-Idle/0002.png" id="7_wukwp"]
[ext_resource type="Texture2D" uid="uid://c5om11bu7mtvh" path="res://art/animation/vesna/Front/F01-Idle/0003.png" id="8_wvm58"] [ext_resource type="Texture2D" uid="uid://c5om11bu7mtvh" path="res://art/animation/vesna/Front/F01-Idle/0003.png" id="8_wvm58"]
@ -286,9 +287,13 @@
[ext_resource type="Texture2D" uid="uid://j4mb1261cx0s" path="res://art/animation/vesna/Back/B02-Walk/0043.png" id="288_jhda8"] [ext_resource type="Texture2D" uid="uid://j4mb1261cx0s" path="res://art/animation/vesna/Back/B02-Walk/0043.png" id="288_jhda8"]
[ext_resource type="Texture2D" uid="uid://cj4aaeif3xnpw" path="res://art/animation/vesna/Back/B02-Walk/0044.png" id="289_btd43"] [ext_resource type="Texture2D" uid="uid://cj4aaeif3xnpw" path="res://art/animation/vesna/Back/B02-Walk/0044.png" id="289_btd43"]
[ext_resource type="Texture2D" uid="uid://djeqa5o26lcvj" path="res://art/animation/vesna/Back/B02-Walk/0045.png" id="290_0oasu"] [ext_resource type="Texture2D" uid="uid://djeqa5o26lcvj" path="res://art/animation/vesna/Back/B02-Walk/0045.png" id="290_0oasu"]
[ext_resource type="Script" uid="uid://bcskt5ckh3rqa" path="res://scripts/CSharp/Common/Farming/FarmingControls2D.cs" id="291_31q85"]
[ext_resource type="Texture2D" uid="uid://c0nhh610veynv" path="res://art/animation/vesna/Back/B02-Walk/0046.png" id="291_xtrl3"] [ext_resource type="Texture2D" uid="uid://c0nhh610veynv" path="res://art/animation/vesna/Back/B02-Walk/0046.png" id="291_xtrl3"]
[ext_resource type="Texture2D" uid="uid://b206kb5kggejq" path="res://art/animation/vesna/Back/B02-Walk/0047.png" id="292_7uo3r"] [ext_resource type="Texture2D" uid="uid://b206kb5kggejq" path="res://art/animation/vesna/Back/B02-Walk/0047.png" id="292_7uo3r"]
[ext_resource type="Texture2D" uid="uid://3pj2q7wtuion" path="res://art/farm/farming/farmobjekte/hoe.png" id="292_28qer"]
[ext_resource type="Texture2D" uid="uid://ci7uer5wecm2a" path="res://art/animation/vesna/Back/B02-Walk/0048.png" id="293_ltqas"] [ext_resource type="Texture2D" uid="uid://ci7uer5wecm2a" path="res://art/animation/vesna/Back/B02-Walk/0048.png" id="293_ltqas"]
[ext_resource type="Texture2D" uid="uid://x8hr8287ff2n" path="res://art/farm/farming/farmobjekte/tools atlas.png" id="293_tyqby"]
[ext_resource type="PackedScene" uid="uid://b1d2e7ely6hyw" path="res://prefabs/farm/base_field_2d.tscn" id="294_28qer"]
[sub_resource type="CircleShape2D" id="CircleShape2D_ssqtd"] [sub_resource type="CircleShape2D" id="CircleShape2D_ssqtd"]
radius = 167.672 radius = 167.672
@ -1192,6 +1197,7 @@ animations = [{
[node name="Player2d" type="Node2D"] [node name="Player2d" type="Node2D"]
y_sort_enabled = true y_sort_enabled = true
script = ExtResource("1_yl84k")
[node name="CharacterBody2D" type="CharacterBody2D" parent="." node_paths=PackedStringArray("_sprite")] [node name="CharacterBody2D" type="CharacterBody2D" parent="." node_paths=PackedStringArray("_sprite")]
position = Vector2(0, 374) position = Vector2(0, 374)
@ -1215,6 +1221,31 @@ sprite_frames = SubResource("SpriteFrames_4yiyq")
animation = &"side_walking" animation = &"side_walking"
offset = Vector2(0, -450) offset = Vector2(0, -450)
[node name="Hoe" type="Sprite2D" parent="CharacterBody2D/visuals"]
visible = false
position = Vector2(-35, -596)
rotation = 0.74351
scale = Vector2(0.24, 0.24)
texture = ExtResource("292_28qer")
region_enabled = true
region_rect = Rect2(980, 179, 374, 1233)
[node name="WateringCan" type="Sprite2D" parent="CharacterBody2D/visuals"]
visible = false
position = Vector2(-38, -603)
scale = Vector2(0.5, 0.5)
texture = ExtResource("293_tyqby")
region_enabled = true
region_rect = Rect2(-1, 1128, 417, 299)
[node name="Camera2D" type="Camera2D" parent="CharacterBody2D"] [node name="Camera2D" type="Camera2D" parent="CharacterBody2D"]
position = Vector2(26, -469) position = Vector2(26, -469)
zoom = Vector2(0.3, 0.3) zoom = Vector2(0.3, 0.3)
[node name="FarmingControls" type="Node2D" parent="." node_paths=PackedStringArray("_hoeSprite", "_wateringCanSprite", "_movingPlayer", "_camera")]
script = ExtResource("291_31q85")
_hoeSprite = NodePath("../CharacterBody2D/visuals/Hoe")
_wateringCanSprite = NodePath("../CharacterBody2D/visuals/WateringCan")
_fieldPrefab = ExtResource("294_28qer")
_movingPlayer = NodePath("../CharacterBody2D")
_camera = NodePath("../CharacterBody2D/Camera2D")

@ -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);
}
}
}

@ -6,8 +6,8 @@ namespace Babushka.scripts.CSharp.Common.CharacterControls;
public partial class VesnaBehaviour : Node public partial class VesnaBehaviour : Node
{ {
[ExportGroup("Farming")] [ExportGroup("Farming")]
[Export] private FieldService _fieldParent; [Export] private FieldService2D _fieldParent;
[Export] private FarmingControls _farmingControls; [Export] private FarmingControls2D _farmingControls;
[Signal] public delegate void ToolPickupEventHandler(bool success); [Signal] public delegate void ToolPickupEventHandler(bool success);

@ -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,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,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,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
}
Loading…
Cancel
Save