Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
3.0 kB
5
Indexable
Never
using Godot;
using System;
using System.Threading.Tasks;

public class Enemy : Node2D
{
	// Declare member variables here. Examples:
	// private int a = 2;
	// private string b = "text";

	PlayerRoot plr;
	KinematicBody2D enemy;
	private Vector2 origPos;
	[Export] private PackedScene scene_SlashEffect;
	[Export] private PackedScene scene_Laser;
	private int health = 3;
	private lvl0 lvl;
	private Line2D pointer;
	private Particles2D particles2D;
	private ulong lastTick = Time.GetTicksMsec();

	CameraController camera;

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		plr = GetNode<PlayerRoot>("../PlayerRoot");
		enemy = GetNode<KinematicBody2D>("EnemyBody");
		pointer = GetNode<Line2D>("Pointer");
		particles2D = GetNode<Particles2D>("Particles2D");
		camera = GetNode<CameraController>("../Camera2D");
		lvl = GetNode<lvl0>("..");
		origPos = Position;
	}

    public override void _PhysicsProcess(float delta)
    {
        ulong currentTime = Time.GetTicksMsec();
		if (currentTime - lastTick > 3000f)
		{
			Charge();
			lastTick = currentTime;
		}
    }

	private async void CreateLaser(Vector2 position)
	{
		Line2D Laser = (Line2D)scene_Laser.Instance();
		AddChild(Laser);
		Tween laserTween = Laser.GetNode<Tween>("Tween");

		Laser.Width = 0;
		Laser.SetPointPosition(0, new Vector2(0,0));
		Laser.SetPointPosition(1, ToLocal(position) * 100f);

		laserTween.InterpolateProperty(Laser, "width", 0, 40, 0.25f, Tween.TransitionType.Back, Tween.EaseType.Out);
		laserTween.InterpolateProperty(Laser, "width", 40, 0, 0.15f, Tween.TransitionType.Sine, Tween.EaseType.Out, 0.25f);
		
		laserTween.Start();

		await ToSignal(GetTree().CreateTimer(0.4f), "timeout");;

		Laser.Free();
	}

	private async void Charge()
	{
		Vector2 playerGlobalPos = plr.player.GlobalPosition;
		particles2D.Visible = true;
		GD.Print(ToLocal(plr.player.Position));
		pointer.SetPointPosition(1, ToLocal(playerGlobalPos) * 100f);
		pointer.Visible = true;

		await ToSignal(GetTree().CreateTimer(1), "timeout");

		var spaceState = GetWorld2d().DirectSpaceState;
		var result = spaceState.IntersectRay(GlobalPosition, playerGlobalPos,  new Godot.Collections.Array { this }, 1, true, true);

		camera.AddTrauma(2f, 0.15f);
		CreateLaser(playerGlobalPos);

		GD.Print(result);

		if (result.Count >= 1)
		{
			if (result["collider"] is Area2D)
			{
				Area2D collider = (Area2D)result["collider"]; // Line #86
				GD.Print(collider.GetParent().Name);
				camera.AddTrauma(2f, 0.15f);
				GD.Print("COLLIDED");
			}
		}

		particles2D.Visible = false;
		pointer.Visible = false;
	}

	public void Die()
	{
		lvl.enemyCount--;
		QueueFree();
	}

	public void CreateSlash()
	{
		Node2D slashEffecet = (Node2D)scene_SlashEffect.Instance();
		slashEffecet.Position = Position;

		GetNode<Node2D>("..").AddChild(slashEffecet);
	}

	public void Damage()
	{
		GD.Print("Damaged");

		health--;

		CreateSlash();

		if (health <= 0)
		{
			Die();
		}
	}
}