public IEnumerable <MapStructSide> GetOccupiedSides(MapSpot spot) { if (!MapSpots.Any(s => spot.X == s.X && spot.Y == s.Y)) { return(null); } var print = _rotationMapping.Where(p => p.Value.X == spot.X && p.Value.Y == spot.Y).SingleOrDefault().Key; if (print == null) { return(null); } var sides = _def.OccupiesSides[print]; if (sides == null) { return(null); } var rotated = MapStructHelper.RotateOccupiedSides(sides, Rotation).ToList(); return(rotated); }
public FruitTree(BuildingDef def, string layerName, MapSpot anchor, IMapController controller, MapRotation rotation) : base(def, layerName, anchor, controller, rotation) { _stageLengths = new List <Dictionary <string, int> >(); _stageLengths.Add(new Dictionary <string, int> { { "HOUR", 1 } }); _stageLengths.Add(new Dictionary <string, int> { { "HOUR", 1 } }); _stageLengths.Add(new Dictionary <string, int> { { "HOUR", 1 } }); var outputConfif = new InventoryConfig() { CanProvideItems = true, CanReceiveItems = false, HasMassLimit = false, MaxMass = -1, RespectsStackLimit = false }; var timeKeeper = GameMaster.Instance.GetController <ITimeKeeper>(); _timeKeeper = timeKeeper; _finishDate = timeKeeper.ProjectTime(_stageLengths[0]); _outputInventory = new DefaultInventory(GameMaster.Instance.GetController <IItemController>(), this, outputConfif); }
public IEnumerable <IMapStructure> GetMapStructsAt(string layerName, MapSpot mapSpot) { foreach (var ret in GetMapStructsAt(layerName, mapSpot.X, mapSpot.Y)) { yield return(ret); } }
/* * MovementDirection MovementDirection WhichWayToTarget( * MapSpot WhereIAm, * SpotType targetType * ) * * Returns MovementDirection that begins the shortest path to a spot of type targetType. */ public static MovementDirection WhichWayToTargetType( MapSpot WhereIAm, SpotType targetType ) { // Defines and assigns delegate function we will use when checking // the spots we've found. bool spotCheck(MapSpot test) { return(test.myType == targetType); } PathFinder.checkSpot = spotCheck; // We can take advantage of the pathfinding algorithm to find // our target Dictionary <MapSpot, MapSpot> pathEdges = PathFinder.CreatePathEdges(WhereIAm); // Use the same function we used for the delgate to get the right MapSpot MapSpot targetSpot = pathEdges.Keys.ToList().Find(spotCheck); if (targetSpot == null) { targetSpot = WhereIAm; } return(PathFinder.FindPathInitialDirection(targetSpot, WhereIAm, pathEdges)); }
public override void DoUpdate() { if (IsActive && Worker.MoveToSpot(_nextSpot)) { _nextSpot = GetNextNextSpot(); } }
private void OnTriggerEnter(Collider col) { if (col.tag == "MapSpot") { currentSpot = col.gameObject.GetComponent <MapSpot>(); } }
public void ChooseSpotForStar() { var spots = mapList.Keys.ToList(); var choice = (int)Mathf.Ceil(Random.Range(0, spots.Count)); StarSpot = spots[choice]; StarSpot.myType = SpotType.EMBLEM; }
public BaseMapStructure(MapStructDef def, string layerName, MapSpot anchor, IMapController controller, MapRotation rotation) : base(def) { MapController = controller ?? throw new ArgumentNullException(nameof(controller)); Anchor = anchor ?? throw new ArgumentNullException(nameof(anchor)); MapLayerName = layerName; _def = def ?? throw new ArgumentNullException(nameof(def)); MapStructValidator.ValidateDef(def); _rotationMapping = MapStructHelper.FootprintToMapSpotsDictionary(def.Footprint, rotation, anchor); }
public bool TryAddBuilding(MapSpot anchor, string defName) { var def = _defs[defName]; var building = DefLoader.CreateInstanct <IBuilding>(def, "GROUND", anchor, _mapController, MapRotation.Default); if (_mapController.CanAddMapStructure(building.MapLayerName, building.MapStructDef, anchor, building.Rotation)) { _mapController.AddMapStructure(building); _buildings.Add(building.Id, building); } return(true); }
/* * Dictionary<MapSpot Destination, MapSpot Origin> CreatePathEdges( * MapSpot WhereIAm * ) * * Returns pathEdge Dictionary that goes the shortest path from WhereIAm to * target MapSpot as identified via checkSpot() delegate function. */ private static Dictionary <MapSpot, MapSpot> CreatePathEdges( MapSpot WhereIAm ) { Queue <MapSpot> toVisit = new Queue <MapSpot>(); Dictionary <MapSpot, MapSpot> cameFrom = new Dictionary <MapSpot, MapSpot>(); toVisit.Enqueue(WhereIAm); Debug.Log("Beginning Search for target"); // Navigate down node tree starting from current position until // we find the target spot in list of navigable neighbors. while (toVisit.Count > 0) { MapSpot thisSpot = toVisit.Dequeue(); List <MapSpot> neighbors = thisSpot.nextSpots.Values.ToList(); foreach (MapSpot nextSpot in neighbors) { // If we've gotten to this spot from somewhere already, // it's not worth navigating down this road anymore if (cameFrom.ContainsKey(nextSpot)) { //PathFinder.CleanPathEdges(ref cameFrom, nextSpot, WhereIAm); continue; } // Leave a breadcrumb so we know where we've been cameFrom.Add(nextSpot, thisSpot); // Call Delegate to perform check. if (PathFinder.checkSpot(nextSpot)) { // We have located the targetMapSpot and can quit searching. // we clear Queue toVisit to exit the while() loop. Debug.Log("Found target"); toVisit.Clear(); } else { // Keep looking. toVisit.Enqueue(nextSpot); } } } return(cameFrom); }
public static IEnumerator MoveMe(GamePlayer player, MapSpot nextSpot, bool finalStop) { var target = nextSpot.gameObject.transform.position; var transform = player.gameObject.transform; var spotCount = nextSpot.currentPieces.Count; target = Utils.CalculatePosition(target, spotCount + 1, spotCount); yield return(player.StartCoroutine( GamePlayerMover.MoveMe( transform, target, Constants.MOVE_TIME ) )); }
public bool CanAddMapStructure(string layerName, MapStructDef mapStructDef, MapSpot anchor, MapRotation rotation) { if (mapStructDef == null) { throw new ArgumentNullException(nameof(mapStructDef)); } if (anchor == null) { throw new ArgumentNullException(nameof(anchor)); } var printDic = MapStructHelper.FootprintToMapSpotsDictionary(mapStructDef.Footprint, rotation, anchor); var layer = GetLayer(layerName); if (layer == null) { throw new Exception($"No layer found with name '{layerName}'."); } foreach (var print in printDic) { var spot = print.Value; if (mapStructDef.FillMapSpots) { if (GetMapStructsAt(layer.LayerName, spot).Any()) { return(false); } } else { var sides = MapStructHelper.RotateOccupiedSides(mapStructDef.OccupiesSides[print.Key], rotation); var curt = GetMapStructsAt(layer.LayerName, spot).SelectMany(x => x.GetOccupiedSides(spot)); if (sides != null && curt != null && curt.Intersect(sides).Any()) { return(false); } } } return(true); }
/* * void CleanPathEdges( * ref Dictionary<MapSpot, MapSpot> edgePaths, * MapSpot endOfLine, * MapSpot pathOrigin * ) * * Accepts edgePaths Dictionary as ref and removes edges for paths that are * not as optimal as some other one. */ private static void CleanPathEdges( ref Dictionary <MapSpot, MapSpot> edgePaths, MapSpot endOfLine, MapSpot pathOrigin ) { Debug.Log("end of less-than-optimal path. cleaning up."); MapSpot current = edgePaths[endOfLine]; edgePaths.Remove(endOfLine); // Follows the path backwards to the origin and removes entries for // the shittier path. while (current != pathOrigin) { var origin = edgePaths[current]; edgePaths.Remove(current); current = origin; } }
/* * MovementDirection FindPathInitialDirection( * MapSpot goal, * MapSpot WhereIAm, * Dictionary<MapSpot, MapSpot> pathEdges * ) * * Returns MovementDirection to begin shortest path to target MapSpot * by navigating through pathEdges Dictionary. */ private static MovementDirection FindPathInitialDirection( MapSpot goal, MapSpot WhereIAm, Dictionary <MapSpot, MapSpot> pathEdges ) { // Go backwards through pathEdges starting from the goal until we find // the spot we first visited MapSpot current = goal; while (current != WhereIAm) { MapSpot origin = pathEdges[current]; if (origin == WhereIAm) { // We have made it back to where the player is and can decide which // direction to go MovementDirection direction = WhereIAm.nextSpotsReverseLookup[current]; Debug.Log(string.Format("I should go, {0}", direction)); return(direction); } current = origin; } // If all else fails, choose a random direction Debug.LogWarning("Pathfinding Error: Choosing random direction"); List <MovementDirection> possibleDirections = WhereIAm.nextSpots.Keys.ToList(); int randomDirectionIndex = (int)UnityEngine.Random.Range(0, possibleDirections.Count); MovementDirection randomDirection = possibleDirections[randomDirectionIndex]; return(randomDirection); }
public DefaultManufacturingBuilding(ManufacturingBuildingDef def, string layerName, MapSpot anchor, IMapController controller, MapRotation rotation) : base(def, layerName, anchor, controller, rotation) { }
public static Dictionary <Tuple <int, int>, MapSpot> FootprintToMapSpotsDictionary(IEnumerable <int[]> footprint, MapRotation rotation, MapSpot anchor) { if (footprint.Any(print => print.Length != 2)) { throw new Exception($"Malformed footprint at least one print is not int[2]"); } var rotated = RotateFootprint(footprint, rotation); var dic = new Dictionary <Tuple <int, int>, MapSpot>(); foreach (var print in rotated) { dic.Add(new Tuple <int, int>(print[0], print[1]), new MapSpot(anchor.X + print[0], anchor.Y + print[1])); } return(dic); }
public BaseBuilding(BuildingDef def, string layerName, MapSpot anchor, IMapController controller, MapRotation rotation) : base(def, layerName, anchor, controller, rotation) { BuildingDef = def ?? throw new ArgumentNullException(nameof(def)); AnchorPoint = anchor ?? throw new ArgumentNullException(nameof(anchor)); }
internal BaseManufacturerBuilding(ManufacturingBuildingDef def, string layerName, MapSpot anchor, IMapController controller, MapRotation rotation) : base(def, layerName, anchor, controller, rotation) { _manufacturingBuildingDef = def ?? throw new ArgumentNullException(nameof(def)); _outputInventory = new DefaultInventory(GameMaster.Instance.GetController <IItemController>(), this, _manufacturingBuildingDef.OutputConfig); _inputInventory = new DefaultInventory(GameMaster.Instance.GetController <IItemController>(), this, _manufacturingBuildingDef.InputConfig); _currentCraftingState = null; }
public bool IsSpotFree(MapSpot spot) { return(IsTileFree(spot.X, spot.Y)); }
public IMapTile GetTileAt(MapSpot spot) { return(GetTileAt(spot.X, spot.Y)); }
public bool IsValidPosition(MapSpot spot) { return(IsValidPosition(spot.X, spot.Y)); }
protected override void OnDraw(Renderer spriterenderer, double elapsedGameTime, float opacity) { base.OnDraw(spriterenderer, elapsedGameTime, opacity); this.hitAreaCache.Clear(); var curMap = this.DataContext as WorldMapInfo; if (curMap == null) { return; } var baseOrigin = new PointF((int)this.Width / 2, (int)this.Height / 2); var drawOrder = new List <DrawItem>(); var addItem = new Action <TextureItem, object>((texture, obj) => { drawOrder.Add(new DrawItem() { Target = obj, TextureItem = texture }); }); //获取鼠标位置 var mousePos = EmptyKeys.UserInterface.Input.InputManager.Current.MouseDevice.GetPosition(this); MapSpot curSpot = null; //绘制底图 if (curMap.BaseImg != null) { addItem(curMap.BaseImg, null); } //绘制link foreach (var link in curMap.MapLinks) { if (link.LinkImg != null) { var pos = new PointF(mousePos.X - (baseOrigin.X - link.LinkImg.Origin.X), mousePos.Y - (baseOrigin.Y - link.LinkImg.Origin.Y)); if (link.LinkImg.HitMap?[(int)pos.X, (int)pos.Y] ?? false) { addItem(link.LinkImg, link); } } } //绘制地图点 foreach (var spot in curMap.MapList) { var texture = this.FindResource("mapImage/" + spot.Type.ToString()) as TextureItem; if (texture != null) { var item = new TextureItem() { Texture = texture.Texture, Origin = new PointF(-spot.Spot.X + texture.Origin.X, -spot.Spot.Y + texture.Origin.Y), Z = texture.Z }; if (spot.IsPreBB && curMap.BaseImg != null) //pre-bb地图点调整 { item.Origin.X += curMap.BaseImg.Origin.X; item.Origin.Y += curMap.BaseImg.Origin.Y; } addItem(item, spot); //判断鼠标位置绘制path if (spot.Path != null) { var rect = new Rect(baseOrigin.X - item.Origin.X, baseOrigin.Y - item.Origin.Y, texture.Texture.Width, texture.Texture.Height); if (rect.Contains(mousePos)) { addItem(spot.Path, null); } } } if (this.CurrentMapID != null && spot.MapNo.Contains(this.CurrentMapID.Value)) { curSpot = spot; } } //绘制当前地图标记 if (curSpot != null) { var posTexture = this.FindResource("curPos") as TextureItem; if (posTexture != null) { var item = new TextureItem() { Texture = posTexture.Texture, Origin = new PointF(-curSpot.Spot.X + posTexture.Origin.X, -curSpot.Spot.Y + posTexture.Origin.Y), Z = 255, }; addItem(item, null); } } //开始绘制 foreach (var item in drawOrder.OrderBy(_item => _item.TextureItem.Z)) { var tex = item.TextureItem; if (tex != null) { var pos = new PointF(baseOrigin.X - tex.Origin.X, baseOrigin.Y - tex.Origin.Y); var size = new Size(tex.Texture.Width, tex.Texture.Height); spriterenderer.Draw(tex.Texture, new PointF(this.RenderPosition.X + pos.X, this.RenderPosition.Y + pos.Y), size, new ColorW(1f, 1f, 1f, opacity), false); item.Position = pos; this.hitAreaCache.Add(item); } } }
public void LoadWzResource() { if (this.IsDataLoaded) { return; } //读取所有世界地图 var worldmapNode = PluginBase.PluginManager.FindWz("Map/WorldMap"); if (worldmapNode == null) //加载失败 { return; } this.worldMaps.Clear(); this.CurrentWorldMap = null; foreach (var imgNode in worldmapNode.Nodes) { var img = imgNode.GetNodeWzImage(); Wz_Node node; if (img != null && img.TryExtract()) { var worldMapInfo = new WorldMapInfo(); //加载地图索引 node = img.Node.Nodes["info"]; if (node != null) { if (!this.UseImageNameAsInfoName) { worldMapInfo.Name = node.Nodes["WorldMap"].GetValueEx <string>(null); } worldMapInfo.ParentMap = node.Nodes["parentMap"].GetValueEx <string>(null); } if (string.IsNullOrEmpty(worldMapInfo.Name)) { var m = Regex.Match(img.Name, @"^(.*)\.img$"); worldMapInfo.Name = m.Success ? m.Result("$1") : img.Name; } //加载地图名称 { var m = Regex.Match(worldMapInfo.Name, @"^WorldMap(.+)$"); if (m.Success) { var stringNode = PluginBase.PluginManager.FindWz("String/WorldMap.img/" + m.Result("$1")); worldMapInfo.DisplayName = stringNode?.Nodes["name"].GetValueEx <string>(null); } } //加载baseImg node = img.Node.Nodes["BaseImg"]?.Nodes["0"]; if (node != null) { worldMapInfo.BaseImg = LoadTextureItem(node); } //加载地图列表 node = img.Node.Nodes["MapList"]; if (node != null) { foreach (var spotNode in node.Nodes) { var spot = new MapSpot(); var location = spotNode.Nodes["spot"]?.GetValueEx <Wz_Vector>(null); if (location != null) { spot.Spot = location.ToPointF(); } else //兼容pre-bb的格式 { spot.IsPreBB = true; spot.Spot = new PointF(spotNode.Nodes["x"].GetValueEx <int>(0), spotNode.Nodes["y"].GetValueEx <int>(0)); } spot.Type = spotNode.Nodes["type"].GetValueEx <int>(0); spot.Title = spotNode.Nodes["title"].GetValueEx <string>(null); spot.Desc = spotNode.Nodes["desc"].GetValueEx <string>(null); spot.NoTooltip = spotNode.Nodes["noToolTip"].GetValueEx <int>(0) != 0; var pathNode = spotNode.Nodes["path"]; if (pathNode != null) { spot.Path = LoadTextureItem(pathNode); } var mapNoNode = spotNode.Nodes["mapNo"]; if (mapNoNode != null) { foreach (var subNode in mapNoNode.Nodes) { spot.MapNo.Add(subNode.GetValue <int>()); } } worldMapInfo.MapList.Add(spot); } } //加载地图链接 node = img.Node.Nodes["MapLink"]; if (node != null) { foreach (var mapLinkNode in node.Nodes) { var link = new MapLink(); link.Index = int.Parse(mapLinkNode.Text); link.Tooltip = mapLinkNode.Nodes["toolTip"].GetValueEx <string>(null); var linkNode = mapLinkNode.Nodes["link"]; if (linkNode != null) { link.LinkMap = linkNode.Nodes["linkMap"].GetValueEx <string>(null); var linkImgNode = linkNode.Nodes["linkImg"]; if (linkImgNode != null) { link.LinkImg = LoadTextureItem(linkImgNode, true); } } worldMapInfo.MapLinks.Add(link); } } this.worldMaps.Add(worldMapInfo); } } //读取公共资源 var worldMapResNode = PluginBase.PluginManager.FindWz("Map/MapHelper.img/worldMap"); var mapImageNode = worldMapResNode?.Nodes["mapImage"]; if (mapImageNode != null) { foreach (var imgNode in mapImageNode.Nodes) { var texture = this.LoadTextureItem(imgNode); var key = "mapImage/" + imgNode.Text; this.Resources[key] = texture; } } var curPosNode = worldMapResNode?.FindNodeByPath(@"curPos\0"); if (curPosNode != null) { var texture = this.LoadTextureItem(curPosNode); this.Resources["curPos"] = texture; } //处理当前地图信息 foreach (var map in this.worldMaps) { if (map.ParentMap != null) { map.ParentMapInfo = this.worldMaps.FirstOrDefault(_map => _map.Name == map.ParentMap); } } this.IsDataLoaded = true; this.JumpToCurrentMap(); }
protected override void OnDraw(Renderer spriterenderer, double elapsedGameTime, float opacity) { base.OnDraw(spriterenderer, elapsedGameTime, opacity); this.hitAreaCache.Clear(); var curMap = this.DataContext as WorldMapInfo; if (curMap == null) { return; } TextureItem baseImg = curMap.BaseImg; SpotState[] spotState = new SpotState[curMap.MapList.Count]; if (curMap.QuestLimit != null && this.SelectedQuestIndex > -1 && this.SelectedQuestIndex < curMap.QuestLimit.Count) { for (int i = 0; i <= this.SelectedQuestIndex; i++) { var quest = curMap.QuestLimit[i]; //重写底图 if (quest.BaseImg != null) { baseImg = quest.BaseImg; } else if (quest.IsDefault) { baseImg = curMap.BaseImg; } //重写spot属性 if (quest.IsDefault) { for (int j = 0; j < spotState.Length; j++) { spotState[j].IsOverride = true; spotState[j].IsVisible = false; } } foreach (var spotIndex in quest.CloseMaps) { spotState[spotIndex].IsOverride = true; spotState[spotIndex].IsVisible = true; spotState[spotIndex].IsOpen = false; spotState[spotIndex].ImgType = quest.CloseMapImageType ?? -1; } foreach (var spotIndex in quest.OpenMaps) { spotState[spotIndex].IsOverride = true; spotState[spotIndex].IsVisible = true; spotState[spotIndex].IsOpen = true; spotState[spotIndex].ImgType = -1; } } } var baseOrigin = new PointF((int)this.Width / 2, (int)this.Height / 2); var drawOrder = new List <DrawItem>(); var addItem = new Action <TextureItem, object>((texture, obj) => { drawOrder.Add(new DrawItem() { Target = obj, TextureItem = texture }); }); //获取鼠标位置 var mousePos = InputManager.Current.MouseDevice.GetPosition(this); MapSpot curSpot = null; //绘制底图 if (baseImg != null) { addItem(baseImg, null); } //绘制link foreach (var link in curMap.MapLinks) { if (link.LinkImg != null) { var pos = new PointF(mousePos.X - (baseOrigin.X - link.LinkImg.Origin.X), mousePos.Y - (baseOrigin.Y - link.LinkImg.Origin.Y)); if (link.LinkImg.HitMap?[(int)pos.X, (int)pos.Y] ?? false) { addItem(link.LinkImg, link); } } } //绘制地图点 for (int i = 0, i0 = curMap.MapList.Count; i < i0; i++) { var spot = curMap.MapList[i]; int spotType = spot.Type; if (spotState[i].IsOverride) //重写判定 { if (!spotState[i].IsVisible) { continue; } if (!spotState[i].IsOpen) //close { if (spotState[i].ImgType > -1) { spotType = spotState[i].ImgType; } } } var texture = this.FindResource("mapImage/" + spotType) as TextureItem; if (texture != null) { var item = new TextureItem() { Texture = texture.Texture, Origin = new PointF(-spot.Spot.X + texture.Origin.X, -spot.Spot.Y + texture.Origin.Y), Z = 128 + texture.Z }; if (spot.IsPreBB && curMap.BaseImg != null) //pre-bb地图点调整 { item.Origin.X += curMap.BaseImg.Origin.X; item.Origin.Y += curMap.BaseImg.Origin.Y; } addItem(item, spot); //判断鼠标位置绘制path if (spot.Path != null) { var rect = new Rect(baseOrigin.X - item.Origin.X, baseOrigin.Y - item.Origin.Y, texture.Texture.Width, texture.Texture.Height); if (rect.Contains(mousePos)) { addItem(spot.Path, null); } } } if (this.CurrentMapID != null && spot.MapNo.Contains(this.CurrentMapID.Value)) { curSpot = spot; } } //绘制当前地图标记 if (curSpot != null) { var posTexture = this.FindResource("curPos") as TextureItem; if (posTexture != null) { var item = new TextureItem() { Texture = posTexture.Texture, Origin = new PointF(-curSpot.Spot.X + posTexture.Origin.X, -curSpot.Spot.Y + posTexture.Origin.Y), Z = 255, }; addItem(item, null); } } //开始绘制 foreach (var item in drawOrder.OrderBy(_item => _item.TextureItem.Z)) { var tex = item.TextureItem; if (tex != null) { var pos = new PointF(baseOrigin.X - tex.Origin.X, baseOrigin.Y - tex.Origin.Y); var size = new Size(tex.Texture.Width, tex.Texture.Height); spriterenderer.Draw(tex.Texture, new PointF(this.RenderPosition.X + pos.X, this.RenderPosition.Y + pos.Y), size, new ColorW(1f, 1f, 1f, opacity), false); item.Position = pos; this.hitAreaCache.Add(item); } } }
public BaseStorageContainer(StorageContainerDef def, string layerName, MapSpot anchor, IMapController controller, MapRotation rotation) : base(def, layerName, anchor, controller, rotation) { StorageContainerDef = def ?? throw new ArgumentNullException(nameof(def)); ItemController = GameMaster.Instance.GetController <IItemController>(); Inventory = new DefaultInventory(ItemController, this, def.InventoryConfig); }
public GoToTask(IJobWorker worker, MapSpot mapSpot) : base(worker) { Destination = mapSpot ?? throw new ArgumentNullException(nameof(mapSpot)); }
public override void Start() { _nextSpot = GetNextNextSpot(); base.Start(); }
public bool MoveToSpot(MapSpot pos) { throw new NotImplementedException(); }