Valks-Games / AvoidTheEnemies

Lets make a game like Vampire Survivors and Halls of Torment!
MIT License
5 stars 1 forks source link

Shooting multiple lasers side by side #6

Open valkyrienyanko opened 1 year ago

valkyrienyanko commented 1 year ago

The player currently shoots a laser every second.

I want to make it so there are upgrades that increases how many lasers the players fire at once.

Untitled

So 2 lasers would fire a little bit to the left of the enemy targets direction and a little bit to the right. And so on. See picture above for what I mean.

Currently this is the laser code.

Notice how the target is set so the laser can calculate its direction. Well this is sort of a problem because how are we going to define lasers at varying angles.

// Player.cs
void ShootLaser()
{
    if (Level.Enemies.Count == 0)
        return;

    for (int i = 0; i < NumLasers; i++)
    {
        var laser = (Projectile)Prefabs.LaserBlue.Instantiate();
        laser.Position = Position;
        laser.Target = GetClosestEnemy().Position;
        GetTree().Root.AddChild(laser);
    }
}
// Projectile.cs
public partial class Projectile : Node2D
{
    public Vector2 Target { get; set; }

    GTimer timerDestroy;
    Vector2 dir;

    public override void _Ready()
    {
        timerDestroy = new(this, 5000);
        timerDestroy.Finished += QueueFree;
        timerDestroy.Start();

        dir = (Target - Position).Normalized();
        Rotation = dir.Angle() + Mathf.Pi / 2;

        GetNode<Area2D>("Area2D").BodyEntered += body =>
        {
            if (body is Blob enemy)
            {
                Level.Enemies.Remove(enemy.GetInstanceId());
                enemy.Kill();
            }
        };
    }

    public override void _PhysicsProcess(double delta)
    {
        var speed = 10;
        Position += dir * speed;
    }
}