Exemplo n.º 1
0
 public override void _Ready()
 {
     linesRoot = GetChild <Node2D>(0);
     lines[0]  = linesRoot.GetChild <Line2D>(0);
     lines[1]  = linesRoot.GetChild <Line2D>(1);
     lines[2]  = linesRoot.GetChild <Line2D>(2);
 }
Exemplo n.º 2
0
    public void IncreaseBodySize()
    {
        Sprite newBodyBlock = (Sprite)_sprite.Duplicate();

        newBodyBlock.Position = ((Node2D)_body.GetChild(_body.GetChildCount() - 1)).Position;

        _body.AddChild(newBodyBlock);
    }
Exemplo n.º 3
0
        public List <PathNode> GetNeighbors(PathNode node)
        {
            List <PathNode> neighbors = new List <PathNode>();

            for (int x = -world.TileSize; x <= world.TileSize; x += world.TileSize)
            {
                for (int y = -world.TileSize; y <= world.TileSize; y += world.TileSize)
                {
                    if (x == 0 && y == 0)
                    {
                        continue;
                    }

                    int checkX = node.coord.x + x;
                    int checkY = node.coord.y + y;

                    //Console.WriteLine($"Pathfinding: checkX,checkY {checkX},{checkY}");

                    if (checkX >= -world.XLimit && checkX < world.XLimit && checkY >= -world.YLimit && checkY < world.YLimit)
                    {
                        if (!grid.ContainsKey($"{checkX}, {checkY}"))
                        {
                            //Console.WriteLine($"Pathfinding: grid doesn't contain check");
                            PathNode pathNode = PathNode.PathNodeFromPosition(new Coord(checkX, checkY), world);
                            neighbors.Add(pathNode);
                            grid[$"{checkX}, {checkY}"] = pathNode;

                            if (visualizeSearch)
                            {
                                Node2D viz = GetNode <Node2D>(new NodePath($"{checkX}, {checkY}"));
                                if (viz != null)
                                {
                                    viz.GetChild <Sprite>(0).SetModulate(new Color(0, 1, 0, 0.5f));
                                }
                                else
                                {
                                    viz = (Node2D)pathNodeVisScene.Instance();
                                    AddChild(viz);
                                    viz.GetChild <Sprite>(0).SetModulate(new Color(0, 1, 0, 0.5f));
                                    viz.SetName($"{checkX}, {checkY}");
                                    viz.SetPosition(pathNode.worldPosition);
                                    pathVis.Add(viz);
                                }
                            }
                        }
                        else
                        {
                            //Console.WriteLine($"Pathfinding: grid already contains neighbor");
                            neighbors.Add(grid[$"{checkX}, {checkY}"]);
                        }
                    }
                }
            }
            //Console.WriteLine($"Pathfinding: neighbors count: {neighbors.Count}");
            return(neighbors);
        }
Exemplo n.º 4
0
 // Called when the node enters the scene tree for the first time.
 public override void _Ready()
 {
     for (int i = 0; i < NumWaves; i++)
     {
         PackedScene scene = (PackedScene)ResourceLoader.Load("res://Scenes/Wave" + i + ".tscn");
         Node2D      root  = (Node2D)scene.Instance();
         this.AddChild(root);
         Waves.Add(new WaveData {
             Spawns = new SpawnData[root.GetChildCount()]
         });
         int longest = 0;
         for (int idx = 0; idx < root.GetChildCount(); idx++)
         {
             SpawnData data = (SpawnData)root.GetChild(idx);
             if (data.SpawnTime >= longest)
             {
                 longest = data.SpawnTime;
             }
             Waves[i].Spawns[idx] = new SpawnData(data);
         }
         Waves[i].Length = longest;
     }
     score = 0;
     LoadHighScore();
 }
Exemplo n.º 5
0
    public void powerup_remover()
    {
        Node2D all_powerups = (Node2D)GetNode("Powerups");

        for (int i = 0; i < all_powerups.GetChildCount(); i += 1)
        {
            all_powerups.GetChild(i).QueueFree();
        }
    }
Exemplo n.º 6
0
    public void remove_bunny()
    {
        var childCount = bunnies.GetChildCount();

        if (childCount != 0)
        {
            var bunny = bunnies.GetChild(childCount - 1);
            bunnies.RemoveChild(bunny);
            bunny.QueueFree();
        }
    }
