Equals() public method

public Equals ( Vector2i, v ) : bool
v Vector2i,
return bool
Ejemplo n.º 1
0
 public bool Equals(InstanceData other)
 {
     return(ID == other.ID &&
            Type == other.Type &&
            Position.Equals(other.Position) &&
            Orientation == other.Orientation);
 }
Ejemplo n.º 2
0
    public override void OnEnter(int tX, int tY, Entity entity)
    {
        ushort   data = Map.GetTile(1, tX, tY).Data;
        Vector2i dir  = Vector2i.zero;

        switch (data)
        {
        case 1:
            dir = Vector2i.right;
            break;

        case 2:
            dir = Vector2i.down;
            break;

        case 3:
            dir = Vector2i.left;
            break;

        default:
            dir = Vector2i.up;
            break;
        }

        Vector2i start = new Vector2i(tX, tY);
        Vector2i end   = Utils.GetLineEnd(start, dir);

        if (end.Equals(start))
        {
            return;
        }

        entity.wait = true;
        entity.StartCoroutine(entity.SlideTo(end, dir));
    }
Ejemplo n.º 3
0
 /// <inheritdoc />
 public bool Equals(TileRef other)
 {
     return(MapIndex.Equals(other.MapIndex) &&
            GridIndex.Equals(other.GridIndex) &&
            GridIndices.Equals(other.GridIndices) &&
            Tile.Equals(other.Tile));
 }
Ejemplo n.º 4
0
    /** Return the block ID at the position (localX, localY, localZ) in the column at colPos. If the position is not currently loaded, return def */
    public ushort GetBlockAt(Vector2i colPos, int localX, int localY, int localZ, ushort def)
    {
        // if other coords are out of bounds find another chunk
        if (localX < 0 || localZ < 0 || localX >= CHUNK_SIZE || localZ >= CHUNK_SIZE)
        {
            int worldX = colPos.x * CHUNK_SIZE + localX;
            int worldZ = colPos.z * CHUNK_SIZE + localZ;
            return(GetBlockAt(worldX, localY, worldZ, def));
        }

        // if Y is out of bounds return something sensible
        if (localY >= WORLD_HEIGHT * CHUNK_SIZE)
        {
            return(Block.AIR);
        }
        if (localY < 0)
        {
            return(Block.BEDROCK);
        }

        // return the block id at this column, or default
        lock (this)
        {
            // try to use the cached col
            if (cachedPosition.Equals(colPos) && cachedColumn != null)
            {
                return(cachedColumn.blockID[localX, localY, localZ]);
            }

            // find the column and cache before returning
            Column col;
            if (loadedData.TryGetValue(colPos, out col))
            {
                // return the block
                cachedPosition = colPos;
                cachedColumn   = col;
                return(col.blockID[localX, localY, localZ]);
            }
            else
            {
                // not found ):
                return(def);
            }
        }
    }
Ejemplo n.º 5
0
 public void Update(float deltaTime, RenderWindow win, Vector2i curPosition)
 {
     if (buttonPosition.Equals(curPosition))
     {
         highlighted = true;
     }
     else
     {
         highlighted = false;
     }
     SetNewTexture();
 }
Ejemplo n.º 6
0
        int IndexFromScreenPos()
        {
            int index = 0;

            while (index < buttonList.Count)
            {
                if (currentScreenPosition.Equals(buttonList[index].buttonPosition))
                {
                    break;
                }
                index++;
            }
            return(index);
        }
Ejemplo n.º 7
0
    public override void OnEnter(int tX, int tY, Entity entity)
    {
        Vector2i start = new Vector2i(tX, tY);

        List <Vector2i> dirs = new List <Vector2i>(4);

        dirs.Add(Vector2i.left);
        dirs.Add(Vector2i.right);
        dirs.Add(Vector2i.up);
        dirs.Add(Vector2i.down);

        Utils.ShuffleList <Vector2i>(dirs);

        Vector2i dir = Vector2i.zero;

        for (int i = dirs.Count - 1; i >= 0; i--)
        {
            Vector2i next = start + dirs[i];

            if (!Map.GetTileTypeSafe(1, next.x, next.y).IsPassable(next.x, next.y))
            {
                dirs.RemoveAt(i);
            }
            else
            {
                dir = dirs[i];
                break;
            }
        }

        if (dir.Equals(Vector2i.zero))
        {
            return;
        }

        Vector2i end = Utils.GetLineEnd(start, dir);

        if (end.Equals(start))
        {
            return;
        }

        entity.wait = true;
        entity.StartCoroutine(entity.SlideTo(end, dir));
    }
Ejemplo n.º 8
0
    private void Update()
    {
        if (terrainThreadInfoQueue.Count > 0)
        {
            for (int i = 0; i < terrainThreadInfoQueue.Count; i++)
            {
                TerrainThreadInfo threadInfo = terrainThreadInfoQueue.Dequeue();
                threadInfo.callback(threadInfo.calculatedInfo);
            }
        }

        currentPos = GetChunkAtPosition(GameManager.PlayerPosition);
        if (!currentPos.Equals(prevPos))
        {
            prevPos = currentPos;
            UpdateTerrain(currentPos, renderDistance);
        }
    }
