public void Deploy(CarSampleConfig config, Transform road, Vector2 point)
    {
        transform.position = point;
        transform.SetParent(road, false);

        m_originalPosition = transform.position;
    }
 private void Awake()
 {
     if (s_config != null)
     {
         Config = s_config;
     }
 }
            public CarWheelRadiusPhenotype(CarSampleConfig config, int entityIndex)
                : base("WheelRadius", WheelRadiusBits)
            {
                MinValue = 0;

                // If entityIndex is greater than wheels count, then should not
                // generate any wheel.
                MaxValue = entityIndex < config.WheelsCount
                                               ? MaxValue = config.MaxWheelRadius
                                               : 0;
            }
            public CarWheelIndexPhenotype(CarSampleConfig config, int entityIndex)
                : base("WheelIndex", WheelIndexBits)
            {
                MinValue = 0;

                // If entityIndex is greater than wheels count, then should not
                // generate any wheel.
                MaxValue = entityIndex < config.WheelsCount
                                               ? config.VectorsCount - 1
                                               : 0;
            }
Example #5
0
        public CarChromosome(CarSampleConfig config)
        {
            m_config = config;

            var phenotypeEntities = new CarVectorPhenotypeEntity[config.VectorsCount];

            for (int i = 0; i < phenotypeEntities.Length; i++)
            {
                phenotypeEntities[i] = new CarVectorPhenotypeEntity(config, i);
            }

            SetPhenotypes(phenotypeEntities);
            CreateGenes();
        }
Example #6
0
        public Vector2 Build(CarSampleConfig config, RoadController road, Vector2 start, int pointsCount, int startPointIndex)
        {
            if (m_config == null)
            {
                m_config = config;

                // Creates the path game object.
                var path = Object.Instantiate(road.PathPrefab) as GameObject;
                path.layer = LayerMask.NameToLayer("Floor");
                path.transform.SetParent(road.transform, false);
                path.transform.rotation = Quaternion.Euler(0, 0, m_config.ZRotation);

                // Gets the polygon component.
                m_polygon           = path.GetComponent <PolygonCollider2D>();
                m_polygon.pathCount = pointsCount;

                var xIndex = startPointIndex;

                // Gets the obstacles container game object.
                m_obstacles = path.transform.Find("Obstacles").gameObject;
                m_obstacles.transform.parent = path.transform;

                var points = new Vector2[pointsCount * 2];

                for (int i = 0; i < pointsCount; i++)
                {
                    var x = start.x + m_config.MaxPointsDistance * xIndex++;
                    points[i] = new Vector2(x, CalculateY(x, xIndex));

                    DeployObstacle(i, points[i], xIndex);
                }

                //  Closes the polygon.
                for (int i = pointsCount; i < points.Length; i++)
                {
                    var point = points[points.Length - i - 1];
                    points[i] = new Vector2(point.x, point.y - Height);
                }

                m_polygon.points = points;
            }
            else
            {
                RedeployObstacles();
            }

            return(m_polygon.points[pointsCount - 1]);
        }
 public CarVectorPhenotypeEntity(CarSampleConfig config, int entityIndex)
 {
     Phenotypes = new IPhenotype[]
     {
         new Phenotype("VectorSize", VectorSizeBits)
         {
             MinValue = 1,
             MaxValue = config.MaxVectorSize
         },
         new Phenotype("VectorAngle", VectorAngleBits)
         {
             MinValue = 0,
             MaxValue = 359
         },
         new CarWheelIndexPhenotype(config, entityIndex),
         new CarWheelRadiusPhenotype(config, entityIndex)
     };
 }
Example #8
0
        public void Build(CarSampleConfig config)
        {
            // Creates the path builders.
            if (m_pathBuilders == null)
            {
                var pathsCount = 1;

                if (config.GapsEachPoints > 0)
                {
                    pathsCount      = Mathf.CeilToInt(config.PointsCount / config.GapsEachPoints) * 2;
                    m_pointsPerPath = config.GapsEachPoints;
                }
                else
                {
                    m_pointsPerPath = config.PointsCount / pathsCount;
                }

                m_pathBuilders = new PathBuilder[pathsCount];

                for (int i = 0; i < pathsCount; i++)
                {
                    m_pathBuilders[i] = new PathBuilder();
                }

                // Call each path builder to build the path.
                var start           = (Vector2)transform.position;
                var end             = start;
                var startPointIndex = 0;

                for (int i = 0; i < m_pathBuilders.Length; i++)
                {
                    end              = m_pathBuilders[i].Build(config, this, start, m_pointsPerPath, startPointIndex);
                    start           += new Vector2(config.MaxGapWidth * (i + 1) / m_pathBuilders.Length, 0);
                    startPointIndex += m_pointsPerPath - 1;
                }

                // Creates the dead-end.
                var deadEnd = Instantiate(DeadEndPrefab, end, Quaternion.identity) as GameObject;
                deadEnd.transform.SetParent(transform, false);
                deadEnd.name = "dead-end";
            }
        }
        public void SetChromosome(CarChromosome chromosome, CarSampleConfig config)
        {
            Chromosome                 = chromosome;
            Chromosome.MaxDistance     = 0;
            chromosome.MaxDistanceTime = 0;
            Distance           = 0;
            DistanceTime       = 0;
            m_startTime        = Time.time;
            transform.rotation = Quaternion.identity;

            m_config             = config;
            m_rb.isKinematic     = false;
            m_rb.velocity        = Vector2.zero;
            m_rb.angularVelocity = 0;

            var phenotypes = chromosome.GetPhenotypes();

            m_polygon.points = phenotypes.Select(p => p.Vector).ToArray();
            var wheelsMass = 0f;

            for (int i = 0; i < phenotypes.Length; i++)
            {
                var p = phenotypes[i];
                PrepareWheel(i, m_polygon.points[p.WheelIndex], p.WheelRadius);
                wheelsMass += p.WheelRadius;
            }

            // The car mass should be greater than wheels sum mass, because the WheelJoint2d get crazy otherwise.
            // If we comment the line bellow and enable the car mass should be greater than wheels sum mass, because the WheelJoint2d get crazy otherwise.
            m_rb.mass = 1 + m_polygon.points.Sum(p => p.magnitude) * VectorMagnitudeMass + wheelsMass;

            if (m_cam != null)
            {
                m_cam.StartFollowing(gameObject);
            }

            StartCoroutine("CheckTimeout");
        }
 public static void SetConfig(CarSampleConfig config)
 {
     s_config = config;
 }