public static GridItem[] GetSlotsInMask(int gridX, int gridY, GridMask mask) { if (mask.w == 0 || mask.l == 0) { Debug.LogError("Mask isn't defined"); return(null); } List <GridItem> items = new List <GridItem>(); for (int i = 0; i < mask.w; i++) { for (int j = 0; j < mask.l; j++) { int indI = i - mask.w / 2; int indJ = j - mask.l / 2; if (gridX + indI > -1 && gridX + indI < m.width && gridY + indJ > -1 && gridY + indJ < m.length) { GridItem item = m.gridSlots.GetItem(gridX + indI, gridY + indJ); if (mask.Get(i, j)) { items.Add(item); } } } } return(items.ToArray()); }
public static bool HasFlags(GridMask source, GridMask flags) { int sourceInt = (int)source; int flagsInt = (int)flags; return((sourceInt & flagsInt) == flagsInt); }
/// <summary> /// Gets slot with shortest distance of all slots in range, on edge of mask. /// </summary> /// <param name="pos">pos which defines which side we want</param> /// <param name="targetSlot">attacked unit, to which we are applying mask</param> /// <returns></returns> public static GridItem ClosestFreeSlotOnEdge(Vector3 pos, GridItem targetSlot, GridMask mask) { // Dir from target to source, then take closest neighbour to it. Vector3 dir = (targetSlot.transform.position - pos); dir.Normalize(); GridItem[] nbrs = GridManager.GetSlotsInMask(targetSlot.gridX, targetSlot.gridY, mask); float[] distsToTarget = GetDistances <GridItem>(targetSlot.transform.position + dir, nbrs); float[] distsToSource = GetDistances <GridItem>(pos - dir, nbrs); // Closest slot in max range is the slot with minimum summed distance. // Slot that also on edge, is furthest away from mask and closest to source int index = 0; float minSum = float.MaxValue; float maxDistFromTarget = float.MinValue; float minDistFromSource = float.MaxValue; for (int i = 0; i < distsToTarget.Length; i++) { if (distsToTarget[i] + distsToSource[i] <= minSum && distsToTarget[i] > maxDistFromTarget && distsToSource[i] < minDistFromSource) { minSum = distsToTarget[i] + distsToSource[i]; maxDistFromTarget = distsToTarget[i]; minDistFromSource = distsToSource[i]; index = i; } } return(nbrs[index]); }
internal static GridItem[] GetSlotsInInteractiveRange(Unit slot, GridMask mask) { if (mask == null) // return all { Debug.Log("Mask is null, returning all slots."); return(m.gridSlots.AsArray()); } return(GetSlotsInMask(slot.gridX, slot.gridY, mask)); }
public void SetSkill(int skillId, int index, SkillSystem.SkEntity skenity = null, string icon = "", int decsId = 0) { if (icon != "") { //Debug.Log("skillId:" + skillId); //Debug.Log("技能列表:" + EffSkill.s_tblEffSkills.Count); // EffSkill skill = null; // if (EffSkill.s_tblEffSkills != null) // skill = EffSkill.s_tblEffSkills.Find(iterSkill1 => EffSkill.MatchId(iterSkill1, skillId)); string skillDes = ""; if (decsId > 0) { skillDes = PELocalization.GetString(decsId); } //int len = 0; //foreach (EffSkill es in EffSkill.s_tblEffSkills) //{ // //Debug.Log("技能列表中技能" + (++len) + ":" + es.m_iconImgPath); // Debug.Log("技能列表中技能ID" + (++len) + ":" + es.m_id); //} // foreach (EffSkill es in EffSkill.s_tblEffSkills) // { // Debug.Log(es.m_name[0]); // } // // if (skill != null && skill.m_iconImgPath != "0") // { // mItemspr.spriteName = skill.m_iconImgPath; // mItemspr.MakePixelPerfect(); // } mItemspr.spriteName = icon; mItemspr.MakePixelPerfect(); mSkillID = skillId; mGridMask = GridMask.GM_Skill; mItemIndex = index; mItemspr.enabled = true; mIsSkill = true; mSkenity = skenity; mSkillDes = skillDes; } else { mItemspr.spriteName = "Null"; mItemspr.MakePixelPerfect(); mItemspr.enabled = false; mIsSkill = false; mSkenity = null; if (null != mSkillCooldown) { mSkillCooldown.fillAmount = 0; } } }
public void SetItem(ItemSample itemSample, ItemPlaceType place = ItemPlaceType.IPT_Null, int index = 0, GridMask gridMask = GridMask.GM_Any) { mGridMask = gridMask; mItemSample = itemSample; mItemPlace = place; mItemIndex = index; if (mPutBackFlag) { CancelDrop(); } mPutBackFlag = false; mGrid = null; if (mItemSample == null) { Clear(); return; } if (null != Grid_N.mActiveGrid) { Grid_N.mActiveGrid.mSkillCooldown.fillAmount = 0; Grid_N.mActiveGrid = null; } GameUI.Instance.mItemPackageCtrl.RestItemState(); //BuildBlockManager.self.QuitBuildMode(); SetIcon(mItemSample.iconTex, mItemSample.iconString0); //ItemObject obj = mItemSample as ItemObject; //ItemAsset.Drag drag = null; //if (null != obj) //{ // drag = obj.GetCmpt<ItemAsset.Drag>(); //} //if (obj != null && drag != null && (place == ItemPlaceType.IPT_Bag || place == ItemPlaceType.IPT_HotKeyBar) // && !GameUI.Instance.bMainPlayerDead // ) //{ // ItemObjDragging dragging = new ItemObjDragging(drag); // DraggingMgr.Instance.Begin(dragging); //} //else //{ // mCurrenReq = null; //} mHasCreated = false; }
/// <summary> /// Checks if target is in mask of source. /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <param name="mask"></param> /// <returns></returns> public static bool IsSlotInMask(GridItem source, GridItem target, GridMask mask) { if (mask.w == 0 || mask.l == 0) { Debug.LogError("Mask isn't defined"); return(false); } int i = (target.gridX - source.gridX) + mask.w / 2; int j = (target.gridY - source.gridY) + mask.l / 2; if (i > -1 && i < mask.w && j > -1 && j < mask.l) { return(mask.Get(i, j)); } return(false); }
public static void RecolorMask(GridItem u, int color, GridMask attackMask) { RecolorRange(color, GetSlotsInMask(u.gridX, u.gridY, attackMask)); }
/// <summary> /// Example: Put range unit on max attack range. /// NOT 100% reliable. /// </summary> /// <param name="curSlot"></param> /// <param name="target"></param> /// <param name="moveMask"></param> /// <param name="attackMask"></param> /// <returns></returns> internal static GridItem ClosestToAttackEdgeOverMoveMask(GridItem curSlot, GridItem target, GridMask moveMask, GridMask attackMask) { // gets slot where unit should be moved to still be in attack range GridItem optimalMovePos = ClosestFreeSlotOnEdge(curSlot.transform.position, target, attackMask); GridItem viable = FurthestFreeSlotOnEdge(curSlot, target, moveMask); List <GridItem> list = new List <GridItem> { optimalMovePos, viable }; list = FilterByMask(list, curSlot, moveMask); if (list.Count == 2) { list = new List <GridItem>() { optimalMovePos } } ; float[] dists = GetDistances <GridItem>(target.transform.position, list.ToArray()); return(list[GetIndexOfMin(dists)]); }
public static List <GridItem> FilterByMask(List <GridItem> items, GridItem source, GridMask mask) { for (int i = 0; i < items.Count; i++) { if (!GridManager.IsSlotInMask(source, items[i], mask)) { items.RemoveAt(i); i--; } } return(items); }
internal static GridItem ClosestToTargetOverMask(GridItem source, GridItem targetSlot, GridMask mask) { List <GridItem> nbrs = Neighbours(targetSlot); nbrs.Add(FurthestFreeSlotOnEdge(source, targetSlot, mask)); nbrs = FilterByMask(nbrs, source, mask); if (nbrs.Count == 0) { Debug.Log("no moves"); return(null); } Vector3 dir = (targetSlot.transform.position - source.transform.position).normalized; float[] dists = GetDistances <GridItem>(targetSlot.transform.position - dir, nbrs.ToArray()); return(nbrs[dists.GetIndexOfMin()]); }
/// <summary> /// Gets slot with shortest distance of all slots in range. /// NOT RELIABLE to return edge. Use closestfreeslotonedge instead. /// </summary> /// <param name="pos">pos which defines which side we want</param> /// <param name="targetSlot">attacked unit, to which we are getting slot</param> /// <returns></returns> public static GridItem ClosestFreeSlotStillInMask(Vector3 pos, GridItem targetSlot, GridMask mask) { // Dir from target to source, then take closest neighbour to it. Vector3 dir = (targetSlot.transform.position - pos).normalized; GridItem[] nbrs = GridManager.GetSlotsInMask(targetSlot.gridX, targetSlot.gridY, mask); float[] distsToTarget = GetDistances <GridItem>(targetSlot.transform.position - dir, nbrs); float[] distsToSource = GetDistances <GridItem>(pos + dir, nbrs); // Closest slot in max range is the slot with minimum summed distance. int index = 0; float minSum = float.MaxValue; for (int i = 0; i < distsToTarget.Length; i++) { if (distsToTarget[i] + distsToSource[i] < minSum) { minSum = distsToTarget[i] + distsToSource[i]; index = i; } } return(nbrs[index]); }
public override IEnumerator FlagUpdate() { m = this; turnDone = false; NullifyUnits(); for (int i = 0; i < units.Count; i++) { units[i].ResetActions(); } while (true) { if (units.Count == 0) { Debug.Log("No units left for player."); break; } if (NoActionsLeft()) { break; } if (Input.GetKeyDown(KeyCode.Escape)) { break; } slot = SelectionManager.GetMouseAsSlot2D(); if (slot == null) { yield return(null); continue; } unit = slot.filledBy; if (unit == null) { unit = SelectionManager.GetMouseAsUnit2D(); } // -- Map changes for selected player unit -- Unit.activeUnit = null; if (curPlayerUnit && curPlayerUnit.HasActions) { Unit.activeUnit = curPlayerUnit; curMask = curPlayerUnit.abilities.BasicMask; //RemaskActive(3); if (!unit) // can move // RemaskUnit(1); { } else if (unit.flag.allianceId == 0) // player, can select // RemaskUnit(3); { } else if (unit.flag.allianceId != 0) // enemy, maybe can attack //RemaskUnit(2); { } } // -- end map changes bool selectionChanged = false; bool mousePress = Input.GetKeyDown(KeyCode.Mouse0); bool selectedPlayerUnit = unit && unit.flag.allianceId == 0; // -- Input -- // For player units, just select it. For enemy, attack them if player unit is selected. if (mousePress && selectedPlayerUnit && unit != curPlayerUnit) { selectionChanged = true; DeselectUnit(); if (selectedPlayerUnit && unit.HasActions) { curPlayerUnit = unit; } if (curPlayerUnit) { PlayerUIAbilityList.AssignAbilitiesToUI(curPlayerUnit.abilities); } } if (curPlayerUnit) { curMask = curPlayerUnit.pathing.moveMask; RemaskActive(3); curMask = curPlayerUnit.abilities.BasicMask; RemaskActive(2); RemaskUnit(1); } // move if (Input.GetKeyDown(KeyCode.Mouse1) && !RunningAbility) { selectionChanged = true; // if unit is already selected, move to that slot if (slot && slot.Walkable && curPlayerUnit && curPlayerUnit.CanMoveTo(slot)) { curMask = curPlayerUnit.abilities.BasicMask; curMask = curPlayerUnit.pathing.moveMask; RemaskActive(0); curMask = curPlayerUnit.abilities.BasicMask; RemaskActive(0); RemaskUnit(0); curPlayerUnit.MoveAction(slot); yield return(null); } } // attack if (!RunningAbility && mousePress && !selectedPlayerUnit && curPlayerUnit && curPlayerUnit.CanAttack) // unit = enemy unit { if (GridManager.IsUnitInAttackRange(curPlayerUnit, unit, curPlayerUnit.abilities.BasicAttack)) { bool aimSuccesful = true;// by default, always hit, if weapon doesn't use cone ability. if (curPlayerUnit.equippedWeapon && curPlayerUnit.equippedWeapon.conePref) { yield return(curPlayerUnit.StartCoroutine(WeaponFireMode.WaitPlayerToSetAim(curPlayerUnit, unit, curPlayerUnit.equippedWeapon.conePref, curPlayerUnit.equippedWeapon.StandardAttack.attackMask.Range))); aimSuccesful = CheckIfEnemyHit(unit); } if (aimSuccesful) { curPlayerUnit.AttackAction(slot, unit, curPlayerUnit.abilities.BasicAttack); curMask = curPlayerUnit.abilities.BasicMask; curMask = curPlayerUnit.pathing.moveMask; RemaskActive(0); curMask = curPlayerUnit.abilities.BasicMask; RemaskActive(0); RemaskUnit(0); } } yield return(null); } // Reload env interaction buttons when player is selected. if (selectionChanged) { if (curPlayerUnit) { UIInteractionController.ShowEnvInteractions(curPlayerUnit); } else { UIInteractionController.ClearEnvInteractions(); } } UIManager.PlayerStandardUi(!curPlayerUnit); UIManager.PlayerSelectAllyUnitUi(curPlayerUnit); // map decolor when unit run out of actions. if (curPlayerUnit && curPlayerUnit.NoActions) { DeselectUnit(); UIInteractionController.ClearEnvInteractions(); } yield return(null); if (slot && curPlayerUnit) { slot.RecolorSlot(0); } NullifyUnits(); } // Wait until all actions are complete for (int i = 0; i < units.Count; i++) { units[i].curSlot.RecolorSlot(0); while (units[i].moving) { yield return(null); } } turnDone = true; yield return(null); }
public CoordMaskPair(int2 coord, GridMask mask) { Coord = coord; Mask = mask; }
public void SetGridMask(GridMask mask) { mGridMask = mask; }