Exemplo n.º 7
0
    public override void _Ready()
    {
        if (!Engine.EditorHint)
        {
            GetNode <Label>("NameLabel").QueueFree();
        }

        if (HasNode("Machine"))
        {
            Machine = GetNode <Home>("Machine");
        }

        playerSpawn = GetNode <Position2D>("PlayerSpawn");

        Node2D spawnersNode = GetNode <Node2D>("Spawners");

        spawners = new Spawner[spawnersNode.GetChildCount()];
        for (int i = 0; i < spawners.Length; i++)
        {
            RayCast2D ray = spawnersNode.GetChild <RayCast2D>(i);
            spawners[i].FacingDir = (int)ray.Scale.x;
            spawners[i].Position  = ray.GlobalPosition;
        }

        Node2D dropPlacesNode = GetNode <Node2D>("DropPlaces");

        dropPlaces = new Vector2[dropPlacesNode.GetChildCount()];
        for (int i = 0; i < dropPlaces.Length; i++)
        {
            Position2D dropPlace = dropPlacesNode.GetChild <Position2D>(i);
            dropPlaces[i] = dropPlace.GlobalPosition;
        }

        Node2D doorsNode = GetNode <Node2D>("Doors");

        doors = new Door[doorsNode.GetChildCount()];
        for (int i = 0; i < doors.Length; i++)
        {
            doors[i] = doorsNode.GetChild <Door>(i);
        }

        transitions = GetNode <Node2D>("Transitions");

        if (!HasNode("ScriptSequence"))
        {
            return;
        }

        stageScript = GetNode <StageScript>("ScriptSequence");
        stageScript.BuildFor(this);
    }
Exemplo n.º 8
0
    public void remove_bunny()
    {
        var childCount = bunnies.GetChildCount();

        if (childCount == 0)
        {
            return;
        }

        var bunny = bunnies.GetChild(childCount - 1);

        speeds.RemoveAt(childCount - 1);
        bunnies.RemoveChild(bunny);
    }
Exemplo n.º 9
0
    public void SetTransitionsLock(bool lockState)
    {
        for (int i = 0; i < transitions.GetChildCount(); i++)
        {
            transitions.GetChild <StageTransition>(i).Locked = lockState;
        }

        for (int i = 0; i < doors.Length; i++)
        {
            if (!lockState)
            {
                doors[i].Open();
            }
            else
            {
                doors[i].Close();
            }
        }
    }
