public void Setup(RL.Map _map, int[] _dir) { map = _map; directions = _dir; lineRenderers = new GameObject[directions.Length]; for (int i = 0; i < directions.Length; i++) { lineRenderers [i] = new GameObject(); lineRenderers [i].transform.parent = transform; lineRenderers [i].transform.position = transform.position; LineRenderer lr = lineRenderers [i].AddComponent <LineRenderer> (); lr.renderer.sortingOrder = 2; lr.renderer.sortingLayerName = "Default"; lr.SetColors(new Color(74 / 255f, 63 / 255f, 148 / 255f, 0.5f), new Color(113 / 255f, 138 / 255f, 145 / 255f, 0.5f)); lr.SetWidth(0.05f, 0.05f); lr.material = (Material)Resources.Load("trapParticle"); lr.useWorldSpace = false; // move out on the direction until we hit a wall, this is our endpoint Vector2i ndir = new Vector2i(RL.Map.nDir[directions[i], 0], RL.Map.nDir[directions[i], 1]); Vector2i np = GetComponent <RLCharacter>().positionI; int distanceCount = 1; np += ndir; while (map.IsOpenTile(np.x, np.y)) { np += ndir; } // np -= ndir*0.5f; lr.SetPosition(0, Vector3.zero); lr.SetPosition(1, new Vector3(np.x - ndir.x * 0.5f, np.y - ndir.y * 0.5f, 0) - transform.position); } }
public void Setup(RL.Map _map, int[] _dir) { map = _map; directions = _dir; lineRenderers = new GameObject[directions.Length]; for (int i = 0; i < directions.Length; i++) { lineRenderers [i] = new GameObject (); lineRenderers [i].transform.parent = transform; lineRenderers [i].transform.position = transform.position; LineRenderer lr = lineRenderers [i].AddComponent<LineRenderer> (); lr.renderer.sortingOrder = 2; lr.renderer.sortingLayerName = "Default"; lr.SetColors(new Color(74/255f,63/255f,148/255f, 0.5f), new Color(113/255f,138/255f,145/255f, 0.5f)); lr.SetWidth (0.05f, 0.05f); lr.material = (Material)Resources.Load ("trapParticle"); lr.useWorldSpace = false; // move out on the direction until we hit a wall, this is our endpoint Vector2i ndir = new Vector2i (RL.Map.nDir[directions[i],0], RL.Map.nDir[directions[i],1]); Vector2i np = GetComponent<RLCharacter>().positionI; int distanceCount = 1; np += ndir; while (map.IsOpenTile (np.x, np.y)) { np += ndir; } // np -= ndir*0.5f; lr.SetPosition (0, Vector3.zero); lr.SetPosition (1, new Vector3(np.x-ndir.x*0.5f, np.y-ndir.y*0.5f, 0)-transform.position); } }
public List<Vector2i> FindPath(Vector2i start, Vector2i end, CostCallback costFn, Map map) { if (start.Equals (end)) { return new List<Vector2i> (); } // clear the lists openList = new Dictionary<ulong, Node> (); closedList = new Dictionary<ulong, Node> (); Node startNode = new Node (start); openList [startNode.Key] = startNode; bool pathfound = false; int searchCount = 0; while (!pathfound && openList.Count > 0 && searchCount < 500) { Node lowestCost = findLowestCost (); openList.Remove (lowestCost.Key); closedList[lowestCost.Key] = lowestCost; if (lowestCost.position.Equals (end)) { pathfound = true; break; } for (int i = 0; i < 4; i++) { Vector2i neighborPosition = new Vector2i (lowestCost.position.x + Map.nDir [i, 0], lowestCost.position.y + Map.nDir [i, 1]); int newCost = lowestCost.distanceCost + costFn (neighborPosition.x, neighborPosition.y); if (map.IsOpenTile (neighborPosition.x, neighborPosition.y) && !openList.ContainsKey (Node.KeyFromPostion (neighborPosition))) { Node newPosition = new Node (neighborPosition); newPosition.heuristicCost = (int)Math.Abs (neighborPosition.x - end.x) + (int)Math.Abs (neighborPosition.y - end.y); newPosition.distanceCost = newCost; newPosition.parent = lowestCost; openList[newPosition.Key] = newPosition; } else if (openList.ContainsKey (Node.KeyFromPostion (neighborPosition)) && openList[Node.KeyFromPostion (neighborPosition)].distanceCost > newCost) { openList [Node.KeyFromPostion (neighborPosition)].distanceCost = newCost; openList [Node.KeyFromPostion (neighborPosition)].parent = lowestCost; } } searchCount++; } Node nextn = null; // couldn't find a path to the end if (!closedList.ContainsKey (Node.KeyFromPostion (end))) { // just build a short list containing the neighbor tiles. // eventually this should find "as near as possible" type tiles to move towards // I guess it depends on the application though startNode = new Node (start); Node lowestCost = startNode; for (int i = 0; i < 4; i++) { Vector2i neighborPosition = new Vector2i (lowestCost.position.x + Map.nDir [i, 0], lowestCost.position.y + Map.nDir [i, 1]); int newCost = lowestCost.distanceCost + costFn (neighborPosition.x, neighborPosition.y); if (map.IsOpenTile (neighborPosition.x, neighborPosition.y) && !openList.ContainsKey (Node.KeyFromPostion (neighborPosition))) { Node newPosition = new Node (neighborPosition); newPosition.heuristicCost = (int)Math.Abs (neighborPosition.x - end.x) + (int)Math.Abs (neighborPosition.y - end.y); newPosition.distanceCost = newCost; newPosition.parent = lowestCost; openList [newPosition.Key] = newPosition; } } nextn = findLowestCost (); } else { nextn = closedList [Node.KeyFromPostion (end)]; } // build the step list List<Vector2i> returnList = new List<Vector2i> (); while (nextn != null) { returnList.Add (nextn.position); nextn = nextn.parent; } returnList.Reverse (); return returnList; }
void GenLevel() { currentLevel++; // clear the map and all current objects foreach (RLCharacter c in objects) { Destroy(c.gameObject); } objects.Clear(); // rebuild the map // build the map map = new RL.Map(14, 12); // build the walls based on the underlying tiles for (int x = 0; x < map.sx; x++) { for (int y = 0; y < map.sy; y++) { /* * assuming there is a sprite in the middle, here are the potential fills * ... * .x. * ... * * ... * xxx * ... * * .x. * .x. * .x. * * oxi // note, need to do two for each, depending on the direction of internal / external * oxx * ooo * * ... * .xx * .x. * * .x. * xx. * ... * * what a pain in the butt. I am not going to f**k with this for right now, because it makes tons of assumptions * about the map structure and room layout and stuff */ Sprite toInsert = tl; bool needsInsert = false; if (!map.IsOpenTile(x, y)) { toInsert = t; needsInsert = true; } if (needsInsert) { GameObject s = (GameObject)Instantiate(spriteHolder, new Vector3(x, y, 0), Quaternion.identity); s.GetComponent <SpriteRenderer> ().sprite = toInsert; s.AddComponent <RLCharacter> (); objects.Add(s.GetComponent <RLCharacter>()); } } } GameObject playerGO = (GameObject)Instantiate(spriteHolder, new Vector3(4, 3, 0), Quaternion.identity); player = playerGO.AddComponent <RLCharacter> (); // player = playerGO.GetComponent<RLCharacter> (); player.GetComponent <SpriteRenderer> ().sprite = playerR; player.name = "player"; objects.Add(player); currentFacing = Facing.RIGHT; // add a random number of monsters int nMonsters = currentLevel; for (int i = 0; i < nMonsters; i++) { GameObject m = (GameObject)Instantiate(monster, new Vector3(Random.Range(1, map.sx - 1), Random.Range(1, map.sy - 1)), Quaternion.identity); m.GetComponent <RLCharacter> ().AddType(RLCharacter.RLTypes.MONSTER); objects.Add(m.GetComponent <RLCharacter> ()); } // add some downward facing stairs GameObject stair = (GameObject)Instantiate(spriteHolder, new Vector3(Random.Range(1, map.sx - 1), Random.Range(1, map.sy - 1)), Quaternion.identity); stair.GetComponent <SpriteRenderer> ().sprite = stairsDown; stair.AddComponent <RLCharacter> (); stair.GetComponent <RLCharacter> ().AddType(RLCharacter.RLTypes.STAIRS_DOWN); objects.Add(stair.GetComponent <RLCharacter> ()); }
void GenLevel() { currentLevel++; // clear the map and all current objects foreach (RLCharacter c in objects) { Destroy (c.gameObject); } objects.Clear (); // rebuild the map // build the map map = new RL.Map (14, 12); // build the walls based on the underlying tiles for (int x = 0; x < map.sx; x++) { for (int y = 0; y < map.sy; y++) { /* * assuming there is a sprite in the middle, here are the potential fills * ... * .x. * ... * * ... * xxx * ... * * .x. * .x. * .x. * * oxi // note, need to do two for each, depending on the direction of internal / external * oxx * ooo * * ... * .xx * .x. * * .x. * xx. * ... * * what a pain in the butt. I am not going to f**k with this for right now, because it makes tons of assumptions * about the map structure and room layout and stuff */ Sprite toInsert = tl; bool needsInsert = false; if (!map.IsOpenTile (x, y)) { toInsert = t; needsInsert = true; } if (needsInsert) { GameObject s = (GameObject)Instantiate (spriteHolder, new Vector3 (x, y, 0), Quaternion.identity); s.GetComponent<SpriteRenderer> ().sprite = toInsert; s.AddComponent<RLCharacter> (); objects.Add (s.GetComponent<RLCharacter>()); } } } GameObject playerGO = (GameObject)Instantiate (spriteHolder, new Vector3 (4, 3, 0), Quaternion.identity); player = playerGO.AddComponent<RLCharacter> (); // player = playerGO.GetComponent<RLCharacter> (); player.GetComponent<SpriteRenderer> ().sprite = playerR; player.name = "player"; objects.Add (player); currentFacing = Facing.RIGHT; // add a random number of monsters int nMonsters = currentLevel; for (int i = 0; i < nMonsters; i++) { GameObject m = (GameObject)Instantiate (monster, new Vector3 (Random.Range (1, map.sx - 1), Random.Range (1, map.sy - 1)), Quaternion.identity); m.GetComponent<RLCharacter> ().AddType (RLCharacter.RLTypes.MONSTER); objects.Add (m.GetComponent<RLCharacter> ()); } // add some downward facing stairs GameObject stair = (GameObject)Instantiate (spriteHolder, new Vector3 (Random.Range (1, map.sx - 1), Random.Range (1, map.sy - 1)), Quaternion.identity); stair.GetComponent<SpriteRenderer> ().sprite = stairsDown; stair.AddComponent<RLCharacter> (); stair.GetComponent<RLCharacter> ().AddType (RLCharacter.RLTypes.STAIRS_DOWN); objects.Add (stair.GetComponent<RLCharacter> ()); }