Initial commit

This commit is contained in:
2023-09-14 10:29:52 +03:00
commit fb0930c348
25 changed files with 463 additions and 0 deletions

28
Scripts/Arrow.cs Normal file
View File

@@ -0,0 +1,28 @@
using Godot;
using System;
public partial class Arrow : RigidBody2D
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public override void _PhysicsProcess(double delta)
{
base._PhysicsProcess(delta);
Godot.Collections.Array<Node2D> node2Ds = GetCollidingBodies();
if (node2Ds.Count == 0)
return;
foreach (var node2D in node2Ds)
node2D.QueueFree();
QueueFree();
}
}

53
Scripts/ArrowThrower.cs Normal file
View File

@@ -0,0 +1,53 @@
using System;
using Godot;
public partial class ArrowThrower : Node2D
{
[Export] public Node2D ArrowsNode;
private bool isAiming = false;
private Vector2 initialPosition;
private PackedScene arrowRef = null;
private RigidBody2D arrow = null;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
arrowRef = GD.Load<PackedScene>("res://Scenes/Entities/arrow.tscn");
MouseInput mouseInput = FindParent("Mouse Input") as MouseInput;
mouseInput.OnMouseDown += OnMouseDown;
mouseInput.OnMouseUp += OnMouseUp;
}
private void OnMouseUp()
{
isAiming = false;
Vector2 direction = (initialPosition - GetGlobalMousePosition()).Normalized();
Vector2 vector2 = GetGlobalMousePosition() - initialPosition;
arrow.Rotation = Mathf.Atan2(-vector2.X, vector2.Y);
arrow.LinearVelocity = direction * 500f;
}
private void OnMouseDown()
{
isAiming = true;
initialPosition = GetGlobalMousePosition();
arrow = arrowRef.Instantiate() as RigidBody2D;
ArrowsNode.AddChild(arrow);
arrow.GlobalPosition = initialPosition;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (!isAiming)
return;
Vector2 vector2 = GetGlobalMousePosition() - initialPosition;
arrow.Rotation = Mathf.Atan2(-vector2.X, vector2.Y);
}
}

15
Scripts/Candy.cs Normal file
View File

@@ -0,0 +1,15 @@
using Godot;
using System;
public partial class Candy : Node2D
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
}

22
Scripts/MouseInput.cs Normal file
View File

@@ -0,0 +1,22 @@
using Godot;
using System;
public partial class MouseInput : Node
{
public Action OnMouseDown = null;
public Action OnMouseUp = null;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("Mouse Left"))
OnMouseDown?.Invoke();
if (Input.IsActionJustReleased("Mouse Left"))
OnMouseUp?.Invoke();
}
}

31
Scripts/MoveUpDown.cs Normal file
View File

@@ -0,0 +1,31 @@
using System;
using Godot;
public partial class MoveUpDown : RigidBody2D
{
[Export] public Node2D Up;
[Export] public Node2D Down;
[Export] private double speed = 1f;
private double t = 0f;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
t += delta * speed;
t = t % 2f;
double posT = Math.Abs(t - 1f);
MoveAndCollide(new Vector2(
(float)Mathf.Lerp(Up.Position.X, Down.Position.X, posT),
(float)Mathf.Lerp(Up.Position.Y, Down.Position.Y, posT)
) - Position);
}
}

45
Scripts/Pillow.cs Normal file
View File

@@ -0,0 +1,45 @@
using Godot;
using System;
public partial class Pillow : RigidBody2D
{
[Export] public float angularSpeed = 1f;
[Export] public Vector2 velocity = new Vector2(1, 0);
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
Rotation = (float)Random.Shared.NextDouble();
AngularVelocity = ((float)Random.Shared.NextDouble() + .5f) * angularSpeed;
LinearVelocity = velocity;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public override void _PhysicsProcess(double delta)
{
base._PhysicsProcess(delta);
Godot.Collections.Array<Node2D> node2Ds = GetCollidingBodies();
Node2D candy = null;
foreach (var node2D in node2Ds)
{
if (node2D.Name != "Candy")
continue;
candy = node2D;
break;
}
if (candy == null)
return;
candy.Visible = false;
Visible = false;
}
}

35
Scripts/ThrowPillow.cs Normal file
View File

@@ -0,0 +1,35 @@
using Godot;
public partial class ThrowPillow : Node2D
{
[Export] public PackedScene pillow;
[Export] public Node pillowsNode;
[Export] public float RPM = 30f;
private float countdown = 0f;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
pillow = GD.Load<PackedScene>("res://Scenes/Entities/pillow.tscn");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
countdown -= (float)delta;
if (countdown > 0f)
return;
ShootPillow();
countdown = 60f / RPM;
}
private void ShootPillow()
{
var instance = pillow.Instantiate<Node2D>();
instance.GlobalPosition = GlobalPosition;
pillowsNode.AddChild(instance);
}
}