example-grass/Eat Da Rich/Pig/Pig.cs

73 lines
1.7 KiB
C#
Raw Normal View History

2025-07-20 11:22:53 +00:00
using Godot;
using System.Collections;
using System.Collections.Generic;
using Godot.Collections;
namespace Rokojori
{
[GlobalClass,Tool]
public partial class Pig:Node
{
[Export]
public RigidBody3D rigidBody3D;
[Export]
public float speed = 5;
2025-07-22 14:08:35 +00:00
[Export]
public Smoothing rotationSmoothing;
[Export]
public Action onSpawn;
[Export]
public bool alive = true;
2025-07-25 08:13:54 +00:00
[Export]
public PackedScene packedBullet;
public void Shoot( Node3D bulletsContainer )
{
var bulletRoot = packedBullet.Instantiate<AnimatableBody3D>();
bulletsContainer.AddChild( bulletRoot );
bulletRoot.GlobalPosition = rigidBody3D.GlobalPosition;
var bullet = bulletRoot.Get<Bullet>();
Action.Trigger( bullet.onActivate );
var moveTowards = bulletRoot.Get<MoveTowards>();
moveTowards.goal = Unique<Player>.Get().transformSource;
}
2025-07-20 11:22:53 +00:00
public override void _Process( double delta )
{
2025-07-22 14:08:35 +00:00
if ( Engine.IsEditorHint() )
2025-07-20 11:22:53 +00:00
{
2025-07-22 14:08:35 +00:00
return;
2025-07-20 11:22:53 +00:00
}
2025-07-22 14:08:35 +00:00
var player = Unique<Player>.Get();
2025-07-20 11:22:53 +00:00
if ( player == null )
{
return;
}
2025-07-22 14:08:35 +00:00
2025-07-20 11:22:53 +00:00
var direction = player.GlobalPosition - rigidBody3D.GlobalPosition;
direction.Y = 0;
2025-07-25 08:13:54 +00:00
direction = direction.Normalized() * speed * 60 * (float)delta;
2025-07-20 11:22:53 +00:00
rigidBody3D.GlobalPosition += direction;
2025-07-22 14:08:35 +00:00
2025-07-20 11:22:53 +00:00
rigidBody3D.LookAt( player.GlobalPosition );
2025-07-22 14:08:35 +00:00
var nextRotation = rigidBody3D.GetGlobalQuaternion();
var rotation = Smoothing.Apply( rotationSmoothing, nextRotation, (float)delta );
rigidBody3D.SetGlobalQuaternion( rotation );
2025-07-20 11:22:53 +00:00
}
2025-07-22 14:08:35 +00:00
2025-07-20 11:22:53 +00:00
}
}