/// <summary> /// 从block中添加规则列表 /// </summary> /// <param name="block">方块</param> /// <param name="pos">方块被添加的坐标(偏移)</param> /// <param name="rotation">旋转附加</param> /// <param name="mirror">镜像附加</param> public void AddPosList(Block block, Vector2Int pos, ShipUnitRotation rotation = ShipUnitRotation.d0, ShipUnitMirror mirror = ShipUnitMirror.Normal) { List <Vector2Int> white = new List <Vector2Int>(), black = new List <Vector2Int>(); //添加白名单 foreach (var wpos in block.WhiteListPos) { WhiteListPos.Add(PosTrans(wpos, rotation, mirror) + pos); } foreach (var bpos in block.BlackListPos) { BlackListPos.Add(PosTrans(bpos, rotation, mirror) + pos); } }
/// <summary> /// 坐标转换函数 /// </summary> /// <param name="orginPos">需转换的坐标</param> /// <param name="rotation">旋转变换</param> /// <param name="mirror">镜像变换</param> /// <returns>转换后坐标</returns> public static Vector2Int PosTrans(Vector2Int orginPos, ShipUnitRotation rotation = ShipUnitRotation.d0, ShipUnitMirror mirror = ShipUnitMirror.Normal) { Vector2Int pos = orginPos; switch (mirror) { case ShipUnitMirror.Normal: break; case ShipUnitMirror.MirrorX: pos.x = -pos.x; break; case ShipUnitMirror.MirrorY: pos.y = -pos.y; break; case ShipUnitMirror.MirrorBoth: pos.x = -pos.x; pos.y = -pos.y; break; } switch (rotation) { case ShipUnitRotation.d0: break; case ShipUnitRotation.d90: pos = new Vector2Int(-pos.y, pos.x); break; case ShipUnitRotation.d180: pos = new Vector2Int(-pos.x, -pos.y); break; case ShipUnitRotation.d270: pos = new Vector2Int(pos.y, -pos.x); break; } return(pos); }
public void Update() { camera.orthographicSize += Input.GetAxis("Scroll"); if (!Testing) { transform.position = 1.6f * (Vector2)currentPos; if (Input.GetButtonDown(MenuButton)) { EventSystem.SetActive(!EventSystem.activeInHierarchy); menu.SetBool("work", EventSystem.activeInHierarchy); SetColor(); if (EventSystem.activeInHierarchy) { EventSystem.GetComponent <EventSystem>() .SetSelectedGameObject((selector.toggle as ComponentTypeSelectToggle).Panel.transform.GetChild(0).gameObject); } } if (currentcomponent != currentComponent) { ShowCurrentComponent(); currentcomponent = currentComponent; } if (inputTimer <= 0 && !EventSystem.activeInHierarchy) { if (Input.GetAxis("Horizontal") > 0) { inputTimer = 0.2f; currentPos += new Vector2Int(1, 0); SetColor(); } if (Input.GetAxis("Horizontal") < 0) { inputTimer = 0.2f; currentPos += new Vector2Int(-1, 0); SetColor(); } if (Input.GetAxis("Vertical") > 0) { inputTimer = 0.2f; currentPos += new Vector2Int(0, 1); SetColor(); } if (Input.GetAxis("Vertical") < 0) { inputTimer = 0.2f; currentPos += new Vector2Int(0, -1); SetColor(); } camera.transform.position += (Vector3) new Vector2(Input.GetAxis("CameraH"), Input.GetAxis("CameraV")); } else if (inputTimer > 0) { inputTimer -= Time.deltaTime; } if (!EventSystem.activeInHierarchy) { if (Input.GetButtonDown("A")) { currentShip.AddComponent(currentComponent.Id, currentComponent.ShipCompoionentLevel, currentPos, rotation, mirror); SetColor(); } if (Input.GetButtonDown("X")) { currentShip.RemoveBlockAtPosition(currentPos); SetColor(); } if (Input.GetButtonDown("Mirror")) { int m = (int)mirror; m++; if (m > 1) { m = 0; } mirror = (ShipUnitMirror)Enum.ToObject(typeof(ShipUnitMirror), m); ShowCurrentComponent(); } if (Input.GetButtonDown("Rotation")) { int m = (int)rotation; m++; if (m > 3) { m = 0; } rotation = (ShipUnitRotation)Enum.ToObject(typeof(ShipUnitRotation), m); ShowCurrentComponent(); } } } if (Testing) { camera.transform.position = currentShip.transform.position + new Vector3(0, 0, -10f); if (Input.GetButtonDown(MenuButton)) { Testing = false; currentShip.GetComponent <Rigidbody2D>().velocity = Vector3.zero; currentShip.transform.position = factory.transform.position; currentShip.transform.rotation = Quaternion.identity; currentShip.GetComponent <ShipControl>().mode = ShipControlMode.Building; foreach (Transform t in transform) { t.gameObject.SetActive(true); } } } }
public Block ForceAddBlockAtPosition(int id, int level, Vector2Int pos, ShipUnitRotation rotation = ShipUnitRotation.d0, ShipUnitMirror mirror = ShipUnitMirror.Normal) { var blockPrefab = GetBlock(id, level); var blockTransform = Instantiate(blockPrefab.gameObject, transform).transform; var block = blockTransform.GetComponent <Block>(); if (id == 0) { core = block; } blockTransform.localPosition = Block.GetLocalPositionFromShipPos(pos); var rot = Vector3.zero; //镜像处理 switch (mirror) { case ShipUnitMirror.Normal: break; case ShipUnitMirror.MirrorX: rot.x = 180; break; case ShipUnitMirror.MirrorY: rot.y = 180; break; case ShipUnitMirror.MirrorBoth: rot.x = 180; rot.y = 180; break; } //旋转处理 switch (rotation) { case ShipUnitRotation.d0: rot.z = 0; break; case ShipUnitRotation.d90: rot.z = 90; break; case ShipUnitRotation.d180: rot.z = 180; break; case ShipUnitRotation.d270: rot.z = 270; break; } if (block.PosTransWithComponent) { blockTransform.rotation = Quaternion.Euler(rot); } block.Rotation = rotation; block.Mirror = mirror; block.ParentShip = this; block.Pos = pos; Blocks.Add(pos, block); AddPosList(block, pos, rotation, mirror); SetFourWaysNearby(pos); if (BlocksChanged != null) { BlocksChanged(Blocks.Count); } return(block); }
/// <summary> /// 替换组件 /// </summary> /// <param name="id"></param> /// <param name="level"></param> /// <param name="pos"></param> /// <param name="rotation"></param> /// <param name="mirror"></param> public void ReplaceComponent(int id, int level, Vector2Int Dpos, Vector2Int Ppos, ShipUnitRotation rotation = ShipUnitRotation.d0, ShipUnitMirror mirror = ShipUnitMirror.Normal) { RemoveBlockAtPosition(Dpos); AddComponent(id, level, Ppos, rotation, mirror); }
/// <summary> /// 坐标组转换函数 /// </summary> /// <param name="PosList">坐标组</param> /// <param name="offset">偏移</param> /// <param name="rotation">旋转</param> /// <param name="mirror">镜像</param> /// <returns></returns> public static List <Vector2Int> PosTransList(List <Vector2Int> PosList, Vector2Int offset, ShipUnitRotation rotation = ShipUnitRotation.d0, ShipUnitMirror mirror = ShipUnitMirror.Normal) { List <Vector2Int> poses = new List <Vector2Int>(PosList); for (int i = 0; i < poses.Count; i++) { poses[i] = PosTrans(poses[i], rotation, mirror) + offset; } return(poses); }
/// <summary> /// 添加某个ID的组件 /// </summary> /// <param name="id">组件ID</param> /// <param name="level">组件等级</param> /// <param name="pos">组件原点坐标</param> /// <param name="rotation">组件旋转</param> /// <param name="mirror">组件镜像</param> public ShipComponent AddComponent(int id, int level, Vector2Int pos, ShipUnitRotation rotation = ShipUnitRotation.d0, ShipUnitMirror mirror = ShipUnitMirror.Normal) { if (id == 0 && core != null) { return(null); } List <Vector2Int> whiteList = new List <Vector2Int>(), blackList = new List <Vector2Int>(); ShipComponent component = GetShipComponent(id, level); bool whiteCheck = false, blackCheck = true; foreach (var bk in component.BlockList) { if (!GetBlock(bk.id, level).CanAddWithoutList&&WhiteListPos.Contains(PosTrans(new Vector2Int(bk.posX, bk.posY), rotation, mirror) + pos)) { whiteCheck = true; } if (GetBlock(bk.id, level).CanAddWithoutList) { whiteCheck = true; } } Debug.Log("white" + whiteCheck); foreach (var bk in component.BlockList) { if ((Blocks.ContainsKey(PosTrans(new Vector2Int(bk.posX, bk.posY), rotation, mirror) + pos))) { blackCheck = false; } if (((BlackListPos.Contains(PosTrans(new Vector2Int(bk.posX, bk.posY), rotation, mirror) + pos)) && (!GetBlock(bk.id, level).CanAddOnBlackList))) { blackCheck = false; } foreach (var p in GetBlock(bk.id, level).CheckPos) { if (Blocks.ContainsKey(PosTrans(new Vector2Int(bk.posX, bk.posY) + p, rotation, mirror) + pos)) { blackCheck = false; } } } if (!whiteCheck || !blackCheck) { return(null); } var componentTransform = Instantiate(GetShipComponent(id, level).gameObject, transform).transform; var shipComponent = componentTransform.GetComponent <ShipComponent>(); componentTransform.localPosition = Block.GetLocalPositionFromShipPos(pos); var rot = Vector3.zero; //镜像处理 switch (mirror) { case ShipUnitMirror.Normal: break; case ShipUnitMirror.MirrorX: rot.x = 180; break; case ShipUnitMirror.MirrorY: rot.y = 180; break; case ShipUnitMirror.MirrorBoth: rot.x = 180; rot.y = 180; break; } //旋转处理 switch (rotation) { case ShipUnitRotation.d0: rot.z = 0; break; case ShipUnitRotation.d90: rot.z = 90; break; case ShipUnitRotation.d180: rot.z = 180; break; case ShipUnitRotation.d270: rot.z = 270; break; } componentTransform.rotation = Quaternion.Euler(rot); shipComponent.ParentShip = this; shipComponent.pos = pos; shipComponent.Rotation = rotation; shipComponent.Mirror = mirror; ShipComponents.Add(pos, shipComponent); shipComponent.Blocks.Clear(); foreach (var bk in component.BlockList) { Block blo = ForceAddBlockAtPosition(bk.id, level, PosTrans(new Vector2Int(bk.posX, bk.posY), rotation, mirror) + pos, rotation, mirror); shipComponent.Blocks.Add(new Vector2Int(bk.posX, bk.posY), blo); if (blo != null) { blo.ParentShipComponent = shipComponent; blo.transform.parent = shipComponent.transform; } } return(shipComponent); }
/// <summary> /// 添加某个ID的组件 /// </summary> /// <param name="id">组件ID</param> /// <param name="level">组件等级</param> /// <param name="pos">组件原点坐标</param> /// <param name="rotation">组件旋转</param> /// <param name="mirror">组件镜像</param> public ShipComponent AddComponent(int id, int level, Vector2Int pos, ShipUnitRotation rotation = ShipUnitRotation.d0, ShipUnitMirror mirror = ShipUnitMirror.Normal) { if (!ComponentPutable(id, level, pos, rotation, mirror)) { return(null); } ShipComponent component = GetShipComponent(id, level); var componentTransform = Instantiate(GetShipComponent(id, level).gameObject, transform).transform; var shipComponent = componentTransform.GetComponent <ShipComponent>(); componentTransform.localPosition = Block.GetLocalPositionFromShipPos(pos); var rot = Vector3.zero; //镜像处理 switch (mirror) { case ShipUnitMirror.Normal: break; case ShipUnitMirror.MirrorX: rot.x = 180; break; case ShipUnitMirror.MirrorY: rot.y = 180; break; case ShipUnitMirror.MirrorBoth: rot.x = 180; rot.y = 180; break; } //旋转处理 switch (rotation) { case ShipUnitRotation.d0: rot.z = 0; break; case ShipUnitRotation.d90: rot.z = 90; break; case ShipUnitRotation.d180: rot.z = 180; break; case ShipUnitRotation.d270: rot.z = 270; break; } componentTransform.rotation = Quaternion.Euler(rot); shipComponent.ParentShip = this; shipComponent.pos = pos; shipComponent.Rotation = rotation; shipComponent.Mirror = mirror; ShipComponents.Add(pos, shipComponent); shipComponent.Blocks.Clear(); foreach (var bk in component.BlockList) { Block blo = ForceAddBlockAtPosition(bk.id, level, PosTrans(new Vector2Int(bk.posX, bk.posY), rotation, mirror) + pos, rotation, mirror); shipComponent.Blocks.Add(new Vector2Int(bk.posX, bk.posY), blo); if (blo != null) { blo.ParentShipComponent = shipComponent; blo.transform.parent = shipComponent.transform; } } return(shipComponent); }
public bool ComponentPutable(int id, int level, Vector2Int pos, ShipUnitRotation rotation = ShipUnitRotation.d0, ShipUnitMirror mirror = ShipUnitMirror.Normal) { if (GetShipComponent(id, level) == null) { return(false); Debug.Log("1"); } if (id == 0 && core != null) { return(false); Debug.Log("2"); } List <Vector2Int> whiteList = new List <Vector2Int>(), blackList = new List <Vector2Int>(); ShipComponent component = GetShipComponent(id, level); bool whiteCheck = false, blackCheck = true; foreach (var bk in component.BlockList) { if (!GetBlock(bk.id, level).CanAddWithoutList&& WhiteListPos.Contains(PosTrans(new Vector2Int(bk.posX, bk.posY), rotation, mirror) + pos)) { whiteCheck = true; } if (GetBlock(bk.id, level).CanAddWithoutList) { whiteCheck = true; } } Debug.Log("white" + whiteCheck); foreach (var bk in component.BlockList) { if ((Blocks.ContainsKey(PosTrans(new Vector2Int(bk.posX, bk.posY), rotation, mirror) + pos))) { blackCheck = false; Debug.Log("b1"); } if (((BlackListPos.Contains(PosTrans(new Vector2Int(bk.posX, bk.posY), rotation, mirror) + pos)) && (!GetBlock(bk.id, level).CanAddOnBlackList))) { blackCheck = false; Debug.Log("b2"); } foreach (var p in GetBlock(bk.id, level).CheckPos) { if (Blocks.ContainsKey(PosTrans(new Vector2Int(bk.posX, bk.posY) + p, rotation, mirror) + pos)) { blackCheck = false; Debug.Log("b3"); } } } if (!whiteCheck || !blackCheck) { return(false); Debug.Log("3"); } return(true); }