void Awake()
 {
     Instance = this;
     foreach (MapInfo info in mapInfoList)
     {
         tilemaps[info.tileMapName] = info.tilemap;
     }
     foreach (TileInfo info in tileInfoList)
     {
         tileIdentifier[info.tileRep] = info.tile;
     }
 }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        float   horizontal = Input.GetAxis(JoystickName + "_Horizontal");
        float   vertical   = Input.GetAxis(JoystickName + "_Vertical");
        Vector3 movement   = new Vector3(0.0f, 0.0f, 0.0f);

        if (hasDestination)
        {
            movement = destinationPoint - gameObject.transform.position;
        }
        else if (Mathf.Abs(horizontal) == 1.0f)
        {
            movement.x = horizontal;
        }
        else if (Mathf.Abs(vertical) == 1.0f)
        {
            movement.y = vertical;
        }

        movement = movement.normalized * Speed;

        //Debug.Log("Movement = " + movement.ToString());
        if (movement.magnitude >= 1)
        {
            Vector3 newPosition = gameObject.transform.position + movement * Time.deltaTime;

            // Check status on Tilemap
            TileMapHandler tlh = GameObject.FindWithTag("Tilemap").GetComponent <TileMapHandler>();
            if (tlh)
            {
                Vector3 newPositionWithBorder = newPosition;
                // Have to calculate for position of "forward" edge based on direction
                if (movement.x > 0)
                {
                    newPositionWithBorder.x += 32 / 2;
                    SetDirection((int)Direction.Right);
                }
                else if (movement.x < 0)
                {
                    // do nothing because we're already at the border on the left side
                    SetDirection((int)Direction.Left);
                }
                if (movement.y > 0)
                {
                    newPositionWithBorder.y += 32 / 2;
                    SetDirection((int)Direction.Up);
                }
                else if (movement.y < 0)
                {
                    // do nothing because we're already at the border on the bottom
                    SetDirection((int)Direction.Down);
                }

                //Debug.Log("Destination Tile Type = " + ((TileHandler.TileType)tlh.GetTileScript(newPosition).CurrentType).ToString());
                if (tlh.GetTileScript(newPositionWithBorder).CurrentType != (int)TileHandler.TileType.Blocked)
                {
                    gameObject.transform.position = newPosition;
                }
                else
                {
                    Debug.Log("Tile was blocked. Index = " + tlh.GetTileIndex(newPositionWithBorder) + ", Attempted Position(w/ border) = " + newPositionWithBorder.ToString());
                }
            }

            float magnitude = (transform.position - destinationPoint).magnitude;
            if (hasDestination && (magnitude < 5.0f))
            {
                transform.position = destinationPoint;
                hasDestination     = false;
            }
        }
        else
        {
            StopAnimation();
        }
    }