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

73 lines
1.7 KiB
C#

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;
[Export]
public Smoothing rotationSmoothing;
[Export]
public Action onSpawn;
[Export]
public bool alive = true;
[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;
}
public override void _Process( double delta )
{
if ( Engine.IsEditorHint() )
{
return;
}
var player = Unique<Player>.Get();
if ( player == null )
{
return;
}
var direction = player.GlobalPosition - rigidBody3D.GlobalPosition;
direction.Y = 0;
direction = direction.Normalized() * speed * 60 * (float)delta;
rigidBody3D.GlobalPosition += direction;
rigidBody3D.LookAt( player.GlobalPosition );
var nextRotation = rigidBody3D.GetGlobalQuaternion();
var rotation = Smoothing.Apply( rotationSmoothing, nextRotation, (float)delta );
rigidBody3D.SetGlobalQuaternion( rotation );
}
}
}