Example #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;
                        }
                    }
                }
            }
        }
Example #2
0
 protected virtual void OnObjectRemoved(GridObject obj)
 {
     if (ObjectRemoved != null)
     {
         ObjectRemoved.Invoke(obj);
     }
 }
Example #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);
            }
        }
Example #4
0
 protected void OnObjectRemoved(GridObject obj)
 {
     if (ObjectRemoved != null)
     {
         ObjectRemoved(obj);
     }
 }
Example #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);
        }
Example #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);
        }
Example #7
0
        public Vector3 SnapObjectWorldPosition(Vector3 worldPosition, GridObject obj)
        {
            Vector2 local        = WorldToLocal(worldPosition);
            Vector2 localSnapped = SnapObjectLocalPosition(local, obj);
            Vector3 worldSnapped = LocalToWorld(localSnapped);

            return(worldSnapped);
        }
Example #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);
        }
Example #9
0
        private GridObject CreateGhost()
        {
            GridObject obj = Instantiate(objectPrefab);

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

            return(obj);
        }
Example #10
0
        public bool RemoveObject(GridObject obj)
        {
            if (objects.Remove(obj))
            {
                obj.OccupiedTiles.Remove(this);

                OnObjectRemoved(obj);
                return(true);
            }

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

                OnObjectAdded(obj);
                return(true);
            }

            return(false);
        }
Example #12
0
        public bool RemoveObject(GridObject obj)
        {
            if (objects.Remove(obj))
            {
                obj.RemoveFromTiles();

                obj.transform.parent = null;

                OnObjectRemoved(obj);

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

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