Ejemplo n.º 9
0
 public static RelativeWayDirection GetWay(Vector2i c1, Vector2i c2)
 {
     if (c1.Equals(c2))
     {
         return(RelativeWayDirection.None);
     }
     if (c1.X == c2.X)
     {
         if (c1.Y < c2.Y)
         {
             return(RelativeWayDirection.Down);
         }
         else
         {
             return(RelativeWayDirection.Up);
         }
     }
     else
     {
         if (c1.Y == c2.Y)
         {
             if (c1.X < c2.X)
             {
                 return(RelativeWayDirection.Right);
             }
             else
             {
                 return(RelativeWayDirection.Left);
             }
         }
         else
         {
             return(RelativeWayDirection.None);
         }
     }
 }
Ejemplo n.º 10
0
 private void StepFromTo(Vector2i from, Vector2i to, out float stepChangedReward, out bool reachedGoal)
 {
     Debug.Assert(map[from.x, from.y] == PlayerInt && currentPlayerPosition.Equals(from));
     stepChangedReward  = 0;
     stepChangedReward += stepCostReward;
     if (to.x < 0 || to.y < 0 || to.x >= mazeDimension.x || to.y >= mazeDimension.y)
     {
         //run to the edge
         stepChangedReward += goToWallReward;
         reachedGoal        = false;
     }
     else
     {
         if (map[to.x, to.y] == WallInt)
         {
             //run into a wall
             //run to the edge
             stepChangedReward += goToWallReward;
             reachedGoal        = false;
         }
         else if (map[to.x, to.y] == GoalInt)
         {
             //reach the goal
             stepChangedReward += maxWinReward;
             reachedGoal        = true;
         }
         else
         {
             //move successfully
             map[currentPlayerPosition.x, currentPlayerPosition.y] = PathInt;
             currentPlayerPosition = to;
             map[currentPlayerPosition.x, currentPlayerPosition.y] = PlayerInt;
             reachedGoal = false;
         }
     }
 }
Ejemplo n.º 11
0
    public List <Vector2i> FindPath(Vector2i start, Vector2i end)
    {
        if (start.Equals(end))
        {
            return(new List <Vector2i>());
        }

        ResetNodes();

        PathNode startNode = searchArea[start.x, start.y];
        PathNode endNode   = searchArea[end.x, end.y];

        startNode.open = true;

        startNode.distanceToEnd = Heuristic(start, end);
        startNode.accumCost     = 0;

        openList.Add(startNode);

        while (openList.Count > 0)
        {
            PathNode current = FindBestNode();

            if (current == null)
            {
                break;
            }

            if (current == endNode)
            {
                return(FindFinalPath(startNode, endNode));
            }

            for (int i = 0; i < 4; i++)
            {
                Vector2i neighborPos = current.tilePos + Vector2i.directions[i];

                PathNode neighbor = null;

                if (Map.InTileBounds(neighborPos.x, neighborPos.y))
                {
                    neighbor = searchArea[neighborPos.x, neighborPos.y];
                }

                if (neighbor == null || !neighbor.passable)
                {
                    continue;
                }

                byte cost      = (byte)(current.accumCost + neighbor.tileCost);
                byte heuristic = Heuristic(neighbor.tilePos, end);

                if (!neighbor.open && !neighbor.closed)
                {
                    neighbor.accumCost     = cost;
                    neighbor.distanceToEnd = (byte)(cost + heuristic);
                    neighbor.parent        = current;
                    neighbor.open          = true;
                    openList.Add(neighbor);
                }
                else
                {
                    if (neighbor.accumCost > cost)
                    {
                        neighbor.accumCost     = cost;
                        neighbor.distanceToEnd = (byte)(cost + heuristic);

                        neighbor.parent = current;
                    }
                }
            }

            openList.Remove(current);
            current.closed = true;
        }

        return(new List <Vector2i>());
    }
Ejemplo n.º 12
0
 public bool Equals(Node other)
 {
     return(Pos.Equals(other.Pos) &&
            Flags == other.Flags);
 }
