/// <summary> /// 查找一个军营 /// </summary> /// <returns></returns> private TileEntity FindCampEntity() { var allEntities = IsoMap.Instance.GetAllEntitiesByOwner(Entity.GetOwner()); // 这里随便寻找一个军营 TODO:后期根据士兵分配选择一个合适的军营 return(allEntities.RubyFind(entity => EntityTypeUtil.IsBarracks(entity.model))); }
public override ReqWrapper Execute() { //占用工人数量 DataCenter.Instance.FreeWorker += 1; EntityModel replaceModel = null; if (entity.buildingVO.buildingStatus == BuildingVO.BuildingStatus.Construct) { replaceModel = entity.model; //判断是否是军营 if (EntityTypeUtil.IsBarracks(entity.model)) { DataCenter.Instance.TotalSpace += entity.model.spaceProvide; } } else if (entity.buildingVO.buildingStatus == BuildingVO.BuildingStatus.Upgrade)//升级 { replaceModel = DataCenter.Instance.FindEntityModelById(entity.model.upgradeId); //判断是否是军营 if (EntityTypeUtil.IsBarracks(entity.model)) { DataCenter.Instance.TotalSpace += (replaceModel.spaceProvide - entity.model.spaceProvide);//军营空间升级 } else if (EntityTypeUtil.IsCenterBuilding(entity.model)) { DataCenter.Instance.Defender.player.baseId = replaceModel.baseId; } } else { Assert.Fail("building status error!"); } DataCenter.Instance.AddExp(replaceModel.buildExp); //替换成建造或者升级完成后的建筑 entity.buildingVO.buildingStatus = BuildingVO.BuildingStatus.On; entity.buildingVO.cid = replaceModel.baseId; var newEntity = entity.ReplaceWith(replaceModel, entity.buildingVO); ((IsoWorldModeBuilder)GameWorld.Instance.CurrentWorldMode).SelectBuilding(newEntity); EventDispather.DispatherEvent(GameEvents.BUILDING_COMPLETE, entity.buildingVO); var buildingCompleteReq = new BuildingCompleteReq(); buildingCompleteReq.sid = sid; if (immediately) { buildingCompleteReq.completeType = BuildingCompleteReq.CompleteType.CompleteImmediately; var now = ServerTime.Instance.Now(); buildingCompleteReq.timestamp = DateTimeUtil.DateTimeToUnixTimestampMS(now); int deltaTime = (int)(DateTimeUtil.UnixTimestampMSToDateTime(entity.buildingVO.endTime) - now).TotalSeconds; DataCenter.Instance.RemoveResource(ResourceType.Diamond, GameDataAlgorithm.TimeToGem(deltaTime)); } else { buildingCompleteReq.completeType = BuildingCompleteReq.CompleteType.Normal; } return new ReqWrapper() { requestType = ReqWrapper.RequestType.BuildingComplete, buildingCompleteReq = buildingCompleteReq }; }
public void Create(WorldType worldType, CampVO defender = null, CampVO attacker = null, BattleReplayVO replayVO = null) { this.worldType = worldType; IsoMap.Instance.Clear(); GameBulletManager.Instance.Init(); GameEffectManager.Instance.Init(); GameSkillManager.Instance.Init(); UpdateManager.Instance.Clear(); DelayManager.Instance.Init(); BattleManager.Instance.Init(); PoolManager.Instance.Clear(); ResourceManager.Instance.ClearCache(); // 初始化数据 if (defender != null) { DataCenter.Instance.Defender = defender; } if (attacker != null) { DataCenter.Instance.Attacker = attacker; } // REMARK:录像模式或回放模式从这里开始处理(之前的模块释放完毕,开始放置建筑之前。) if (worldType == WorldType.Battle) { GameRecord.StartRecord(); } else if (worldType == WorldType.Replay) { Assert.Should(replayVO != null); DataCenter.Instance.Attacker = replayVO.attacker; DataCenter.Instance.Defender = replayVO.defender; GameRecord.StartReplay(replayVO); } // 添加建筑 for (int i = 0; i < DataCenter.Instance.Defender.buildings.Count; i++) { var buildingVO = DataCenter.Instance.Defender.buildings[i]; var entityModel = DataCenter.Instance.FindEntityModelById(buildingVO.cid); Assert.Should(entityModel != null); var tileEntity = TileEntity.Create(OwnerType.Defender, entityModel); tileEntity.buildingVO = buildingVO; tileEntity.SetTilePosition(new TilePoint(buildingVO.x, buildingVO.y)); IsoMap.Instance.ForceAddEntity(tileEntity); } // 添加工人,士兵(仅在建造模式下) if (worldType == WorldType.Home) { foreach (var armyVo in DataCenter.Instance.Defender.armies) { for (int i = 0; i < armyVo.amount; i++) { var actor = DataCenter.Instance.FindEntityModelById(armyVo.cid); var tileEntity = TileEntity.Create(OwnerType.Defender, actor); tileEntity.SetTilePosition(new TilePoint(0, 0)); IsoMap.Instance.ForceAddEntity(tileEntity); } } // TODO:临时 for (int i = 0; i < DataCenter.Instance.Defender.player.maxWorker; i++) { var actor = DataCenter.Instance.FindEntityModelByResourceName("Pworker"); var tileEntity = TileEntity.Create(OwnerType.Defender, actor); tileEntity.SetTilePosition(new TilePoint(5, 5)); IsoMap.Instance.ForceAddEntity(tileEntity); tileEntity.HideEntity(); } //计算最大人口 DataCenter.Instance.TotalSpace = 0; foreach (var buildingVo in DataCenter.Instance.Defender.buildings) { var model = DataCenter.Instance.FindEntityModelById(buildingVo.cid); if (EntityTypeUtil.IsBarracks(model)) { DataCenter.Instance.TotalSpace += model.spaceProvide; } } //计算已使用人口 DataCenter.Instance.SpaceUsed = 0; foreach (var armyVo in DataCenter.Instance.Defender.armies) { var model = DataCenter.Instance.FindEntityModelById(armyVo.cid); DataCenter.Instance.SpaceUsed += model.spaceUse * armyVo.amount; } } //把资源平均分配到资源库 AverageResourceStorageComponents(ResourceType.Gold); AverageResourceStorageComponents(ResourceType.Oil); switch (worldType) { case WorldType.Battle: IsoMap.Instance.InitGuardAreaMap(); IsoMap.Instance.ShowGuardAreaMap(false); CurrentWorldMode = new IsoWorldModeAttack(); BattleManager.Instance.PreStartBattle(); BattleUIManager.Instance.ShowBattleUI(); break; case WorldType.Replay: CurrentWorldMode = new IsoWorldModeReplay(); BattleManager.Instance.PreStartBattle(); BattleManager.Instance.StartBattle(); // TODO:暂时先放这里 BattleUIManager.Instance.ShowReplayUI(); break; case WorldType.Visit: CurrentWorldMode = new IsoWorldModeVisit(); break; case WorldType.Home: CurrentWorldMode = new IsoWorldModeBuilder(); HomeLandManager.Instance.ShowMyHomeLandUI(); break; } }
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); }