public void DamagedBy(Entity enemy) { if (KnownEntities.Contains(enemy)) { KnownEntities.MoveElement(enemy, -2); } }
internal void TrackEntity(Entity entity) { if (!KnownEntities.Contains(entity.EntityId)) { KnownEntities.Add(entity.EntityId); if (entity is PlayerEntity) { var player = entity as PlayerEntity; var selectedItem = player.SelectedItem.Id; if (selectedItem == -1) { selectedItem = 0; } SendPacket(new SpawnPlayerPacket(player.EntityId, UUID, player.Username, MathHelper.CreateAbsoluteInt(player.Position.X), MathHelper.CreateAbsoluteInt(player.Position.Y), MathHelper.CreateAbsoluteInt(player.Position.Z), MathHelper.CreateRotationByte(player.Yaw), MathHelper.CreateRotationByte(player.Pitch), selectedItem, player.Metadata)); if (!player.SelectedItem.Empty) { SendPacket(new EntityEquipmentPacket(entity.EntityId, EntityEquipmentPacket.EntityEquipmentSlot.HeldItem, player.SelectedItem)); } // TODO: Send armor } else if (entity is ObjectEntity) { var objectEntity = entity as ObjectEntity; SendPacket(new SpawnObjectPacket(objectEntity.EntityId, objectEntity.EntityType, MathHelper.CreateAbsoluteInt(objectEntity.Position.X), MathHelper.CreateAbsoluteInt(objectEntity.Position.Y), MathHelper.CreateAbsoluteInt(objectEntity.Position.Z), MathHelper.CreateRotationByte(objectEntity.Yaw), MathHelper.CreateRotationByte(objectEntity.Pitch), objectEntity.Data, 0, 0, 0)); // TODO: Velocity stuff here if (objectEntity.SendMetadataToClients) { SendPacket(new EntityMetadataPacket(objectEntity.EntityId, objectEntity.Metadata)); } } } }
public void AttackedBy(Entity enemy) { if (KnownEntities.Contains(enemy)) { KnownEntities.MoveElement(enemy, -1); } }
public void Flee() { Console.WriteLine($"{Name} is attempting to flee!"); PressAnyKeyToContinue(); var allOpenSpaces = Location.Map.GetAllOpenSpaces(); if (allOpenSpaces.Length > 0) { var allOpenMapPoints = new List <MapPoint>(); foreach (var openSpace in allOpenSpaces) { allOpenMapPoints.Add(openSpace.ToMapPoint(Location.Map)); } // order all open spaces by the sum of their distance to every enemy: allOpenMapPoints.OrderByDescending(mP => KnownEntities.Sum(e => e.Location.DistanceTo(mP))); // the first one should be the farthest from all enemies: PathfindTo(allOpenMapPoints[0]); } else { Console.WriteLine($"There is nowhere for {Name} to run!"); PressAnyKeyToContinue(); } }
internal void ForgetEntity(Entity entity) { if (KnownEntities.Contains(entity.EntityId)) { KnownEntities.Remove(entity.EntityId); } SendPacket(new DestroyEntityPacket(new int[] { entity.EntityId })); }
public void DamagedBy(Entity enemy) { if (!Attackers.Contains(enemy)) { Attackers.Add(enemy); } if (KnownEntities.Contains(enemy)) { KnownEntities.MoveElement(enemy, -2); } }
public void Prioritize() { Search(); KnownItems.OrderBy(i => i.Location.DistanceTo(this.Location)) .ThenByDescending(i => i is Consumable); KnownEntities.OrderBy(e => e.Team == this.Team) // nonsentient npcs are only interested in enemies .ThenBy(e => e.Location.DistanceTo(this.Location)) // especially ones that are closest .ThenBy(e => e.CurrentHp.Value) // and have less hp remaining .ThenByDescending(e => hasLineOfSightTo(e)); // and are in line of sight var enemies = KnownEntities.Where(e => e.Team != Team); }
private void addToKnown(List <IMappable> knownObjects) { foreach (var obj in knownObjects) { if (obj is Entity) { KnownEntities.Add(obj as Entity); } else if (obj is Item) { KnownItems.Add(obj as Item); } } }
public void Prioritize() { if (KnownItems.Count > 0) { KnownItems.OrderBy(i => i.Location.DistanceTo(this.Location)) // order items by closest .ThenByDescending(i => i is Consumable); // give priority to consumables } if (KnownEntities.Count > 0) { KnownEntities.OrderBy(e => e.Location.DistanceTo(this.Location)) // order enemies by closest .ThenBy(e => e.CurrentHp.Value) // and those that have less hp remaining .ThenByDescending(e => hasLineOfSightTo(e)); // and are in line of sight } }
public void HandleMovementUpdate(EntityAssociatedData <IMovementData> movementUpdate, bool forceHandleLocal = false) { if (!KnownEntities.isEntityKnown(movementUpdate.EntityGuid)) { if (Logger.IsInfoEnabled) { Logger.Info($"TODO: Received movement update too soon. Must enable deferred movement update queueing for entities that are about to spawn."); } return; } try { if (!forceHandleLocal) { //Cheap check, and we're on another thread so performance doesn't really matter if (movementUpdate.EntityGuid == PlayerDetails.LocalPlayerGuid) { if (movementUpdate.Data.isUserCreated) { return; //don't handle user created movement data about ourselves. It'll just make movement abit janky locally. } } } IMovementGenerator <GameObject> generator = MovementGeneratorFactory.Create(movementUpdate); //We just initialize this casually, the next update tick in Unity3D will start the movement generator, the old generator actually might be running right now //at the time this is set. MovementGeneratorMappable.ReplaceObject(movementUpdate.EntityGuid, generator); MovementDataMappable.ReplaceObject(movementUpdate.EntityGuid, movementUpdate.Data); } catch (Exception e) { string error = $"Failed to handle Movement Data for Entity: {movementUpdate.EntityGuid} Type: {movementUpdate.Data.GetType().Name} Error: {e.Message}"; if (Logger.IsErrorEnabled) { Logger.Error(error); } throw new InvalidOperationException(error, e); } }
protected override void OnEventFired(object source, ContentPrefabCompletedDownloadEventArgs args) { //It's possible we don't know about the entity anymore, since this is a long async process. if (!KnownEntities.isEntityKnown(args.EntityGuid)) { if (Logger.IsInfoEnabled) { Logger.Info($"Encountered {nameof(ContentPrefabCompletedDownloadEventArgs)} for unknown Entity: {args.EntityGuid}"); } return; } //We need to check, and release if there is any, before we replace it. if (PrefabHandleMappable.ContainsKey(args.EntityGuid)) { IPrefabContentResourceHandle handle = PrefabHandleMappable.RetrieveEntity(args.EntityGuid); handle.Release(); PrefabHandleMappable.RemoveEntityEntry(args.EntityGuid); } PrefabHandleMappable.AddObject(args.EntityGuid, args.PrefabHandle); }