public override void MakePreparations(Living living) { ComponentSelectable entityData = living.GetExactComponent <ComponentSelectable>(); Point2D start = entityData.MapLocation; if (start != this.Destination) { List <PathLink> pth; ComponentMovement movementComponent = living.GetExactComponent <ComponentMovement>(); //Handle reroute if (movementComponent.QueuedMovement.Count > 0) { //Get a path to the nearest/next tile so that path finding and the screen location sync up. PathLink previous = movementComponent.QueuedMovement.Peek(); movementComponent.QueuedMovement.Clear(); movementComponent.QueuedMovement.Enqueue(previous); pth = MainPathFinder.GetRoute(living.Dimension, previous.Destination, this.Destination); } //No reroute else { pth = MainPathFinder.GetRoute(living.Dimension, start, this.Destination); } MagicalLifeAPI.Util.Extensions.EnqueueCollection(movementComponent.QueuedMovement, pth); ClientSendRecieve.Send <RouteCreatedMessage>(new RouteCreatedMessage(pth, living.ID, living.Dimension)); } }
public override void ModifyWorld() { ComponentSelectable livingData = this.Living.GetExactComponent <ComponentSelectable>(); Chunk chunk = MagicalLifeAPI.World.Data.World.GetChunkByTile(this.Living.Dimension, livingData.MapLocation.X, livingData.MapLocation.Y); chunk.Creatures.Add(this.Living.ID, this.Living); }
private static void RenderChunk(Chunk chunk, int dimension) { foreach (Tile tile in chunk) { ComponentSelectable selectable = tile.GetExactComponent <ComponentSelectable>(); StartingPoint.X = RenderInfo.tileSize.X * selectable.MapLocation.X; StartingPoint.Y = RenderInfo.tileSize.Y * selectable.MapLocation.Y; DrawTile(tile, StartingPoint); } DrawEntities(dimension, chunk); }
public void GetComponent_StateUnderTest_ExpectedBehavior() { // Arrange HasComponents unitUnderTest = this.CreateHasComponents(); // Act ComponentSelectable result = unitUnderTest.GetComponent <ComponentSelectable>(); ComponentHarvestable result2 = unitUnderTest.GetComponent <ComponentHarvestable>(); // Assert Assert.IsNotNull(result); Assert.IsNotNull(result2); }
public void Initialize(Dimension dimension) { this.Grid = new Grid(dimension.Width * Chunk.Width, dimension.Height * Chunk.Height, 1); foreach (Tile item in dimension) { ComponentSelectable selected = item.GetExactComponent <ComponentSelectable>(); Position pos = new Position(selected.MapLocation.X, selected.MapLocation.Y); this.Grid.SetCellCost(pos, item.MovementCost); if (!item.IsWalkable) { this.Grid.BlockCell(pos); MasterLog.DebugWriteLine("Blocking tile: " + pos.ToString()); } } }
/// <summary> /// Initializes a new tile object. /// </summary> /// <param name="location">The 3D location of this tile in the map.</param> /// <param name="movementCost">This value is the movement cost of walking on this tile. It should be between 1 and 100</param> protected Tile(Point2D location, int dimension, int movementCost, int footStepSound) : base(true) { ComponentSelectable selectable = new ComponentSelectable(SelectionType.Tile); selectable.MapLocation = location; selectable.Dimension = dimension; this.AddComponent(selectable); this.AddComponent(new ComponentRenderer()); this.IsWalkable = true; this.MovementCost = movementCost; Tile.TileCreatedHandler(new TileEventArgs(this)); this.FootStepSound = footStepSound; }
public override bool CreateDependencies(Living l) { List <Point2D> result = WorldUtil.GetNeighboringTiles(this.Target, l.Dimension); result.RemoveAll(x => !World.Data.World.GetTile(l.Dimension, x.X, x.Y).IsWalkable); ComponentSelectable entitySelected = l.GetExactComponent <ComponentSelectable>(); Point2D adjacentLocation = PathUtil.GetFirstReachable(result, entitySelected.MapLocation, l.Dimension); if (adjacentLocation == null) { return(false); } else { MoveTask task = new MoveTask(this.BoundID, adjacentLocation); this.Dependencies.PreRequisite.Add(task); return(true); } }
/// <summary> /// Drops the result of the harvesting down. /// </summary> /// <param name="l"></param> /// <param name="drop"></param> private void DropItem(Living l, Item drop) { //The tile the entity is standing on ComponentSelectable entityS = l.GetExactComponent <ComponentSelectable>(); Tile entityOn = World.Data.World.GetTile(l.Dimension, entityS.MapLocation.X, entityS.MapLocation.Y); if (entityOn.MainObject == null || entityOn.MainObject.GetType() == drop.GetType()) { ItemAdder.AddItem(drop, entityS.MapLocation, l.Dimension); } else { l.Inventory.AddItem(drop); Point2D emtpyTile = ItemFinder.FindMainObjectEmptyTile(entityOn.GetExactComponent <ComponentSelectable>().MapLocation, l.Dimension); DropItemTask task = new DropItemTask(emtpyTile, l.Dimension, drop, l.ID, Guid.NewGuid()); task.ReservedFor = l.ID; TaskManager.Manager.AddTask(task); } }
/// <summary> /// Used to handle the new selection state as dictated by the last <see cref="HistoricalInput"/> added to <see cref="History"/>. /// </summary> private static void HandleNewSelectionHistory() { HistoricalInput lastHistory = History.Last(); if (!lastHistory.OrderedToTile) { if (lastHistory.DeselectingAll) { Selected.Clear(); MasterLog.DebugWriteLine("Deselected all"); } foreach (HasComponents item in lastHistory.DeselectSome) { ComponentSelectable selected = item.GetExactComponent <ComponentSelectable>(); Selected.Remove(item); MasterLog.DebugWriteLine("Deselected: " + selected.MapLocation.ToString()); } Selected.AddRange(lastHistory.Selected); } }
private void InputHistory_InputAdded() { HistoricalInput last = InputHistory.History.Last(); if (last.ActionSelected == ActionSelected.Till) { foreach (HasComponents item in last.Selected) { ComponentSelectable selected = item.GetExactComponent <ComponentSelectable>(); Tile tile = World.GetTile(RenderInfo.Dimension, selected.MapLocation.X, selected.MapLocation.Y); if (tile.HasComponent <ComponentTillable>() && tile.ImpendingAction == ActionSelected.None && tile.MainObject == null) { TillTask task = new TillTask(tile.GetExactComponent <ComponentSelectable>().MapLocation, Guid.NewGuid(), RenderInfo.Dimension); tile.ImpendingAction = ActionSelected.Till; TaskManager.Manager.AddTask(task); } } } }
private void Move(HasComponents selectable, Point2D target) { if (World.Dimensions[RenderInfo.Dimension][target.X, target.Y].IsWalkable) { switch (selectable) { case Living living: ComponentSelectable positionData = living.GetExactComponent <ComponentSelectable>(); Point2D start = positionData.MapLocation; if (start != target) { List <PathLink> pth; ComponentMovement movementComponent = living.GetExactComponent <ComponentMovement>(); //Handle reroute if (movementComponent.QueuedMovement.Count > 0) { //Get a path to the nearest/next tile so that path finding and the screen location sync up. PathLink previous = movementComponent.QueuedMovement.Peek(); movementComponent.QueuedMovement.Clear(); movementComponent.QueuedMovement.Enqueue(previous); pth = MainPathFinder.GetRoute(RenderInfo.Dimension, previous.Destination, target); } //No reroute else { pth = MainPathFinder.GetRoute(RenderInfo.Dimension, positionData.MapLocation, target); } Extensions.EnqueueCollection(movementComponent.QueuedMovement, pth); } break; default: break; } } }
private void InputHistory_InputAdded() { HistoricalInput last = InputHistory.History.Last(); if (last.ActionSelected == ActionSelected.Mine) { foreach (HasComponents item in last.Selected) { ComponentSelectable selectable = item.GetExactComponent <ComponentSelectable>(); Tile tile = World.GetTile(RenderInfo.Dimension, selectable.MapLocation.X, selectable.MapLocation.Y); if (tile.MainObject != null && tile.ImpendingAction == ActionSelected.None) { if (tile.MainObject is RockBase) { HarvestTask task = new HarvestTask(selectable.MapLocation, Guid.NewGuid()); tile.ImpendingAction = ActionSelected.Mine; TaskManager.Manager.AddTask(task); } } } } }
/// <summary> /// Lets a chunk know that there is an item of the specified type in the specified tile position. /// </summary> private static void RememberWhichTile(Item item, Point2D mapLocation, Chunk chunk) { if (!chunk.Items.ContainsKey(item.ItemID)) { //chunk.Items doesn't store one key and value for every item in the game upfront. chunk.Items.Add(item.ItemID, new RTree <Point2D>()); } RTree <Point2D> itemLocations = chunk.Items[item.ItemID]; List <Point2D> result = itemLocations.Contains(new Rectangle(mapLocation.X, mapLocation.Y, mapLocation.X, mapLocation.Y)); if (result.Count > 0) { //The chunk already knows that there is an item of the specified type in the specified position. } else { Rectangle r = new Rectangle(mapLocation.X, mapLocation.Y, mapLocation.X, mapLocation.Y); Tile tile = WorldUtil.GetTile(mapLocation, chunk); ComponentSelectable selectable = tile.GetExactComponent <ComponentSelectable>(); itemLocations.Add(r, selectable.MapLocation); } }
/// <summary> /// Moves the entity in a direction. /// </summary> /// <param name="entity"></param> /// <param name="source"></param> /// <param name="destination"></param> public static void Move(Living entity, Tile source, Tile destination) { ComponentSelectable sLocation = source.GetExactComponent <ComponentSelectable>(); ComponentSelectable dLocation = destination.GetExactComponent <ComponentSelectable>(); Direction direction = DetermineMovementDirection(sLocation.MapLocation, dLocation.MapLocation); float xMove = 0; float yMove = 0; AnimatedTexture animated = (AnimatedTexture)entity.Visual; switch (direction) { case Direction.North: yMove = -1; animated.StartSequence(Human.UpSequence); break; case Direction.South: yMove = 1; animated.StartSequence(Human.DownSequence); break; case Direction.East: xMove = 1; animated.StartSequence(Human.RightSequence); break; case Direction.West: xMove = -1; animated.StartSequence(Human.LeftSequence); break; case Direction.NorthWest: xMove = -1; yMove = -1; break; case Direction.NorthEast: xMove = 1; yMove = -1; break; case Direction.SouthWest: xMove = -1; yMove = 1; break; case Direction.SouthEast: xMove = 1; yMove = 1; break; default: throw new InvalidOperationException("Unexpected value for direction: " + direction.ToString()); } ComponentMovement movementComponent = entity.GetExactComponent <ComponentMovement>(); xMove *= (float)movementComponent.Movement.GetValue(); yMove *= (float)movementComponent.Movement.GetValue(); float movementPenalty = (float)Math.Abs(CalculateMovementReduction(xMove, yMove)) * -1; if (MathUtil.GetDistance(movementComponent.TileLocation, dLocation.MapLocation) > movementComponent.Movement.GetValue()) { //The character fell short of reaching the next tile movementComponent.TileLocation = new Point2DDouble((float)movementComponent.TileLocation.X + xMove, (float)movementComponent.TileLocation.Y + yMove); FootStepSound(entity, source); } else { //The character made it to the next tile. entity.GetExactComponent <ComponentSelectable>().MapLocation = dLocation.MapLocation; movementComponent.TileLocation = new DataTypes.Point2DDouble(dLocation.MapLocation.X, dLocation.MapLocation.Y); movementComponent.QueuedMovement.Dequeue(); movementPenalty = (float)MathUtil.GetDistance(movementComponent.TileLocation, dLocation.MapLocation); FootStepSound(entity, destination); //If this entity is the current client's and therefore that clients responsibility to report about if (entity.PlayerID == SettingsManager.PlayerSettings.Settings.PlayerID) { ClientSendRecieve.Send(new WorldModifierMessage(new LivingLocationModifier(entity.ID, sLocation.MapLocation, dLocation.MapLocation, entity.Dimension))); } } movementComponent.Movement.AddModifier(new ModifierDouble(movementPenalty, new TimeRemoveCondition(1), Lang.NormalMovement)); }