Ejemplo n.º 1
0
    void Start()
    {
        Crowd = new RobotCrowd();
        Crowd.EndBase = this;
        Crowd.StartBase = this;

        Selection = this.transform.FindChild("Selection");

        if (IsMasterBase)
        {
            StarShipMaterial = this.transform.parent.renderer.material;

            StarShipMaterial.color = GameplayController.Instance.PlayerColors[JoueurId];
            secondeRobotGeneration = 1f / GameplayController.Instance.NbRobotToCreatePerSecond;
            StartCoroutine(GenerateRobots());
        }

        StartCoroutine(CrowdUpdate());
    }
Ejemplo n.º 2
0
    public void SendRobots(PlayerInput playerInput)
    {
        SoundSendRobots();

        RobotCrowd crowd = new RobotCrowd();
        crowd.StartBase = playerInput.StartBase;
        crowd.EndBase = playerInput.EndBase;
        crowd.Arrow = playerInput.Arrow;

        for (int i = 0; i < (int)playerInput.NbRobotToCreate; i++)
        {
            if (crowd.StartBase.Crowd.listRobot.Count > 0)
            {
                Robot robot = crowd.StartBase.Crowd.listRobot[0];
                robot.Crowd = crowd;
                robot.isArrived = false;

                crowd.StartBase.Crowd.listRobot.Remove(robot);

                crowd.listRobot.Add(robot);
            }
        }

        DeSelectRobots(playerInput.StartBase.Crowd);
        DeSelectRobots(crowd);

        listRobotCrowd.Add(crowd);
    }
