public static GameObject LoadAndCreate(EntityModel model) { //TODO remove this var last = model.nameForResource.Substring(model.nameForResource.Length - 2); int lastInt; if (last != "01" && int.TryParse(last, out lastInt)) { Debug.LogError("由于没有其他等级的模型,暂时使用1级的模型"); model.nameForResource = model.nameForResource.Replace(last, "01");//因为没有其他资源,暂时都使用1级的资源 } string name = String.Empty; if (EntityTypeUtil.IsAnyActor(model.entityType)) { name = "Actors/" + model.nameForResource; } else if (model.entityType == EntityType.Wall) { name = "Walls/" + model.nameForResource; } else if (EntityTypeUtil.IsAnyTrap(model)) { name = "Trap/" + model.nameForResource; } else { name = "Buildings/" + model.nameForResource; } return((GameObject)ResourceManager.Instance.LoadAndCreate(model.raceType + "/Entities/" + name)); }
public static string GetItemIconByModel(EntityModel model) { if (EntityTypeUtil.IsSkill(model)) { //技能 return("UI_IconSkill_" + model.nameForResource); } else if (EntityTypeUtil.IsAnyActor(model.entityType)) { //兵种 return(model.nameForResource.Substring(0, model.nameForResource.Length - 2) + "_IconSmall"); } return(""); }
private void UpdateDestroy(float dt) { // 死亡了 但尚未置于死亡状态(则释放) if (currentHp <= 0 && this.State != EntityStateType.Dead) { this.State = EntityStateType.Dead; if (GameWorld.Instance.worldType == WorldType.Battle || GameWorld.Instance.worldType == WorldType.Replay) { BattleManager.Instance.OnEntityDestroy(this); } //创建战斗时被销毁的残渣 if (EntityTypeUtil.IsAnyBuilding(entityType)) { GameObject.Destroy(view.body.gameObject); GameObject.Destroy(view.hpBar.gameObject); if (view.shadow != null) { GameObject.Destroy(view.shadow.gameObject); } GameEffectManager.Instance.AddEffect("Phouse_die", GetRenderPosition());//TODO var destroyView = (GameObject)ResourceManager.Instance.LoadAndCreate("Misc/DestroyedBuildingView"); view.AddSubView(destroyView, Vector3.zero); IsoHelper.FaceToWorldCamera(destroyView.transform); IsoHelper.MoveAlongCamera(destroyView.transform, 19); Destroy(false); IsoMap.Instance.RemoveBuildingMapData(this);//移除占地数据,对象还是在IsoMap中 } else if (EntityTypeUtil.IsAnyActor(entityType)) { GameEffectManager.Instance.AddEffect("Psoldier_die", GetRenderPosition()); IsoMap.Instance.DelayRemoveEntity(this); Destroy(); } else { IsoMap.Instance.DelayRemoveEntity(this); Destroy(); } } }
/// <summary> /// 获取指定范围内的对象列表(攻击范围、溅射范围、治疗范围等) /// </summary> /// <param name="attacker">过滤掉的对象</param> /// <param name="ownerType">阵营</param> /// <param name="x">范围的中心点X</param> /// <param name="y">范围的中心点Y</param> /// <param name="range">范围</param> /// <param name="blindrange">盲区范围</param> /// <param name="filter">过滤器,如果过滤器返回true,则指定的对象被过滤掉。</param> /// <returns></returns> public List <TileEntity> GetEntitiesByRange(TileEntity attacker, OwnerType ownerType, float x, float y, float range, float blindrange, Func <TileEntity, bool> filter = null) { List <TileEntity> ret = new List <TileEntity>(); float blindrange2 = blindrange * blindrange; foreach (TileEntity targeter in m_entities[(int)ownerType]) { if (attacker == targeter) { continue; } if (targeter.IsDead()) { continue; } if (!EntityTypeUtil.IsAnyTargeter(targeter.entityType)) { continue; } // 不是限定类型的目标则过滤掉 if (attacker != null && EntityTypeUtil.IsAnyActor(targeter.entityType) && attacker.model.onlyAttackTargetType != EntityType.None && attacker.model.onlyAttackTargetType != targeter.entityType) { continue; } if (filter != null && filter(targeter)) { continue; } if (targeter.IsInAttackRange(x, y, range, blindrange2)) { ret.Add(targeter); } } return(ret); }
protected override List <TileEntity> TryLockTargeters(out TilePoint?_targetPos, out LinkedList <IMoveGrid> _targetRoute) { _targetPos = null; _targetRoute = null; // 获取全部目标 List <TileEntity> allTargeters = new List <TileEntity>(); foreach (var entity in IsoMap.Instance.GetAllEntitiesByOwner(Entity.GetTargetOwner())) { if (entity.IsDead() || !EntityTypeUtil.IsAnyActor(entity.entityType)) { continue; } // 不是限定类型的目标则过滤掉 if (EntityTypeUtil.IsAnyActor(entity.entityType) && Attacker.model.onlyAttackTargetType != EntityType.None && Attacker.model.onlyAttackTargetType != entity.entityType) { continue; } allTargeters.Add(entity); } if (allTargeters.Count == 0) { return(null); } // 筛选1个直线最近的目标 var nearest_targets = FindTargetsNearestLinear(Attacker.GetCurrentPositionCenter(), allTargeters, 1); var targeter = nearest_targets[0]; Vector2 p = targeter.GetCurrentPositionCenter(); _targetPos = new TilePoint((int)p.x, (int)p.y); return(nearest_targets); }
public static TileEntity Create(OwnerType owner, EntityModel model, bool isFriendly = false) { var entityObj = LoadAndCreate(model); var tileEntity = new TileEntity(); tileEntity.owner = owner; tileEntity.model = model; tileEntity.State = EntityStateType.Idle; tileEntity.aiType = (EntityAiType)model.targetType; tileEntity.entityType = model.entityType; tileEntity.Friendly = isFriendly; if (model.tileSize > 0) { tileEntity.width = tileEntity.height = model.tileSize * 2; // REMARK:建筑实际占用格子扩大2倍 } else { tileEntity.width = tileEntity.height = 1; } tileEntity.currentHp = tileEntity.maxHp = Mathf.Max(model.hp, 1); // REMARK:这里设置最大hp的最小值为1,不然对于工人等直接就是dead状态。 tileEntity.animationNamePrefix = model.nameForResource + "_"; // REMARK:建筑按照格子建造、士兵按照边行走(所以士兵位置比建筑偏移(0.5,0.5)) if (EntityTypeUtil.IsAnyActor(model.entityType)) { tileEntity.renderTileSizeOffset = new Vector2((tileEntity.width * 0.5f) - 1.0f, (tileEntity.height * 0.5f) - 1.0f); } else { tileEntity.renderTileSizeOffset = new Vector2((tileEntity.width * 0.5f) - 0.5f, (tileEntity.height * 0.5f) - 0.5f); } tileEntity.view = entityObj.GetComponent <EntityViewComponent>(); if (tileEntity.view == null) { if (EntityTypeUtil.IsAnyActor(tileEntity.entityType)) { tileEntity.view = entityObj.AddComponent <ActorView>(); } else if (EntityTypeUtil.IsAnyTower(tileEntity.entityType)) { tileEntity.view = entityObj.AddComponent <TowerView>(); } else { tileEntity.view = entityObj.AddComponent <EntityViewComponent>(); } } if (EntityTypeUtil.IsAnyBuilding(tileEntity.entityType))//[建筑类] { //1格以上建筑有地皮 if (model.tileSize > 2) { var floorBase = (GameObject)ResourceManager.Instance.LoadAndCreate(model.raceType + "/Entities/Misc/Floor4");//地皮 tileEntity.view.AddSubView(floorBase, Vector3.zero); IsoHelper.FaceToWorldCamera(floorBase.transform); IsoHelper.MoveAlongCamera(floorBase.transform, Constants.FLOOR_Z_ORDER); floorBase.transform.localScale = Vector3.one * model.tileSize / 4;//REMARK 现有素材占4x4个tilesize } if (EntityTypeUtil.IsBarracks(model)) { tileEntity.blockingRange = 3; } else { tileEntity.blockingRange = model.tileSize * 2 - 1; } tileEntity.AddComponent <ConstructBuildingComponent>(); if (model.entityType == EntityType.Tower) { tileEntity.AddComponent <TowerComponent>(); } else if (model.entityType == EntityType.Wall) { tileEntity.AddComponent <WallComponent>(); } else if (EntityTypeUtil.IsGatherResourceBuilding(model)) { tileEntity.AddComponent <GatherResourceBuildingComponent>(); } else if (EntityTypeUtil.IsStorageResoruceBuilding(model)) { tileEntity.AddComponent <ResourceStorageBuildingComponent>(); } else if (EntityTypeUtil.IsArmyShop(model)) { tileEntity.AddComponent <ProductSoldierBuildingComponent>(); } else if (EntityTypeUtil.IsSkillShop(model)) { tileEntity.AddComponent <ProductSkillBuildingComponent>(); } else if (EntityTypeUtil.IsWorkerHouse(model)) { tileEntity.AddComponent <WorkerHouseComponent>(); } else if (EntityTypeUtil.IsFederal(model)) { tileEntity.AddComponent <FederalComponent>(); } else if (EntityTypeUtil.IsResearch(model)) { tileEntity.AddComponent <ResearchBuildingComponent>(); } } else if (EntityTypeUtil.IsAnyActor(model.entityType)) //[角色类] { // 战斗模式 家园模式 通用组件 tileEntity.AddComponent <ActorMoveComponent>(); if (GameWorld.Instance.worldType == WorldType.Home || GameWorld.Instance.worldType == WorldType.Visit) //家园模式 或者 访问模式 { // 家园模式 // 工人(添加工人组件&添加到工人小屋中) if (EntityTypeUtil.IsWorker(model)) { tileEntity.AddComponent <WorkmanComponent>(); var houseComp = IsoMap.Instance.GetWorkerHouseComponent(); Assert.Should(houseComp != null, "worker house is not exist..."); houseComp.AddAWorkman(tileEntity); } else { tileEntity.AddComponent <ArmyComponent>(); } } else //战斗模式 { tileEntity.AddComponent <GameBufferComponent>(); // 设置士兵的监视范围(寻找目标用) REMARK:设定为4.0秒可以移动的范围 tileEntity.monitorRange = Mathf.Clamp(model.speed * 4.0f, 5.0f, 20.0f); if (EntityTypeUtil.IsBombMan(model)) { tileEntity.monitorRange = 0.0f; // 暂时不限制炸弹人 tileEntity.AddComponent <BombManComponent>(); } else if (EntityTypeUtil.IsCurer(model)) { tileEntity.AddComponent <CurerComponent>(); } else if (isFriendly) // 友军/援军 { tileEntity.AddComponent <FriendComponent>(); } else { tileEntity.AddComponent <ActorComponent>(); } // 除【友军/援军】以外添加拆墙组件(REMARK:友军/援军不可拆墙) if (!isFriendly) { tileEntity.AddComponent <ActorDestroyWallComponent>(); } } } else if (EntityTypeUtil.IsAnyTrap(model)) // [陷阱类] { tileEntity.AddComponent <TrapComponent>(); } Assert.Should((tileEntity.blockingRange == 0 || (tileEntity.blockingRange % 2) == 1), "invalid blockingRange..."); Assert.Should(tileEntity.blockingRange <= tileEntity.width - 1 && tileEntity.blockingRange <= tileEntity.height - 1, "invalid blockingRange..."); return(tileEntity); }
protected override List <TileEntity> TryLockTargeters(out TilePoint?_targetPos, out LinkedList <IMoveGrid> _targetRoute) { _targetPos = null; _targetRoute = null; var allTargeters = IsoMap.Instance.GetAllEntitiesByOwner(Attacker.GetTargetOwner()).RubySelect(tar => { if (tar.IsDead()) { return(false); } // 不是限定类型的目标则过滤掉 if (EntityTypeUtil.IsAnyActor(tar.entityType) && Attacker.model.onlyAttackTargetType != EntityType.None && Attacker.model.onlyAttackTargetType != tar.entityType) { return(false); } return(true); }); if (allTargeters.Count == 0) { return(null); } List <TileEntity> targets = null; // 1、有优先目标的士兵先筛选优先目标 if (Entity.aiType != EntityAiType.Other) { targets = allTargeters.RubySelect(tar => { return(tar.entityType == (EntityType)Entity.aiType); }); } // 2、没有优先目标 or 优先目标为空 则优先筛选援军 if (targets == null || targets.Count == 0) { targets = allTargeters.RubySelect(tar => { return(tar.Friendly); }); } // 3、没有援军则筛选默认的非墙建筑 if (targets.Count == 0) { targets = allTargeters.RubySelect(tar => { return(EntityTypeUtil.IsAnyNonWallBuilding(tar.entityType)); }); } // 4、未找到目标直接返回 if (targets.Count == 0) { return(null); } #if REALTIME_AI //float t1 = UnityEngine.Time.realtimeSinceStartup; //for (int i = 0; i < 5000; i++) //{ // FindTargetsNearestTimeCost(Attacker, targets); //} //float t2 = UnityEngine.Time.realtimeSinceStartup; //Debug.Log(t2 - t1); //Debug.Break(); // 筛选最近目标(最小时间 or 最近距离) return(SelectTargeters(targets, out _targetPos, out _targetRoute)); #else // 从备选列表中筛选最近的目标 IEnumerator <IsoGridTarget> iterator = FindTargetsNearestTimeCost(Attacker, targets); if (iterator == null) { return(null); } // 延迟处理 DelayManager.Instance.AddDelayAi <IsoGridTarget>(Attacker, OnDelayLockTargetersCompleted, iterator); Entity.State = EntityStateType.Thinking; return(null); #endif }
/// <summary> /// 有单位死亡了 /// </summary> /// <param name="entity"></param> public void OnEntityDestroy(TileEntity entity) { //如果死亡的是建筑 if (EntityTypeUtil.IsAnyNonWallBuilding(entity.entityType)) { defenderBuildingCount--; DestroyBuildingPercent = (float)(totalDefenderBuildingCount - defenderBuildingCount) / totalDefenderBuildingCount; //摧毁50% 获得一颗星 if (!hasGotHalfDestroyStar && DestroyBuildingPercent > 0.5f) { hasGotHalfDestroyStar = true; BattleStar++; } Debug.Log("摧毁:" + DestroyBuildingPercent + "%"); //摧毁基地 获得一颗星 if (EntityTypeUtil.IsCenterBuilding(entity.model)) { BattleStar++; } //摧毁100% 获得一颗星 if (defenderBuildingCount == 0) { BattleStar++; } EventDispather.DispatherEvent(GameEvents.BATTLE_PROGRESS_CHANGE); } else if (entity.GetOwner() == OwnerType.Attacker && EntityTypeUtil.IsAnyActor(entity.entityType)) { attackerArmyCount--; if (EntityTypeUtil.IsCurer(entity.model)) { attackerCurerCount--; } } else if (entity.GetOwner() == OwnerType.Defender) { if (EntityTypeUtil.IsAnyActor(entity.entityType)) { if (entity.Friendly) { var found = false; foreach (var killedDefenderDonatedArmy in killedDefenderDonatedArmies) { if (killedDefenderDonatedArmy.cid == entity.model.baseId) { killedDefenderDonatedArmy.amount++; found = true; break; } } if (!found) { killedDefenderDonatedArmies.Add(new ArmyVO() { amount = 1, cid = entity.model.baseId }); } } } else if (EntityTypeUtil.IsAnyTrap(entity.model)) { brokenTraps.Add(entity.buildingVO.sid); } } CheckBattleEnd(); }
/// <summary> /// 类型获取对象列表 /// </summary> /// <param name="ownerType"></param> /// <param name="aiType"></param> /// <param name="attacker"></param> /// <param name="includeFriend">是否包含友军(默认只获取建筑型目标)</param> /// <returns></returns> public List <TileEntity> GetEntitiesByTT(OwnerType ownerType, EntityAiType aiType, TileEntity attacker, bool includeFriend = false) { List <TileEntity> ret = new List <TileEntity>(); foreach (TileEntity entity in m_entities[(int)ownerType]) { if (entity == attacker) { continue; } if (entity.IsDead()) { continue; } // 不是限定类型的目标则过滤掉 if (EntityTypeUtil.IsAnyActor(entity.entityType) && attacker.model.onlyAttackTargetType != EntityType.None && attacker.model.onlyAttackTargetType != entity.entityType) { continue; } switch (aiType) { case EntityAiType.PriorToWall: if (entity.entityType == EntityType.Wall) { ret.Add(entity); } break; case EntityAiType.PriorToTower: if (entity.entityType == EntityType.Tower) { ret.Add(entity); } break; case EntityAiType.PriorToResource: if (entity.entityType == EntityType.Resource) { ret.Add(entity); } break; default: { // 建筑目标 if (EntityTypeUtil.IsAnyNonWallBuilding(entity.entityType)) { ret.Add(entity); } // 援军 else if (includeFriend && entity.Friendly) { ret.Add(entity); } } break; } } return(ret); }