static int SearchRoad(IntPtr L) { try { ToLua.CheckArgsCount(L, 9); int[][] arg0 = ToLua.CheckObjectArray <int[]>(L, 1); int arg1 = (int)LuaDLL.luaL_checknumber(L, 2); int arg2 = (int)LuaDLL.luaL_checknumber(L, 3); int arg3 = (int)LuaDLL.luaL_checknumber(L, 4); int arg4 = (int)LuaDLL.luaL_checknumber(L, 5); int arg5 = (int)LuaDLL.luaL_checknumber(L, 6); int arg6 = (int)LuaDLL.luaL_checknumber(L, 7); bool arg7 = LuaDLL.luaL_checkboolean(L, 8); System.Action arg8 = null; LuaTypes funcType9 = LuaDLL.lua_type(L, 9); if (funcType9 != LuaTypes.LUA_TFUNCTION) { arg8 = (System.Action)ToLua.CheckObject(L, 9, typeof(System.Action)); } else { LuaFunction func = ToLua.ToLuaFunction(L, 9); arg8 = DelegateFactory.CreateDelegate(typeof(System.Action), func) as System.Action; } System.Collections.Generic.IList <Node> o = AStarPathFinding.SearchRoad(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); ToLua.PushObject(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public void FindPath(int from, int to) { path = AStarPathFinding.FindPath(from, to); path.CalcParams(); // Drawing path for debugging //path.DrawPath(); }
public void FindPathTo(int to) { path = AStarPathFinding.FindPath(FindClosestNode(), to); path.CalcParams(); // Drawing path for debugging //path.DrawPath(); }
public void Initialize(NavMeshPathGraph navMeshGraph, AStarPathFinding pathFindingAlgorithm) { this.draw = true; this.navMesh = navMeshGraph; this.AStarPathFinding = pathFindingAlgorithm; this.AStarPathFinding.NodesPerSearch = 2000; this.actionsPerformed = 0; this.characterAnimator = this.GetComponentInChildren <Animator>(); }
private static void WalkTheDungeon(Game gameInstance) { Logger.Info("Walking the dungeon..."); var pathFinder = new AStarPathFinding(); var movementProfile = new HumanoidMovement(); var visibilityMap = new VisibilityMap(gameInstance.Terrain.Width, gameInstance.Terrain.Height, null, null); var walkableLocations = gameInstance.Terrain.WalkableLocations(movementProfile).ToList(); FloodFill.Fill(gameInstance.Terrain, visibilityMap, movementProfile, walkableLocations[Rng.Next(walkableLocations.Count - 1)]); var walkableUnseenLocations = GetWalkableUnseenLocations(gameInstance.Terrain, visibilityMap, movementProfile).ToList(); while (walkableUnseenLocations.Count > 0) { var closestExploredLocation = GetClosestExploredLocation(gameInstance.Terrain, visibilityMap, movementProfile, walkableUnseenLocations[Rng.Next(walkableUnseenLocations.Count - 1)]); var closestUnexploredLocation = GetClosestUnexploredLocation(gameInstance.Terrain, visibilityMap, movementProfile, closestExploredLocation.Value); var movementPath = pathFinder.FindPath(gameInstance.Terrain, new List <IActor>(), new DiggerMovement(), closestExploredLocation.Value, closestUnexploredLocation.Value); foreach (var node in movementPath) { gameInstance.Terrain[node.Location] = new Floor(); visibilityMap[node.Location].WasSeen = true; } visibilityMap[closestUnexploredLocation.Value].WasSeen = false; FloodFill.Fill(gameInstance.Terrain, visibilityMap, movementProfile, closestUnexploredLocation.Value); walkableUnseenLocations = GetWalkableUnseenLocations(gameInstance.Terrain, visibilityMap, movementProfile).ToList(); } //digger.VisibilityMap.UpdateVisibilityMap(gameInstance.Terrain, gameInstance.LightMap, digger.Location.Coordinate); //var command = digger.Intellect.GetNextAction(); //while (command is MoveCommand) //{ // var result = command.Execute(); // if ((!result.Success) && (command is MoveCommand)) // { // // The move action failed // // Ask the actor for a default action on bump // var defaultBumpAction = digger.Intellect.GetDefaultBumpAction((MoveCommand)command); // if (defaultBumpAction != null) // defaultBumpAction.Execute(); // } // digger.VisibilityMap.UpdateVisibilityMap(gameInstance.Terrain, gameInstance.LightMap, digger.Location.Coordinate); // command = digger.Intellect.GetNextAction(); //} //gameInstance.RemoveActor(digger); Logger.Info("Dungeon walk complete."); }
void Update() { if (!pathfinding) { pathfinding = gameObject.GetComponent <AStarPathFinding>(); } if (Input.GetKeyDown(KeyCode.Space)) { PerformAction(); } }
protected virtual void Start() { tilemapNodes = TilemapNodes.Instance; pathFinder = GetComponent <AStarPathFinding>(); currentMapNode = tilemapNodes.GetNearestNode(new Vector2(0, 0)); transform.position = currentMapNode.position; startMovementTransform = transform; startMovementRotation = sprite.transform.rotation; currentMapNode.isTraversable = false; }
// Start is called before the first frame update void Start() { player = GameObject.FindGameObjectWithTag("Player"); playerInSight = false; decisionMaking = GetComponent <GuardDecisionMaking>(); // Generate patrol path path = AStarPathFinding.FindPath(patrolFrom, patrolTo); path.CalcParams(); //path = AStarPathFinding.FindPath(0, 27); //path.CalcParams(); // Drawing path for debugging //path.DrawPath(); }
public void FindPathToRoom(int room) { int chosenNode = -1; //List<int> possibleNodes; switch (room) { case 0: //possibleNodes = new List<int>() {2,3,4}; //chosenNode = possibleNodes[Random.Range(0, 3)]; chosenNode = 27; break; case 1: //possibleNodes = new List<int>() { 14, 44 }; //chosenNode = possibleNodes[Random.Range(0, 2)]; chosenNode = 28; break; case 2: //possibleNodes = new List<int>() { 11, 12, 25, 26 }; //chosenNode = possibleNodes[Random.Range(0, 4)]; chosenNode = 29; break; case 3: //possibleNodes = new List<int>() { 14, 43 }; //chosenNode = possibleNodes[Random.Range(0, 4)]; chosenNode = 32; break; case 4: //possibleNodes = new List<int>() { 16, 45, 46, 47 }; //chosenNode = possibleNodes[Random.Range(0, 4)]; chosenNode = 31; break; case 5: //possibleNodes = new List<int>() { 16, 45, 46, 47 }; //chosenNode = possibleNodes[Random.Range(0, 4)]; chosenNode = 30; break; } path = AStarPathFinding.FindPath(FindClosestNode(), chosenNode); path.CalcParams(); // Drawing path for debugging //path.DrawPath(); }
void Start() { Walls = new List <GameObject>(); Agent = GetComponentInChildren <RobotAgent>(); grid = GetComponent <Grid>(); grid.WorldBottomLeft = transform.position - Vector3.right * grid.gridWorldSize.x / 2 - Vector3.forward * grid.gridWorldSize.y / 2; grid.CreateGrid(); aStarPath = GetComponent <AStarPathFinding>(); aStarPath.grid = grid; aStarPath.seeker = Agent.transform; aStarPath.target = Target.transform; grid.player = Agent.transform; SetTargetAtRandomPosition(); initializeMap(); aStarPath.enabled = false; }
private void GeneratePaths() { var enemies = new List <Piece>(); Piece player = null; foreach (var piece in _board.Pieces) { if (piece.PlayerID == 0) { player = piece; } if (piece.PlayerID == 1) { enemies.Add(piece); } } var playerTile = _board.TileOf(player); var playerNeigberingTiles = Neighbours(_board, playerTile); var availebleEnemies = enemies.ToList().FindAll(e => !playerNeigberingTiles.Contains(_board.TileOf(e))); List <Tile> NeighbourStrategy(Tile t) => Neighbours(_board, t); var pf = new AStarPathFinding <Tile>(NeighbourStrategy, Distance, Distance); foreach (var toTile in playerNeigberingTiles) { var toPosition = toTile.Position; var path = new List <Tile>(); for (int i = 0; i < availebleEnemies.Count; i++) { var fromTile = _board.TileOf(availebleEnemies[i]); path = pf.Path(fromTile, toTile); if (path.Count > 0) { availebleEnemies[i].QueuedPath = path; availebleEnemies.RemoveAt(i); break; } } } }
static int NearObstacleCount(IntPtr L) { try { ToLua.CheckArgsCount(L, 4); Node arg0 = (Node)ToLua.CheckObject(L, 1, typeof(Node)); int[][] arg1 = ToLua.CheckObjectArray <int[]>(L, 2); int arg2 = (int)LuaDLL.luaL_checknumber(L, 3); int arg3 = (int)LuaDLL.luaL_checknumber(L, 4); int o = AStarPathFinding.NearObstacleCount(arg0, arg1, arg2, arg3); LuaDLL.lua_pushinteger(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
/// <summary> /// 重巡路径 /// </summary> private void ReFindPath() { //Debug.Log("重新寻路"); // 重新寻路设置目标点 // 清空当前目标点 clusterData.ClearTarget(); // 取最近的对方建筑 var buildingList = DisplayerManager.Single.GetBuildingByType(GetTypeArray(clusterData.AllData.MemberData.ObjID.ObjType)); // TODO 区分地面与空中 int[][] mapData = null; // 当前地图数据 switch (clusterData.AllData.MemberData.GeneralType) { case Utils.GeneralTypeAir: mapData = LoadMap.Single.GetAirMapData(); break; case Utils.GeneralTypeSurface: mapData = LoadMap.Single.GetSurfaceMapData(); break; } // 当前单位位置映射 var startPos = Utils.PositionToNum(LoadMap.Single.MapPlane.transform.position, clusterData.transform.position, ClusterManager.Single.UnitWidth, ClusterManager.Single.MapWidth, ClusterManager.Single.MapHeight); // 当前目标位置映射 var endPos = Utils.PositionToNum(LoadMap.Single.MapPlane.transform.position, GetClosestBuildingPos(buildingList), ClusterManager.Single.UnitWidth, ClusterManager.Single.MapWidth, ClusterManager.Single.MapHeight); // 生成路径 var path = AStarPathFinding.SearchRoad(mapData, startPos[0], startPos[1], endPos[0], endPos[1], (int)clusterData.Diameter, (int)clusterData.Diameter + 1); if (path != null && path.Count > 0) { // 清空当前目标点 clusterData.ClearTarget(); clusterData.PushTargetList(Utils.NumToPostionByList(LoadMap.Single.MapPlane.transform.position, path, ClusterManager.Single.UnitWidth, ClusterManager.Single.MapWidth, ClusterManager.Single.MapHeight)); } else { Debug.Log("xingjin 目标点不可达:start:" + startPos[0] + "," + startPos[1] + " end:" + endPos[0] + "," + endPos[1]); } //clusterData.PushTargetList(Utils.NumToPostionByList(LoadMap.Single.MapPlane.transform.position, path, ClusterManager.Single.UnitWidth, ClusterManager.Single.MapWidth, ClusterManager.Single.MapHeight)); }
static int ISearchRoad(IntPtr L) { try { ToLua.CheckArgsCount(L, 7); int[][] arg0 = ToLua.CheckObjectArray <int[]>(L, 1); int arg1 = (int)LuaDLL.luaL_checknumber(L, 2); int arg2 = (int)LuaDLL.luaL_checknumber(L, 3); int arg3 = (int)LuaDLL.luaL_checknumber(L, 4); int arg4 = (int)LuaDLL.luaL_checknumber(L, 5); int arg5 = (int)LuaDLL.luaL_checknumber(L, 6); int arg6 = (int)LuaDLL.luaL_checknumber(L, 7); System.Collections.IEnumerator o = AStarPathFinding.ISearchRoad(arg0, arg1, arg2, arg3, arg4, arg5, arg6); ToLua.Push(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public void FindPath(AStarPoint e) { if (pathTweener != null) { pathTweener.Kill(); } AStarPoint s = new AStarPoint((int)player.transform.position.x, (int)player.transform.position.y); List <AStarPoint> pathList; if (!AStarPathFinding.FindPath(s, e, out pathList)) { AudioManager.Instance.PlayClip(AudioManager.Instance.why); Anim.SetTrigger("Why"); return; } else { pathTweener = player.transform.DOPath(pathList.ToVector3Array(), pathList.Count * 0.15f); pathTweener.SetEase(Ease.Linear); pathTweener.OnComplete(() => pathTweener = null).OnKill(() => pathTweener = null); } }
static int _CreateAStarPathFinding(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 0) { AStarPathFinding obj = new AStarPathFinding(); ToLua.PushObject(L, obj); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to ctor method: AStarPathFinding.New")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public void CalculPath(GameObject run) { /// Fill the _grid with all the childs and their node /// GetGrid(run); /// /// Find the path in the grid /// var path = new AStarPathFinding(_grid, start, goal); /// /// Fill the goodPath List with only the Transform of all the nodes that are in the best path /// RetracePath(goal, start); /// if (DrawPathDebug) { DrawPath(); } }
/// <summary> /// 重巡路径 /// </summary> /// <param name="fsm"></param> private void ReFindPath(SoldierFSMSystem fsm) { //Debug.Log(""); // 重新寻路设置目标点 // 当前地图数据 int[][] mapData = null; // 当前地图数据 switch (clusterData.AllData.MemberData.GeneralType) { case Utils.GeneralTypeAir: mapData = LoadMap.Single.GetAirMapData(); break; case Utils.GeneralTypeSurface: mapData = LoadMap.Single.GetSurfaceMapData(); break; } // 当前单位位置映射 var startPos = Utils.PositionToNum(LoadMap.Single.MapPlane.transform.position, clusterData.transform.position, ClusterManager.Single.UnitWidth, ClusterManager.Single.MapWidth, ClusterManager.Single.MapHeight); // 当前目标位置映射 var endPos = Utils.PositionToNum(LoadMap.Single.MapPlane.transform.position, fsm.EnemyTarget.ClusterData.transform.position, ClusterManager.Single.UnitWidth, ClusterManager.Single.MapWidth, ClusterManager.Single.MapHeight); // 生成路径 var path = AStarPathFinding.SearchRoad(mapData, startPos[0], startPos[1], endPos[0], endPos[1], (int)clusterData.Diameter, (int)clusterData.Diameter + 1); if (path != null && path.Count > 0) { // 清空当前目标点 clusterData.ClearTarget(); targetPos = fsm.EnemyTarget.ClusterData.transform.position; clusterData.PushTargetList(Utils.NumToPostionByList(LoadMap.Single.MapPlane.transform.position, path, ClusterManager.Single.UnitWidth, ClusterManager.Single.MapWidth, ClusterManager.Single.MapHeight)); } else { Debug.Log("zhuiji 目标点不可达:start:" + startPos[0] + "," + startPos[1] + " end:" + endPos[0] + "," + endPos[1]); } }
public void SetTarget(Point target) { TargetPosition = target; var arr = new int[_map.Width, _map.Height]; for (var x = 0; x < _map.Width; x++) { for (var y = 0; y < _map.Height; y++) { if (_map.GetCellDepth(new Point(x, y)) <= Draft) { arr[x, y] = -1; } } } var path = AStarPathFinding.FindPath(arr, CurrentPosition, target); if (path == null) { return; } var pathQueue = new Queue <Point>(path); // Remove current position from path queue if (pathQueue.Peek() == CurrentPosition) { pathQueue.Dequeue(); } SetPath(pathQueue); SetState(new TransferState(this, _map, Cell.Size)); }
/// <summary> /// 执行 /// </summary> /// <param name="frame"></param> /// <param name="blackBoard"></param> public void OnceFrame(long frame, IBlackBoard blackBoard) { actionFrame = frame; // 检查是否死亡 if (Hp <= 0) { MemberManager.Remove(this); Debug.Log("单位死亡Id:" + Id); return; } if (IsLocal && IsAI) { if (pathList != null && pathList.Count > 0) { // 继续前进 var nextNode = pathList.Pop(); Debug.Log("继续前进:" + nextNode.X + "," + nextNode.Y); // 跑出显示命令, 并等待显示部分反馈的帧数 SendCmd(new Commend(MemberManager.FrameCount, Id, OptionType.Move) { Param = new Dictionary <string, string>() { { "fromX", "" + X }, { "fromY", "" + Y }, { "toX", "" + nextNode.X }, { "toY", "" + nextNode.Y }, } }); } else { var width = blackBoard.MapBase.MapWidth; var height = blackBoard.MapBase.MapHeight; var couldNotPass = true; int targetX = 0; int targetY = 0; while (couldNotPass) { // 随机获取目标位置 targetX = RandomPacker.Single.GetRangeI(0, width); targetY = RandomPacker.Single.GetRangeI(0, height); var path = AStarPathFinding.SearchRoad( BlackBoard.Single.MapBase.GetMapArray(MapManager.MapObstacleLayer), X, Y, targetX, targetY, 1, 1); if (path != null && path.Count > 0) { couldNotPass = false; pathList = new Stack <Node>(path.ToArray()); } var index = RandomPacker.Single.GetRangeI(0, MemberManager.MemberCount); // 随机攻击一个目标 var targetMember = MemberManager.Get(index); if (targetMember != null) { SendCmd(new Commend(MemberManager.FrameCount, targetMember.Id, OptionType.Attack) { Param = new Dictionary <string, string>() { { "atkId", "" + Id }, { "atkType", "" + 1 }, { "dmg", "" + 10 }, } }); } } // 向目标寻路, 如果不可达继续寻路 var nextNode = pathList.Pop(); Debug.Log("重新寻路前进:" + nextNode.X + "," + nextNode.Y); // 跑出显示命令, 并等待显示部分反馈的帧数 SendCmd(new Commend(MemberManager.FrameCount, Id, OptionType.Move) { Param = new Dictionary <string, string>() { { "fromX", "" + X }, { "fromY", "" + Y }, { "toX", "" + nextNode.X }, { "toY", "" + nextNode.Y }, } }); } } }
void Awake() { instance = this; pathfinding = GetComponent <AStarPathFinding>(); }
/// <summary> /// 控制 /// </summary> private void Control() { if (Input.GetMouseButtonDown(1)) { // 释放技能 var ray = MainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; Physics.Raycast(ray, out hit); if (hit.collider != null && hit.collider.name.Equals(LoadMap.MapPlane.name)) { SkillManager.Single.DoSkillNum(1009, new FormulaParamsPacker() { StartPos = new Vector3(hit.point.x, 0, hit.point.z), TargetPos = new Vector3(hit.point.x, 0, hit.point.z), ReleaseMember = new DisplayOwner(scaner.gameObject, scaner), ReceiverMenber = new DisplayOwner(scaner.gameObject, scaner), }); } } if (Input.GetMouseButtonDown(0)) { var skill = SkillManager.Single.CreateSkillInfo(20001); Debug.Log(skill.SkillName); // 加载地图数据 //Debug.Log(PacketManage.Single.GetPacket("mapdatapack").LoadString("mapdata")); // 获取地图上的点击点 var ray = MainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; Physics.Raycast(ray, out hit); // 点击到底板 if (hit.collider != null && hit.collider.name.Equals(LoadMap.MapPlane.name)) { var posOnMap = Utils.PositionToNum(LoadMap.MapPlane.transform.position, hit.point, UnitWidth, MapWidth, MapHeight); Debug.Log("start:" + lastTimeTargetX + "," + lastTimeTargetY + " end:" + posOnMap[0] + "," + posOnMap[1]); // 加载文件内容 var mapInfoData = InitMapInfo(); var path = AStarPathFinding.SearchRoad(mapInfoData, lastTimeTargetX, lastTimeTargetY, posOnMap[0], posOnMap[1], DiameterX, DiameterY, IsJumpPoint); // 根据path放地标, 使用组队寻路跟随过去 //StartCoroutine(Step(path)); var loadMapPos = LoadMap.GetCenter(); ClusterManager.Single.Init(loadMapPos.x, loadMapPos.z, MapWidth, MapHeight, UnitWidth, mapInfoData); //LoadMap.RefreshMap(); StartMoving(path, mapInfoData, lastTimeTargetX, lastTimeTargetY); // 缓存上次目标点 lastTimeTargetX = posOnMap[0]; lastTimeTargetY = posOnMap[1]; } //var target = GameObject.Find("item0"); //if (target != null) //{ // //var testEffect = EffectsFactory.Single.CreateLinerEffect("linePrfb.prefab", null, target, 3, null, 12); // var testEffect = EffectsFactory.Single.CreateLinerEffect("linePrfb.prefab", null, new Vector3(0,0,0), new Vector3(100, 0, 100), 3, null, 12); // testEffect.Begin(); //} Debug.Log(Profiler.GetMonoUsedSize() + "/" + Profiler.GetTotalAllocatedMemory()); } if (Input.GetMouseButtonDown(2)) { // 获取地图上的点击点 var ray = MainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; Physics.Raycast(ray, out hit); // 点击到底板 if (hit.collider != null && hit.collider.name.Equals(LoadMap.MapPlane.name)) { var posOnMap = Utils.PositionToNum(LoadMap.MapPlane.transform.position, hit.point, UnitWidth, MapWidth, MapHeight); // 加载文件内容 var mapInfoData = InitMapInfo(); // 根据path放地标, 使用组队寻路跟随过去 StartCoroutine(AStarPathFinding.ISearchRoad(mapInfoData, lastTimeTargetX, lastTimeTargetY, posOnMap[0], posOnMap[1], DiameterX, DiameterY)); // 缓存上次目标点 lastTimeTargetX = posOnMap[0]; lastTimeTargetY = posOnMap[1]; } } if (Input.GetMouseButton(0)) { Utils.DrawGraphics(new RectGraphics(new Vector2(0, 0), 10, 10, 90), Color.white); } // 上下左右移动 if (Input.GetKey(KeyCode.UpArrow)) { MainCamera.transform.localPosition = new Vector3(MainCamera.transform.localPosition.x, MainCamera.transform.localPosition.y, MainCamera.transform.localPosition.z + 1); } if (Input.GetKey(KeyCode.DownArrow)) { MainCamera.transform.localPosition = new Vector3(MainCamera.transform.localPosition.x, MainCamera.transform.localPosition.y, MainCamera.transform.localPosition.z - 1); } if (Input.GetKey(KeyCode.LeftArrow)) { MainCamera.transform.localPosition = new Vector3(MainCamera.transform.localPosition.x - 1, MainCamera.transform.localPosition.y, MainCamera.transform.localPosition.z); } if (Input.GetKey(KeyCode.RightArrow)) { MainCamera.transform.localPosition = new Vector3(MainCamera.transform.localPosition.x + 1, MainCamera.transform.localPosition.y, MainCamera.transform.localPosition.z); } // 升高下降 if (Input.GetKey(KeyCode.PageUp)) { MainCamera.transform.localPosition = new Vector3(MainCamera.transform.localPosition.x, MainCamera.transform.localPosition.y + 1, MainCamera.transform.localPosition.z); } if (Input.GetKey(KeyCode.PageDown)) { MainCamera.transform.localPosition = new Vector3(MainCamera.transform.localPosition.x, MainCamera.transform.localPosition.y - 1, MainCamera.transform.localPosition.z); } if (Input.GetKeyUp(KeyCode.P)) { // 暂停 ClusterManager.Single.Pause(); } if (Input.GetKeyUp(KeyCode.G)) { // 继续 ClusterManager.Single.GoOn(); } if (Input.GetKeyUp(KeyCode.R)) { InitMapInfo(); } //if (Input.GetKey(KeyCode.A)) //{ // // 添加统计单位 // FightDataStatistical.Single.AddCostData(new ArmyTypeData() // { // ArmyId = 1, // ArmyType = 1, // Camp = 1, // GeneralType = 1, // SingleCost = 10 // }); //} //if (Input.GetKey(KeyCode.S)) //{ // // 打印统计数据 // Debug.Log("cost:" + FightDataStatistical.Single.GetCostData(1)); //} // 绘制关闭列表 DrawCloseMap(AStarPathFinding.closePathMap); }
void Start() { mainCam = FindObjectOfType <Camera>(); initDronePos = drone.transform.position; grid = GetComponent <Grid>(); plane = GetComponent <MeshCollider>(); cellsList = new List <Vector3Int>(); buildingsList = new List <GameObject>(); matterCellsList = new List <Vector3Int>(); matterBlocksList = new List <GameObject>(); // check width and height inputs if (minWidth > 0) { minWidth = -minWidth; } if (minWidth == 0) { minWidth = -1; } if (maxWidth < 0) { maxWidth = -maxWidth; } if (maxWidth == 0) { maxWidth = 1; } if (minHeight > 0) { minHeight = -minHeight; } if (minHeight == 0) { minHeight = -1; } if (maxHeight < 0) { maxHeight = -maxHeight; } if (maxHeight == 0) { maxHeight = 1; } // define astar as a new component aStar = this.gameObject.AddComponent <AStarPathFinding>(); // Init grid, cube size and FogOfWar size grid.cellSize = new Vector3(gridSize, gridSize, 1f); buildingSize = transform.localScale.x * gridSize; // Init Fog of War foWQuad = transform.Find("FogOfWar").transform; int _fowWidth = ((maxWidth - minWidth) % 2 == 0) ? (5 + maxWidth - minWidth) : (6 + maxWidth - minWidth); // Update for even width int _fowHeight = ((maxHeight - minHeight) % 2 == 0) ? (5 + maxHeight - minHeight) : (6 + maxHeight - minHeight); // Update for even height foWQuad.localScale = new Vector3(_fowWidth * gridSize, _fowHeight * gridSize, 0f); foWTexture = new Texture2D(_fowWidth, _fowHeight); foWQuad.GetComponent <MeshRenderer>().material.SetTexture("_MainTex", foWTexture); for (int i = 0; i < _fowWidth; i++) { for (int j = 0; j < _fowHeight; j++) { foWTexture.SetPixel(i, j, Color.black); } } foWTexture.Apply(); // Init cave with darkMatter blocks for (int x = minWidth; x <= maxWidth; x++) { for (int y = minHeight; y <= maxHeight; y++) { Vector3Int c = new Vector3Int(x, y, 0); GameObject block = Instantiate(darkMatterBlock, grid.CellToWorld(c), Quaternion.identity); block.transform.localScale = new Vector3(buildingSize, 90 * buildingSize / 100, buildingSize); // darkMatter is slightly smaller than building blocks to see the building ghost block.transform.Translate(new Vector3(0f, (90 * buildingSize / 100) / 2, 0f)); block.transform.SetParent(darkMatterHandler.transform); matterCellsList.Add(c); matterBlocksList.Add(block); } } // Init cave borders with indestructible blocks for (int x = minWidth - 1; x <= maxWidth + 1; x++) { Vector3Int c = new Vector3Int(x, minHeight - 1, 0); GameObject border = Instantiate(indestructibleBlock, grid.CellToWorld(c), Quaternion.identity); border.transform.localScale = new Vector3(buildingSize, buildingSize, buildingSize); border.transform.Translate(new Vector3(0f, buildingSize / 2, 0f)); border.transform.SetParent(buildingsHandler.transform); cellsList.Add(c); buildingsList.Add(border); c = new Vector3Int(x, maxHeight + 1, 0); border = Instantiate(indestructibleBlock, grid.CellToWorld(c), Quaternion.identity); border.transform.localScale = new Vector3(buildingSize, buildingSize, buildingSize); border.transform.Translate(new Vector3(0f, buildingSize / 2, 0f)); border.transform.SetParent(buildingsHandler.transform); cellsList.Add(c); buildingsList.Add(border); } for (int y = minHeight; y <= maxHeight; y++) { Vector3Int c = new Vector3Int(minWidth - 1, y, 0); GameObject border = Instantiate(indestructibleBlock, grid.CellToWorld(c), Quaternion.identity); border.transform.localScale = new Vector3(buildingSize, buildingSize, buildingSize); border.transform.Translate(new Vector3(0f, buildingSize / 2, 0f)); border.transform.SetParent(buildingsHandler.transform); cellsList.Add(c); buildingsList.Add(border); c = new Vector3Int(maxWidth + 1, y, 0); border = Instantiate(indestructibleBlock, grid.CellToWorld(c), Quaternion.identity); border.transform.localScale = new Vector3(buildingSize, buildingSize, buildingSize); border.transform.Translate(new Vector3(0f, buildingSize / 2, 0f)); border.transform.SetParent(buildingsHandler.transform); cellsList.Add(c); buildingsList.Add(border); } // Place startBuilding @ startPoint if (startPoint.x < minWidth) { startPoint.x = minWidth; } if (startPoint.x > maxWidth) { startPoint.x = maxWidth; } if (startPoint.y < minHeight) { startPoint.y = minHeight; } if (startPoint.y > maxHeight) { startPoint.y = maxHeight; } Vector3Int cell = new Vector3Int(startPoint.x, startPoint.y, 0); deleteDarkMatter(cell); GameObject instance = Instantiate(startBuilding, grid.CellToWorld(cell), Quaternion.identity); instance.transform.localScale = new Vector3(buildingSize, buildingSize, buildingSize); instance.transform.Translate(new Vector3(0f, buildingSize / 2, 0f)); instance.transform.SetParent(buildingsHandler.transform); cellsList.Add(cell); buildingsList.Add(instance); // Move drone @ startPoint drone.transform.Translate(grid.CellToWorld(cell)); // Move Camera over drone postion mainCam.transform.Translate(mainCam.transform.InverseTransformVector(drone.transform.position - initDronePos)); if (mainCam.GetComponent <CameraManager>() != null) { mainCam.GetComponent <CameraManager>().SetInitPosition(mainCam.transform.position); } // Set visible FogOfWar at drone position UpdateFoW(cell); // Place endBuilding @ endPoint if (endPoint.x < minWidth) { endPoint.x = minWidth; } if (endPoint.x > maxWidth) { endPoint.x = maxWidth; } if (endPoint.y < minHeight) { endPoint.y = minHeight; } if (endPoint.y > maxHeight) { endPoint.y = maxHeight; } cell = new Vector3Int(endPoint.x, endPoint.y, 0); deleteDarkMatter(cell); endBuildingInstance = Instantiate(endBuilding, grid.CellToWorld(cell), Quaternion.identity); endBuildingInstance.transform.localScale = new Vector3(buildingSize, buildingSize, buildingSize); endBuildingInstance.transform.Translate(new Vector3(0f, buildingSize / 2, 0f)); endBuildingInstance.transform.SetParent(buildingsHandler.transform); cellsList.Add(cell); buildingsList.Add(endBuildingInstance); // Place indestructible blocks at positions if they exist if (indestrictiblePositions.Length > 0) { for (int i = 0; i < indestrictiblePositions.Length; i++) { if (indestrictiblePositions[i].x < minWidth) { indestrictiblePositions[i].x = minWidth; } if (indestrictiblePositions[i].x > maxWidth) { indestrictiblePositions[i].x = maxWidth; } if (indestrictiblePositions[i].y < minHeight) { indestrictiblePositions[i].y = minHeight; } if (indestrictiblePositions[i].y > maxHeight) { indestrictiblePositions[i].y = maxHeight; } cell = new Vector3Int(indestrictiblePositions[i].x, indestrictiblePositions[i].y, 0); if (!cellsList.Contains(cell)) { deleteDarkMatter(cell); instance = Instantiate(indestructibleBlock, grid.CellToWorld(cell), Quaternion.identity); instance.transform.localScale = new Vector3(buildingSize, buildingSize, buildingSize); instance.transform.Translate(new Vector3(0f, buildingSize / 2, 0f)); instance.transform.SetParent(buildingsHandler.transform); cellsList.Add(cell); buildingsList.Add(instance); } } } // Init booleans for coroutines buildingEnds = true; stopBuilding = false; droneIsMoving = false; // Init UI Manager (used for both mode1 and mode2) uiManager = FindObjectOfType <UIManager>(); }