示例#1
0
    // -------------------------------------------------------------------------------

    public Plant SpawnPlant(PlantTypes plantType, Vector3 position, Planetoid planetoidOwner, Vector3 groundNormal, bool spawnedByPlayer)
    {
        var plantData = mPlantDataDict[plantType];
        var newPlant  = GameObjectPooler.Instance.GetPooledInstance(plantData.ModelPrefab, position, Quaternion.identity, planetoidOwner.transform);

        newPlant.layer = mPlantLayerId;

        // Point the animator to the correct controller!
        var animator = newPlant.GetComponent <Animator>();

        animator.runtimeAnimatorController = plantData.AnimatorOverrideController;

        // Create a dirt decal
        var dirtDecalPosition = newPlant.transform.position + (newPlant.transform.up * DirtDecalOffset);
        var dirtDecal         = GameObjectPooler.Instance.GetPooledInstance(DirtDecalPrefab, newPlant.transform);

        dirtDecal.transform.position = dirtDecalPosition;

        var       baseCollider = newPlant.GetComponent <SphereCollider>();
        Plant     plantComp    = newPlant.GetComponent <Plant>();
        Rigidbody rigidBody    = newPlant.GetComponent <Rigidbody>();

        // If this is a newly created Plant object, we'll need to add some components!
        if (baseCollider == null)
        {
            baseCollider            = newPlant.AddComponent <SphereCollider>(); // Add a sphere collider which sits at the base of the plant
            plantComp               = newPlant.AddComponent <Plant>();
            plantComp.MyAudioSource = newPlant.AddComponent <AudioSource>();    // Add an AudioSource for sound effects
            rigidBody               = newPlant.AddComponent <Rigidbody>();
        }

        // Initialise our various components
        baseCollider.radius = plantData.BaseRadius;
        plantComp.OnSpawned(groundNormal, plantData, dirtDecal, planetoidOwner, spawnedByPlayer);
        rigidBody.useGravity  = false;
        rigidBody.constraints = RigidbodyConstraints.FreezeAll;
        plantComp.MyAudioSource.playOnAwake = false;
        plantComp.MyAudioSource.loop        = false;

        return(plantComp);
    }
示例#2
0
    // -------------------------------------------------------------------------------

    public void Initialise(PlantTypes plantType, int count)
    {
        mPlantType         = plantType;
        SeedImage.sprite   = PlantManager.Instance.GetPlantData(plantType).SeedSprite;
        SeedCountText.text = count.ToString();

        // If we've just spawned a plant then let's jiggle
        // around a bit to make sure the player knows they've used a seed
        if (ScaleUpOnUpdated && !mIsTweening && mIsVisible && count > 0)
        {
            mIsTweening = true;

            LeanTween.scale(gameObject, OnUpdateTweenScale, 0.25f).setOnComplete(() =>
            {
                LeanTween.scale(gameObject, Vector3.one, 0.4f).setEaseOutBounce().setOnComplete(() =>
                {
                    mIsTweening = false;
                });
            });
        }
    }
示例#3
0
        public double[] calculateForagingAgentSensors(IAgent agent)
        {
            // Each plant type has its own set of sensors, plus we have one sensor for the velocity input.
            double[] sensors = new double[PlantTypes.Count() * SENSORS_PER_OBJECT_TYPE + (DistinguishPredators ? PredatorTypes : 1) * SENSORS_PER_OBJECT_TYPE + 1];

            sensors[0] = agent.Velocity / agent.MaxVelocity;

            // For every plant
            foreach (var plant in Plants)
            {
                // if the plant isn't available for eating then we do not activate the sensors
                if (!plant.AvailableForEating(agent))
                {
                    continue;
                }

                // Calculate the distance to the object from the teacher
                int[] distanceAndOrientation = _sensorDictionary.getDistanceAndOrientation((int)agent.X, (int)agent.Y, (int)plant.X, (int)plant.Y);
                int   dist = distanceAndOrientation[0];
                int   pos  = distanceAndOrientation[1];

                // If it's too far away for the pred to see
                if (dist > AgentHorizon)
                {
                    continue;
                }

                // Identify the appropriate sensor
                int sIdx = getSensorIndex(agent, plant.Species.SpeciesId * SENSORS_PER_OBJECT_TYPE + 1, pos);

                if (sIdx == -1)
                {
                    continue;
                }

                // Add the signal strength for this plant to the sensor
                sensors[sIdx] += 1.0 - dist / AgentHorizon;
            }

            // For every predator
            foreach (var predator in Predators)
            {
                // Calculate the distance to the predator from the pred
                int[] distanceAndOrientation = _sensorDictionary.getDistanceAndOrientation((int)agent.X, (int)agent.Y, (int)predator.X, (int)predator.Y);
                int   dist = distanceAndOrientation[0];
                int   pos  = distanceAndOrientation[1];

                // If it's too far away for the teacher to see
                if (dist > AgentHorizon)
                {
                    continue;
                }

                // Identify the appropriate sensor
                int sIdx = getSensorIndex(agent, PlantTypes.Count() * SENSORS_PER_OBJECT_TYPE + (DistinguishPredators ? predator.AttackType - 1 : 0), pos);

                if (sIdx == -1)
                {
                    continue;
                }

                // Add the signal strength for this plant to the sensor
                sensors[sIdx] += 1.0 - dist / AgentHorizon;
            }

            return(sensors);
        }
示例#4
0
    // -------------------------------------------------------------------------------

    public bool CheckCanSpawnPlant(PlantTypes plantType, Vector3 position, Vector3 groundNormal, out Vector3 averageGroundNormal)
    {
        var radius = mPlantDataDict[plantType].BaseRadius;

        return(PlanetoidObjectSpawnHelper.Instance.CanSpawnObject(radius, position, groundNormal, out averageGroundNormal));
    }