Esempio n. 1
0
    public override void _Ready()
    {
        PackedScene obstacle = (PackedScene)ResourceLoader.Load("res://Obstacle.tscn");
        PackedScene player   = (PackedScene)ResourceLoader.Load("res://player.tscn");

        tileSize     = this.GetCellSize();
        halfTileSize = tileSize / 2;
        gridSize     = new Vector2(16, 16);
        grid         = new int?[(int)gridSize.x, (int)gridSize.y];
        random       = new Random();

        player Player = (player)player.Instance();

        Player.SetPosition(MapToWorld(new Vector2(4, 4)) + halfTileSize);
        AddChild(Player);

        Vector2[] position = new Vector2[5];

        for (int i = 0; i < 5; i++)
        {
            Vector2 gridPos = new Vector2(random.Next() % (int)gridSize.x, random.Next() % (int)gridSize.y);
            if (!Array.Exists(position, element => element == gridPos))
            {
                position[i] = gridPos;
            }
            else
            {
                i--;
            }
        }

        int obstAmount = 0;

        foreach (var pos in position)
        {
            KinematicBody2D newObstacle = (KinematicBody2D)obstacle.Instance();
            newObstacle.SetPosition(MapToWorld(pos) + halfTileSize);
            grid[(int)pos.x, (int)pos.y] = (int)ENTITY_TYPES.OBSTACLE;
            AddChild(newObstacle);
            obstAmount++;
        }


        SetProcess(true);
        // Called every time the node is added to the scene.
        // Initialization here
    }