Ejemplo n.º 13
0
        public List <Vector2i> FindPath(Vector2i start, Vector2i end, CostCallback costFn, Map map)
        {
            if (start.Equals(end))
            {
                return(new List <Vector2i> ());
            }
            // clear the lists
            openList   = new Dictionary <ulong, Node> ();
            closedList = new Dictionary <ulong, Node> ();
            Node startNode = new Node(start);

            openList [startNode.Key] = startNode;
            bool pathfound   = false;
            int  searchCount = 0;

            while (!pathfound && openList.Count > 0 && searchCount < 500)
            {
                Node lowestCost = findLowestCost();
                openList.Remove(lowestCost.Key);
                closedList[lowestCost.Key] = lowestCost;

                if (lowestCost.position.Equals(end))
                {
                    pathfound = true;
                    break;
                }
                for (int i = 0; i < 4; i++)
                {
                    Vector2i neighborPosition = new Vector2i(lowestCost.position.x + Map.nDir [i, 0], lowestCost.position.y + Map.nDir [i, 1]);

                    int newCost = lowestCost.distanceCost + costFn(neighborPosition.x, neighborPosition.y);

                    if (map.IsOpenTile(neighborPosition.x, neighborPosition.y) && !openList.ContainsKey(Node.KeyFromPostion(neighborPosition)))
                    {
                        Node newPosition = new Node(neighborPosition);
                        newPosition.heuristicCost = (int)Math.Abs(neighborPosition.x - end.x) + (int)Math.Abs(neighborPosition.y - end.y);
                        newPosition.distanceCost  = newCost;
                        newPosition.parent        = lowestCost;
                        openList[newPosition.Key] = newPosition;
                    }
                    else if (openList.ContainsKey(Node.KeyFromPostion(neighborPosition)) && openList[Node.KeyFromPostion(neighborPosition)].distanceCost > newCost)
                    {
                        openList [Node.KeyFromPostion(neighborPosition)].distanceCost = newCost;
                        openList [Node.KeyFromPostion(neighborPosition)].parent       = lowestCost;
                    }
                }
                searchCount++;
            }
            Node nextn = null;

            // couldn't find a path to the end
            if (!closedList.ContainsKey(Node.KeyFromPostion(end)))
            {
                // just build a short list containing the neighbor tiles.
                // eventually this should find "as near as possible" type tiles to move towards
                // I guess it depends on the application though
                startNode = new Node(start);
                Node lowestCost = startNode;
                for (int i = 0; i < 4; i++)
                {
                    Vector2i neighborPosition = new Vector2i(lowestCost.position.x + Map.nDir [i, 0], lowestCost.position.y + Map.nDir [i, 1]);
                    int      newCost          = lowestCost.distanceCost + costFn(neighborPosition.x, neighborPosition.y);
                    if (map.IsOpenTile(neighborPosition.x, neighborPosition.y) && !openList.ContainsKey(Node.KeyFromPostion(neighborPosition)))
                    {
                        Node newPosition = new Node(neighborPosition);
                        newPosition.heuristicCost  = (int)Math.Abs(neighborPosition.x - end.x) + (int)Math.Abs(neighborPosition.y - end.y);
                        newPosition.distanceCost   = newCost;
                        newPosition.parent         = lowestCost;
                        openList [newPosition.Key] = newPosition;
                    }
                }
                nextn = findLowestCost();
            }
            else
            {
                nextn = closedList [Node.KeyFromPostion(end)];
            }
            // build the step list
            List <Vector2i> returnList = new List <Vector2i> ();

            while (nextn != null)
            {
                returnList.Add(nextn.position);
                nextn = nextn.parent;
            }
            returnList.Reverse();
            return(returnList);
        }
Ejemplo n.º 14
0
        public static void Main()
        {
            /*
            RenderWindow window = new RenderWindow(new VideoMode(200, 200), "SFML works!");

            Console.WriteLine("test");
            Console.ReadKey();
            */

            // Create the main window
            RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML window");
            window.Closed += new EventHandler(OnClose);

            // Load a sprite to display
            Texture texture = new Texture("media/image/mario.png");
            Sprite sprite = new Sprite(texture);

            // Create a graphical text to display
            Font font = new Font("media/font/arial.ttf");
            Text text = new Text("Hello SFML", font, 50);

            // Load & play music
            Music music = new Music("media/sound/mario.ogg");
            music.Play();

            // starts the Stopwatch
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            // last mouse position
            Vector2i last_mouse_position = new Vector2i(0, 0);

            // Start the game loop
            while (window.IsOpen())
            {
                // Process events
                window.DispatchEvents();

                float speed = 0.1f * Convert.ToSingle(stopwatch.Elapsed.TotalMilliseconds);
                stopwatch.Reset();
                stopwatch.Start();

                if (Keyboard.IsKeyPressed(Keyboard.Key.Up))
                {
                    sprite.Position = new Vector2f(sprite.Position.X, sprite.Position.Y - speed);
                }
                if (Keyboard.IsKeyPressed(Keyboard.Key.Down))
                {
                    sprite.Position = new Vector2f(sprite.Position.X, sprite.Position.Y + speed);
                }
                if (Keyboard.IsKeyPressed(Keyboard.Key.Left))
                {
                    sprite.Position = new Vector2f(sprite.Position.X - speed, sprite.Position.Y);
                }
                if (Keyboard.IsKeyPressed(Keyboard.Key.Right))
                {
                    sprite.Position = new Vector2f(sprite.Position.X + speed, sprite.Position.Y);
                }

                // only if its a new mouse position
                if (!last_mouse_position.Equals(Mouse.GetPosition()))
                {
                    if (Mouse.IsButtonPressed(Mouse.Button.Left))
                    {
                        Console.WriteLine("Klick Maustaste: Links; X: " + Mouse.GetPosition().X
                            + "; Y: " + Mouse.GetPosition().X);
                        last_mouse_position = Mouse.GetPosition();
                    }
                    if (Mouse.IsButtonPressed(Mouse.Button.Middle))
                    {
                        Console.WriteLine("Klick Maustaste: Mitte; X: " + Mouse.GetPosition().X
                            + "; Y: " + Mouse.GetPosition().X);
                        last_mouse_position = Mouse.GetPosition();
                    }
                    if (Mouse.IsButtonPressed(Mouse.Button.Right))
                    {
                        Console.WriteLine("Klick Maustaste: Rechts; X: " + Mouse.GetPosition().X
                            + "; Y: " + Mouse.GetPosition().X);
                        last_mouse_position = Mouse.GetPosition();
                    }
                }

                // Clear screen
                window.Clear();

                // Draw the sprite
                window.Draw(sprite);

                // Draw the string
                window.Draw(text);

                // Update the window
                window.Display();
            }
        }
Ejemplo n.º 15
0
 private bool Equals(Vector2I other)
 {
     return(position.Equals(other.position));
 }
