protected void Wander(TileManager.Tile stayNearTile, int stayNearTileDistance) { if (wanderTimer <= 0) { List <TileManager.Tile> validWanderTiles = overTile.surroundingTiles .Where(tile => tile != null && tile.walkable && tile.buildable && GameManager.humanM.humans.Find(human => human.overTile == tile) == null) .Where(tile => tile.GetObjectInstanceAtLayer(2) == null) .ToList(); if (stayNearTile != null) { validWanderTiles = validWanderTiles.Where(t => Vector2.Distance(t.obj.transform.position, stayNearTile.obj.transform.position) <= stayNearTileDistance).ToList(); if (Vector2.Distance(obj.transform.position, stayNearTile.obj.transform.position) > stayNearTileDistance) { MoveToTile(stayNearTile, false); return; } } if (validWanderTiles.Count > 0) { MoveToTile(validWanderTiles[UnityEngine.Random.Range(0, validWanderTiles.Count)], false); } wanderTimer = UnityEngine.Random.Range(10f, 20f); } else { wanderTimer -= 1 * GameManager.timeM.deltaTime; } }
public Caravan(int numTraders, CaravanTypeEnum caravanType, List <TileManager.Tile> spawnTiles, TileManager.Tile targetTile) { this.numTraders = numTraders; this.caravanType = caravanType; this.targetTile = targetTile; location = GameManager.caravanM.CreateLocation(); for (int i = 0; i < numTraders && spawnTiles.Count > 0; i++) { TileManager.Tile spawnTile = spawnTiles[UnityEngine.Random.Range(0, spawnTiles.Count)]; SpawnTrader(spawnTile); spawnTiles.Remove(spawnTile); } inventory = new ResourceManager.Inventory(this, int.MaxValue, int.MaxValue); resourceGroup = GameManager.resourceM.GetRandomResourceGroup(); foreach (ResourceManager.Resource resource in resourceGroup.resources.OrderBy(r => UnityEngine.Random.Range(0f, 1f))) // Randomize resource group list { int resourceGroupResourceCount = Mathf.Clamp(resourceGroup.resources.Count, minDistinctResources + 1, int.MaxValue); // Ensure minimum count of (minimumDistinctResources + 1) if (UnityEngine.Random.Range(0f, 1f) < Mathf.Clamp(((resourceGroupResourceCount - inventory.resources.Count) - minDistinctResources) / (float)(resourceGroupResourceCount - minDistinctResources), minDistinctResourceChance, 1f)) // Decrease chance of additional distinct resources on caravan as distinct resources on caravan increase { int resourceAvailableAmount = resource.GetAvailableAmount(); int caravanAmount = Mathf.RoundToInt(Mathf.Clamp(UnityEngine.Random.Range(resourceAvailableAmount * minAvailableAmountModifier, resourceAvailableAmount * maxAvailableAmountModifier), UnityEngine.Random.Range(minMinimumCaravanAmount, maxMinimumCaravanAmount), int.MaxValue)); // Ensure a minimum number of the resource on the caravan inventory.ChangeResourceAmount(resource, caravanAmount, false); } } }
/// <summary> /// Déplace pacman en direction d'une tile /// </summary> /// <param name="t">La tile cible</param> void MoveTowardsTile(TileManager.Tile t) { Vector2 p = Vector2.MoveTowards(transform.position, new Vector3(t.x, t.y), Speed); Direction = new Vector3(t.x, t.y) - transform.position; GetComponent <Rigidbody2D>().MovePosition(p); }
private bool MineCheck(AntDir antDir) { TileManager.Tile tile = TileManager.Tile.공백; switch (antDir) { case AntDir.Up: default: if (!TileManager.instance.DirtWallCheck(location, new Vector2Int(location.x, location.y + 1))) { // tile = TileManager.instance.GetTile(new Vector2Int(location.x, location.y + 1)); if (TileManager.instance.DigCheck(new Vector2Int(location.x, location.y + 1))) { nextLocation = location; nextLocation.y++; return(true); } } return(false); case AntDir.Down: if (!TileManager.instance.DirtWallCheck(location, new Vector2Int(location.x, location.y - 1))) { //tile = TileManager.instance.GetTile(new Vector2Int(location.x, location.y - 1)); if (TileManager.instance.DigCheck(new Vector2Int(location.x, location.y - 1))) { nextLocation = location; nextLocation.y--; return(true); } } return(false); case AntDir.Right: if (!TileManager.instance.DirtWallCheck(location, new Vector2Int(location.x + 1, location.y))) { // tile = TileManager.instance.GetTile(new Vector2Int(location.x + 1, location.y)); if (TileManager.instance.DigCheck(new Vector2Int(location.x + 1, location.y))) { nextLocation = location; nextLocation.x++; return(true); } } return(false); case AntDir.Left: if (!TileManager.instance.DirtWallCheck(location, new Vector2Int(location.x - 1, location.y))) { // tile = TileManager.instance.GetTile(new Vector2Int(location.x - 1, location.y)); if (TileManager.instance.DigCheck(new Vector2Int(location.x - 1, location.y))) { nextLocation = location; nextLocation.x--; return(true); } } return(false); } }
public void ChaseLogic() { Vector3 currentPos = new Vector3(transform.position.x + .499f, transform.position.y + .499f); TileManager.Tile currentTile = tileManager.GetTileByPos(currentPos); targetTile = GetTargetTile(); nextTile = tileManager.GetTileByPos(currentPos + ghostMovement.Direction); if (nextTile.Occupied) { if (ghostMovement.Direction == Vector3.left || ghostMovement.Direction == Vector3.right) { ghostMovement.Direction = currentTile.down == null ? Vector3.up : Vector3.down; } else { ghostMovement.Direction = currentTile.left == null ? Vector3.right : Vector3.left; } } else if (currentTile.IsIntersection) { float shortstDis = float.MaxValue; Vector3 dir = ghostMovement.Direction; if (currentTile.up != null && ghostMovement.Direction != Vector3.down && shortstDis > tileManager.GetTileDistance(targetTile, currentTile.up)) { ghostMovement.Direction = Vector3.up; shortstDis = tileManager.GetTileDistance(targetTile, currentTile.up); } if (currentTile.right != null && ghostMovement.Direction != Vector3.left && shortstDis > tileManager.GetTileDistance(targetTile, currentTile.right)) { ghostMovement.Direction = Vector3.right; shortstDis = tileManager.GetTileDistance(targetTile, currentTile.right); } if (currentTile.left != null && ghostMovement.Direction != Vector3.right && shortstDis > tileManager.GetTileDistance(targetTile, currentTile.left)) { ghostMovement.Direction = Vector3.left; shortstDis = tileManager.GetTileDistance(targetTile, currentTile.left); } if (currentTile.down != null && ghostMovement.Direction != Vector3.up && shortstDis > tileManager.GetTileDistance(targetTile, currentTile.down)) { ghostMovement.Direction = Vector3.down; } if (dir.Equals(ghostMovement.Direction)) { Debug.Log("dir change"); } } else { ghostMovement.Direction = ghostMovement.Direction; } }
private TileManager.Tile GetTargetTilePerGhost() { Vector3 targetPos; TileManager.Tile targetTile; Vector3 dir; // get the target tile position (round it down to int so we can reach with Index() function) switch (name) { case "blinky": // target = pacman targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f); targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "pinky": // target = pacman + 4*pacman's direction (4 steps ahead of pacman) dir = target.GetComponent <PlayerController>().getDir(); targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f) + 4 * dir; // if pacmans going up, not 4 ahead but 4 up and 4 left is the target // read about it here: http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior // so subtract 4 from X coord from target position if (dir == Vector3.up) { targetPos -= new Vector3(4, 0, 0); } targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "inky": // target = ambushVector(pacman+2 - blinky) added to pacman+2 dir = target.GetComponent <PlayerController>().getDir(); var blinkyPos = GameObject.Find("blinky").transform.position; var ambushVector = target.position + 2 * dir - blinkyPos; targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f) + 2 * dir + ambushVector; targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "clyde": targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f); targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; if (manager.distance(targetTile, currentTile) < 9) { targetTile = tiles[manager.Index(0, 2)]; } break; default: targetTile = null; Debug.Log("TARGET TILE NOT ASSIGNED"); break; } return(targetTile); }
/// <summary> /// Etat pendant lequel pacman se déplace d'un tile à un autre /// </summary> void AI_MovingToNextTile() { if (NextTile == null) { Brain.SetState(AI_DecideDirection); return; } // Si on atteint le tile destination if (Vector3.Distance(transform.position, new Vector3(NextTile.x, NextTile.y)) <= 0.000000000001) { // Si on a une liste de waypoints, continuer if (Waypoints.Count > 0) { NextTile = Waypoints.Dequeue(); // Si on est déjà sur le nextTile passer au suivant while (NextTile == CurrentTile) { NextTile = Waypoints.Dequeue(); } // A changer List <TileManager.Tile> wp = new List <TileManager.Tile>(Waypoints); // On vérifie si un ghost ne s'est pas mis sur le reste du chemin foreach (TileManager.Tile t in wp) { // Si c'est le cas on annule tout et on cherche un autre chemin if (t.isDangerous) { Waypoints.Clear(); NextTile = null; Brain.SetState(AI_DecideDirection); break; } } } // Sinon reset le next else { NextTile = null; // Si on arrive à une intersection, choisir une direction // On devrait toujours rentrer dans ce if normalement if (CurrentTile.isIntersection) { Brain.SetState(AI_DecideDirection); } } } else { // On n'a pas encore atteint le prochain tile, on se dirige vers celui-ci MoveTowardsTile(NextTile); } }
private void DrawTile(TileManager.Tile tile, int x, int y) { if (TileDrawings[x, y] == null) // If a tile has not already been placed at the defined coordinates... { TileDrawings[x, y] = tile; // Add a tile to the drawing loop. } else // If a tile has already been placed at the defined coordinates... { TileDrawings[x, y] = null; // Erase the tile. TileDrawings[x, y] = tile; } Refresh(); // IT'S REDRAWING TIME! }
TileManager.Tile GetTargetTilePerGhost() { Vector3 targetPos; TileManager.Tile targetTile; Vector3 dir; // get the target tile position (round it down to int so we can reach with Index() function) switch (name) { case "ghost1": // target = pacman targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f); targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "ghost2": // target = pacman + 4*pacman's direction (4 steps ahead of pacman) dir = target.GetComponent <PacStudentController>().getDir(); targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f) + 4 * dir; if (dir == Vector3.up) { targetPos -= new Vector3(4, 0, 0); } targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "ghost3": // target = ambushVector(pacman+2 - ghost1) added to pacman+2 dir = target.GetComponent <PacStudentController>().getDir(); Vector3 ghost1Pos = GameObject.Find("ghost1").transform.position; Vector3 ambushVector = target.position + 2 * dir - ghost1Pos; targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f) + 2 * dir + ambushVector; targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "ghost4": targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f); targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; if (manager.distance(targetTile, currentTile) < 9) { targetTile = tiles[manager.Index(0, 2)]; } break; default: targetTile = null; Debug.Log("TARGET TILE NOT ASSIGNED"); break; } return(targetTile); }
public bool TileMoveOnCheck(TileManager.Tile tile) { switch (tile) { case TileManager.Tile.공백: case TileManager.Tile.나뭇잎: case TileManager.Tile.응가: case TileManager.Tile.바위: return(false); default: return(true); } }
public virtual void Update() { overTileChanged = false; TileManager.Tile newOverTile = GameManager.colonyM.colony.map.GetTileFromPosition(obj.transform.position); if (overTile != newOverTile) { overTileChanged = true; overTile = newOverTile; SetColour(overTile.sr.color); SetVisible(overTile.visible); } MoveToTile(null, false); SetMoveSprite(); }
public PathfindingTile(TileManager.Tile tile, PathfindingTile cameFrom, float cost) { this.tile = tile; this.cameFrom = cameFrom; this.cost = cost; if (cameFrom != null) { pathDistance = cameFrom.pathDistance + 1; } else { pathDistance = 0; } }
public Life(TileManager.Tile spawnTile, float startingHealth) { gender = GetRandomGender(); overTile = spawnTile; obj = MonoBehaviour.Instantiate(GameManager.resourceM.tilePrefab, overTile.obj.transform.position, Quaternion.identity); obj.GetComponent <SpriteRenderer>().sortingOrder = 10; // Life Sprite previousPosition = obj.transform.position; health = startingHealth; GameManager.lifeM.life.Add(this); }
public void RunLogic() { // get current pos Vector3 currentPos = new Vector3(transform.position.x + .499f, transform.position.y + .499f); TileManager.Tile currentTile = tileManager.GetTileByPos(currentPos); nextTile = tileManager.GetTileByPos(currentPos + ghostMovement.Direction); if (nextTile.Occupied) { if (ghostMovement.Direction == Vector3.left || ghostMovement.Direction == Vector3.right) { ghostMovement.Direction = currentTile.down == null ? Vector3.up : Vector3.down; } else { ghostMovement.Direction = currentTile.left == null ? Vector3.right : Vector3.left; } } else if (currentTile.IsIntersection) { List <Vector3> availableDir = new List <Vector3>(); if (currentTile.up != null && currentTile.up.Occupied == false && ghostMovement.Direction != Vector3.down) { availableDir.Add(Vector3.up); } if (currentTile.down != null && currentTile.down.Occupied == false && ghostMovement.Direction != Vector3.up) { availableDir.Add(Vector3.down); } if (currentTile.left != null && currentTile.left.Occupied == false && ghostMovement.Direction != Vector3.right) { availableDir.Add(Vector3.left); } if (currentTile.right != null && currentTile.right.Occupied == false && ghostMovement.Direction != Vector3.left) { availableDir.Add(Vector3.right); } int randIndex = Random.Range(0, availableDir.Count); ghostMovement.Direction = availableDir[randIndex]; } else { ghostMovement.Direction = ghostMovement.Direction; } }
TileManager.Tile GetTargetTilePerGhost() { Vector3 targetPos; TileManager.Tile targetTile; Vector3 dir; switch (name) { case "blinky": targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f); targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "pinky": dir = target.GetComponent <PlayerController>().getDir(); targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f) + 4 * dir; if (dir == Vector3.up) { targetPos -= new Vector3(4, 0, 0); } targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "inky": dir = target.GetComponent <PlayerController>().getDir(); Vector3 blinkyPos = GameObject.Find("blinky").transform.position; Vector3 ambushVector = target.position + 2 * dir - blinkyPos; targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f) + 2 * dir + ambushVector; targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "clyde": targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f); targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; if (manager.distance(targetTile, currentTile) < 9) { targetTile = tiles[manager.Index(0, 2)]; } break; default: targetTile = null; break; } return(targetTile); }
/// <summary> /// L'état initial. Permet à pacman de choisir une direction au lancement du jeu /// </summary> void AI_InitialState() { //Waypoints = TileManager.GetPathTo(CurrentTile, Manager.GetTile(2, 30)); //NextTile = Waypoints.Dequeue(); if (Random.Range(0f, 1f) > 0.5) { NextTile = CurrentTile.right; } else { NextTile = CurrentTile.left; } Brain.SetState(AI_MovingToNextTile); }
public static bool PathExists(TileManager.Tile startTile, TileManager.Tile endTile, bool breakTooLong, int breakAfterTiles, WalkableSetting walkableSetting, DirectionSetting directionSetting) { PathfindingTile currentTile = new PathfindingTile(startTile, null, 0); List <PathfindingTile> frontier = new List <PathfindingTile>() { currentTile }; List <PathfindingTile> checkedTiles = new List <PathfindingTile>() { currentTile }; int breakCounter = 0; while (frontier.Count > 0) { currentTile = frontier[0]; frontier.RemoveAt(0); if (breakTooLong) { if (breakCounter >= breakAfterTiles) { break; } breakCounter += 1; } if (currentTile.tile == endTile) { return(true); } foreach (TileManager.Tile nTile in (directionSetting == DirectionSetting.Horizontal ? currentTile.tile.horizontalSurroundingTiles : (directionSetting == DirectionSetting.Diagonal ? currentTile.tile.diagonalSurroundingTiles : currentTile.tile.surroundingTiles))) { if (nTile != null && checkedTiles.Find(o => o.tile == nTile) == null && (walkableSetting == WalkableSetting.Walkable ? nTile.walkable : (walkableSetting != WalkableSetting.NonWalkable || !nTile.walkable))) { PathfindingTile pTile = new PathfindingTile(nTile, null, Vector2.Distance(nTile.obj.transform.position, endTile.obj.transform.position)); frontier.Add(pTile); checkedTiles.Add(pTile); } } frontier = frontier.OrderBy(o => o.cost).ToList(); } return(false); }
public bool SetCheck(Vector2Int v, TileManager.Tile tileType) { TileManager.Tile tile = TileManager.instance.GetTile(TileManager.instance.tileMap, new Vector3Int(v.x, v.y, 0)); TileManager.Tile item = TileManager.instance.GetTile(TileManager.instance.tileItem, new Vector3Int(v.x, v.y, 0)); if (tileType == TileManager.Tile.나뭇잎 || tileType == TileManager.Tile.응가 || tileType == TileManager.Tile.바위) { if (CAntManager.Instance.AntLocationCheck(v)) { return(false); } if (CAntManager.Instance.NextAntLocationCheck(v)) { return(false); } } if (tileType == TileManager.Tile.꽃줄기) { //꽃줄기는 흙 + 바위 + 위험구역에 설치가능 if ((tile == TileManager.Tile.흙 && item == TileManager.Tile.공백) || (item == TileManager.Tile.바위 && !TileManager.instance.tileItem.GetTile(new Vector3Int(v.x, v.y, 0)).name.Equals("SRock")) || (item == TileManager.Tile.데드존 && !TileManager.instance.tileItem.GetTile(new Vector3Int(v.x, v.y, 0)).name.Equals("DeadZone"))) { return(true); } } else if (tileType == TileManager.Tile.바위) { //바위는 공백에 설치가능 if (tile == TileManager.Tile.공백 && item == TileManager.Tile.공백) { return(true); } } else { //나머지는 흙에만 설치가능 if (tile == TileManager.Tile.흙 && item == TileManager.Tile.공백) { return(true); } } return(false); }
TileManager.Tile GetTargetTile() { Vector3 targetPos; TileManager.Tile _targetTile = new TileManager.Tile(0, 0); switch (name) { case "blinky": // target=pacman targetPos = new Vector3(target.transform.position.x, target.transform.position.y); _targetTile = tileManager.GetTileByPos(targetPos); break; default: break; } return(_targetTile); }
public int CalculateMoveSpriteIndex() { int moveSpriteIndex = 0; if (path.Count > 0) { TileManager.Tile previousTile = GameManager.colonyM.colony.map.GetTileFromPosition(previousPosition); if (previousTile != path[0]) { moveSpriteIndex = previousTile.surroundingTiles.IndexOf(path[0]); if (moveSpriteIndex == -1) { moveSpriteIndex = 0; } moveSpriteIndex = moveSpriteIndices[moveSpriteIndex]; } } return(moveSpriteIndex); }
void RenderSelect(Vector2Int v, TileManager.Tile tile) { Color color; //색깔 파레트 초기화 for (int x = -palette; x <= palette; x++) { for (int y = -palette; y <= palette; y++) { TileManager.instance.SetTile(TileManager.instance.tileSelect, new Vector2Int(x, y), TileManager.Tile.공백); TileManager.instance.tileSelect.SetTileFlags(new Vector3Int(x, y, 0), TileFlags.None); color = TileManager.instance.tileSelect.GetColor(new Vector3Int(x, y, 0)); TileManager.instance.tileSelect.SetColor(new Vector3Int(x, y, 0), new Color(1, 1, 1, color.a)); } } //설치할 오브젝트를 표시 TileManager.instance.SetTile(TileManager.instance.tileSelect, v, tile); //선택 파레트 중 설치할 오브젝트가 표시된 부분의 색조정 for (int x = -palette; x <= palette; x++) { for (int y = -palette; y <= palette; y++) { TileManager.Tile emp = TileManager.instance.GetTile(TileManager.instance.tileSelect, new Vector3Int(x, y, 0)); if (emp != TileManager.Tile.공백) { TileManager.instance.tileSelect.SetTileFlags(new Vector3Int(x, y, 0), TileFlags.None); color = TileManager.instance.tileSelect.GetColor(new Vector3Int(x, y, 0)); if (SetCheck(new Vector2Int(x, y), tile)) { //설치가능하면 녹색 TileManager.instance.tileSelect.SetColor(new Vector3Int(x, y, 0), new Color(0, 1, 0, color.a)); } else { //설치불가능하면 적색 TileManager.instance.tileSelect.SetColor(new Vector3Int(x, y, 0), new Color(1, 0, 0, color.a)); } } } } }
public void SetItemCoolTime(int n, TileManager.Tile tile) { if (n < 0) { return; } if (tile == TileManager.Tile.꿀) { cooltime[n] = GameManager.instance.honeyCoolTime; } else if (tile == TileManager.Tile.나뭇잎) { cooltime[n] = GameManager.instance.leafCoolTime; } else if (tile == TileManager.Tile.꽃줄기) { cooltime[n] = GameManager.instance.flowerStemCoolTime; } else if (tile == TileManager.Tile.마늘) { cooltime[n] = GameManager.instance.garlicCoolTime; } else if (tile == TileManager.Tile.거미줄) { cooltime[n] = GameManager.instance.spiderWebCoolTime; } else if (tile == TileManager.Tile.방울) { cooltime[n] = GameManager.instance.bubbleCoolTime; } else if (tile == TileManager.Tile.응가) { cooltime[n] = GameManager.instance.pooCoolTime; } else if (tile == TileManager.Tile.애벌레) { cooltime[n] = GameManager.instance.larvaCoolTime; } else if (tile == TileManager.Tile.바위) { cooltime[n] = GameManager.instance.rockCoolTime; } }
public Human(TileManager.Tile spawnTile, float startingHealth) : base(spawnTile, startingHealth) { bodyIndices = GetBodyIndices(gender); moveSprites = GameManager.humanM.humanMoveSprites[bodyIndices[Appearance.Skin]]; inventory = new ResourceManager.Inventory(this, 50000, 50000); SetName(GameManager.humanM.GetName(gender)); humanObj = MonoBehaviour.Instantiate(GameManager.resourceM.humanPrefab, obj.transform, false); int appearanceIndex = 1; foreach (Appearance appearance in clothes.Keys) { humanObj.transform.Find(appearance.ToString()).GetComponent <SpriteRenderer>().sortingOrder = obj.GetComponent <SpriteRenderer>().sortingOrder + appearanceIndex; appearanceIndex += 1; } GameManager.humanM.humans.Add(this); }
public bool MoveToTile(TileManager.Tile tile, bool allowEndTileNonWalkable) { if (tile != null) { startPathLength = 0; path = PathManager.FindPathToTile(overTile, tile, allowEndTileNonWalkable); startPathLength = path.Count; moveTimer = 0; previousPosition = obj.transform.position; } if (path.Count > 0) { obj.transform.position = Vector2.Lerp(previousPosition, path[0].obj.transform.position, moveTimer); if (moveTimer >= 1f) { previousPosition = obj.transform.position; obj.transform.position = path[0].obj.transform.position; moveTimer = 0; path.RemoveAt(0); } else { moveTimer += 2 * GameManager.timeM.deltaTime * overTile.walkSpeed * moveSpeedMultiplier; } } else { path.Clear(); if (!Mathf.Approximately(Vector2.Distance(obj.transform.position, overTile.obj.transform.position), 0f)) { path.Add(overTile); moveTimer = 0; } return(true); } return(false); }
public void SpawnTrader(TileManager.Tile spawnTile) { Trader trader = new Trader(spawnTile, 1f, this); traders.Add(trader); List <TileManager.Tile> targetTiles = new List <TileManager.Tile>() { targetTile }; targetTiles.AddRange(targetTile.horizontalSurroundingTiles.Where(t => t != null && t.walkable && t.buildable)); List <TileManager.Tile> additionalTargetTiles = new List <TileManager.Tile>(); foreach (TileManager.Tile tt in targetTiles) { additionalTargetTiles.AddRange(tt.horizontalSurroundingTiles.Where(t => t != null && t.walkable && t.buildable && !additionalTargetTiles.Contains(t))); } targetTiles.AddRange(additionalTargetTiles); trader.MoveToTile(targetTiles[UnityEngine.Random.Range(0, targetTiles.Count)], false); }
void FixedUpdate() { DISTANCE = Vector3.Distance(transform.position, waypoint); if (GameManager.gameState == GameManager.GameState.Game) { animate(); switch (state) { case State.Wait: Wait(); break; case State.Init: Init(); break; case State.Scatter: Scatter(); break; case State.Chase: ChaseAI(); break; case State.Run: RunAway(); break; } } // Mise à jours du tile actuel Vector3 CurrentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f); CurrentTile = Manager.tiles[Manager.Index((int)CurrentPos.x, (int)CurrentPos.y)]; }
void FixedUpdate() { // Suivant l'état du jeu on appel l'IA ou l'animation de mort switch (GameManager.gameState) { case GameManager.GameState.Game: // C'est ici qu'on peut faire un appel à l'IA // FSM.Run(AI_IdleState); Animate(); break; case GameManager.GameState.Dead: if (!_deadPlaying) { StartCoroutine("PlayDeadAnimation"); } break; } // Mise à jours de la tile actuelle par rapport à la position du pacman Vector3 CurrentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f); CurrentTile = Tiles[Manager.Index((int)CurrentPos.x, (int)CurrentPos.y)]; }
//Retrieved from: https://drive.google.com/drive/folders/0B23Gdj5FRrfkcERMWjdJaTlmaWc //On 8/03/2017 public void AILogic() { // get current tile the ghost is in Vector3 currentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f); currentTile = tiles[tileManager.Index((int)currentPos.x, (int)currentPos.y)]; targetTile = GetTargetTilePerGhost(); // get the next tile according to direction if (ghost.direction.x > 0) { nextTile = tiles[tileManager.Index((int)(currentPos.x + 1), (int)currentPos.y)]; } if (ghost.direction.x < 0) { nextTile = tiles[tileManager.Index((int)(currentPos.x - 1), (int)currentPos.y)]; } if (ghost.direction.y > 0) { nextTile = tiles[tileManager.Index((int)currentPos.x, (int)(currentPos.y + 1))]; } if (ghost.direction.y < 0) { nextTile = tiles[tileManager.Index((int)currentPos.x, (int)(currentPos.y - 1))]; } if (nextTile.occupied || currentTile.isIntersection) { //--------------------- // IF WE BUMP INTO WALL if (nextTile.occupied && !currentTile.isIntersection) { // if ghost moves to right or left and there is wall next tile if (ghost.direction.x != 0) { if (currentTile.down == null) { ghost.direction = Vector3.up; } else { ghost.direction = Vector3.down; } } // if ghost moves to up or down and there is wall next tile else if (ghost.direction.y != 0) { if (currentTile.left == null) { ghost.direction = Vector3.right; } else { ghost.direction = Vector3.left; } } } //--------------------------------------------------------------------------------------- // IF WE ARE AT INTERSECTION // calculate the distance to target from each available tile and choose the shortest one if (currentTile.isIntersection) { float dist1, dist2, dist3, dist4; dist1 = dist2 = dist3 = dist4 = 999999f; if (currentTile.up != null && !currentTile.up.occupied && !(ghost.direction.y < 0)) { dist1 = tileManager.distance(currentTile.up, targetTile); } if (currentTile.down != null && !currentTile.down.occupied && !(ghost.direction.y > 0)) { dist2 = tileManager.distance(currentTile.down, targetTile); } if (currentTile.left != null && !currentTile.left.occupied && !(ghost.direction.x > 0)) { dist3 = tileManager.distance(currentTile.left, targetTile); } if (currentTile.right != null && !currentTile.right.occupied && !(ghost.direction.x < 0)) { dist4 = tileManager.distance(currentTile.right, targetTile); } float min = Mathf.Min(dist1, dist2, dist3, dist4); if (min == dist1) { ghost.direction = Vector3.up; } if (min == dist2) { ghost.direction = Vector3.down; } if (min == dist3) { ghost.direction = Vector3.left; } if (min == dist4) { ghost.direction = Vector3.right; } } } // if there is no decision to be made, designate next waypoint for the ghost else { ghost.direction = ghost.direction; // setter updates the waypoint } }
public void RunAwayLogic() { // get current tile Vector3 currentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f); currentTile = tiles[tileManager.Index((int)currentPos.x, (int)currentPos.y)]; // get the next tile according to direction if (ghost.direction.x > 0) { nextTile = tiles[tileManager.Index((int)(currentPos.x + 1), (int)currentPos.y)]; } if (ghost.direction.x < 0) { nextTile = tiles[tileManager.Index((int)(currentPos.x - 1), (int)currentPos.y)]; } if (ghost.direction.y > 0) { nextTile = tiles[tileManager.Index((int)currentPos.x, (int)(currentPos.y + 1))]; } if (ghost.direction.y < 0) { nextTile = tiles[tileManager.Index((int)currentPos.x, (int)(currentPos.y - 1))]; } if (nextTile.occupied || currentTile.isIntersection) { //--------------------- // IF WE BUMP INTO WALL if (nextTile.occupied && !currentTile.isIntersection) { // if ghost moves to right or left and there is wall next tile if (ghost.direction.x != 0) { if (currentTile.down == null) { ghost.direction = Vector3.up; } else { ghost.direction = Vector3.down; } } // if ghost moves to up or down and there is wall next tile else if (ghost.direction.y != 0) { if (currentTile.left == null) { ghost.direction = Vector3.right; } else { ghost.direction = Vector3.left; } } } //--------------------------------------------------------------------------------------- // IF WE ARE AT INTERSECTION // choose one available option at random if (currentTile.isIntersection) { List <TileManager.Tile> availableTiles = new List <TileManager.Tile>(); TileManager.Tile chosenTile; if (currentTile.up != null && !currentTile.up.occupied && !(ghost.direction.y < 0)) { availableTiles.Add(currentTile.up); } if (currentTile.down != null && !currentTile.down.occupied && !(ghost.direction.y > 0)) { availableTiles.Add(currentTile.down); } if (currentTile.left != null && !currentTile.left.occupied && !(ghost.direction.x > 0)) { availableTiles.Add(currentTile.left); } if (currentTile.right != null && !currentTile.right.occupied && !(ghost.direction.x < 0)) { availableTiles.Add(currentTile.right); } int rand = Random.Range(0, availableTiles.Count); chosenTile = availableTiles[rand]; ghost.direction = Vector3.Normalize(new Vector3(chosenTile.x - currentTile.x, chosenTile.y - currentTile.y, 0)); //Debug.Log (ghost.name + ": Chosen Tile (" + chosenTile.x + ", " + chosenTile.y + ")" ); } } // if there is no decision to be made, designate next waypoint for the ghost else { ghost.direction = ghost.direction; // setter updates the waypoint } }
public TileSelector(TileManager.Tile tile) { InitializeComponent(); CurrentTile = tile; }
TileManager.Tile GetTargetTilePerGhost() { Vector3 targetPos; TileManager.Tile targetTile; Vector3 dir; // get the target tile position (round it down to int so we can reach with Index() function) switch(name) { case "blinky": // target = pacman targetPos = new Vector3 (target.position.x+0.499f, target.position.y+0.499f); targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "pinky": // target = pacman + 4*pacman's direction (4 steps ahead of pacman) dir = target.GetComponent<PlayerController>().getDir(); targetPos = new Vector3 (target.position.x+0.499f, target.position.y+0.499f) + 4*dir; // if pacmans going up, not 4 ahead but 4 up and 4 left is the target // read about it here: http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior // so subtract 4 from X coord from target position if(dir == Vector3.up) targetPos -= new Vector3(4, 0, 0); targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "inky": // target = ambushVector(pacman+2 - blinky) added to pacman+2 dir = target.GetComponent<PlayerController>().getDir(); Vector3 blinkyPos = GameObject.Find ("blinky").transform.position; Vector3 ambushVector = target.position + 2*dir - blinkyPos ; targetPos = new Vector3 (target.position.x+0.499f, target.position.y+0.499f) + 2*dir + ambushVector; targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; break; case "clyde": targetPos = new Vector3 (target.position.x+0.499f, target.position.y+0.499f); targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; if(manager.distance(targetTile, currentTile) < 9) targetTile = tiles[manager.Index (0, 2)]; break; default: targetTile = null; Debug.Log ("TARGET TILE NOT ASSIGNED"); break; } return targetTile; }
public void AILogic() { // get current tile Vector3 currentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f); currentTile = tiles[manager.Index ((int)currentPos.x, (int)currentPos.y)]; targetTile = GetTargetTilePerGhost(); // get the next tile according to direction if(ghost.direction.x > 0) nextTile = tiles[manager.Index ((int)(currentPos.x+1), (int)currentPos.y)]; if(ghost.direction.x < 0) nextTile = tiles[manager.Index ((int)(currentPos.x-1), (int)currentPos.y)]; if(ghost.direction.y > 0) nextTile = tiles[manager.Index ((int)currentPos.x, (int)(currentPos.y+1))]; if(ghost.direction.y < 0) nextTile = tiles[manager.Index ((int)currentPos.x, (int)(currentPos.y-1))]; if(nextTile.occupied || currentTile.isIntersection) { //--------------------- // IF WE BUMP INTO WALL if(nextTile.occupied && !currentTile.isIntersection) { // if ghost moves to right or left and there is wall next tile if(ghost.direction.x != 0) { if(currentTile.down == null) ghost.direction = Vector3.up; else ghost.direction = Vector3.down; } // if ghost moves to up or down and there is wall next tile else if(ghost.direction.y != 0) { if(currentTile.left == null) ghost.direction = Vector3.right; else ghost.direction = Vector3.left; } } //--------------------------------------------------------------------------------------- // IF WE ARE AT INTERSECTION // calculate the distance to target from each available tile and choose the shortest one if(currentTile.isIntersection) { float dist1, dist2, dist3, dist4; dist1 = dist2 = dist3 = dist4 = 999999f; if(currentTile.up != null && !currentTile.up.occupied && !(ghost.direction.y < 0)) dist1 = manager.distance(currentTile.up, targetTile); if(currentTile.down != null && !currentTile.down.occupied && !(ghost.direction.y > 0)) dist2 = manager.distance(currentTile.down, targetTile); if(currentTile.left != null && !currentTile.left.occupied && !(ghost.direction.x > 0)) dist3 = manager.distance(currentTile.left, targetTile); if(currentTile.right != null && !currentTile.right.occupied && !(ghost.direction.x < 0)) dist4 = manager.distance(currentTile.right, targetTile); float min = Mathf.Min(dist1, dist2, dist3, dist4); if(min == dist1) ghost.direction = Vector3.up; if(min == dist2) ghost.direction = Vector3.down; if(min == dist3) ghost.direction = Vector3.left; if(min == dist4) ghost.direction = Vector3.right; } } // if there is no decision to be made, designate next waypoint for the ghost else { ghost.direction = ghost.direction; // setter updates the waypoint } }
public void RunLogic() { // get current tile Vector3 currentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f); currentTile = tiles[manager.Index ((int)currentPos.x, (int)currentPos.y)]; // get the next tile according to direction if(ghost.direction.x > 0) nextTile = tiles[manager.Index ((int)(currentPos.x+1), (int)currentPos.y)]; if(ghost.direction.x < 0) nextTile = tiles[manager.Index ((int)(currentPos.x-1), (int)currentPos.y)]; if(ghost.direction.y > 0) nextTile = tiles[manager.Index ((int)currentPos.x, (int)(currentPos.y+1))]; if(ghost.direction.y < 0) nextTile = tiles[manager.Index ((int)currentPos.x, (int)(currentPos.y-1))]; //Debug.Log (ghost.direction.x + " " + ghost.direction.y); //Debug.Log (ghost.name + ": Next Tile (" + nextTile.x + ", " + nextTile.y + ")" ); if(nextTile.occupied || currentTile.isIntersection) { //--------------------- // IF WE BUMP INTO WALL if(nextTile.occupied && !currentTile.isIntersection) { // if ghost moves to right or left and there is wall next tile if(ghost.direction.x != 0) { if(currentTile.down == null) ghost.direction = Vector3.up; else ghost.direction = Vector3.down; } // if ghost moves to up or down and there is wall next tile else if(ghost.direction.y != 0) { if(currentTile.left == null) ghost.direction = Vector3.right; else ghost.direction = Vector3.left; } } //--------------------------------------------------------------------------------------- // IF WE ARE AT INTERSECTION // choose one available option at random if(currentTile.isIntersection) { List<TileManager.Tile> availableTiles = new List<TileManager.Tile>(); TileManager.Tile chosenTile; if(currentTile.up != null && !currentTile.up.occupied && !(ghost.direction.y < 0)) availableTiles.Add (currentTile.up); if(currentTile.down != null && !currentTile.down.occupied && !(ghost.direction.y > 0)) availableTiles.Add (currentTile.down); if(currentTile.left != null && !currentTile.left.occupied && !(ghost.direction.x > 0)) availableTiles.Add (currentTile.left); if(currentTile.right != null && !currentTile.right.occupied && !(ghost.direction.x < 0)) availableTiles.Add (currentTile.right); int rand = Random.Range(0, availableTiles.Count); chosenTile = availableTiles[rand]; ghost.direction = Vector3.Normalize(new Vector3(chosenTile.x - currentTile.x, chosenTile.y - currentTile.y, 0)); //Debug.Log (ghost.name + ": Chosen Tile (" + chosenTile.x + ", " + chosenTile.y + ")" ); } } // if there is no decision to be made, designate next waypoint for the ghost else { ghost.direction = ghost.direction; // setter updates the waypoint } }