/// <summary> /// 判断矩形是否超出边界 /// </summary> /// <param name="rc"></param> /// <param name="xMax"> 边界x的最大值</param> /// <param name="yMax"> 边界y的最大值</param> /// <returns></returns> public static bool JudgeBeyondBoundary(RectAngle rc, int xMax, int yMax) { if (rc.PosX + rc.Row <= xMax && rc.PosY + rc.Column <= yMax) { return(false); } return(true); }
private void GenerateMap(Island island) { string pos = RandomPosition(); if (pos != null) { int posX = int.Parse(pos.Split(',')[0]); int posY = int.Parse(pos.Split(',')[1]); RectAngle islandRc = new RectAngle(posX, posY, island.mapItemRealRow, island.mapItemRealColumn); IslandPlaceInfo info = new IslandPlaceInfo(); info.rc = islandRc; info.island = island; if (MapItemTool.JudgeBeyondBoundary(islandRc, mapInfo.mapRow, mapInfo.mapColumn)) { //当前获取到的随机地址不可存放物体(超过地图限制),重新获取地点 tempCellInfos.Add(pos); mapCells.Remove(pos); GenerateMap(island); return; } //查看是否与以放置的物体重叠 foreach (IslandPlaceInfo placeInfo in islandsPlaced) { //判断要放置的物体与以放置的物体是否重叠 if (MapItemTool.JudgeMapItemOverLay(info.rc, placeInfo.rc)) { //当前获取到的随机地址不可存放物体(存放物体范围内已被占用),重新获取地点 tempCellInfos.Add(pos); mapCells.Remove(pos); GenerateMap(island); return; } } //说明获取到的地址ok,放置物体 islandsPlaced.Add(info); ResetCellInfos(); RemoveRCItemCells(info); //放置海岛后,生成下一个海岛,继续放置 PlaceRandomIsland(); } else { //已经没有可以放置该物体的点了,去除该物体的预制体及可以存放个数 Debug.Log("已经没有可以放置该物体的点了" + currentIsland.name); if (isLandCount.ContainsKey(currentIsland.name)) { isLandCount.Remove(currentIsland.name); } islandCanPlace.Remove(currentIsland); ResetCellInfos(); PlaceRandomIsland(); } }
private void RemoveRCItemCells(IslandPlaceInfo info) { RectAngle rc = info.rc; int posX = rc.PosX; int posY = rc.PosY; for (int i = posX; i < posX + rc.Row; i++) { for (int j = posY; j < posY + rc.Column; j++) { mapCells.Remove(i + "," + j); } } }
public Rocket(bool initDNA) { width = 25; height = 5; acc = new Vector(0, 0); vel = new Vector(0, 0); time = lifespan; timeActual = 0; body = new RectAngle(bmp.Width / 2.0F, bmp.Height - Math.Max(width, height) / 2 - 5, width, height, 0); if (initDNA) { dna = new DNA(); } }
public void PlaceIslands(List <IslandPlaceInfo> infos) { foreach (IslandPlaceInfo info in infos) { Island island = info.island; GameObject templet = islandPrefabs[island.name]; GameObject go = Instantiate(templet, Vector3.zero, Quaternion.identity, RandomObjs.transform); RectAngle rc = info.rc; //Debug.Log("放置海岛:"+island.name + " x:" + rc.PosX + " y:" + rc.PosY); float startX = -mapInfo.mapXLength * 0.5f; float startZ = mapInfo.mapZLength * 0.5f; float goPosX = startX + (rc.PosX) * mapInfo.cellSize + 0.5f * island.xlength; float goPosZ = startZ - (rc.PosY) * mapInfo.cellSize - 0.5f * island.zlength; go.transform.position = new Vector3(goPosX, 0.0f, goPosZ); go.SetActive(true); go.name = templet.name; } }
/* * 矩形重叠 * if (rc1.x + rc1.width > rc2.x && * rc2.x + rc2.width > rc1.x && * rc1.y + rc1.height > rc2.y && * rc2.y + rc2.height > rc1.y * ) * return true; * else * return false; */ public static bool JudgeMapItemOverLay(RectAngle rc1, RectAngle rc2) { int right1 = rc1.PosX + rc1.Row; int bottom1 = rc1.PosY + rc1.Column; int right2 = rc2.PosX + rc2.Row; int bottom2 = rc2.PosY + rc2.Column; ////先简单检测,如果未重叠直接返回,否则进行具体的重叠检测 float centerOffsetX = Mathf.Abs((rc1.PosX + right1) * 0.5f - (rc2.PosX + right2) * 0.5f); float centerOffsetY = Mathf.Abs((rc1.PosY + bottom1) * 0.5f - (rc2.PosY + bottom2) * 0.5f); float addX = (rc1.Row + rc2.Row) * 0.5f; float addY = (rc1.Column + rc2.Column) * 0.5f; if (centerOffsetX < addX && centerOffsetY < addY) { return(true); } else { return(false); } }
public bool intersectsWith(RectAngle r) { return(Left <= r.Right && Right >= r.Left && Top <= r.Bottom && Bottom >= r.Top); }