/// <summary> /// Pick up a tile from the store /// </summary> /// <param name="gridPos">Which tile we clicked</param> public override void PickupTile(Vector2 gridPos) { // During the place state, player cannot pick tiles up from the store if (StateManager.State == GameState.PLACE_STATE) { return; } // During the buy phase ATile tile = grid[(int)gridPos.x, (int)gridPos.y]; if (tile == null) { return; } // The player can only pick tiles up if they have the funds for it if (StateManager.ActivePlayer.CanAfford(tile.Price)) { GivePlayerTile(tile.name); } else { Extensions.DoPopUp("Insufficient funds", 2); } }
/// <summary> /// Place a tile onto the board /// </summary> /// <param name="tile">The tile to be place onto the board</param> /// <returns></returns> public override bool PlaceTile(ATile tile) { // During the buy phase, placing a tile removes its cost from the debt of the active player, and destroys the tile if (StateManager.State == GameState.BUY_STATE) { StateManager.ActivePlayer.Debt += tile.Price; Destroy(tile.gameObject); } // During the place phase, placing a tile deletes it if its owner is not the active player else { if (tile.owner != StateManager.ActivePlayer) { Destroy(tile.gameObject); StateManager.ActionCounter--; } else { Extensions.DoPopUp("Cannot Destroy own tiles", 2); } } // We succesfully placed the tile in the store return(true); }
void Update() { switch (_state) { case EUnitState.Stop: break; case EUnitState.Move: MajActualFloor(); _currentTileId = MajCurrentTileId(_currentTileId, _currentTile); _nextTile = GetNexTile(_currentTileId); if (_nextTile != null) { MoveToTile(_nextTile); } break; case EUnitState.SearchPath: MajActualFloor(); if (PathData != null && _currentTile != null) { _pathToFollow = FloorManager.Instance.GetPath(_currentTile, PathData.destinationTile); SetOnTopOfObject(_currentTile.transform); _state = EUnitState.Move; } else { Debug.Log("No path data or currentTile for this Unit : " + this.name); } break; } }
internal void OnDestinationReach(ATile a_destinationReach) { if (a_destinationReach.Equals(PathData.destinationTile)) { Destroy(this.gameObject); } }
/// <summary> /// Pick a tile up from the board /// </summary> /// <param name="gridPos">The position in the grid of the tile we want to pick up</param> public override void PickupTile(Vector2 gridPos) { ATile tile = grid[(int)gridPos.x, (int)gridPos.y]; if (tile == null) { return; } // We cannot pick tiles up if we are in the buy phase, or if we own them, but didn't place them this week if (StateManager.State == GameState.BUY_STATE || (tile.owner == StateManager.ActivePlayer && tile.placedWeek != StateManager.WeekCounter)) { return; } // Taking a tile back takes back an action. if (tile.owner == StateManager.ActivePlayer) { StateManager.ActionCounter++; } // Remove the reward of the placed tile from the rewards that player gets every turn. tile.owner.Reward -= tile.Reward; base.PickupTile(gridPos); }
/// <summary> /// Place a tile onto the board /// </summary> /// <param name="tile">The tile to be place onto the board</param> /// <returns></returns> public override bool PlaceTile(ATile tile) { // Prevent illegal placing if (StateManager.State == GameState.BUY_STATE || !base.PlaceTile(tile)) { return(false); } // Placing a tile takes an action StateManager.ActionCounter--; tile.tilePositionState = TilePositionState.Board; // Tiles can be freely picked up if they were placed by the player this turn. Otherwise you can only pick up opponents tiles to destroy them. if (tile.owner == StateManager.ActivePlayer) { tile.placedWeek = StateManager.WeekCounter; } // Add the reward of the placed tile to the rewards that player gets every turn. tile.owner.Reward += tile.Reward; // We succeeded in placing the tile return(true); }
private void MajActualFloor() { int layerMask = 1 << 8; // Physics RaycastHit hit; // Does the ray intersect any objects excluding the player layer if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, Mathf.Infinity, layerMask)) { //SetOnTopOfObject(hit.collider.gameObject.transform); if (_currentGameObject != hit.collider.gameObject) { _currentGameObject = hit.collider.gameObject; ATile tile = hit.collider.gameObject.GetComponent <ATile>(); if (tile && tile != _currentTile) { _currentTile = tile; } Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.yellow); } } else { Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * 1000, Color.white); } }
/// <summary> /// Generates a tile /// </summary> /// <param name="i">The iteration, used to get the right tile</param> /// <param name="pos">The grid position for the new tile</param> ATile GenerateTile(int i, Vector2 pos) { ATile tile = Instantiate(prefabs[i]); tile.transform.position = GridPosToWorld(pos, transform.position.y); tile.storagePlace = this; tile.gridPos = pos; tile.tilePositionState = TilePositionState.Store; return(tile); }
// Set variables at the Start protected override void Start() { base.Start(); width = 3; height = 2; grid = new ATile[width, height]; tileWidth = 1.25f; tileHeight = 1.2f; }
// Set variables at the Start protected override void Start() { base.Start(); width = 7; height = 7; grid = new ATile[width, height]; tileWidth = 1.333f; tileHeight = tileWidth; }
/// <summary> /// Generates the tile in its grid /// </summary> void GenerateTiles() { // Iterate over all prefabs, calculating the gridpos and setting up the rest for (int i = 0; i < prefabs.Length; i++) { Vector2 pos = new Vector2(i % width, i / width); ATile tile = GenerateTile(i, pos); grid[(int)pos.x, (int)pos.y] = tile; } }
/// <summary> /// Place a tile onto the board /// </summary> /// <param name="tile">The tile to be place onto the board</param> /// <returns></returns> public override bool PlaceTile(ATile tile) { // Prevent illegal moves if (tile.owner != owner || owner != StateManager.ActivePlayer || !base.PlaceTile(tile)) { return(false); } // Set tile to greenhouse tile.tilePositionState = TilePositionState.GreenHouse; return(true); }
private AStarWrapperTile GetWrapperFromTile(List <AStarWrapperTile> a_allTiles, ATile a_tile) { foreach (AStarWrapperTile tile in a_allTiles) { if (tile.tile.Equals(a_tile)) { return(tile); } } return(null); }
void Update() { if (UnitManager.gameUnits[UnitManager.currUnitTurn].playerControlled & UnitManager.actionPoints > 0) { if (Input.GetMouseButtonDown(0)) { mousePos = Input.mousePosition; tilePos = tileMap.WorldToCell(Camera.main.ScreenToWorldPoint(mousePos)); //Thorws a raycast in the direction of the target RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(mousePos), Vector2.zero, Mathf.Infinity, mask); if (hit.collider != null) { /* * Calculate pathfinding and apply * * Insert Code Here * */ Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3Int clickPos = tileMap.WorldToCell(mouseWorldPos); // Temporary movment code // jumpt to mouse click StartCoroutine(moveOverSeconds(UnitManager.gameUnits[UnitManager.currUnitTurn].gameObject, tilePos)); // get cselected tile ATile aTile = (ATile)tileMap.GetTile(tilePos); //apply tile movement cost to available action points UnitManager.actionPoints -= aTile.movementSpeed; } } } // if is AI controlled unit's turn and still has action points else if (UnitManager.actionPoints > 0)//AI's turn { AIUnitController.CalculateAITurn(); } // if is AI controlled unit's turn and has no more action points else if (!UnitManager.gameUnits[UnitManager.currUnitTurn].playerControlled & UnitManager.actionPoints <= 0) { //Initiate next unit turn } // else wait for player to click end turn button }
private ATile GetNexTile(int a_currentTileId) { ATile nextTile = null; if (a_currentTileId > -1 && _pathToFollow != null) { int destinationTileId = _currentTileId; if (_currentTileId < _pathToFollow.Count - 1) { destinationTileId++; } nextTile = _pathToFollow[destinationTileId]; } return(nextTile); }
private void MoveToTile(ATile a_tileDestination) { float x = Corp(this.transform.position.x, a_tileDestination.transform.position.x + (height * a_tileDestination.transform.up.x), speed * SGMGameManager.Instance.timeManager.DeltaTime(ETimeCategory.Gameplay)); float y = Corp(this.transform.position.y, a_tileDestination.transform.position.y + (height * a_tileDestination.transform.up.y), speed * SGMGameManager.Instance.timeManager.DeltaTime(ETimeCategory.Gameplay)); float z = Corp(this.transform.position.z, a_tileDestination.transform.position.z + (height * a_tileDestination.transform.up.z), speed * SGMGameManager.Instance.timeManager.DeltaTime(ETimeCategory.Gameplay)); this.transform.position = new Vector3(x, y, z); /* * float distance = Vector3.Distance(this.transform.position, new Vector3( * a_tileDestination.transform.position.x + (height * a_tileDestination.transform.up.x), * a_tileDestination.transform.position.y + (height * a_tileDestination.transform.up.y), * a_tileDestination.transform.position.z + (height * a_tileDestination.transform.up.z) * )); * if( distance > -0.1 && distance < 0.1) * canMove = false; */ }
private void ComputeInterPaths(int x, int y) { ATile tile = atiles[x, y]; for (int ea = 0; ea < 3; ++ea) { Edge edgeA = tile.edges[ea]; if (edgeA == null) { continue; } for (int eb = ea + 1; eb < 4; ++eb) { Edge edgeB = tile.edges[eb]; if (edgeB == null) { continue; } AStar.Path shortestPath = null; for (int a = 0; a < edgeA.pts.Length; ++a) { Vec2I ptA = edgeA.pts[a]; for (int b = 0; b < edgeB.pts.Length; ++b) { Vec2I ptB = edgeB.pts[b]; var path = AStar.FindPath( cacheInternal, ptA, tile.passFn, pt => pt == ptB, pt => Vec2I.Distance(ptB, pt)); if (shortestPath == null || path.g < shortestPath.g) { shortestPath = path; } } } tile.edges[ea].pathsToEdge[eb] = shortestPath; tile.edges[eb].pathsToEdge[ea] = shortestPath.Reversed(); } } }
/// <summary> /// Place a tile onto the board /// </summary> /// <param name="tile">The tile to be place onto the board</param> /// <returns></returns> public virtual bool PlaceTile(ATile tile) { // Get the grid position where the mouse hovers Vector3 pos = Extensions.inputSystem.GetCursorScreenPosition(10); Vector2 gridPos = WorldToGridPos(pos); // If there is already a tile, or if we try to illegally move a tile, return a fail if (grid[(int)gridPos.x, (int)gridPos.y] != null || (tile.owner != StateManager.ActivePlayer && tile.gridPos != gridPos)) { return(false); } // Place tile in the grid and set tile variables grid[(int)gridPos.x, (int)gridPos.y] = tile; tile.transform.position = GridPosToWorld(gridPos, transform.position.y); tile.storagePlace = this; tile.gridPos = gridPos; // Return succes return(true); }
/// <summary> /// Pick a tile up from the board /// </summary> /// <param name="gridPos">The position in the grid of the tile we want to pick up</param> public virtual void PickupTile(Vector2 gridPos) { // Only possible if the player has actions left if (StateManager.ActionCounter <= 0) { return; } // If there is tile, place it to hand and reset variables ATile tile = grid[(int)gridPos.x, (int)gridPos.y]; if (tile != null) { tile.storagePlace = null; tile.tilePositionState = TilePositionState.Hand; grid[(int)gridPos.x, (int)gridPos.y] = null; } else { Debug.Log("Tried to remove non-existent tile"); } }
private int MajCurrentTileId(int a_currentTileId, ATile a_currentTile) { int currentTileId = -1; if (a_currentTileId == -1 || !a_currentTile.Equals(_pathToFollow[a_currentTileId])) { currentTileId = -1; for (int i = 0; i < _pathToFollow.Count; i++) { if (_currentTile.Equals(_pathToFollow[i])) { currentTileId = i; } } } else { currentTileId = a_currentTileId; } return(currentTileId); }
//private readonly AStar.ISearchCache cacheAbstract; /* Debug Vis Stuff * * void KD_AddCircle(Vec2I pt, bool big) * { * GameObject child = new GameObject(); * child.transform.SetParent(transform, false); * * var color = Color.red * (big ? .8f : .4f); * float rad = big ? .5f : .4f; * * List<Vec2> pts = new List<Vec2>(); * for (int i = 0; i < 32; ++i) * { * float theta = i * 2 * Mathf.PI / 32f; * float x = Mathf.Cos(theta) * rad; * float y = Mathf.Sin(theta) * rad; * pts.Add(new Vec2(x + pt.x + .5f, y + pt.y + .5f)); * } * * * child.AddLineRenderer("Highlight", 2001, color, 1 / 32f, true, true, pts.ToArray()); * } * * void KD_AddPath(IEnumerable<Vec2I> path) * { * GameObject child = new GameObject(); * child.transform.SetParent(transform, false); * child.AddLineRenderer("Highlight", 2000, Color.black, 1 / 32f, false, true, * path.Select(pt => pt + new Vec2(.5f, .5f)).ToArray()); * } * * void KD_AddSquare(Vec2I o) * { * GameObject child = new GameObject(); * child.transform.SetParent(transform, false); * child.AddLineRenderer("Highlight", 1999, Color.white, 1 / 32f, true, true, * new Vec2[] {o, new Vec2(o.x + 8, o.y), new Vec2(o.x+8, o.y+8), new Vec2(o.x, o.y+8)} ); * } * * // K_deubg vis * for (int xa = 0; xa < 8; ++xa) * { * for (int ya = 0; ya < 8; ++ya) * { * int x = 8 * xa; * int y = 8 * ya; * Vec2I o = new Vec2I(x, y); * KD_AddSquare(o); * var tile = nav.atiles[xa, ya]; * for (int ea = 0; ea < 4; ++ea) * { * var edgeA = tile.edges[ea]; * if (edgeA == null) continue; * * for (int i = 0; i < edgeA.pts.Length; ++i) * KD_AddCircle(o + edgeA.pts[i], ea % 2 == 0); * * for (int eb = ea + 1; eb < 4; ++eb) * { * var path = edgeA.pathsToEdge[eb]; * if (path != null) * KD_AddPath(path.pts.Select(pt => pt + o)); * } * } * } * }*/ public Nav__Unfinished(Vec2I size, Func <Vec2I, bool> passFn) { BB.Assert(size.x % aDim == 0); BB.Assert(size.y % aDim == 0); //this.size = size; this.passFn = passFn; this.asize = new Vec2I(size.x / aDim, size.y / aDim); this.atiles = new ATile[asize.x, asize.y]; for (int x = 0; x < asize.x; ++x) { for (int y = 0; y < asize.y; ++y) { atiles[x, y] = new ATile(passFn, x, y); } } this.cacheInternal = AStar.CreateSearchCache(new Vec2I(aDim, aDim)); //this.cacheAbstract = AStar.CreateSearchCache(asize); ComputeAbstract(); // ??? }
/// <summary> /// Picking a tile up from the store makes a new one of the correct type and color, and gives it to the player /// </summary> /// <param name="name">What kind of tile we want to make</param> public void GivePlayerTile(string name) { int playerNr = StateManager.ActivePlayerNumber; Player player = StateManager.ActivePlayer; // Only create tiles with a valid name if (!Extensions.playerTiles[playerNr].ContainsKey(name)) { return; } // Create the tile ATile created = Instantiate(Extensions.playerTiles[playerNr][name]); created.storagePlace = null; created.tilePositionState = TilePositionState.Hand; created.owner = player; // Add the cost of the tile to the debt of the player Debug.Log("Adding Player Debt: " + StateManager.ActivePlayer.Debt + "\nAdding: " + created.Price); player.Debt -= created.Price; Debug.Log("Added Debt, debt remaining: " + StateManager.ActivePlayer.Debt); }
internal void OnTeleport(ATile a_destinationToGo) { transform.position = a_destinationToGo.transform.position; SetOnTopOfObject(a_destinationToGo.transform); }
public AStarWrapperTile(ATile a_tile) { this.tile = a_tile; }
internal void RegisterFloor(ATile a_floor) { floors.Add(a_floor); }
internal List <ATile> GetPath(ATile a_origin, ATile a_dest) { AStarPathFinder pathFinder = new AStarPathFinder(); return(pathFinder.GetPath(floors, a_origin, a_dest)); }
// Source : http://theory.stanford.edu/~amitp/GameProgramming/ImplementationNotes.html public List <ATile> GetPath(List <ATile> a_tiles, ATile a_fromTile, ATile a_toTile) { List <ATile> path = new List <ATile>(); // Initialize array List <AStarWrapperTile> allWrappedTiles = new List <AStarWrapperTile>(); List <AStarWrapperTile> open = new List <AStarWrapperTile>(); List <AStarWrapperTile> close = new List <AStarWrapperTile>(); // Initialize AStarTiles AStarWrapperTile destination = null; foreach (ATile tile in a_tiles) { AStarWrapperTile wrappedTile = new AStarWrapperTile(tile); allWrappedTiles.Add(wrappedTile); if (tile.Equals(a_fromTile)) { open.Add(wrappedTile); } if (tile.Equals(a_toTile)) { destination = wrappedTile; } } // Compute Algorithm AStarWrapperTile currentNode = null; while (open.Count > 0) { float lowF = -1; // find the node with the least f on the open list, call it "q" foreach (AStarWrapperTile tile in open) { if (lowF == -1 || tile.f < lowF) { lowF = tile.f; currentNode = tile; } } if (currentNode == destination) { break; } // pop q off the open list open.Remove(currentNode); // push q on the closed list close.Add(currentNode); List <Link> connections = currentNode.tile.GetConnections(); for (int i = 0; i < connections.Count; i++) { Link link = connections[i]; AStarWrapperTile wrappedTileNeighboor = GetWrapperFromTile(allWrappedTiles, link.Tile); if (wrappedTileNeighboor != null && wrappedTileNeighboor.tile.isWalkable && !close.Contains(wrappedTileNeighboor)) { float gScore = currentNode.g + currentNode.getCostTo(wrappedTileNeighboor); if (!open.Contains(wrappedTileNeighboor)) { wrappedTileNeighboor.h = wrappedTileNeighboor.getHeuristicTo(destination); open.Add(wrappedTileNeighboor); wrappedTileNeighboor.parent = currentNode; wrappedTileNeighboor.g = gScore; wrappedTileNeighboor.f = wrappedTileNeighboor.g + wrappedTileNeighboor.h; } else if (gScore < wrappedTileNeighboor.g) { wrappedTileNeighboor.parent = currentNode; wrappedTileNeighboor.g = gScore; wrappedTileNeighboor.f = wrappedTileNeighboor.g + wrappedTileNeighboor.h; } } } } // Construct final path AStarWrapperTile cursor = destination; while (cursor != null) { path.Add(cursor.tile); cursor = cursor.parent; } path.Reverse(); return(path); }