Exemple #1
0
 private List <GameObject> TilesReachableFrom(GameObject tile, int movement)
 {
     if (tile.layer != 8)
     {
         return(null);
     }
     else
     {
         List <GameObject> output = new List <GameObject>();
         output.Add(tile);
         if (Adjacency.TileUp(tile) != null && movement >= TileCost(Adjacency.TileUp(tile)))
         {
             output.AddRange(TilesReachableFrom(Adjacency.TileUp(tile), movement - TileCost(Adjacency.TileUp(tile))));
         }
         if (Adjacency.TileDown(tile) != null && movement >= TileCost(Adjacency.TileDown(tile)))
         {
             output.AddRange(TilesReachableFrom(Adjacency.TileDown(tile), movement - TileCost(Adjacency.TileDown(tile))));
         }
         if (Adjacency.TileLeft(tile) != null && movement >= TileCost(Adjacency.TileLeft(tile)))
         {
             output.AddRange(TilesReachableFrom(Adjacency.TileLeft(tile), movement - TileCost(Adjacency.TileLeft(tile))));
         }
         if (Adjacency.TileRight(tile) != null && movement >= TileCost(Adjacency.TileRight(tile)))
         {
             output.AddRange(TilesReachableFrom(Adjacency.TileRight(tile), movement - TileCost(Adjacency.TileRight(tile))));
         }
         return(output);
     }
 }
Exemple #2
0
 // Update is called once per frame
 void Update()
 {
     if (target.transform.position.x < transform.position.x - hSize / 2)
     {
         tile = Adjacency.TileLeft(tile);
         transform.position = new Vector3(tile.transform.position.x, tile.transform.position.y, transform.position.z);
     }
     else if (target.transform.position.x > transform.position.x + hSize / 2)
     {
         tile = Adjacency.TileRight(tile);
         transform.position = new Vector3(tile.transform.position.x, tile.transform.position.y, transform.position.z);
     }
     else if (target.transform.position.y < transform.position.y - vSize / 2)
     {
         tile = Adjacency.TileDown(tile);
         transform.position = new Vector3(tile.transform.position.x, tile.transform.position.y, transform.position.z);
     }
     else if (target.transform.position.y > transform.position.y + vSize / 2)
     {
         tile = Adjacency.TileUp(tile);
         transform.position = new Vector3(tile.transform.position.x, tile.transform.position.y, transform.position.z);
     }
 }
Exemple #3
0
    private void Update()
    {
        transform.position = currentTile.transform.position;
        currentTile        = Adjacency.TileOf(gameObject);
        currentUnit        = Adjacency.UnitOn(currentTile);
        timePassed         = timePassed + Time.deltaTime;

        if (!active)
        {
            active = true;
            if (Input.GetAxis("Vertical") > 0 && Adjacency.TileUp(currentTile) != null && timePassed > delay)
            {
                currentTile = Adjacency.TileUp(currentTile);
                timePassed  = 0;
            }
            else if (Input.GetAxis("Horizontal") < 0 && Adjacency.TileLeft(currentTile) != null && timePassed > delay)
            {
                currentTile = Adjacency.TileLeft(currentTile);
                timePassed  = 0;
            }
            else if (Input.GetAxis("Vertical") < 0 && Adjacency.TileDown(currentTile) != null && timePassed > delay)
            {
                currentTile = Adjacency.TileDown(currentTile);
                timePassed  = 0;
            }
            else if (Input.GetAxis("Horizontal") > 0 && Adjacency.TileRight(currentTile) != null && timePassed > delay)
            {
                currentTile = Adjacency.TileRight(currentTile);
                timePassed  = 0;
            }

            if (Input.GetButtonDown("Submit") && currentUnit != null && !selected && !currentUnit.GetComponent <Unit>().HasActed())
            {
                selected     = true;
                selectedUnit = currentUnit;
                Debug.Log("selected");
                foreach (GameObject tile in (selectedUnit.GetComponent <Unit>() as Unit).ReachableTiles())
                {
                    tile.GetComponent <SpriteRenderer>().enabled = true;
                }
            }

            if (Input.GetButtonDown("Submit") && currentUnit == null && selected)
            {
                if ((selectedUnit.GetComponent <Unit>() as Unit).ReachableTiles().Contains(currentTile))
                {
                    foreach (GameObject tile in (selectedUnit.GetComponent <Unit>() as Unit).ReachableTiles())
                    {
                        tile.GetComponent <SpriteRenderer>().enabled = false;
                    }
                    selectedUnit.transform.position = currentTile.transform.position;
                    selected = false;
                    selectedUnit.GetComponent <Unit>().EndAction();
                }
                else
                {
                    Debug.Log("not in range");
                }
            }

            active = false;
        }
    }