Ejemplo n.º 16
0
    public Stack <Vector2i> getPath(Vector2i start, Vector2i goal)
    {
        HashSet <Vector2i> closedSet = new HashSet <Vector2i>();
        HashSet <Vector2i> openSet   = new HashSet <Vector2i>();

        openSet.Add(start);
        int rows             = map.getRows();
        int columns          = map.getColumns();
        int maxLengthAllowed = 200;

        // Creation and initialization of both scores to Infinity
        float[,] gScore = new float[columns, rows];
        float[,] fScore = new float[columns, rows];
        Dictionary <Vector2i, Vector2i> cameFrom = new Dictionary <Vector2i, Vector2i>();

        for (int x = 0; x < rows; x++)
        {
            for (int y = 0; y < columns; y++)
            {
                gScore[y, x] = Mathf.Infinity;
                fScore[y, x] = Mathf.Infinity;
            }
        }
        gScore[start.y, start.x] = 0;
        fScore[start.y, start.x] = start.sqrDistanceTo(goal);

        while (openSet.Count > 0)
        {
            Vector2i current = getLowest(openSet, fScore);
            if (current.Equals(goal))
            {
                return(reconstructPath(cameFrom, goal));
            }

            openSet.Remove(current);
            closedSet.Add(current);
            List <Vector2i> neighbors = map.getNeighbors(current);
            if (neighbors.Count == 0)
            {
                Debug.Log("Neighbor count: " + neighbors.Count);
            }
            foreach (Vector2i neighbor in neighbors)
            {
                if (closedSet.Contains(neighbor))
                {
                    continue;                                             // Ignore the neighbor that is already evaluated
                }
                float tentativeGScore = gScore[current.y, current.x] + 1; // Using 1 to estimate the distance between tiles. May use a cost value instead
                if (tentativeGScore >= maxLengthAllowed)
                {
                    return(null);
                }

                if (!openSet.Contains(neighbor))
                {
                    openSet.Add(neighbor);
                }
                else if (tentativeGScore >= gScore[neighbor.y, neighbor.x])
                {
                    continue;
                }

                cameFrom[neighbor]             = current;
                gScore[neighbor.y, neighbor.x] = tentativeGScore;
                fScore[neighbor.y, neighbor.x] = tentativeGScore + neighbor.sqrDistanceTo(goal);
            }
        }
        Debug.Log("openSet: " + openSet.Count);
        Debug.Log("End of algorithm");
        return(null);
    }
