Exemple #1
0
    public override void _PhysicsProcess(float delta)
    {
        if (PlayerState.Is(PlayerState.State.Pause))
        {
            return;
        }

        if (dead)
        {
            return;
        }
        time += delta;
        if (time >= LIFETIME)
        {
            Explosion();
            dead = true;
            Delay.StartDelay(this, 0.3f, () => QueueFree());
        }


        /*Teleportation loots*/
        Vector2 p             = GetViewportTransform().origin *CurrentCamera.GetXZoom();
        int     viewportSizeX = Mathf.FloorToInt(GetViewport().Size.x *CurrentCamera.GetXZoom());
        Vector2 vecMin        = Convertion.Location2World(p) * -1;
        Vector2 vecMax        = Convertion.Location2World(new Vector2(p.x * -1 + viewportSizeX, p.y));

        if (vecMin.x < 0)
        {
            if (!mirrored)
            {
                int i = (int)Mathf.Abs(vecMin.x / Chunk.size) + 1;
                if (Convertion.Location2World(Position).x >= (World.size - i) * Chunk.size)
                {
                    Position = Position - new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0);
                    mirrored = true;
                }
            }
            else if (-vecMin.x + prev_x_viewport >= 0.90f * World.size * Chunk.size)
            {
                int i = (int)Mathf.Abs(vecMin.x / Chunk.size) + 1;
                if (Convertion.Location2World(Position).x >= (World.size - i) * Chunk.size)
                {
                    Position = Position - new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0);
                    mirrored = false;
                }
            }
        }
        else if (vecMax.x >= World.size * Chunk.size)
        {
            if (!mirrored)
            {
                int i = (int)Mathf.Abs((vecMax.x - World.size * Chunk.size) / Chunk.size) + 1;
                if (Convertion.Location2World(Position).x <= i * Chunk.size)
                {
                    Position = Position + new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0);
                    mirrored = true;
                }
            }
            else if (vecMin.x - prev_x_viewport >= 0.90f * World.size * Chunk.size)
            {
                int i = (int)Mathf.Abs((vecMax.x - World.size * Chunk.size) / Chunk.size) + 1;
                if (Convertion.Location2World(Position).x <= i * Chunk.size)
                {
                    Position = Position + new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0);
                    mirrored = false;
                }
            }
        }
        else if (vecMax.x < World.size * Chunk.size && vecMin.x >= 0)
        {
            if (mirrored)
            {
                if (Convertion.Location2World(Position).x < 0)
                {
                    Position = Position + new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0);
                }
                else
                {
                    Position = Position - new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0);
                }

                mirrored = false;
            }
        }
        prev_x_viewport = vecMin.x;
        /*----------------------*/

        var    bodies = area.GetOverlappingBodies();
        Node2D Player = null;

        foreach (var body in bodies)
        {
            if (((Node2D)body).GetGroups().Contains("Player"))
            {
                Player = (Node2D)body;
            }
        }
        Node2D NearestLoot = null;
        var    areas       = area.GetOverlappingAreas();

        foreach (var a in areas)
        {
            Node2D n = (Node2D)a;
            if (n.GetGroups().Contains("loot") && n.GetNode <Loot>("..") != this)
            {
                Loot l = n.GetNode <Loot>("..");
                if (l.GetLootType() == GetLootType() && GetLootAmount() < STACKSIZE && l.GetLootAmount() < STACKSIZE)
                {
                    NearestLoot = n.GetNode <Loot>("..");
                    break;
                }
            }
        }

        if (Player != null)
        {
            Vector2 vec = Player.Position - Position;
            Position = Position + (vec.Normalized() * speed);
        }
        else if (NearestLoot != null)
        {
            Vector2 vec = NearestLoot.Position - Position;
            Position = Position + (vec.Normalized() * 4.0f);
        }
        else
        {
            Position = new Vector2(Position.x, Position.y + Mathf.Sin((float)OS.GetTicksMsec() * 0.004f + dephase) * 0.2f);
        }
    }