Ejemplo n.º 3
0
    IEnumerator UpdateBoidAndLaser()
    {
        LayerMask layerMask = layermaskRobot;
        Collider2D[] colliders = new Collider2D[20];
        Vector2 relativePos;
        Vector2 separation;
        Vector3 vec;
        Vector2 center;
        Vector2 velocity;
        Vector2 follow;
        Vector2 randomize;
        Vector2 steer;

        while (Life > 0)
        {
            //---> Lorsque le robot est dans une foule en mouvement vers une autre base, la détection des bases est activée
            if (!isArrived)
                layerMask = layerMask | (1 << 13);

            bool doBoidComputation = futureTimeBoidComputation < Time.time && Crowd != null;

            //---> Si il n'y a pas de calcul de boid à faire (uniquement le calcul des combats), ignorer la couche courante de robot
            if (!doBoidComputation)
            {
                layerMask = layerMask ^ (1 << _layer);
            }

            separation = Vector2.zero;

            int countCollision = Physics2D.OverlapCircleNonAlloc(_transform.position, GameplayController.Instance.distanceAttackRobots, colliders, layerMask);

            for (int i = 0; i < countCollision; i++)
            {
                Collider2D collider = colliders[i];

                //--> Couches 8 à 12 : Robots
                if (collider.gameObject.layer < 13)
                {
                    Robot robotCollided = collider.GetComponent<Robot>();

                    //--- Calcul la séparation des boids
                    if (doBoidComputation)
                    {
                        relativePos = _transform.position - robotCollided._transform.position;

                        if (relativePos.sqrMagnitude > 0 && relativePos.sqrMagnitude <= GameplayController.Instance.distanceBetweenRobots)
                        {
                            //relativePos.Normalize();
                            separation += relativePos / relativePos.sqrMagnitude;
                        }
                    }
                    //---

                    //---> Calcul du tir de laser entre robots adversaires
                    if (robotCollided.JoueurId != this.JoueurId && robotCollided.Life > 0)
                    {
                        //float laserCoolDown = 0.3f;

                        //---> Les zombies ne peuvent tirer qu'une fois par seconde
                        //if (GamePlayController.JoueurZombieId == this.JoueurId)
                        //    laserCoolDown = 1f;

                        if (Time.time - lastHitLaser > 0.3f)
                        {

                            GameplayController.Instance.CreateLaser(this, robotCollided);
                            lastHitLaser = Time.time;

                            //---> Conversion du robot adversaire en zombie
                            if (GameplayController.Instance.JoueurZombieId == this.JoueurId && robotCollided.Life <= 1 && GameplayController.Instance.CountRobot[this.JoueurId] < GameplayController.Instance.MaxRobotInGamePerPlayer)
                            {
                                //Debug.Log("Zombified");
                                this.Crowd.listRobot.Add(robotCollided);
                                robotCollided.Crowd.listRobot.Remove(robotCollided);

                                robotCollided.Crowd = this.Crowd;
                                GameplayController.Instance.CountRobot[robotCollided.JoueurId]--;
                                GameplayController.Instance.CountRobot[this.JoueurId]++;
                                robotCollided.Life = 4;
                                robotCollided.JoueurId = this.JoueurId;
                                robotCollided.gameObject.layer = this._layer;
                                robotCollided._mesh.colors = GameplayController.Instance.GetColors(this.JoueurId, 4);
                                robotCollided.isArrived = this.isArrived;
                            }
                            else
                            {
                                robotCollided.Life--;
                            }
                        }
                    }
                }
                //---> Couche 13, entrée dans une base
                else if (!isArrived && collider.gameObject.layer == 13)
                {
                    RobotBase robotBaseCollided = collider.GetComponent<RobotBase>();

                    if (this.Crowd.EndBase == robotBaseCollided)
                    {
                        isArrived = true;
                        this.Crowd.listRobot.Remove(this);

                        if (this.Crowd.EndBase.Crowd.listRobot.Count > 0 && this.Crowd.EndBase.Crowd.listRobot[0].JoueurId != JoueurId)
                        {
                            this.Crowd.EndBase.Crowd.listRobot[0].Life = 0;
                            this.Life = 0;
                        }
                        else
                        {
                            this.Crowd.EndBase.Crowd.listRobot.Add(this);
                            this.Crowd = this.Crowd.EndBase.Crowd;

                            //---> Capture de la base énemmie
                            if (this.Crowd.EndBase.IsMasterBase && this.Crowd.EndBase.JoueurId != JoueurId)
                            {
                                GameplayController.Instance.SoundCaptureBase();

                                this.Crowd.EndBase.JoueurId = JoueurId;
                                this.Crowd.EndBase.StarShipMaterial.color = GameplayController.Instance.PlayerColors[JoueurId];
                            }
                        }

                        //isArrived = true;
                        //this.Crowd.listRobot.Remove(this);
                        //this.Crowd.EndBase.Crowd.listRobot.Add(this);
                    }
                }
            }

            if (doBoidComputation)
            {
                //--- Calcul des forces du boid : attraction vers le centre + vélocité + mouvement vers la cible + séparation des autres boids + mouvement aléatoire
                vec = new Vector3(Crowd.EndBase.transform.position.x, Crowd.EndBase.transform.position.y, -1f);

                center = Crowd.flockCenter - _transform.position;
                velocity = Crowd.flockVelocity - _rigidbody.velocity;
                follow = vec - _transform.position;
                randomize = new Vector3((Random.value * 2) - 1, (Random.value * 2) - 1, 0);

                randomize.Normalize();

                steer = GameplayController.Instance.centerWeight * center +
                                GameplayController.Instance.velocityWeight * velocity +
                                GameplayController.Instance.separationWeight * separation +
                                (isArrived ? 0.5f : GameplayController.Instance.followWeight) * follow +
                                GameplayController.Instance.randomizeWeight * randomize;
                //---

                //--- Application de la force au rigidbody du robot
                _rigidbody.velocity = steer * Time.deltaTime;

                float speed = _rigidbody.velocity.magnitude;

                if (speed > GameplayController.Instance.maxVelocity)
                {
                    _rigidbody.velocity = _rigidbody.velocity.normalized * GameplayController.Instance.maxVelocity;
                }
                else if (speed < GameplayController.Instance.minVelocity)
                {
                    _rigidbody.velocity = _rigidbody.velocity.normalized * GameplayController.Instance.minVelocity;
                }
                //---

                if (isArrived)
                    _transform.rotation = Quaternion.FromToRotation(Vector3.up, follow);
                else
                    _transform.rotation = Quaternion.FromToRotation(Vector3.up, -follow);

                futureTimeBoidComputation = Time.time + Random.Range(0.3f, 0.5f);
            }
            //TODO : utiliser le laser cooldown
            yield return new WaitForSeconds(0.3f);
        }

        //---> La boucle while s'execute tant que le robot a de la vie.
        //     Lorsque la boucle se termine, le robot est détruit
        GameplayController.Instance.DeleteRobot(this);
    }
Ejemplo n.º 4
0
 public void DeSelectRobots(RobotCrowd crowd)
 {
     for (int i = 0; i < (int)crowd.listRobot.Count; i++)
     {
         crowd.listRobot[i]._mesh.colors = GetColors(crowd.listRobot[i].JoueurId, 4);
     }
 }