/// <summary> /// Finds a path from a location, to a location, within a number of steps /// </summary> /// <param name="fromLocation">The location to start from</param> /// <param name="toLocation">The location to end up at</param> /// <param name="stepsLeft">The numver of steps left</param> /// <returns>A list of gameobjects in the path</returns> public static List <GameObject> GetPathToLocation(LocationStruct fromLocation, LocationStruct toLocation, int stepsLeft) { List <GameObject> tiles = new List <GameObject>(); if (stepsLeft < 0) { return(tiles); } else if (fromLocation.row == toLocation.row && fromLocation.column == toLocation.column) { GameObject currTile = Board.GetTileFromLocation(fromLocation); tiles.Add(currTile); } else { List <GameObject> neighbourTiles = Board.neighbourTilesStrait(fromLocation, true); foreach (GameObject neighbour in neighbourTiles) { List <GameObject> retTiles = GetPathToLocation(neighbour.GetComponent <BoardLocation>().location, toLocation, stepsLeft - 1); if (retTiles.Count > 0) { tiles.Add(Board.GetTileFromLocation(fromLocation)); foreach (GameObject currTile in retTiles) { tiles.Add(currTile); } break; } } } return(tiles); }
/// <summary> /// Get the closest free tile to the current object /// </summary> /// <param name="location">The location to start at</param> /// <returns>The closest free tile</returns> #pragma warning disable 0219 public static LocationStruct?GetClosestFreeTile(LocationStruct location) { int range = 1; int row = location.row; int column = location.column; TileControl currTile = Board.GetTileControlFromLocation(location); while (range < boardDimensions) { List <GameObject> neighbourTiles = Board.neighbourTiles(location, range); foreach (GameObject go in neighbourTiles) { TileControl tc = go.GetComponent <TileControl>(); if (tc != null) { if (tc.occupyingObject == null) { return(go.GetComponent <BoardLocation>().location); } } } range++; } return(null); }
/// <summary> /// Returns tiles that are adjecant to the current tile, but not diagonally /// </summary> /// <param name="location">The location of the queried tile</param> /// <returns>List of adjecant tiles</returns> public static List <GameObject> neighbourTilesStrait(LocationStruct location, bool ShouldbeFree) { List <GameObject> tiles = new List <GameObject>(); int row = location.row; int column = location.column; if (row + 1 < boardDimensions) { tiles.Add(board[row + 1, column]); } if (column + 1 < boardDimensions) { tiles.Add(board[row, column + 1]); } if (row - 1 >= 0) { tiles.Add(board[row - 1, column]); } if (column - 1 >= 0) { tiles.Add(board[row, column - 1]); } if (ShouldbeFree) { for (int i = tiles.Count - 1; i >= 0; i--) { if (tiles[i].GetComponent <TileControl>().occupyingObject != null) { tiles.Remove(tiles[i]); } } } return(tiles); }
/// <summary> /// Handles a turn start /// </summary> public override void HandleTurnStart() { for (int i = buildQueue.Count - 1; i >= 0; i--) { if (buildQueue[i].turnsLeft <= 0) { LocationStruct?freeTileNullable = Board.GetClosestFreeTile(location); if (freeTileNullable == null) { Debug.LogError("Couldn't find empty location, retrying next turn"); continue; } LocationStruct freeTile = (LocationStruct)freeTileNullable; GameObject newUnit = (GameObject)PhotonNetwork.Instantiate(Utils.RemoveClone(buildQueue[i].productionObject.name), Board.GetTileFromLocation(freeTile).transform.position, new Quaternion(), 0); TileControl tc = Board.GetTileControlFromLocation(freeTile); tc.SetOccupyingObject(newUnit); newUnit.GetComponent <BoardLocation>().SetLocation(freeTile.row, freeTile.column); buildQueue.Remove(buildQueue[i]); } else { buildQueue[i].turnsLeft--; } } }
/// <summary> /// Handles left clicking when placing a building /// </summary> /// <param name="location">The location the action occured from</param> private void BuildingMouseLeft(LocationStruct location) { Building building = currentObject.GetComponent <Building>(); if (building == null) { return; } ResourceCost resCost = currentObject.GetComponent <ResourceCost>(); if (resCost == null) { Debug.Log(string.Format("Buildable object {0} does not have a resource cost script", currentObject.name)); } if (!resCost.CheckCost()) { return; } resCost.DecreaseResources(); selectedPrefab = null; GameObject tile = Board.board[location.row, location.column]; TileControl tc = tile.GetComponent <TileControl>(); GameObject actualObject = (GameObject)PhotonNetwork.Instantiate(Utils.RemoveClone(currentObject.name), currentObject.transform.position, currentObject.transform.rotation, 0); Destroy(HUD.currentObject); currentObject = null; tc.SetOccupyingObject(actualObject); actualObject.GetComponent <BoardLocation>().SetLocation(location.row, location.column); currState = ActionState.NO_ACTION; }
/// <summary> /// Handles the mouseover state while a building is being placed /// </summary> /// <param name="location">The location the mouseoverstate occured from</param> private void BuildingMouseOver(LocationStruct location) { if (currState != ActionState.PLACING_BUILDING) { return; } if (selectedPrefab == null) { return; } if (!selectedPrefab.GetComponent <Building>().IsValidTile(location)) { return; } if (currentObject == null) { currentObject = (GameObject)Instantiate(selectedPrefab, Vector3.zero, new Quaternion(0f, 0f, 0f, 0f)); currentObject.collider.enabled = false; } TileControl tc = Board.board[location.row, location.column].GetComponent <TileControl>(); if (tc.occupyingObject != null) { return; } tc.setObjectProperties(currentObject); }
/// <summary> /// Checks whther the passed tile is free /// overload to easily pass a row and column /// </summary> /// <param name="row">The row to check</param> /// <param name="column">The column to check</param> /// <returns>True if the tile is free, false otherwise</returns> public static bool IsFreeTile(int row, int column) { LocationStruct location = new LocationStruct(); location.row = row; location.column = column; return(IsFreeTile(location)); }
/// <summary> /// Returns a locationstruct from a row and column if passed location is free /// </summary> /// <param name="row">The row of the location</param> /// <param name="column">The column of the location</param> /// <returns>Locationstruct if it was free, null if it wasn't</returns> public static LocationStruct?GetIfFreeTile(int row, int column) { LocationStruct location = new LocationStruct(); location.row = row; location.column = column; if (IsFreeTile(location)) { return(location); } return(null); }
/// <summary> /// Handles mouseover messages /// Also handles Clicks /// </summary> /// <param name="location">The location where the event originated</param> public void MouseOver(LocationStruct location) { if (GUIUtility.hotControl > 0) { return; } //It's not our turn, return if (!MyTurn) { return; } //We haven't selected any object, instead of informing the object of our actions, select whatever object we //clicked, if it was a tile select the object it contained if (currState != ActionState.PLACING_BUILDING && Input.GetMouseButtonDown((int)MouseButtons.LEFT)) { TileControl tc = Board.board[location.row, location.column].GetComponent <TileControl>(); if (tc != null) { HUD.currentObject = tc.occupyingObject; } return; } //We're placing a building, info should be handled by this object else if (currState == ActionState.PLACING_BUILDING) { BuildingMouseOver(location); if (Input.GetMouseButtonDown((int)MouseButtons.LEFT)) { BuildingMouseLeft(location); } if (Input.GetMouseButtonDown((int)MouseButtons.RIGHT)) { Destroy(currentObject); currentObject = null; currState = ActionState.NO_ACTION; return; } } //An object is selected, route our actions to said object else if (currentObject != null) { currentObject.SendMessage("MouseEnter", location, SendMessageOptions.DontRequireReceiver); if (Input.GetMouseButtonDown((int)MouseButtons.RIGHT)) { currentObject.SendMessage("RightClick", location, SendMessageOptions.DontRequireReceiver); } if (Input.GetMouseButtonDown((int)MouseButtons.MIDDLE)) { currentObject.SendMessage("MiddleClick", location, SendMessageOptions.DontRequireReceiver); } } }
/////////////////////////////////////////////////////////////////////// /// Listing 6-11 public List <string> PeopleEmployeedWithinLocation_Structs(int amount, LocationStruct location) { List <string> result = new List <string>(); PersonDataStruct[] input = service.GetPersonsInBatchStructs(amount); DateTime now = DateTime.Now; for (int i = 0; i < input.Length; ++i) { ref PersonDataStruct item = ref input[i]; if (now.Subtract(item.BirthDate).TotalDays > 18 * 365) { var employee = service.GetEmployeeStruct(item.EmployeeId); if (locationService.DistanceWithStruct(ref location, employee.Address) < 10.0) { string name = string.Format("{0} {1}", item.Firstname, item.Lastname); result.Add(name); } } }
public static int AddLocation(LocationStruct location) { if (!IsValidLocation(location)) { return(-1); } string query = string.Format("INSERT INTO Location(Postcode,City, Address) VALUES ({0},'{1}','{2}')", location.postCode, location.city, location.address); OleDbDataReader reader = DatabaseHelper.ExecuteQuery(query); DataTable tbl = DatabaseHelper.GetTable("Location", null, null, true); int locationID = -1; foreach (DataRow row in tbl.Rows) { if (row["Address"].ToString() == location.address) { locationID = int.Parse(row["Location ID"].ToString()); } } return(locationID); }
/// <summary> /// Gets all neighbouring tiles, includes diagonal tiles /// </summary> /// <param name="currLocation">The location to start at</param> /// <param name="range">The range within which to look</param> /// <returns>List containing the neighbouring tiles</returns> public static List <GameObject> neighbourTiles(LocationStruct currLocation, int range) { List <GameObject> adjecantObjects = new List <GameObject>(); int row = currLocation.row; int column = currLocation.column; //Set the range to get objects from int startRangeRow = (row - range < 0) ? 0 : row - range; int startRangeColumn = (column - range < 0) ? 0 : column - range; int endRangeRow = (row + range >= boardDimensions) ? boardDimensions - 1 : row + range; int endRangeColumn = (column + range >= boardDimensions) ? boardDimensions - 1 : column + range; for (int rowNum = startRangeRow; rowNum <= endRangeRow; rowNum++) { for (int colNum = startRangeColumn; colNum <= endRangeColumn; colNum++) { adjecantObjects.Add(board[rowNum, colNum]); } } return(adjecantObjects); }
public List <string> ComplexProcessingBasedOnStructs() { int amount = Amount; LocationStruct location = new LocationStruct(); List <string> result = new List <string>(); InputDataStruct[] input = service.GetDataBatchStructs(amount); DateTime now = DateTime.Now; for (int i = 0; i < input.Length; ++i) { ref InputDataStruct item = ref input[i]; if (now.Subtract(item.BirthDate).TotalDays > 18 * 365) { var employee = service.GetEmployeeStruct(item.EmployeeId); if (locationService.DistanceWithStruct(ref location, employee.Address) < 10.0) { string name = string.Format("{0} {1}", item.Firstname, item.Lastname); result.Add(name); } } }
private void SetLocation() { LocationStruct loc = new LocationStruct(int.Parse(txt_Postcode.Text), txt_Address.Text, txt_City.Text); if (locCmbRelationship.ContainsKey(loc)) { MessageBox.Show("This location already exists. Please use the dropdown box to select this pre-existing location"); } else { locID = loc; } //string query = string.Format("INSERT INTO Location(Postcode,City, Address) VALUES ({0},'{1}','{2}')", int.Parse(txt_Postcode.Text), txt_City.Text, txt_Address.Text); //OleDbDataReader reader = DatabaseHelper.ExecuteQuery(query); //DataTable tbl = DatabaseHelper.GetTable("Location", null, null, true); //foreach (DataRow row in tbl.Rows) //{ // if (row["Address"].ToString() == txt_Address.Text) // locID = int.Parse(row["Location ID"].ToString()); //} Close(); }
/// <summary> /// Right click on an object /// </summary> /// <param name="location">The location this occured</param> public void RightClick(LocationStruct location) { if (walkableTiles.Contains(Board.GetTileFromLocation(location))) { currentPath = Board.GetPathToLocation(boardLocation.location, location, stepsLeft); boardLocation.SetLocation(location.row, location.column); stepsLeft -= currentPath.Count; } else if (attackableTiles.Contains(Board.GetTileFromLocation(location))) { TileControl target = Board.GetTileControlFromLocation(location); GameObject targetObject = target.occupyingObject; if (targetObject == null) { return; } targetObject.SendMessage("Damage", Damage, SendMessageOptions.DontRequireReceiver); Debug.Log("Sentmessage"); attacksLeft--; stepsLeft = 0; } SetWalkableTilesHighlight(false); UpdateWalkableObjects(); }
#pragma warning restore 0219 /// <summary> /// Gets the tilecontrol blonging to the tile of which the location is passed /// </summary> /// <param name="location">The location we want the Tilecontrol from</param> /// <returns>The tilecontrol</returns> public static TileControl GetTileControlFromLocation(LocationStruct location) { return board[location.row, location.column].GetComponent<TileControl>(); }
/// <summary> /// Gets the tile that lives on the current location /// </summary> /// <param name="location">The location of the tile</param> /// <returns>The tile</returns> public static GameObject GetTileFromLocation(LocationStruct location) { return(board[location.row, location.column]); }
/// <summary> /// Initializes this object /// </summary> public override void Initialize() { location = gameObject.GetComponent <BoardLocation>().location; }
/// <summary> /// Handles the mouseover state while a building is being placed /// </summary> /// <param name="location">The location the mouseoverstate occured from</param> private void BuildingMouseOver(LocationStruct location) { if (currState != ActionState.PLACING_BUILDING) { return; } if (selectedPrefab == null) { return; } if (!selectedPrefab.GetComponent<Building>().IsValidTile(location)) { return; } if (currentObject == null) { currentObject = (GameObject)Instantiate(selectedPrefab, Vector3.zero, new Quaternion(0f, 0f, 0f, 0f)); currentObject.collider.enabled = false; } TileControl tc = Board.board[location.row, location.column].GetComponent<TileControl>(); if (tc.occupyingObject != null) { return; } tc.setObjectProperties(currentObject); }
/// <summary> /// Gets all neighbouring tiles, includes diagonal tiles /// Overload for a range of 1 /// </summary> /// <param name="currLocation">The location to start at</param> /// <returns>List containing the neighbouring tiles</returns> public static List <GameObject> neighbourTiles(LocationStruct location) { return(Board.neighbourTiles(location, 1)); }
/// <summary> /// Gets the actual position of a tile from a locationstruct /// </summary> /// <param name="location">The locationstruct</param> /// <returns>Vector3 the location</returns> public static Vector3 GetTileLocation(LocationStruct location) { return(board[location.row, location.column].gameObject.transform.position); }
/// <summary> /// Mouse hovering over object /// </summary> /// <param name="location">The location this event occured</param> public void MouseEnter(LocationStruct location) { }
/// <summary> /// Initializes this object /// </summary> public override void Initialize() { location = gameObject.GetComponent<BoardLocation>().location; }
/// <summary> /// Returns a locationstruct from a row and column if passed location is free /// </summary> /// <param name="row">The row of the location</param> /// <param name="column">The column of the location</param> /// <returns>Locationstruct if it was free, null if it wasn't</returns> public static LocationStruct? GetIfFreeTile(int row, int column) { LocationStruct location = new LocationStruct(); location.row = row; location.column = column; if (IsFreeTile(location)) { return location; } return null; }
/// <summary> /// Middle click on an object /// </summary> /// <param name="location">The location this occured</param> public void MiddleClick(LocationStruct location) { }
/// <summary> /// Checks whther the passed tile is free /// overload to easily pass a row and column /// </summary> /// <param name="row">The row to check</param> /// <param name="column">The column to check</param> /// <returns>True if the tile is free, false otherwise</returns> public static bool IsFreeTile(int row, int column) { LocationStruct location = new LocationStruct(); location.row = row; location.column = column; return IsFreeTile(location); }
/// <summary> /// Checks if the tile at location is still free /// </summary> /// <param name="location">The location to check</param> /// <returns>True if it is, false otherwise</returns> public static bool IsFreeTile(LocationStruct location) { return GetTileControlFromLocation(location).occupyingObject == null; }
/// <summary> /// Gets the tile that lives on the current location /// </summary> /// <param name="location">The location of the tile</param> /// <returns>The tile</returns> public static GameObject GetTileFromLocation(LocationStruct location) { return board[location.row, location.column]; }
/// <summary> /// Checks if the tile at location is still free /// </summary> /// <param name="location">The location to check</param> /// <returns>True if it is, false otherwise</returns> public static bool IsFreeTile(LocationStruct location) { return(GetTileControlFromLocation(location).occupyingObject == null); }
/// <summary> /// Handles left clicking when placing a building /// </summary> /// <param name="location">The location the action occured from</param> private void BuildingMouseLeft(LocationStruct location) { Building building = currentObject.GetComponent<Building>(); if (building == null) { return; } ResourceCost resCost = currentObject.GetComponent<ResourceCost>(); if (resCost == null) { Debug.Log(string.Format("Buildable object {0} does not have a resource cost script", currentObject.name)); } if (!resCost.CheckCost()) { return; } resCost.DecreaseResources(); selectedPrefab = null; GameObject tile = Board.board[location.row, location.column]; TileControl tc = tile.GetComponent<TileControl>(); GameObject actualObject = (GameObject) PhotonNetwork.Instantiate(Utils.RemoveClone(currentObject.name), currentObject.transform.position, currentObject.transform.rotation, 0); Destroy(HUD.currentObject); currentObject = null; tc.SetOccupyingObject(actualObject); actualObject.GetComponent<BoardLocation>().SetLocation(location.row, location.column); currState = ActionState.NO_ACTION; }
public bool IsValidTile(LocationStruct location) { TileControl tc = Board.GetTileControlFromLocation(location); return tc.TileType == RequiredTileType || RequiredTileType == TileType.Any; }
/// <summary> /// Finds a path from a location, to a location, within a number of steps /// </summary> /// <param name="fromLocation">The location to start from</param> /// <param name="toLocation">The location to end up at</param> /// <param name="stepsLeft">The numver of steps left</param> /// <returns>A list of gameobjects in the path</returns> public static List<GameObject> GetPathToLocation(LocationStruct fromLocation, LocationStruct toLocation, int stepsLeft) { List<GameObject> tiles = new List<GameObject>(); if (stepsLeft < 0) { return tiles; } else if (fromLocation.row == toLocation.row && fromLocation.column == toLocation.column) { GameObject currTile = Board.GetTileFromLocation(fromLocation); tiles.Add(currTile); } else { List<GameObject> neighbourTiles = Board.neighbourTilesStrait(fromLocation, true); foreach (GameObject neighbour in neighbourTiles) { List<GameObject> retTiles = GetPathToLocation(neighbour.GetComponent<BoardLocation>().location, toLocation, stepsLeft - 1); if (retTiles.Count > 0) { tiles.Add(Board.GetTileFromLocation(fromLocation)); foreach (GameObject currTile in retTiles) { tiles.Add(currTile); } break; } } } return tiles; }
/// <summary> /// Handles a right click /// </summary> /// <param name="location">The location the right click was initiated</param> public void RightClick(LocationStruct location) { HUD.currentObject = null; }
/// <summary> /// A left click on an object /// </summary> /// <param name="location">The location this occured</param> public void LeftClick(LocationStruct location) { }
public bool IsValidTile(LocationStruct location) { TileControl tc = Board.GetTileControlFromLocation(location); return(tc.TileType == RequiredTileType || RequiredTileType == TileType.Any); }
#pragma warning restore 0219 /// <summary> /// Gets the tilecontrol blonging to the tile of which the location is passed /// </summary> /// <param name="location">The location we want the Tilecontrol from</param> /// <returns>The tilecontrol</returns> public static TileControl GetTileControlFromLocation(LocationStruct location) { return(board[location.row, location.column].GetComponent <TileControl>()); }
/// <summary> /// Gets all neighbouring tiles, includes diagonal tiles /// </summary> /// <param name="currLocation">The location to start at</param> /// <param name="range">The range within which to look</param> /// <returns>List containing the neighbouring tiles</returns> public static List<GameObject> neighbourTiles(LocationStruct currLocation, int range) { List<GameObject> adjecantObjects = new List<GameObject>(); int row = currLocation.row; int column = currLocation.column; //Set the range to get objects from int startRangeRow = (row - range < 0) ? 0 : row - range; int startRangeColumn = (column - range < 0) ? 0 : column - range; int endRangeRow = (row + range >= boardDimensions) ? boardDimensions - 1 : row + range; int endRangeColumn = (column + range >= boardDimensions) ? boardDimensions - 1 : column + range; for (int rowNum = startRangeRow; rowNum <= endRangeRow; rowNum++) { for (int colNum = startRangeColumn; colNum <= endRangeColumn; colNum++) { adjecantObjects.Add(board[rowNum, colNum]); } } return adjecantObjects; }
/// <summary> /// Get the closest free tile to the current object /// </summary> /// <param name="location">The location to start at</param> /// <returns>The closest free tile</returns> #pragma warning disable 0219 public static LocationStruct? GetClosestFreeTile(LocationStruct location) { int range = 1; int row = location.row; int column = location.column; TileControl currTile = Board.GetTileControlFromLocation(location); while (range < boardDimensions) { List<GameObject> neighbourTiles = Board.neighbourTiles(location, range); foreach (GameObject go in neighbourTiles) { TileControl tc = go.GetComponent<TileControl>(); if (tc != null) { if (tc.occupyingObject == null) { return go.GetComponent<BoardLocation>().location; } } } range++; } return null; }
internal double DistanceWithStruct(ref LocationStruct location, string address) { return(1.0); }
/// <summary> /// Gets all neighbouring tiles, includes diagonal tiles /// Overload for a range of 1 /// </summary> /// <param name="currLocation">The location to start at</param> /// <returns>List containing the neighbouring tiles</returns> public static List<GameObject> neighbourTiles(LocationStruct location) { return Board.neighbourTiles(location, 1); }
/// <summary> /// Returns tiles that are adjecant to the current tile, but not diagonally /// </summary> /// <param name="location">The location of the queried tile</param> /// <returns>List of adjecant tiles</returns> public static List<GameObject> neighbourTilesStrait(LocationStruct location, bool ShouldbeFree) { List<GameObject> tiles = new List<GameObject>(); int row = location.row; int column = location.column; if (row + 1 < boardDimensions) { tiles.Add(board[row + 1, column]); } if (column + 1 < boardDimensions) { tiles.Add(board[row, column + 1]); } if (row - 1 >= 0) { tiles.Add(board[row - 1, column]); } if (column - 1 >= 0) { tiles.Add(board[row, column - 1]); } if (ShouldbeFree) { for (int i = tiles.Count - 1; i >= 0; i--) { if (tiles[i].GetComponent<TileControl>().occupyingObject != null) { tiles.Remove(tiles[i]); } } } return tiles; }
/// <summary> /// Gets the actual position of a tile from a locationstruct /// </summary> /// <param name="location">The locationstruct</param> /// <returns>Vector3 the location</returns> public static Vector3 GetTileLocation(LocationStruct location) { return board[location.row, location.column].gameObject.transform.position; }
/// <summary> /// Handles mouseover messages /// Also handles Clicks /// </summary> /// <param name="location">The location where the event originated</param> public void MouseOver(LocationStruct location) { if (GUIUtility.hotControl > 0) { return; } //It's not our turn, return if (!MyTurn) { return; } //We haven't selected any object, instead of informing the object of our actions, select whatever object we //clicked, if it was a tile select the object it contained if (currState != ActionState.PLACING_BUILDING && Input.GetMouseButtonDown((int)MouseButtons.LEFT)) { TileControl tc = Board.board[location.row, location.column].GetComponent<TileControl>(); if (tc != null) { HUD.currentObject = tc.occupyingObject; } return; } //We're placing a building, info should be handled by this object else if (currState == ActionState.PLACING_BUILDING) { BuildingMouseOver(location); if (Input.GetMouseButtonDown((int)MouseButtons.LEFT)) { BuildingMouseLeft(location); } if (Input.GetMouseButtonDown((int)MouseButtons.RIGHT)) { Destroy(currentObject); currentObject = null; currState = ActionState.NO_ACTION; return; } } //An object is selected, route our actions to said object else if (currentObject != null) { currentObject.SendMessage("MouseEnter", location, SendMessageOptions.DontRequireReceiver); if (Input.GetMouseButtonDown((int)MouseButtons.RIGHT)) { currentObject.SendMessage("RightClick", location, SendMessageOptions.DontRequireReceiver); } if (Input.GetMouseButtonDown((int)MouseButtons.MIDDLE)) { currentObject.SendMessage("MiddleClick", location, SendMessageOptions.DontRequireReceiver); } } }