예제 #1
0
파일: Kill.cs 프로젝트: dmbyer/Hedron
        public override CommandResult Execute(CommandEventArgs commandEventArgs)
        {
            try
            {
                base.Execute(commandEventArgs);
            }
            catch (StateException)
            {
                if (commandEventArgs.Entity.State == EntityState.Combat)
                {
                    return(CommandResult.Failure("You are already in combat!"));
                }
                else
                {
                    var errMessage = $"Unexpected entity state: {commandEventArgs.Entity.State}.";
                    Logger.Error(nameof(CommandService), nameof(Kill), errMessage);
                    return(CommandResult.Failure(errMessage));
                }
            }
            catch (CommandException ex)
            {
                return(ex.CommandResult);
            }

            var           room     = commandEventArgs.Entity.GetInstanceParentRoom();
            var           entities = room.Animates.GetAllEntities <EntityAnimate>();
            EntityAnimate entity   = commandEventArgs.Entity;
            uint?         targetID = null;

            // Find first matching target
            targetID = Parse.MatchOnEntityNameByOrder(commandEventArgs.Argument, entities.Cast <IEntity>().ToList())?.Instance;

            if (targetID == entity?.Instance)
            {
                return(CommandResult.Failure("You cannot kill yourself."));
            }

            if (targetID != null)
            {
                CombatHandler.Enter(commandEventArgs.Entity.Instance, targetID, false);
            }
            else
            {
                return(CommandResult.Failure("There is no such target."));
            }

            var targetName = DataAccess.Get <EntityAnimate>(targetID, CacheType.Instance).ShortDescription;

            return(CommandResult.Success($"You attack {targetName}!"));
        }