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; } } } } }
protected virtual void OnObjectRemoved(GridObject obj) { if (ObjectRemoved != null) { ObjectRemoved.Invoke(obj); } }
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); } }
protected void OnObjectRemoved(GridObject obj) { if (ObjectRemoved != null) { ObjectRemoved(obj); } }
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); }
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); }
public Vector3 SnapObjectWorldPosition(Vector3 worldPosition, GridObject obj) { Vector2 local = WorldToLocal(worldPosition); Vector2 localSnapped = SnapObjectLocalPosition(local, obj); Vector3 worldSnapped = LocalToWorld(localSnapped); return(worldSnapped); }
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); }
private GridObject CreateGhost() { GridObject obj = Instantiate(objectPrefab); for (int i = 0; i < rotations; i++) { obj.Rotate(true); } return(obj); }
public bool RemoveObject(GridObject obj) { if (objects.Remove(obj)) { obj.OccupiedTiles.Remove(this); OnObjectRemoved(obj); return(true); } return(false); }
public bool AddObject(GridObject obj) { if (CanAddObject(obj)) { objects.Add(obj); obj.OccupiedTiles.Add(this); OnObjectAdded(obj); return(true); } return(false); }
public bool RemoveObject(GridObject obj) { if (objects.Remove(obj)) { obj.RemoveFromTiles(); obj.transform.parent = null; OnObjectRemoved(obj); return(true); } else { return(false); } }
private void DestroyGridObject(GridObject obj) { Destroy(obj.gameObject); }
protected virtual void OnObjectPlaced(GridObject obj) { }
protected virtual bool CanPlaceObject(Vector3 position, GridObject obj) { return(grid.CanPlaceObject(position, obj)); }
public bool CanPlaceObject(Vector3 worldPosition, GridObject obj) { Vector2 local = WorldToLocal(worldPosition); return(CanPlaceObject(local, obj)); }
public bool Contains(GridObject obj) { return(objects.Contains(obj)); }
protected virtual void OnObjectRemoved(GridObject obj) { }
public override void OnActivate() { ghost = CreateGhost(); }