Exemplo n.º 1
0
        public override void Update()
        {
            Vector3 mouse;

            if (RaycastMousePosition(out mouse))
            {
                if (Input.GetKeyDown(KeyCode.R))
                {
                    ghost.Rotate(true);
                    rotations = (rotations + 1) % 4;
                }

                Vector3 snappedPosition = grid.SnapObjectWorldPosition(mouse, ghost);
                ghost.transform.position = snappedPosition;

                if (Input.GetMouseButtonDown(0) && CanPlaceObject(snappedPosition, ghost))
                {
                    if (EventSystem.current.IsPointerOverGameObject() == false)
                    {
                        if (grid.PlaceObject(snappedPosition, ghost))
                        {
                            OnObjectPlaced(ghost);
                            ghost = CreateGhost();
                            ghost.transform.position = snappedPosition;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
 protected virtual void OnObjectRemoved(GridObject obj)
 {
     if (ObjectRemoved != null)
     {
         ObjectRemoved.Invoke(obj);
     }
 }
Exemplo n.º 3
0
        public bool PlaceObject(Vector2 localPosition, GridObject obj)
        {
            Vector2        zeroPos         = localPosition - obj.Origin + new Vector2(tileSize * 0.5f, tileSize * 0.5f);
            GridCoordinate zeroTilePositon = LocalToGrid(zeroPos);

            if (CanPlaceObject(localPosition, obj))
            {
                for (int x = 0; x < obj.Width; x++)
                {
                    for (int y = 0; y < obj.Height; y++)
                    {
                        int tileX = zeroTilePositon.X + x;
                        int tileY = zeroTilePositon.Y + y;

                        tiles[tileX, tileY].AddObject(obj);
                    }
                }

                obj.transform.parent = transform;

                objects.Add(obj);
                OnObjectPlaced(obj);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 4
0
 protected void OnObjectRemoved(GridObject obj)
 {
     if (ObjectRemoved != null)
     {
         ObjectRemoved(obj);
     }
 }
Exemplo n.º 5
0
        public bool CanPlaceObject(Vector2 localPosition, GridObject obj)
        {
            Vector2        zeroPos         = localPosition - obj.Origin + new Vector2(tileSize * 0.5f, tileSize * 0.5f);
            GridCoordinate zeroTilePositon = LocalToGrid(zeroPos);

            for (int x = 0; x < obj.Width; x++)
            {
                for (int y = 0; y < obj.Height; y++)
                {
                    int tileX = zeroTilePositon.X + x;
                    int tileY = zeroTilePositon.Y + y;

                    if (IsValidPosition(tileX, tileY) == false)
                    {
                        return(false);
                    }
                    if (tiles[tileX, tileY].CanAddObject(obj) == false)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 6
0
        public bool CanAddObject(GridObject obj)
        {
            if (Contains(obj))
            {
                return(false);
            }
            if (obj.Obstructive)
            {
                bool tileObstructed = false;

                for (int i = 0; i < objects.Count; i++)
                {
                    tileObstructed = objects[i].Obstructive;

                    if (tileObstructed)
                    {
                        break;
                    }
                }

                return(!tileObstructed);
            }

            return(true);
        }
Exemplo n.º 7
0
        public Vector3 SnapObjectWorldPosition(Vector3 worldPosition, GridObject obj)
        {
            Vector2 local        = WorldToLocal(worldPosition);
            Vector2 localSnapped = SnapObjectLocalPosition(local, obj);
            Vector3 worldSnapped = LocalToWorld(localSnapped);

            return(worldSnapped);
        }
Exemplo n.º 8
0
        public Vector2 SnapObjectLocalPosition(Vector2 localPosition, GridObject obj)
        {
            Vector2        halfTile      = new Vector2(tileSize * 0.5f, tileSize * 0.5f);
            Vector2        corner        = localPosition - obj.Origin + halfTile;
            GridCoordinate cornerGridPos = LocalToGrid(corner);
            Vector2        cornerSnapPos = GridToLocal(cornerGridPos) - halfTile;

            return(cornerSnapPos + obj.Origin);
        }
Exemplo n.º 9
0
        private GridObject CreateGhost()
        {
            GridObject obj = Instantiate(objectPrefab);

            for (int i = 0; i < rotations; i++)
            {
                obj.Rotate(true);
            }

            return(obj);
        }
Exemplo n.º 10
0
        public bool RemoveObject(GridObject obj)
        {
            if (objects.Remove(obj))
            {
                obj.OccupiedTiles.Remove(this);

                OnObjectRemoved(obj);
                return(true);
            }

            return(false);
        }
Exemplo n.º 11
0
        public bool AddObject(GridObject obj)
        {
            if (CanAddObject(obj))
            {
                objects.Add(obj);
                obj.OccupiedTiles.Add(this);

                OnObjectAdded(obj);
                return(true);
            }

            return(false);
        }
Exemplo n.º 12
0
        public bool RemoveObject(GridObject obj)
        {
            if (objects.Remove(obj))
            {
                obj.RemoveFromTiles();

                obj.transform.parent = null;

                OnObjectRemoved(obj);

                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 13
0
 private void DestroyGridObject(GridObject obj)
 {
     Destroy(obj.gameObject);
 }
Exemplo n.º 14
0
 protected virtual void OnObjectPlaced(GridObject obj)
 {
 }
Exemplo n.º 15
0
 protected virtual bool CanPlaceObject(Vector3 position, GridObject obj)
 {
     return(grid.CanPlaceObject(position, obj));
 }
Exemplo n.º 16
0
        public bool CanPlaceObject(Vector3 worldPosition, GridObject obj)
        {
            Vector2 local = WorldToLocal(worldPosition);

            return(CanPlaceObject(local, obj));
        }
Exemplo n.º 17
0
 public bool Contains(GridObject obj)
 {
     return(objects.Contains(obj));
 }
Exemplo n.º 18
0
 protected virtual void OnObjectRemoved(GridObject obj)
 {
 }
Exemplo n.º 19
0
 public override void OnActivate()
 {
     ghost = CreateGhost();
 }