//获取从s到e的路径,返回路径长度小于等于step static public Vector3 GetPathByStep(Vector3 s, Vector3 e, int step) { VoxelBlocks map = GameObject.Find("Voxel Map").GetComponentInChildren <VoxelBlocks>(); List <Vector3> path = null; BlockInfo block = map.GetBlockByLogicPos(e); if (block != null && block.entity != null) { BlockType blockType = block.entity.GetComponent <BlockInfoComponent>().m_blockType; block.entity.GetComponent <BlockInfoComponent>().m_blockType = BlockType.None; path = GetPath(s, e); block.entity.GetComponent <BlockInfoComponent>().m_blockType = blockType; path.Remove(path[path.Count - 1]); } else { path = GetPath(s, e); } if (path != null && path.Count > step) { path.RemoveRange(step, path.Count - step); } if (path != null && path.Count > 0) { return(path[path.Count - 1]); } return(Vector3.down); }
//每帧都会调用对应的实体 public override void Execute(List <BasicEntity> entities) { foreach (BasicEntity e in entities) { AttackComponent attack = e.GetComponent <AttackComponent> (); VoxelBlocks map = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks> (); AbilityComponent ab = e.GetComponent <AbilityComponent> (); InputComponent input = e.GetComponent <InputComponent> (); StateComponent ap = e.GetComponent <StateComponent> (); int i = AiToInput.GetAbilityCount(e, M_LinkedType); //检测是否按下攻击键 if (i >= ab.m_temporaryAbility.Count || i != input.currentKey) { continue; } //若无攻击对象则获取周围可攻击对象 if (attack.enemy == null) { attack.enemy = GetEnemyAround(e); if (attack.enemy == null) { return; } } List <Vector3> el = new List <Vector3> (); foreach (var enemy in attack.enemy) { el.Add(enemy.GetComponent <BlockInfoComponent> ().m_logicPosition); } UISimple ui = GameObject.Find("UI").GetComponent <UISimple> (); ui.ShowUI(el, 2); //左键攻击 if (input.leftButtonDown) { BasicEntity enemy = map.GetBlockByLogicPos(input.currentPos).entity; //检测当前选中敌人是否处于攻击范围内 List <BasicEntity> list = GetEnemyAround(e); if (list != null && !list.Contains(enemy)) { attack.enemy = list; return; } //扣除敌人HP值 DeadComponent dead = enemy.GetComponent <DeadComponent> (); StateComponent state = e.GetComponent <StateComponent> (); dead.hp -= attack.STR; state.m_actionPoint -= 1; state.Invoke("AnimationEnd", 1); state.AnimationStart(); //播放攻击动画 //播放敌人受击动画 //减少AP } } }
static private bool checkWall(BlockInfo blockInfo) { VoxelBlocks voxelBlocks = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>(); //获取下方方块的逻辑位置 Vector3 logPos = blockInfo.logPos; logPos.z--; //若该位置没有东西就判断其下方的方块 if (blockInfo.entity == null) { BasicEntity entity = voxelBlocks.GetBlockByLogicPos(logPos).entity; if (entity == null) { Debug.Log(logPos); return(true); } if (entity.GetComponent <BlockInfoComponent>().m_blockType != BlockType.Floor) { return(true); } return(false); } if (blockInfo.entity.GetComponent <BlockInfoComponent>().m_blockType != BlockType.None) { return(true); } return(false); }
static public List <Vector3> GetArea(Vector3 pos, int ridus) { List <Vector3> area = new List <Vector3>(); VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>(); Vector3 nPos = pos; for (int i = -ridus; i < ridus; i++) { nPos.x = pos.x + i; for (int j = -ridus; j < ridus; j++) { nPos.y = pos.y + j; if ((nPos - pos).magnitude >= ridus) { continue; } if (map.GetBlockByLogicPos(nPos) != null) { area.Add(nPos); } } } return(area); }
void Bomb(BasicEntity entity, float mr, float ridus, int demage) { InputComponent input = entity.GetComponent <InputComponent>(); VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>(); //获取鼠标位置与角色位置的距离 float r = (input.currentPos - entity.GetComponent <BlockInfoComponent>().m_logicPosition).magnitude; //若距离大于预设值则退出 if (r > mr) { return; } //获取炸弹投掷后可能影响的范围 List <Vector3> ar = FindPath.GetArea(input.currentPos, Mathf.FloorToInt(ridus)); //显示炸弹影响范围 List <DeadComponent> mc = new List <DeadComponent>(); UISimple ui = GameObject.Find("UI").GetComponent <UISimple>(); ui.ShowUI(ar, 3); //获取该范围内所有的敌人的生命脚本 foreach (var pos in ar) { BlockInfo block = map.GetBlockByLogicPos(pos); if (block != null && block.entity != null) { if (block.entity.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Enemy) { mc.Add(block.entity.GetComponent <DeadComponent>()); } } } //显示可被影响的敌人 //若按下鼠标左键则扔出炸弹,影响周围敌人。 if (input.leftButtonDown) { Debug.Log(mc.Count); foreach (var m in mc) { if (m != null) { m.hp -= demage; } } ItemComponent itemComp = entity.GetComponent <ItemComponent>(); //移除炸弹 itemComp.item.Remove(ItemType.Bomb); entity.GetComponent <ItemComponent>().current = ItemType.Null; //使用一次消耗一点行动点 StateComponent state = entity.GetComponent <StateComponent>(); state.m_actionPoint -= 1; state.AnimationStart(); state.Invoke("AnimationEnd", 1); } }
public override void Execute(List <BasicEntity> entities) { foreach (var entity in entities) { KnockComponent knock = entity.gameObject.GetComponent <KnockComponent>(); AbilityComponent ab = entity.GetComponent <AbilityComponent>(); InputComponent input = entity.GetComponent <InputComponent>(); StateComponent ap = entity.GetComponent <StateComponent>(); int i = AiToInput.GetAbilityCount(entity, M_LinkedType); if (i >= ab.m_temporaryAbility.Count || i != input.currentKey) { knock.m_area = null; continue; } //获取影响范围 if (knock.m_area == null) { knock.m_area = FindPath.GetArea(knock.GetComponent <BlockInfoComponent>().m_logicPosition, knock.m_ridus); } UISimple ui = GameObject.Find("UI").GetComponent <UISimple>(); ui.ShowUI(knock.m_area, 3); VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>(); List <BasicEntity> enemy = new List <BasicEntity>(); //获取影响范围内的敌人 foreach (var pos in knock.m_area) { var e = map.GetBlockByLogicPos(pos).entity; if (e != null) { if (e.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Enemy) { enemy.Add(e); } } } //UI显示范围与敌人 if (input.leftButtonDown) { foreach (var e in enemy) { Debug.Log(e.name); e.GetComponent <MonitorComponent>().m_voice.Add(entity.GetComponent <BlockInfoComponent>().m_logicPosition); } ui.ShowUI(null, 3); StateComponent state = entity.GetComponent <StateComponent>(); state.m_actionPoint -= 1; state.AnimationStart(); state.Invoke("AnimationEnd", 1); } } }
bool GetItem(BasicEntity entity, Vector3 mousePos) { VoxelBlocks map = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks>(); //获取角色位置 Vector3 pos = entity.GetComponent <BlockInfoComponent>().m_logicPosition; //判断鼠标位置是否在角色旁边 if ((Mathf.Abs(pos.x - mousePos.x) <= 1.5f) && (Mathf.Abs(pos.y - mousePos.y) <= 1.5f)) { //获取鼠标位置上的格子信息 BlockInfo blockInfo = map.GetBlockByLogicPos(mousePos); if (blockInfo == null) { return(false); } BasicEntity itemEntity = blockInfo.entity; if (itemEntity != null) { ItemInfoComponent itemInfo = itemEntity.GetComponent <ItemInfoComponent>(); //若该格子上不存在物品则退出 if (itemInfo == null) { return(false); } ItemComponent itemComp = entity.GetComponent <ItemComponent>(); //判断物品是否超出持有上限 if (itemInfo.num + itemComp.item.Count > itemComp.numLimit) { //删除物品 itemComp.item.RemoveRange(0, itemInfo.num + itemComp.item.Count - itemComp.numLimit); } //添加物品 for (int i = 0; i < itemInfo.num; i++) { itemComp.item.Add(itemInfo.type); } map.RemoveBlock(mousePos); itemInfo.m_entity.RemoveAllComponent(); GameObject.Destroy(itemInfo.gameObject); } return(true); } else { return(false); } }
static private NodeItem BItoNI(Vector3 pos) { VoxelBlocks voxelBlocks = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>(); NodeItem node; BlockInfo block = voxelBlocks.GetBlockByLogicPos(pos); if (block == null) { node = new NodeItem(false, pos); } else { node = new NodeItem(checkWall(block), block.logPos); } return(node); }
static public bool CallFriend(BasicEntity entity, List <BasicEntity> player) { if (player.Count == 0 || entity == null) { return(false); } MonitorComponent monitor = entity.GetComponent <MonitorComponent> (); List <Vector3> view = monitor.m_view; VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks> (); bool isFind = false; foreach (Vector3 pos in view) { BasicEntity e = map.GetBlockByLogicPos(pos).entity; if (e != null && e.GetComponent <BlockInfoComponent> ().m_blockType == BlockType.Enemy) { isFind = true; e.GetComponent <MonitorComponent> ().m_enemy.AddRange(player); } } return(isFind); }
//检查是否能看到该位置 private bool InView(Vector3 s, Vector3 e) { VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks> (); Vector3 next = s; int deltCol = (int)(e.x - s.x); int deltRow = (int)(e.y - s.y); int stepCol, stepRow; int fraction; //初始化路径数组 stepCol = (int)Mathf.Sign(deltCol); //获取x轴上的前进方向 stepRow = (int)Mathf.Sign(deltRow); //获取z轴上的前进方向 deltRow = Mathf.Abs(deltRow * 2); deltCol = Mathf.Abs(deltCol * 2); if (deltCol > deltRow) { fraction = deltRow * 2 - deltCol; while (next.x != e.x) { if (fraction >= 0) { next.y += stepRow; fraction = fraction - 2 * deltCol; } next.x += stepCol; fraction = fraction + deltRow; BlockInfo bi = map.GetBlockByLogicPos(next); if (bi == null) { return(false); } BasicEntity entity = bi.entity; if (entity != null && entity.GetComponent <BlockInfoComponent> ().m_blockType == BlockType.Wall) { return(false); } } } else { fraction = deltCol * 2 - deltRow; while (next.y != e.y) { if (fraction >= 0) { next.x += stepCol; fraction = fraction - 2 * deltRow; } next.y += stepRow; fraction = fraction + deltCol; BlockInfo bi = map.GetBlockByLogicPos(next); if (bi == null) { return(false); } BasicEntity entity = bi.entity; if (entity != null && entity.GetComponent <BlockInfoComponent> ().m_blockType == BlockType.Wall) { return(false); } } } return(true); }
public override void Execute(List <BasicEntity> entities) { foreach (var e in entities) { MonitorComponent monitor = e.GetComponent <MonitorComponent>(); if (monitor == null) { return; } //若监视范围为空或者实体进行移动,重置监控范围 List <BasicEntity> oldEnemy = monitor.m_enemy; monitor.m_enemy = new List <BasicEntity>(); UISimple ui = GameObject.Find("UI").GetComponent <UISimple>(); VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>(); if (monitor.m_view == null || monitor.m_view.Contains(e.GetComponent <BlockInfoComponent>().m_logicPosition)) { List <Vector3> list = FindPath.GetArea(e.GetComponent <BlockInfoComponent>().m_logicPosition, monitor.m_SightArea); //获取自身位置 Vector3 s = e.GetComponent <BlockInfoComponent>().m_logicPosition; //初始化视野列表 monitor.m_view = new List <Vector3>(); //获取所有能看到的点 foreach (var pos in list) { if (InView(pos, s)) { //添加视野点并判断是否为玩家 monitor.m_view.Add(pos); BasicEntity entity = map.GetBlockByLogicPos(pos).entity; //判断是否为玩家 if (entity != null) { if (entity.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Player && !monitor.m_enemy.Contains(entity)) { //添加到敌人列表里 monitor.m_enemy.Add(entity); } } } } //判断视野里是否出现新的敌人 foreach (BasicEntity enemy in monitor.m_enemy) { if (!oldEnemy.Contains(enemy)) { monitor.GetComponent <StateComponent>().FindEnemy(); break; } } //ui.ShowWornArea(e); } else { //查找所有在视野里的玩家 foreach (var pos in monitor.m_view) { BasicEntity entity = map.GetBlockByLogicPos(pos).entity; //判断是否为玩家 if (entity != null) { if (entity.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Player && !monitor.m_enemy.Contains(entity)) { //添加到敌人列表里 monitor.m_enemy.Add(entity); } } } } foreach (var enemy in oldEnemy) { if (monitor.m_enemy.Contains(enemy)) { continue; } monitor.m_voice.Add(enemy.GetComponent <BlockInfoComponent>().m_logicPosition); } } }