コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ThingView"/> class.
        /// </summary>
        /// <param name="thing">The thing to view.</param>
        /// <param name="world">The world.</param>
        public ThingView(BaseThing thing, World world)
        {
            this.thing = thing;

            if (this.thing is BaseMovingThing moving && moving.IsMoving)
            {
                (this.MovingTowardsX, this.MovingTowardsY) = moving.GetDestination(world);
            }
        }
コード例 #2
0
        /// <summary>
        /// Evaluates the expressions in the Value property with the given parameters.
        /// </summary>
        /// <param name="rdm">The random number generator</param>
        /// <param name="thing">The thing.</param>
        /// <returns>The result of evaluation.</returns>
        public string EvalDynamicValueSafe(Random rdm, BaseThing thing)
        {
            if (!this.Dynamic)
            {
                throw new InvalidOperationException("Can only call EvalDynamicValue if Dynamic.");
            }

            return(this.EvalDynamicValue(rdm, thing).ToString() ?? string.Empty);
        }
コード例 #3
0
        /// <summary>
        /// Adds a thing to the correct grid square.
        /// </summary>
        /// <param name="thing">The thing to add.</param>
        public void AddThing(BaseThing thing)
        {
            if (thing is null)
            {
                throw new ArgumentNullException(nameof(thing));
            }

            this.GetSquare(thing.X, thing.Y).AddThing(thing);
        }
コード例 #4
0
        /// <summary>
        /// Adds a thing to this square.
        /// </summary>
        /// <param name="thing">The thing to add.</param>
        /// <exception cref="InvalidOperationException">A WorldSquare was added to the square when it already had a world square.</exception>
        public void AddThing(BaseThing thing)
        {
            if (thing.ThingType == ThingType.WorldSquare)
            {
                if (this.SquareDefinition != null)
                {
                    throw new InvalidOperationException($"Can not add World Squares to a square which already has a world square.");
                }

                this.SquareDefinition = thing as WorldSquare;
                return;
            }

            this.ThingsInSquare.Add(thing);
        }
コード例 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AspectView"/> class.
        /// </summary>
        /// <param name="thing">The thing to get the Aspect info for.</param>
        /// <param name="name">The name of the Aspect.</param>
        public AspectView(BaseThing thing, string name)
        {
            this.Title     = name;
            this.Effective = thing.EffectiveAspect(name, "None");

            if (thing.BaseAspects.TryGetValue(name, out string? @base) && !string.IsNullOrEmpty(@base))
            {
                this.Base = @base;
            }

            if (thing.DynamicAspects.TryGetValue(name, out string?dynamic) && !string.IsNullOrEmpty(dynamic))
            {
                this.Dynamic = dynamic;
            }

            if (thing.BaseDefinition.Aspects.TryGetValue(name, out AspectDefinition? definition) && definition.Dynamic)
            {
                this.DynamicScript = definition.DynamicValue;
            }

            var current = thing.GetCurrentAspectEffect(name);

            if (current != null)
            {
                this.CurrentEffect = current.Value;
            }

            StringBuilder sb = new StringBuilder();

            if (this.DynamicScript != null)
            {
                sb.AppendLine($"Dynamic Script: {this.DynamicScript}");
            }

            foreach (AspectEffect effect in thing.GetAspectEffectsModifying(name))
            {
                EffectView view = new EffectView(effect);
                string     str  = $"{view.Title} {view.EffectString}";
                if (effect == current)
                {
                    str += $" (Current)";
                }

                sb.AppendLine(str);
            }

            this.Tooltip = sb.ToString();
        }
コード例 #6
0
ファイル: ThingTest.cs プロジェクト: SoveltoOyj/T2D
        private async Task <BaseThing> CreateATestThing(HttpClient client)
        {
            string us    = Guid.NewGuid().ToString();
            string id    = $"{cfqdn}/{us}";
            string title = "New Thing";

            BaseThing newThing = new BaseThing
            {
                Id    = id,
                Title = title
            };

            var postResponse = await client.PostAsJsonAsync($"{_url}", newThing);

            var created = await postResponse.Content.ReadAsJsonAsync <BaseThing>();

            Assert.NotNull(created);
            Assert.Equal(id, created.Id);
            Assert.Equal(title, created.Title);
            return(created);
        }
コード例 #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeView"/> class.
        /// </summary>
        /// <param name="thing">The thing to get the attribute info for.</param>
        /// <param name="name">The name of the attribute.</param>
        public AttributeView(BaseThing thing, string name)
        {
            this.Title = name;
            if (thing.BaseAttributes.TryGetValue(name, out int @base))
            {
                this.Base = @base;
            }

            if (thing.DynamicAttributes.TryGetValue(name, out int dynamic))
            {
                this.Dynamic = dynamic;
            }

            if (thing.BaseDefinition.Attributes.TryGetValue(name, out AttributeDefinition? def))
            {
                this.DynamicScript = def.DynamicValue;
            }

            this.Effective = thing.EffectiveAttribute(name);

            StringBuilder sb = new StringBuilder();

            if (this.DynamicScript != null)
            {
                sb.AppendLine($"Dynamic Script: {this.DynamicScript}");
            }

            foreach (AttributeEffect effect in thing.GetAttributeEffectsModifying(name))
            {
                this.EffectValue += effect.Manitude;
                EffectView view = new EffectView(effect);
                sb.AppendLine($"{view.Title} {view.EffectString}");
            }

            this.Tooltip = sb.ToString();
        }
