Exemple #1
0
 public LocalCameraProcessor(LocalCameraProfile profile, ShooterProcessor shooter)
 {
     this.profile = profile;
     this.shooter = shooter;
     InitialiseCamera();
     ToggleMouseLock();
 }
Exemple #2
0
    /// <summary>
    /// The Process method here is handling AI behaviour.
    /// The best way to handle AI would be to have a separate system for this, but for
    /// the sake of this example the AI logic is embedded in this processor.
    /// </summary>
    protected override void Process()
    {
        // TODO: Set these as AI system constants.
        const float aiAwarenessRadius = 100f;
        const float idealDistance     = 10f;

        Collider[] colliders = Physics.OverlapSphere(this.profile.Rigidbody.position, aiAwarenessRadius, System.ShooterMask);

        if (colliders.Length == 1)
        {
            return;
        }

        float            closestDistance = Mathf.Infinity;
        ShooterProcessor closest         = null;

        // Find closest shooter.
        for (int i = 0; i < colliders.Length; i++)
        {
            // There are only two colliders, which are direct child of the main instance transform.
            if (colliders[i].transform.parent == this.profile.Transform)
            {
                continue;
            }

            if (!this.worldSystem.WorldInstance.FindShooter(colliders[i].transform.parent, out ShooterProcessor iteration))
            {
                continue;
            }

            // Important note: Shooter's this.profile.Transform.position is always 0,0,0!
            // this.profile.Rigidbody or this.profile.Head hold the true position.
            float distance = Vector3.Distance(this.profile.Rigidbody.position, iteration.Profile.Rigidbody.position);

            if (distance >= closestDistance)
            {
                continue;
            }

            closestDistance = distance;
            closest         = iteration;
        }

        if (closest == null)
        {
            // Roam.
            this.profile.Rigidbody.velocity = new Vector3(UnityEngine.Random.value, 0, UnityEngine.Random.value).normalized *System.MoveSpeed;
            return;
        }

        Vector3 enemyDirection = (closest.Profile.Rigidbody.position - this.profile.Rigidbody.position).normalized;

        this.profile.Rigidbody.velocity = closestDistance >= idealDistance ?
                                          enemyDirection.normalized * System.MoveSpeed :
                                          Vector3.zero;

        // Ready to fire.
        Aim(enemyDirection);
        Shoot();
    }
Exemple #3
0
 private void OnDead(object sender, ShooterProcessor e)
 {
     if (this.profile.IsAI)
     {
         Dispose();
     }
 }
Exemple #4
0
    private void OnShooterDeployed(object sender, Type instanceType)
    {
        ShooterProcessor shooter = (ShooterProcessor)sender;

        shooter.Dead += OnShooterDead;
        UpdateEnemyCount();
    }
Exemple #5
0
    public LocalProcessor(LocalSystem system, GameObject instance) : base(system, instance)
    {
        this.profile              = new LocalProfile(GameObject);
        this.parentSystem         = (WorldSystem)System.Parent;
        this.shooter              = this.parentSystem.ShooterSystem.DeployInstance();
        this.shooter.Profile.IsAI = false;
        this.shooter.Profile.Rigidbody.position = new Vector3(50f, 0.5f, 50f);

        SubProcessors.Add(new LocalCameraProcessor(this.profile.CameraProfile, this.shooter));

        this.shooter.Dead += OnShooterDead;
    }
Exemple #6
0
    /// <summary>
    /// Returns true if the attack killed the Shooter.
    /// </summary>
    public bool Attack(float damage, ShooterProcessor attacker)
    {
        this.profile.Health -= damage;
        bool dead = this.profile.Health <= 0;

        if (dead)
        {
            Dead?.Invoke(this, attacker);
        }

        return(dead);
    }
Exemple #7
0
    private void SpawnShooter()
    {
        float timeSinceLastSpawn = Time.timeSinceLevelLoad - this.profile.LastShooterSpawnedSeconds;

        if (timeSinceLastSpawn < 60f / this.system.EnemySpawner.frequencyPM || this.system.EnemySpawner.maxObjects <= this.system.LocalSystem.Instances.Count)
        {
            return;
        }

        this.profile.LastShooterSpawnedSeconds = Time.timeSinceLevelLoad;

        ShooterProcessor shooter = this.system.ShooterSystem.DeployInstance();

        shooter.Profile.Rigidbody.position = RandomPositionAroundPlayer(120f);
    }
Exemple #8
0
    private void OnShooterDead(object sender, ShooterProcessor killer)
    {
        bool localScore = killer == this.system.WorldSystem.LocalSystem.LocalInstance.Shooter;

        if (localScore)
        {
            this.profile.AddLocalScore(10);
        }
        else
        {
            this.profile.AddEnemyScore(1);
        }

        UpdateEnemyCount();
    }
Exemple #9
0
    public bool FindShooter(Transform transform, out ShooterProcessor processor)
    {
        foreach (ShooterProcessor shooterProcessor in System.ShooterSystem.Instances)
        {
            if (shooterProcessor.Profile.Transform != transform)
            {
                continue;
            }

            processor = shooterProcessor;
            return(true);
        }

        processor = null;
        return(false);
    }
Exemple #10
0
    private void OnShooterDeployed(object sender, Type instanceType)
    {
        ShooterProcessor shooter = (ShooterProcessor)sender;

        shooter.Fire += OnShooterFire;
    }
Exemple #11
0
 private void OnShooterDead(object sender, ShooterProcessor e)
 {
     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
 }