Ejemplo n.º 17
0
    public Level GenerateLevel() {

        SetUpArray();

        InitialiseStartingBlock();

        // Loops several times to add extra/overlapping routes.
        for (int i = 0; i < 3; i++) {
            currentChunk = startChunk;
            endReached = false;
            previousMove = Direction.None;

            // Loop while the end goal chunk has not been placed.
            while (!endReached) {
                // Get a random number between 0 and 4 (inclusive).
                int randVal = Random.Range(0, 5);

                switch (previousMove) {
                    case Direction.Left:
                        level.chunkGrid[currentChunk.x, currentChunk.y].exits.Add(ChunkExit.Right);
                        break;
                    case Direction.Right:
                        level.chunkGrid[currentChunk.x, currentChunk.y].exits.Add(ChunkExit.Left);
                        break;
                    case Direction.Up:
                        level.chunkGrid[currentChunk.x, currentChunk.y].exits.Add(ChunkExit.Bottom);
                        break;
                    case Direction.Down:
                        level.chunkGrid[currentChunk.x, currentChunk.y].exits.Add(ChunkExit.Top);
                        break;
                }

                switch (randVal) {
                    case 0:
                    case 1:
                        // Move the path left.
                        if (CheckAdjacentIsValid(Direction.Left)) {
                            level.chunkGrid[currentChunk.x, currentChunk.y].exits.Add(ChunkExit.Left);
                            currentChunk = new Vector2i(currentChunk.x - 1, currentChunk.y);
                            level.chunkGrid[currentChunk.x, currentChunk.y].types.Add(ChunkType.Path);
                            previousMove = Direction.Left;
                        }
                        break;
                    case 2:
                    case 3:
                        // Move the path right.
                        if (CheckAdjacentIsValid(Direction.Right)) {
                            level.chunkGrid[currentChunk.x, currentChunk.y].exits.Add(ChunkExit.Right);
                            currentChunk = new Vector2i(currentChunk.x + 1, currentChunk.y);
                            level.chunkGrid[currentChunk.x, currentChunk.y].types.Add(ChunkType.Path);
                            previousMove = Direction.Right;
                        }
                        break;
                    case 4:
                        // Drop the path down.
                        if (!currentChunk.Equals(startChunk)) {
                            if (CheckAdjacentIsValid(Direction.Down)) {
                                level.chunkGrid[currentChunk.x, currentChunk.y].exits.Add(ChunkExit.Bottom);
                                currentChunk = new Vector2i(currentChunk.x, currentChunk.y - 1);
                                level.chunkGrid[currentChunk.x, currentChunk.y].types.Add(ChunkType.Path);
                                previousMove = Direction.Down;
                            } else {
                                // Set end goal chunk.
                                if (i == 0) {
                                    level.chunkGrid[currentChunk.x, currentChunk.y].types.Add(ChunkType.Path, ChunkType.EndGoal);
                                }
                            }
                        }
                        break;
                        
                }

                // If the current chunk is the end goal, set the end as having been reached.
                if (level.chunkGrid[currentChunk.x, currentChunk.y].types.HasFlag(ChunkType.EndGoal)) {
                    endReached = true;
                }
            }
        }

        // Set fountains.
        for (int i = 0; i < 4; i++) {
            bool fountainSet = false;
            while (!fountainSet) {
                foreach (Chunk c in level.chunkGrid) {
                    if (c.types.HasFlag(ChunkType.Path) && !c.types.HasFlag(ChunkType.PlayerStart) && !c.types.HasFlag(ChunkType.EndGoal) && !c.types.HasFlag(ChunkType.ItemFountain) && !c.types.HasFlag(ChunkType.EnemySpawner)) {
                        if (Random.value < 0.01f) {
                            c.types.Add(ChunkType.ItemFountain);
                            fountainSet = true;
                            break;
                        }
                    }
                }
            }
        }
        int fountainsInstantiated = 0;

        // Set enemy spawners.
        foreach (Chunk c in level.chunkGrid) {
            if (c.types.HasFlag(ChunkType.Path) && !c.types.HasFlag(ChunkType.PlayerStart) && !c.types.HasFlag(ChunkType.EndGoal) && !c.types.HasFlag(ChunkType.ItemFountain) && !c.types.HasFlag(ChunkType.EnemySpawner)) {
                if (Random.value < 0.5f) {
                    c.types.Add(ChunkType.EnemySpawner);
                }
            }
        }

        // Instantiate chunks based on their type and their exits.
        // TODO: Move into its own method. -Dean
        for (int gX = 0; gX < level.chunkGrid.GetLength(0); gX++) {
            for (int gY = 0; gY < level.chunkGrid.GetLength(1); gY++) {
                Vector3 position = new Vector3(gX * chunkSize.x, gY * chunkSize.y, 0);

                Chunk current = level.chunkGrid[gX, gY];

                GameObject chunk = new GameObject();

                if (current.types.HasFlag(ChunkType.PlayerStart)) {
                    // If the chunk is the player start.
                    if (current.exits.Is(ChunkExit.Left)) {
                        // Left exit only.
                        chunk = InstantiateFromArray(StartLeftChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right)) {
                        // Right exit only.
                        chunk = InstantiateFromArray(StartRightChunks, position);
                    } else if (current.exits.Is(ChunkExit.Bottom)) {
                        // Bottom exit only.
                        chunk = InstantiateFromArray(StartDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right)) {
                        // Left and right exits.
                        chunk = InstantiateFromArray(StartLeftRightChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Bottom)) {
                        // Left and bottom exits.
                        chunk = InstantiateFromArray(StartLeftDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right, ChunkExit.Bottom)) {
                        // Right and bottom exits.
                        chunk = InstantiateFromArray(StartRightDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right, ChunkExit.Bottom)) {
                        // Left, right and bottom exits.
                        chunk = InstantiateFromArray(StartLeftRightDownChunks, position);
                    } else {
                        Debug.Log("Start chunk error");
                    }

                    // Spawn player
                    GameManager.Instance.Player = Instantiate(Player, chunk.transform.FindChild("StartPoint").transform.position, Quaternion.identity) as GameObject;
                    
                } else if (current.types.HasFlag(ChunkType.EndGoal)) {
                    // If the chunk is the end goal.
                    if (current.exits.Is(ChunkExit.Left)) {
                        // Left exit only.
                        chunk = InstantiateFromArray(EndLeftChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right)) {
                        // Right exit only.
                        chunk = InstantiateFromArray(EndRightChunks, position);
                    } else if (current.exits.Is(ChunkExit.Top)) {
                        // Bottom exit only.
                        chunk = InstantiateFromArray(EndUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right)) {
                        // Left and right exits.
                        chunk = InstantiateFromArray(EndLeftRightChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Top)) {
                        // Left and bottom exits.
                        chunk = InstantiateFromArray(EndLeftUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right, ChunkExit.Top)) {
                        // Right and bottom exits.
                        chunk = InstantiateFromArray(EndRightUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right, ChunkExit.Top)) {
                        // Left, right and bottom exits.
                        chunk = InstantiateFromArray(EndLeftRightUpChunks, position);
                    } else {
                        Debug.Log("End chunk error");
                    }
                } else if (current.types.HasFlag(ChunkType.ItemFountain)) {
                    // If chunk is an item fountain.
                    if (current.exits.Is(ChunkExit.Left)) {
                        // Left exit only.
                        chunk = InstantiateFromArray(FountainLeftChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right)) {
                        // Right exit only.
                        chunk = InstantiateFromArray(FountainRightChunks, position);
                    } else if (current.exits.Is(ChunkExit.Bottom)) {
                        // Bottom exit only.
                        chunk = InstantiateFromArray(FountainDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Top)) {
                        // Top exit only.
                        chunk = InstantiateFromArray(FountainUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right)) {
                        // Left and right exits.
                        chunk = InstantiateFromArray(FountainLeftRightChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Bottom)) {
                        // Left and bottom exits.
                        chunk = InstantiateFromArray(FountainLeftDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Top)) {
                        // Left and top exits.
                        chunk = InstantiateFromArray(FountainLeftUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right, ChunkExit.Bottom)) {
                        // Right and bottom exits.
                        chunk = InstantiateFromArray(FountainRightDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right, ChunkExit.Top)) {
                        // Right and top exits.
                        chunk = InstantiateFromArray(FountainRightUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Bottom, ChunkExit.Top)) {
                        // Bottom and top exits.
                        chunk = InstantiateFromArray(FountainUpDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right, ChunkExit.Bottom)) {
                        // Left, right and bottom exits.
                        chunk = InstantiateFromArray(FountainLeftRightDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right, ChunkExit.Top)) {
                        // Left, right and top exits.
                        chunk = InstantiateFromArray(FountainLeftRightUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Bottom, ChunkExit.Top)) {
                        // Left, bottom and top exits.
                        chunk = InstantiateFromArray(FountainLeftUpDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right, ChunkExit.Bottom, ChunkExit.Top)) {
                        // Right, bottom and top exits.
                        chunk = InstantiateFromArray(FountainRightUpDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right, ChunkExit.Bottom, ChunkExit.Top)) {
                        // Left, right, bottom and top exits.
                        chunk = InstantiateFromArray(FountainLeftRightUpDownChunks, position);
                    } else {
                        Debug.Log("Fountain chunk error");
                    }
                    switch (fountainsInstantiated) {
                        case 0:
                            chunk.transform.Find("Fountain").GetComponent<FountainScript>().SetFountainType(FountainType.Earth);
                            break;
                        case 1:
                            chunk.transform.Find("Fountain").GetComponent<FountainScript>().SetFountainType(FountainType.Water);
                            break;
                        case 2:
                            chunk.transform.Find("Fountain").GetComponent<FountainScript>().SetFountainType(FountainType.Fire);
                            break;
                        case 3:
                            chunk.transform.Find("Fountain").GetComponent<FountainScript>().SetFountainType(FountainType.Electric);
                            break;
                    }
                    fountainsInstantiated++;
                } else {
                    // Place chunks based on their exits.
                    if (current.exits.Is(ChunkExit.Left)) {
                        // Left exit only.
                        chunk = InstantiateFromArray(LeftChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right)) {
                        // Right exit only.
                        chunk = InstantiateFromArray(RightChunks, position);
                    } else if (current.exits.Is(ChunkExit.Bottom)) {
                        // Bottom exit only.
                        chunk = InstantiateFromArray(DownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Top)) {
                        // Top exit only.
                        chunk = InstantiateFromArray(UpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right)) {
                        // Left and right exits.
                        chunk = InstantiateFromArray(LeftRightChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Bottom)) {
                        // Left and bottom exits.
                        chunk = InstantiateFromArray(LeftDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Top)) {
                        // Left and top exits.
                        chunk = InstantiateFromArray(LeftUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right, ChunkExit.Bottom)) {
                        // Right and bottom exits.
                        chunk = InstantiateFromArray(RightDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right, ChunkExit.Top)) {
                        // Right and top exits.
                        chunk = InstantiateFromArray(RightUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Bottom, ChunkExit.Top)) {
                        // Bottom and top exits.
                        chunk = InstantiateFromArray(UpDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right, ChunkExit.Bottom)) {
                        // Left, right and bottom exits.
                        chunk = InstantiateFromArray(LeftRightDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right, ChunkExit.Top)) {
                        // Left, right and top exits.
                        chunk = InstantiateFromArray(LeftRightUpChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Bottom, ChunkExit.Top)) {
                        // Left, bottom and top exits.
                        chunk = InstantiateFromArray(LeftUpDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Right, ChunkExit.Bottom, ChunkExit.Top)) {
                        // Right, bottom and top exits.
                        chunk = InstantiateFromArray(RightUpDownChunks, position);
                    } else if (current.exits.Is(ChunkExit.Left, ChunkExit.Right, ChunkExit.Bottom, ChunkExit.Top)) {
                        // Left, right, bottom and top exits.
                        chunk = InstantiateFromArray(LeftRightUpDownChunks, position);
                    } else {
                        // Non-path chunks (currently impassible).
                        chunk = InstantiateFromArray(ImpassibleChunks, position);
                    }
                }

                if (current.types.HasFlag(ChunkType.EnemySpawner)) {
                    chunk.transform.Find("EnemySpawner").GetComponent<EnemySpawner>().Enemy = EnemySpawner.GetComponent<EnemySpawner>().Enemy;
                    chunk.transform.Find("EnemySpawner").GetComponent<EnemySpawner>().spawnChance = EnemySpawner.GetComponent<EnemySpawner>().spawnChance;
                    chunk.transform.Find("EnemySpawner").GetComponent<EnemySpawner>().spawnDelay = EnemySpawner.GetComponent<EnemySpawner>().spawnDelay;
                    chunk.transform.Find("EnemySpawner").GetComponent<EnemySpawner>().spawnLimit = EnemySpawner.GetComponent<EnemySpawner>().spawnLimit;
                } else {
                    if (chunk.transform.Find("EnemySpawner") != null) {
                        chunk.transform.Find("EnemySpawner").gameObject.SetActive(false);
                    }
                }


                chunk.GetComponent<ChunkScript>().tileGrid = new GameObject[16, 16];

                for (int i = 0; i < chunk.transform.childCount; i++) {
                    if (chunk.transform.GetChild(i).tag == "Platform") {
                        int x = (int)chunk.transform.GetChild(i).transform.localPosition.x;
                        int y = (int)chunk.transform.GetChild(i).transform.localPosition.y;

                        chunk.GetComponent<ChunkScript>().tileGrid[x, y] = chunk.transform.GetChild(i).gameObject;
                    }
                }

                for (int tX = 0; tX < 16; tX++) {
                    for (int tY = 0; tY < 16; tY++) {
                        if (chunk.GetComponent<ChunkScript>().tileGrid[tX, tY] != null) {

                            bool up = false;
                            if(tY < 15)
                                up = (chunk.GetComponent<ChunkScript>().tileGrid[tX, tY + 1] != null);
                            bool down = false;
                            if(tY > 0)
                                down = (chunk.GetComponent<ChunkScript>().tileGrid[tX, tY - 1] != null);
                            bool left = false;
                            if(tX > 0)
                                left = (chunk.GetComponent<ChunkScript>().tileGrid[tX - 1, tY] != null);
                            bool right = false;
                            if(tX < 15)
                                right = (chunk.GetComponent<ChunkScript>().tileGrid[tX + 1, tY] != null);

                            if (!left) {
                                if (!right) {
                                    if (!up) {
                                        chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.Top;
                                    } else {
                                        if (!down) {
                                            chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.PlainBottom;
                                        } else {
                                            chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.Plain;
                                        }
                                    }
                                } else {
                                    if (!up) {
                                        chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.LeftTop;
                                    } else {
                                        chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.Left;
                                    }
                                }
                            } else {
                                if (!right) {
                                    if (!up) {
                                        chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.RightTop;
                                    } else {
                                        chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.Right;
                                    }
                                } else {
                                    if (!up) {
                                        chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.Top;
                                    } else {
                                        if (!down) {
                                            chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.PlainBottom;
                                        } else {
                                            chunk.GetComponent<ChunkScript>().tileGrid[tX, tY].GetComponent<SpriteRenderer>().sprite = EarthSprites.Plain;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


            }
        }

        return level;
    }
Ejemplo n.º 18
0
        public List<Vector2i> FindPath(Vector2i start, Vector2i end, CostCallback costFn, Map map)
        {
            if (start.Equals (end)) {
                return new List<Vector2i> ();
            }
            // clear the lists
            openList = new Dictionary<ulong, Node> ();
            closedList = new Dictionary<ulong, Node> ();
            Node startNode = new Node (start);
            openList [startNode.Key] = startNode;
            bool pathfound = false;
            int searchCount = 0;
            while (!pathfound && openList.Count > 0 && searchCount < 500) {
                Node lowestCost = findLowestCost ();
                openList.Remove (lowestCost.Key);
                closedList[lowestCost.Key] = lowestCost;

                if (lowestCost.position.Equals (end)) {
                    pathfound = true;
                    break;
                }
                for (int i = 0; i < 4; i++) {

                    Vector2i neighborPosition = new Vector2i (lowestCost.position.x + Map.nDir [i, 0], lowestCost.position.y + Map.nDir [i, 1]);

                    int newCost = lowestCost.distanceCost + costFn (neighborPosition.x, neighborPosition.y);

                    if (map.IsOpenTile (neighborPosition.x, neighborPosition.y) && !openList.ContainsKey (Node.KeyFromPostion (neighborPosition))) {
                        Node newPosition = new Node (neighborPosition);
                        newPosition.heuristicCost = (int)Math.Abs (neighborPosition.x - end.x) + (int)Math.Abs (neighborPosition.y - end.y);
                        newPosition.distanceCost = newCost;
                        newPosition.parent = lowestCost;
                        openList[newPosition.Key] = newPosition;
                    } else if (openList.ContainsKey (Node.KeyFromPostion (neighborPosition)) && openList[Node.KeyFromPostion (neighborPosition)].distanceCost > newCost) {
                        openList [Node.KeyFromPostion (neighborPosition)].distanceCost = newCost;
                        openList [Node.KeyFromPostion (neighborPosition)].parent = lowestCost;
                    }
                }
                searchCount++;
            }
            Node nextn = null;
            // couldn't find a path to the end
            if (!closedList.ContainsKey (Node.KeyFromPostion (end))) {
                // just build a short list containing the neighbor tiles.
                // eventually this should find "as near as possible" type tiles to move towards
                // I guess it depends on the application though
                startNode = new Node (start);
                Node lowestCost = startNode;
                for (int i = 0; i < 4; i++) {
                    Vector2i neighborPosition = new Vector2i (lowestCost.position.x + Map.nDir [i, 0], lowestCost.position.y + Map.nDir [i, 1]);
                    int newCost = lowestCost.distanceCost + costFn (neighborPosition.x, neighborPosition.y);
                    if (map.IsOpenTile (neighborPosition.x, neighborPosition.y) && !openList.ContainsKey (Node.KeyFromPostion (neighborPosition))) {
                        Node newPosition = new Node (neighborPosition);
                        newPosition.heuristicCost = (int)Math.Abs (neighborPosition.x - end.x) + (int)Math.Abs (neighborPosition.y - end.y);
                        newPosition.distanceCost = newCost;
                        newPosition.parent = lowestCost;
                        openList [newPosition.Key] = newPosition;
                    }
                }
                nextn = findLowestCost ();
            } else {
                nextn = closedList [Node.KeyFromPostion (end)];
            }
            // build the step list
            List<Vector2i> returnList = new List<Vector2i> ();
            while (nextn != null) {
                returnList.Add (nextn.position);
                nextn = nextn.parent;
            }
            returnList.Reverse ();
            return returnList;
        }
Ejemplo n.º 19
0
 public bool Equals(PathNode other)
 {
     return(tilePos.Equals(other.tilePos));
 }
Ejemplo n.º 20
0
        public Vector2iList FindPath(Vector2i start, Vector2i destination, MarbleRegion[] regionen, JumpHolePointToPointList jumpHoles)
        {
            //LockActions()
            if (_Searching)
            {
                throw new Exception("Lock Error");
            }
            Regions        = regionen;
            StartPos       = start;
            DestinationPos = destination;

            WayPoint wp = null;

            try
            {
                _Searching = true;

                Map.Reset();

                Map.JumpHoles  = jumpHoles;
                AllowJumpHoles = jumpHoles.Count != 0;

                if (StartPos.Equals(DestinationPos))
                {
                    return(null);
                }
                if (!Allowed(Map[DestinationPos]))
                {
                    return(null);
                }

                wp        = Map[StartPos];
                wp.HCost  = CalcHCost(wp);
                wp.GWCost = 0;
                wp.Parent = null;
                OpenList.Push(wp);

                var callCount = 0;
                var startTime = DateTime.Now;

                while (!OpenList.IsEmpty)
                {
                    callCount += 1;
                    if (callCount == 100000)
                    {
                        callCount = 0;
                        if ((DateTime.UtcNow - startTime).TotalSeconds > 10)
                        {
                            return(null);
                        }
                    }
                    wp          = OpenList.Pop();
                    wp.IsClosed = true;
                    AddNextNodes(wp);
                    if (wp.Position.Equals(DestinationPos))
                    {
                        return(GetWay(wp));
                    }
                }
                return(null);
            }
            finally
            {
                OpenList.Clear();
                _Searching = false;
                //UnLockActions()
            }
        }
Ejemplo n.º 21
0
 public static bool IsInvalid(this Vector2i current) => current.Equals(new Vector2i(-1, -1));
Ejemplo n.º 22
0
        private void BigGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            /*
             * if(e.MiddleButton == MouseButtonState.Pressed)
             * {
             *  Point p = e.GetPosition(canvasBoard);
             *  attackBoard.UpdateBoard(new Vector2i((int)p.X / canvasBoard.cellWidth, (int)p.Y / canvasBoard.cellHeight), shipBoard.Attack(new Vector2i((int)p.X / canvasBoard.cellWidth, (int)p.Y / canvasBoard.cellHeight)));
             *  canvasBoard.InvalidateVisual();
             * }
             */

            if (e.RightButton == MouseButtonState.Pressed)
            {
                if (firstPoint != null)
                {
                    firstPoint             = null;
                    secondPoint            = null;
                    canvasBoard.firstPoint = null;
                    canvasBoard.InvalidateVisual();
                }

                else
                {
                    Point p    = e.GetPosition(canvasBoard);
                    Ship  temp = shipBoard.ContainsShip((int)p.X / canvasBoard.cellWidth, (int)p.Y / canvasBoard.cellHeight);
                    if (temp != null)
                    {
                        shipBoard.RemoveShip(temp);
                        canvasBoard.InvalidateVisual();
                    }
                }
            }
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point pos = e.GetPosition(canvasBoard);
                //MessageBox.Show(string.Format("Left: {0} | Middle: {1} | Right: {2} \nPos: X:{3} Y:{4} \nCell: ({5},{6})", e.LeftButton, e.MiddleButton, e.RightButton, pos.X, pos.Y, (int)pos.X/32, (int)pos.Y/32));
                if ((pos.X >= 0 && pos.X < canvasBoard.gridWidth) && (pos.Y >= 0 && pos.Y < canvasBoard.gridHeight))
                {
                    if (firstPoint == null)
                    {
                        firstPoint             = new Vector2i((int)pos.X / canvasBoard.cellWidth, (int)pos.Y / canvasBoard.cellHeight);
                        canvasBoard.firstPoint = firstPoint;
                        canvasBoard.InvalidateVisual();
                    }
                    if (firstPoint != null)
                    {
                        secondPoint = new Vector2i((int)pos.X / canvasBoard.cellWidth, (int)pos.Y / canvasBoard.cellHeight);
                        if (!(firstPoint.y == secondPoint.y || firstPoint.x == secondPoint.x))
                        {
                            secondPoint = null;
                        }


                        else if (firstPoint.y == secondPoint.y && firstPoint.x == secondPoint.x)
                        {
                            secondPoint = null;
                        }
                    }

                    if (firstPoint != null && secondPoint != null)
                    {
                        Orientation orientation;
                        if (firstPoint.x == secondPoint.x)
                        {
                            orientation = Orientation.Vertical;
                        }
                        else
                        {
                            orientation = Orientation.Horizontal;
                        }

                        Vector2i startPoint = orientation == Orientation.Horizontal ? (firstPoint.x < secondPoint.x ? firstPoint : secondPoint) : (firstPoint.y < secondPoint.y ? firstPoint : secondPoint);

                        int size = orientation == Orientation.Horizontal ? (startPoint.Equals(firstPoint) ? secondPoint.x - startPoint.x : firstPoint.x - startPoint.x) : (startPoint.Equals(firstPoint) ? secondPoint.y - startPoint.y : firstPoint.y - startPoint.y);

                        shipBoard.AddShip(new Ship(orientation, startPoint, size + 1));
                        canvasBoard.InvalidateVisual();
                        firstPoint             = null;
                        secondPoint            = null;
                        startPoint             = null;
                        canvasBoard.firstPoint = null;
                    }
                }
            }
        }
Ejemplo n.º 23
0
 public bool Equals(Wire other)
 {
     return(Pos.Equals(other.Pos) &&
            Length == other.Length &&
            Direction == other.Direction);
 }
        private Vector2i GetValidStandGroundPosition()
        {
            Vector2i targetPosition = GetClosestValidShootingPositionToPlayer(GetValidShootingPositions().FindAll(position => position.ManhattanDistance(HomePosition) <= StandGroundRadius));

            return(targetPosition.Equals(new Vector2i(-1, -1)) ? HomePosition : targetPosition);
        }