Exemplo n.º 10
0
        private PathNode[] FindLocalPath(Coord startPos, Coord targetPos)
        {
            //Console.WriteLine($"Pathfinding: finding path start: ({startPos.x},{startPos.y}) to ({targetPos.x},{targetPos.y})");
            for (int i = pathVis.Count - 1; i >= 0; i--)
            {
                pathVis[i].GetChild <Sprite>(0).SetModulate(new Color(0.0f, 0, 0, 0.0f));
            }
            //pathVis = new List<Node2D>();

            Stopwatch sw = new Stopwatch();

            sw.Start();

            PathNode[] waypoints   = new PathNode[0];
            bool       pathSuccess = false;

            PathNode startNode  = PathNode.PathNodeFromPosition(startPos, world);
            PathNode targetNode = PathNode.PathNodeFromPosition(targetPos, world);

            startNode.coord  = startPos;
            targetNode.coord = targetPos;

            //Save Nodes to Grid
            grid[$"{startPos.x}, {startPos.y}"]   = startNode;
            grid[$"{targetPos.x}, {targetPos.y}"] = targetNode;

            if (visualizeSearch)
            {
                //startNode vis
                Node2D viz = GetNode <Node2D>(new NodePath($"{startPos.x}, {startPos.y}"));
                if (viz != null)
                {
                    viz.GetChild <Sprite>(0).SetModulate(new Color(0, 1, 0, 0.5f));
                }
                else
                {
                    viz = (Node2D)pathNodeVisScene.Instance();
                    AddChild(viz);
                    viz.GetChild <Sprite>(0).SetModulate(new Color(0, 1, 0, 0.5f));
                    viz.SetName($"{startPos.x}, {startPos.y}");
                    viz.SetPosition(startNode.worldPosition);
                    pathVis.Add(viz);
                }

                //targetNode vis
                Node2D targetViz = GetNode <Node2D>(new NodePath($"{targetPos.x}, {targetPos.y}"));
                if (targetNode != null)
                {
                    targetViz.GetChild <Sprite>(0).SetModulate(new Color(.5f, 0, 1));
                }
                else
                {
                    targetViz = (Node2D)pathNodeVisScene.Instance();
                    targetViz.GetChild <Sprite>(0).SetModulate(new Color(.5f, 0, 1));
                    AddChild(targetViz);
                    targetViz.SetName($"{targetPos.x}, {targetPos.y}");
                    targetViz.SetPosition(targetNode.worldPosition);
                    pathVis.Add(targetViz);
                }
            }



            //Console.WriteLine($"startNode speed: {startNode.moveSpeed}, targetNode speed: {targetNode.moveSpeed}");

            if (startNode.moveSpeed > 0 && targetNode.moveSpeed > 0)
            {
                Heap <PathNode>    openSet   = new Heap <PathNode>(maxLocalSize);
                HashSet <PathNode> closedSet = new HashSet <PathNode>();
                openSet.Add(startNode);



                while (openSet.Count > 0)
                {
                    //Console.WriteLine($"Pathfinding: looking...{closedSet.Count}, {openSet.Count}");
                    PathNode currentNode = openSet.RemoveFirst();

                    closedSet.Add(currentNode);

                    if (visualizeSearch)
                    {
                        //change node color
                        Node2D viz = GetNode <Node2D>(new NodePath($"{currentNode.coord.x}, {currentNode.coord.y}"));
                        viz.GetChild <Sprite>(0).SetModulate(new Color(0.2f, 0, 0, 0.5f));
                    }


                    //Console.WriteLine($"Pathfinding: current node fCost ({currentNode.fCost}) hCost ({currentNode.hCost}) gCost ({currentNode.gCost}), pos ({currentNode.coord.x},{currentNode.coord.y})");

                    if (currentNode.Equals(targetNode))
                    {
                        sw.Stop();
                        //Console.WriteLine("Path found:" + sw.ElapsedMilliseconds + " ms");
                        pathSuccess = true;
                        break;
                    }

                    foreach (PathNode neighbor in GetNeighbors(currentNode))
                    {
                        if (neighbor.moveSpeed == 0 || closedSet.Contains(neighbor))
                        {
                            //Console.WriteLine($"Pathfinding: skip neighbor cost: {neighbor.moveSpeed}");
                            continue;
                        }

                        float newMovementCostToNeighbor;

                        if (currentNode.biome == TileType.Water && neighbor.biome == TileType.Water)
                        {
                            newMovementCostToNeighbor = currentNode.gCost + (GetDistance(currentNode, neighbor)) / (currentNode.moveSpeed);
                        }
                        else
                        {
                            newMovementCostToNeighbor = currentNode.gCost + (GetDistance(currentNode, neighbor) + (Mathf.Abs(currentNode.elev - neighbor.elev) * world.ElevChangeCost * world.TileSize)) / (currentNode.moveSpeed);
                        }
                        //Console.WriteLine($"Pathfinding: neighbor cost: {newMovementCostToNeighbor}");
                        if (newMovementCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor))
                        {
                            gCosts[$"({neighbor.coord.x},{neighbor.coord.y})-({startNode.coord.x},{startNode.coord.y})"] = newMovementCostToNeighbor;
                            neighbor.gCost = newMovementCostToNeighbor;
                            if (gCosts.ContainsKey($"({neighbor.coord.x},{neighbor.coord.y})-({targetNode.coord.x},{targetNode.coord.y})"))
                            {
                                neighbor.hCost = gCosts[$"({neighbor.coord.x},{neighbor.coord.y})-({targetNode.coord.x},{targetNode.coord.y})"];
                            }
                            else
                            {
                                neighbor.hCost = GetDistance(neighbor, targetNode) / 0.5f;
                            }

                            neighbor.parent = currentNode;

                            if (!openSet.Contains(neighbor))
                            {
                                openSet.Add(neighbor);
                            }
                            else
                            {
                                openSet.UpdateItem(neighbor);
                            }
                        }
                    }
                }
            }

            if (pathSuccess)
            {
                world.GetTile(targetNode.worldPosition).distanceToLoctaion[$"{startNode.worldPosition.ToString()}"] = targetNode.fCost;
                world.GetTile(startNode.worldPosition).distanceToLoctaion[$"{targetNode.worldPosition.ToString()}"] = targetNode.fCost;
                waypoints = RetracePath(grid[$"{startPos.x}, {startPos.y}"], grid[$"{targetPos.x}, {targetPos.y}"]);
            }
            return(waypoints);
        }