コード例 #8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="thing"></param>
 /// <param name="session"></param>
 /// <param name="role">not necessary in dbContext!</param>
 /// <param name="attribute">not necessary in dbContext</param>
 /// <returns></returns>
 public static bool QueryAttributeRight(BaseThing thing, Session session, Role role, T2D.Entities.Attribute attribute)
 {
     return(true);
 }
コード例 #9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="thing"></param>
 /// <param name="session"></param>
 /// <param name="role">not necessary in dbContect!</param>
 /// <returns></returns>
 public static bool QueryRelationsRight(BaseThing thing, Session session, Role role)
 {
     return(true);
 }
コード例 #10
0
 public static bool QueryMyRolesRight(BaseThing thing, Session session)
 {
     return(true);
 }
コード例 #11
0
        /// <summary>
        /// Completes the movement onto the specified thing.
        /// </summary>
        /// <param name="newThing">The new version of the inputted thing.</param>
        public void ApplyMovement(BaseMovingThing newThing)
        {
            bool  canFly     = newThing.MovingThingDefinition.EvalFlies(this.random, newThing);
            float landSpeed  = newThing.MovingThingDefinition.EvalLandSpeed(this.random, newThing);
            float waterSpeed = newThing.MovingThingDefinition.EvalWaterSpeed(this.random, newThing);

            float waterSpeedRatio = 1;

            if (!canFly)
            {
                waterSpeedRatio = landSpeed / waterSpeed;
            }

            int startX = newThing.X;
            int startY = newThing.Y;

            int  destinationX, destinationY;
            Guid?thingMovingTowards = null;

            switch (newThing.MoveType)
            {
            case MoveType.ToCoords:
                destinationX = newThing.MoveToCoordX ?? throw new InvalidOperationException("Object is moving towards a coord but MoveToCoordX is null.");
                destinationY = newThing.MoveToCoordY ?? throw new InvalidOperationException("Object is moving towards a coord but MoveToCoordY is null.");
                break;

            case MoveType.ToThing:
                BaseThing thingToMoveTo =
                    this.world.FindThing(newThing.MoveToThing ?? throw new InvalidOperationException("Object is moving towards a thing but MoveToThing is null."));
                destinationX       = thingToMoveTo.X;
                destinationY       = thingToMoveTo.Y;
                thingMovingTowards = newThing.MoveToThing;
                break;

            default:
                throw new InvalidOperationException($"Unsupported movetype {newThing.MoveType}");
            }

            bool NeedsRecalcPath()
            {
                // If we're moving towards a thing, we need to ensure it hasn't moved.
                var lastEntry = newThing.Path?.LastOrDefault();

                if (lastEntry == null || thingMovingTowards == null)
                {
                    return(false);
                }

                return(lastEntry.X != destinationX || lastEntry.Y != destinationY);
            }

            // The residual movement is filled with full movement, and will be reduced as we process.
            newThing.ResidualMovement = landSpeed + newThing.ResidualMovement;

            if (newThing.Path == null || NeedsRecalcPath())
            {
                // A new path needs calculating.
                if (canFly)
                {
                    newThing.Path = FlightPath((startX, startY), (destinationX, destinationY)).ToImmutableList();
                }
                else
                {
                    var path = this.finder.FindPath(new Point(startX, startY), new Point(destinationX, destinationY), waterSpeedRatio);

                    if (path != null)
                    {
                        newThing.Path = path.Select(p => new Location()
                        {
                            X = p.X, Y = p.Y
                        }).ToImmutableList();
                    }
                }
            }

            // Walk the path until we're out of movement.
            while (true)
            {
                var pathEntry = newThing.Path?.FirstOrDefault();
                if (pathEntry == null)
                {
                    // We're done walking.
                    break;
                }

                var   square       = this.world.Grid.GetSquare(pathEntry.X, pathEntry.Y);
                float movementCost = square.GetMovementCost(this.random);
                bool  isWater      = square.SquareDefinition?.Definition.IsWater ?? false;

                if (!CanMove(newThing, movementCost, isWater, waterSpeedRatio))
                {
                    // Costs too much to enter this square.
                    break;
                }

                // Remove this path entry.
                newThing.Path = newThing.Path?.RemoveAt(0);

                // Apply the movement
                newThing.X = pathEntry.X;
                newThing.Y = pathEntry.Y;

                if (isWater)
                {
                    newThing.ResidualMovement -= movementCost * waterSpeedRatio;
                }
                else
                {
                    newThing.ResidualMovement -= movementCost;
                }
            }

            if (newThing.X == destinationX && newThing.Y == destinationY)
            {
                Log.Info($"{newThing.BaseDefinition.Name} {newThing.Name} has completed its movement.");
                newThing.CompleteMovement();

                // If moving towards a site, enter the site.
                if (thingMovingTowards.HasValue && newThing is ICanEnterSites enterSites)
                {
                    enterSites.InSiteId = thingMovingTowards;
                }
            }
        }
コード例 #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseThingPres"/> class.
 /// </summary>
 /// <param name="inner">The thing this represents.</param>
 /// <param name="world">The world.</param>
 public BaseThingPres(BaseThing inner, World world)
 {
     this.Inner = inner;
     this.World = world;
 }