Exemple #1
0
        public AnimalEntity(Scene scene, Vector2 location, AnimalType <T> animal, float rotation = 0) : base(scene, EntityType.Game, location, bodyType: BodyType.Dynamic)
        {
            AnimalType             = animal;
            AddComponent(animation = new AnimatedSpriteComponent(animal.Asset, animal.Size, animal.AssetOrigin));
            AddComponent(new PhysicsComponent(animal.CollisionShape));
            AddComponent(viewSensor = new SensorComponent("view",
                                                          new PolygonShape(PolygonTools.CreateCapsule(animal.ViewDistance, animal.ViewSize / 2, 4, animal.ViewSize, 8), 1),
                                                          e => e is PlayerEntity && ((PlayerEntity)e).State == PlayerState.Luring));

            AddComponent(hearingSensor = new SensorComponent("hearing",
                                                             new CircleShape(animal.HearingDistance, 1),
                                                             e => e is PlayerEntity && ((PlayerEntity)e).State == PlayerState.Shouting));

            AddComponent(new MovementBasicsComponent());
            AddComponent(new RaycastSensorComponent(animal.ViewDistance));
            AddComponent(new AudioSourceComponent());

            aiComponent = new StateBasedAIComponent <T>(animal.InitalState);

            AddComponent(aiComponent);
            aiComponent.StateChanged += AiComponent_StateChanged1;

            // [FOREACH PERFORMANCE] Should not allocate garbage
            foreach (var b in animal.Behaviors)
            {
                AddComponent(b());
            }

            Body.Rotation = rotation;
        }
Exemple #2
0
    public void Die()
    {
        // move entity out of any touch sensors so they will have a chance to turn off before it's destroyed
        transform.position = KILL_LOCATION;
        SensorComponent sensor = GetComponent <SensorComponent>();

        if (sensor != null)
        {
            // make sure activators are removed from any outputs
            sensor.ClearActivators();
        }
        StartCoroutine(DestroyCoroutine());
    }
Exemple #3
0
        public ChickenEntity(Scene scene, Vector2 location, float rotation = 0, int chickenType = -1) : base(scene, location, CreateType(scene.Game.Content, chickenType), rotation)
        {
            AddComponent(proximitySensor = new SensorComponent(
                             "proximity",
                             new PolygonShape(PolygonTools.CreateCapsule(2f, 0.1f, 1, 0.8f, 8), 1f),
                             e => e is ChickenEntity
                             ));

            AddComponent(playerProximitySensor = new SensorComponent(
                             "playerproximity",
                             new CircleShape(0.3f, 1f),
                             e => e is PlayerEntity
                             ));
        }
Exemple #4
0
        /// <summary>
        /// Checks that the shape of the rank 2 observation input placeholder is the same as the corresponding sensor.
        /// </summary>
        /// <param name="tensorProxy">The tensor that is expected by the model</param>
        /// <param name="sensorComponent">The sensor that produces the visual observation.</param>
        /// <returns>
        /// If the Check failed, returns a string containing information about why the
        /// check failed. If the check passed, returns null.
        /// </returns>
        static string CheckRankTwoObsShape(
            TensorProxy tensorProxy, SensorComponent sensorComponent)
        {
            var shape  = sensorComponent.GetObservationShape();
            var dim1Bp = shape[0];
            var dim2Bp = shape[1];
            var dim1T  = tensorProxy.Channels;
            var dim2T  = tensorProxy.Width;

            if ((dim1Bp != dim1T) || (dim2Bp != dim2T))
            {
                return($"An Observation of the model does not match. " +
                       $"Received TensorProxy of shape [?x{dim1Bp}x{dim2Bp}] but " +
                       $"was expecting [?x{dim1T}x{dim2T}].");
            }
            return(null);
        }
Exemple #5
0
        /// <summary>
        /// Checks that the shape of the visual observation input placeholder is the same as the corresponding sensor.
        /// </summary>
        /// <param name="tensorProxy">The tensor that is expected by the model</param>
        /// <param name="sensorComponent">The sensor that produces the visual observation.</param>
        /// <returns>
        /// If the Check failed, returns a string containing information about why the
        /// check failed. If the check passed, returns null.
        /// </returns>
        static string CheckVisualObsShape(
            TensorProxy tensorProxy, SensorComponent sensorComponent)
        {
            var shape    = sensorComponent.GetObservationShape();
            var heightBp = shape[0];
            var widthBp  = shape[1];
            var pixelBp  = shape[2];
            var heightT  = tensorProxy.shape[1];
            var widthT   = tensorProxy.shape[2];
            var pixelT   = tensorProxy.shape[3];

            if ((widthBp != widthT) || (heightBp != heightT) || (pixelBp != pixelT))
            {
                return($"The visual Observation of the model does not match. " +
                       $"Received TensorProxy of shape [?x{widthBp}x{heightBp}x{pixelBp}] but " +
                       $"was expecting [?x{widthT}x{heightT}x{pixelT}].");
            }
            return(null);
        }
Exemple #6
0
    public virtual void Start()
    {
        if (entity.sensor != null)
        {
            sensorComponent = entity.sensor.MakeComponent(gameObject);
        }
        sensorWasOn = false;
        foreach (EntityBehavior behavior in entity.behaviors)
        {
            if (behavior.target.targetEntityIsActivator)
            {
                activatorBehaviors.Add(behavior);
                continue;
            }

            Behaviour c;
            Entity    targetEntity = behavior.target.targetEntity.entity;
            if (targetEntity != null)
            {
                c = behavior.MakeComponent(targetEntity.component.gameObject);
                targetedComponents.Add(c);
            }
            else
            {
                c = behavior.MakeComponent(gameObject);
            }
            if (behavior.condition == EntityBehavior.Condition.OFF)
            {
                offComponents.Add(c);
                c.enabled = true;
            }
            else if (behavior.condition == EntityBehavior.Condition.ON)
            {
                onComponents.Add(c);
                c.enabled = false;
            }
            else
            {
                c.enabled = true;
            }
        }
    }