【自作ゲーム開発28】本拠地システムの追加【Unity】


注意
本記事で掲載されている動作の実装方法及びプログラムのソースコードは最適な方法ではない可能性があります。
今後不具合等が判明した場合には修正及び改良をおこなう可能性があります。
また今後自分で同機能を実装する場合の参考にする可能性もあるためソースコードだけではなく説明しつつ記事を進めていきます。

はじめに

Unityによる自作ゲーム開発進捗その28になります!

今回はプレイヤーの本拠地システムを作成し、3レーンとも最後には本拠地へ攻撃するようになります。

既存のスクリプトを5つ更新します。

以下に動画でのコピー用にソースコードを掲載しています。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BaseSystem : MonoBehaviour{
    [SerializeField]
    private Canvas m_hitpoint_canvas;

    [SerializeField]
    private Slider m_hitpoint_bar;

    [SerializeField]
    private ParticleSystem m_smoke_effect;

    [SerializeField]
    private ParticleSystem m_bomb_effect;

    [SerializeField]
    private GameObject m_put_base;

    private BaseSystem m_main_base;

    enum STATE {
        WAIT,
        SMOKE,
        BOMB
    }; STATE e_state;

    private void Start() {
        m_main_base = GameObject.Find("MainBase").GetComponent<BaseSystem>();
        m_main_base.MainBaseInitial();
        BaseInitialize(3000); // タスク1
    }

    public void BaseInitialize(float a_hitpoint) {
        // タスク2
        m_hitpoint_canvas.worldCamera = Camera.main;
        m_hitpoint_bar.maxValue = a_hitpoint;
        m_hitpoint_bar.value = a_hitpoint;
    }

    public void BaseDamage(float a_damage) {
        float l_value = m_hitpoint_bar.value - a_damage;

        BaseState(l_value);
    }

    public void MainBaseDamage(float a_damage) {
        m_main_base.BaseDamage(a_damage);
    }

    public void MainBaseInitial() {
        BaseInitialize(100000); // タスク3
    }

    private void BaseState(float a_hitpoint) {
        switch (e_state) {
            case STATE.WAIT:
                if(a_hitpoint < 0) a_hitpoint = 0;

                if(a_hitpoint < m_hitpoint_bar.maxValue / 2) {
                    m_smoke_effect.Play();
                    e_state = STATE.SMOKE;
                }

                m_hitpoint_bar.value = a_hitpoint;
                    
                break;

            case STATE.SMOKE:
                if (a_hitpoint < 0) {
                    a_hitpoint = 0;
                    StartCoroutine("BaseBomb");
                    e_state = STATE.BOMB;
                }

                m_hitpoint_bar.value = a_hitpoint;

                break;

            case STATE.BOMB:
                break;
        }
    }

    IEnumerator BaseBomb() {
        m_bomb_effect.Play();
        m_smoke_effect.Stop();
        m_put_base.SetActive(false);
        yield return new WaitForSeconds(2.0f);
        this.gameObject.SetActive(false);
    }
}

/*
    [規則]
    p_ 外部アクセス
    m_ メンバー変数
    l_ ローカル変数
    a_ 引数

    [説明]
    

    [バージョン]
    2021-02-19 関数呼び出しによる自身へのダメージ及び体力ゲージの可視化
    2021-03-12 HP低下による煙とHP0による爆発とオブジェクトの休息
    2021-03-25 煙エフェクト違和感解消のためコルーチンを2秒に
    2021-07-22 プレイヤー側の本拠地システム

    [タスク]
  タスク1 スタート関数は使用せずに設置者から呼び出す    
  タスク2 オブジェクトの起動処理も必要
  タスク3 臨時ヒットポイント
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterManager : MonoBehaviour {
    static class Number {
        public const int Left = 0;
        public const int Center = 1;
        public const int Right = 2;
    }

    [SerializeField]
    private GameObject m_mob_prefab;

    [SerializeField]
    private AutoController[] m_auto_script = new AutoController[6];

    [SerializeField]
    private PopSystem[] m_pop_point = new PopSystem[3];

    private List<GameObject> m_mob_prefab_list = new List<GameObject>();
    private List<AssultEnemy> m_mob_prefab_script = new List<AssultEnemy>();

    private GameObject[] m_left_base = new GameObject[5];
    private GameObject[] m_center_base = new GameObject[5];
    private GameObject[] m_right_base = new GameObject[5];

    private float m_time_count = 0.0f;

    private int m_left_state = 3;
    private int m_center_state = 3;
    private int m_right_state = 3;
    private int m_attacker1_position = Number.Center;
    private int m_attacker2_position = Number.Center;
    private int[] m_character_position = new int[6];

    private short m_mob_count = 0;

    public void AttackerCommand(int a_attacker, int a_position) {
        switch (a_position) {
            case Number.Left:
                if (a_attacker == 1) {
                    m_attacker1_position = Number.Left;
                    m_auto_script[m_character_position[4]].LineChange("Left", m_left_state); // タスク3
                } else {
                    m_attacker2_position = Number.Left;
                    m_auto_script[m_character_position[5]].LineChange("Left", m_left_state); // タスク3
                }
            break;

            case Number.Center:
                if (a_attacker == 1) {
                    m_attacker1_position = Number.Center;
                    m_auto_script[m_character_position[4]].LineChange("Center", m_center_state); // タスク3
                } else {
                    m_attacker2_position = Number.Center;
                    m_auto_script[m_character_position[5]].LineChange("Center", m_center_state); // タスク3
                }
            break;

            case Number.Right:
                if (a_attacker == 1) {
                    m_attacker1_position = Number.Right;
                    m_auto_script[m_character_position[4]].LineChange("Right", m_right_state); // タスク3
                } else {
                    m_attacker2_position = Number.Right;
                    m_auto_script[m_character_position[5]].LineChange("Right", m_right_state); // タスク3
                }
            break;
        }
    }

    public void Initialize(int[] a_number) {
        Array.Copy(a_number, m_character_position, m_character_position.Length);

        m_mob_count = 30;

        string l_line = "Left";

        for (int l_loop = 0; l_loop < m_mob_count; l_loop += 1) {
            if (l_loop == m_mob_count / 3) l_line = "Center";
            if (l_loop == m_mob_count - (m_mob_count / 3)) l_line = "Right";
            var l_instance = Instantiate(m_mob_prefab, new Vector3(75.0f, 2.0f, 10.0f), Quaternion.Euler(0, 0, 0));
            l_instance.SetActive(false);
            m_mob_prefab_list.Add(l_instance);
            m_mob_prefab_script.Add(l_instance.GetComponent<AssultEnemy>());
            m_mob_prefab_script[l_loop].Initialize(l_line);
        }

        for (int l_loop = 0; l_loop < 4; l_loop += 1) {
            // タスク2
            m_left_base[l_loop] = GameObject.Find(string.Format("LeftBase{0}", l_loop));
            m_center_base[l_loop] = GameObject.Find(string.Format("CenterBase{0}", l_loop));
            m_right_base[l_loop] = GameObject.Find(string.Format("RightBase{0}", l_loop));
        }

        m_auto_script[m_character_position[0]].Initialize("Center", 3); // タスク3
        m_auto_script[m_character_position[1]].Initialize("Left", 0); // タスク3
        m_auto_script[m_character_position[2]].Initialize("Center", 0); // タスク3
        m_auto_script[m_character_position[3]].Initialize("Right", 0); // タスク3
        m_auto_script[m_character_position[4]].Initialize("Center", 1); // タスク3
        m_auto_script[m_character_position[5]].Initialize("Center", 2); // タスク3      
    }

    private void Update() {
        m_time_count += Time.deltaTime;

        if (m_time_count > 1.0f) {
            BaseCheck();
            ActiveObject();
        }
    }

    private void BaseCheck() {
        if (!m_left_base[m_left_state].activeSelf) {
            m_left_state -= 1;

            if (m_left_state < 0) m_left_state = 0;

            switch (m_left_state) {
                case 2:
                    m_pop_point[0].MoveObject(new Vector3(50.0f, 2.5f, 90.0f));
                    break;

                case 1:
                    m_pop_point[0].MoveObject(new Vector3(50.0f, 2.5f, 65.0f));
                    break;

                case 0:
                    m_pop_point[0].MoveObject(new Vector3(50.0f, 2.5f, 40.0f));
                    break;
            }

            for (int l_loop = 0; l_loop < m_mob_count / 3; l_loop += 1) {
                m_mob_prefab_script[l_loop].BaseChange(m_left_state);
            }

            m_auto_script[m_character_position[1]].BaseChange(m_left_state); // タスク3
            if (m_attacker1_position == Number.Left) m_auto_script[m_character_position[4]].BaseChange(m_left_state); // タスク3
            if (m_attacker2_position == Number.Left) m_auto_script[m_character_position[5]].BaseChange(m_left_state); // タスク3
        }

        if (!m_center_base[m_center_state].activeSelf) {
            m_center_state -= 1;

            if (m_center_state < 0) m_center_state = 0;

            switch (m_center_state) {
                case 2:
                    m_pop_point[1].MoveObject(new Vector3(75.0f, 2.5f, 90.0f));
                    break;

                case 1:
                    m_pop_point[1].MoveObject(new Vector3(75.0f, 2.5f, 65.0f));
                    break;

                case 0:
                    m_pop_point[1].MoveObject(new Vector3(75.0f, 2.5f, 40.0f));
                    break;
            }

            for (int l_loop = m_mob_count / 3; l_loop < m_mob_count - (m_mob_count / 3); l_loop += 1) {
                m_mob_prefab_script[l_loop].BaseChange(m_center_state);
            }

            m_auto_script[m_character_position[2]].BaseChange(m_center_state); // タスク3
            if (m_attacker1_position == Number.Center) m_auto_script[m_character_position[4]].BaseChange(m_center_state); // タスク3
            if (m_attacker2_position == Number.Center) m_auto_script[m_character_position[5]].BaseChange(m_center_state); // タスク3
        }

        if (!m_right_base[m_right_state].activeSelf) {
            m_right_state -= 1;

            if (m_right_state < 0) m_right_state = 0;

            switch (m_right_state) {
                case 2:
                    m_pop_point[2].MoveObject(new Vector3(110.0f, 2.5f, 90.0f));
                    break;

                case 1:
                    m_pop_point[2].MoveObject(new Vector3(110.0f, 2.5f, 65.0f));
                    break;

                case 0:
                    m_pop_point[2].MoveObject(new Vector3(110.0f, 2.5f, 40.0f));
                    break;
            }

            for (int l_loop = m_mob_count - (m_mob_count / 3); l_loop < m_mob_count; l_loop += 1) {
                m_mob_prefab_script[l_loop].BaseChange(m_right_state);
            }

            m_auto_script[m_character_position[3]].BaseChange(m_right_state); // タスク3
            if (m_attacker1_position == Number.Right) m_auto_script[m_character_position[4]].BaseChange(m_right_state); // タスク3
            if (m_attacker2_position == Number.Right) m_auto_script[m_character_position[5]].BaseChange(m_right_state); // タスク3
        }
    }

    private void ActiveObject() {
        m_time_count = 0.0f;

        for (int l_loop = 0; l_loop < m_mob_count / 3; l_loop += 1) {
            if (m_mob_prefab_list[l_loop].activeSelf == false) {
                m_mob_prefab_list[l_loop].SetActive(true);
                m_mob_prefab_script[l_loop].MobState(true);
                break;
            }
        }

        for (int l_loop = m_mob_count / 3; l_loop < m_mob_count - (m_mob_count / 3); l_loop += 1) {
            if (m_mob_prefab_list[l_loop].activeSelf == false) {
                m_mob_prefab_list[l_loop].SetActive(true);
                m_mob_prefab_script[l_loop].MobState(true);
                break;
            }
        }

        for (int l_loop = m_mob_count - (m_mob_count / 3); l_loop < m_mob_count; l_loop += 1) {
            if (m_mob_prefab_list[l_loop].activeSelf == false) {
                m_mob_prefab_list[l_loop].SetActive(true);
                m_mob_prefab_script[l_loop].MobState(true);
                break;
            }
        }
    }
}

/*
    [規則]
    p_ 外部アクセス
    m_ メンバー変数
    l_ ローカル変数
    a_ 引数

    [説明]

    [バージョン]
    2020-12-12 指定数生成及びプーリング呼び出し
    2021-03-12 管理拠点を配列化し拠点管理を行いモブに伝える
    2021-03-17 センターライン指揮官キャラの生成と名前変更(旧CenterMobManagerEnemy)
    2021-03-25 戦闘ラインを1から3ラインに変更
    2021-04-05 敵キャラクター生成位置を真ん中へ(左下では生成時にナビエリアが遠くエラーが出る)
    2021-05-19 プレイヤーからのライン変更指示を遊撃キャラクターに反映させる
    2021-05-28 スタート関数を廃止してプレイヤーコントローラーから初期化を呼ぶように変更
    2021-06-04 メニュー画面からのキャラクター配置を戦闘画面へ反映させる
    2021-06-10 戦闘エリア変更に伴ってモブの初期位置変更
    2021-06-10 川の作成に伴ってモブの沸くポイントの高さ修正
    2021-07-22 プレイヤー側の本拠地システムに対応

    [タスク]
    (済)タスク1 プレイヤーキャラのステータスを反映させる
    (済)タスク2 臨時防衛拠点数
    タスク3 メニュー画面から各ラインのキャラクターを受け取るようにする
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class DefensePlayer : MonoBehaviour{
    private const float m_serch_speed = 100.0f; // 補足1

    [SerializeField]
    private SerchObject m_serch_system;

    [SerializeField]
    private BoxCollider m_attack_area;

    private NavMeshAgent m_navmesh_agent;
    private Animator m_animator;
    private GameObject m_target_object;
    private Transform[] m_serch_object = new Transform[5];

    private float m_time_count = 0.0f;

    private int m_attack_switch = 0;
    private int m_base_state = 3;

    private bool m_serch_state = false;
    private bool m_attack_state = false;

    enum STATE {
        SET,
        SERCH,
        MOVE,
        ATTACK
    }; STATE e_state;

    public void BaseChange(int a_number) {
        m_base_state = a_number;

        m_navmesh_agent.SetDestination(m_serch_object[m_base_state].transform.position);
        m_attack_state = false;
        m_attack_area.enabled = false;
        m_animator.SetInteger("animation_attack", 0);
        m_animator.SetBool("animation_move", true);
        e_state = STATE.SET;
    }

    public void Initialize(string a_line, int a_number) {
        m_navmesh_agent = GetComponent<NavMeshAgent>();
        m_animator = GetComponent<Animator>();

        for (int l_loop = 0; l_loop < 4; l_loop += 1) {
            // タスク4
            GameObject l_object = GameObject.Find(string.Format("{0}Base{1}", a_line, l_loop));
            m_serch_object[l_loop] = l_object.transform.Find(string.Format("SetPosition{0}", a_number));
        }

        m_navmesh_agent.SetDestination(m_serch_object[m_base_state].transform.position);
        m_animator.SetBool("animation_move", true);
    }

    private void FixedUpdate() {
        switch (e_state) {
            case STATE.SET:
                if ((this.transform.position - m_serch_object[m_base_state].transform.position).sqrMagnitude < 1.0f) {
                    m_animator.SetBool("animation_move", false);
                    e_state = STATE.SERCH;
                }
            break;

            case STATE.SERCH:
                m_time_count += Time.deltaTime;

                if ((m_time_count > m_serch_speed) && (!m_serch_state)) {
                    m_navmesh_agent.ResetPath();
                    m_navmesh_agent.velocity = Vector3.zero;
                    m_animator.SetBool("animation_move", false);
                    m_serch_state = true;
                    m_serch_system.SerchStart(m_serch_object[m_base_state], "Enemy");
                }

                if ((m_time_count > m_serch_speed * 2.0f) && (m_serch_state)) {
                    m_serch_state = false;
                    m_time_count = 0.0f;
                    m_target_object = m_serch_system.SerchEnd(m_serch_object[m_base_state], 0); // タスク3

                    if (m_target_object != null) {
                        m_navmesh_agent.SetDestination(m_target_object.transform.position);
                        e_state = STATE.MOVE;
                    }
                }
            break;

            case STATE.MOVE:
                if (m_target_object.activeSelf == false) {
                    m_time_count = m_serch_speed * 2.0f;
                    m_navmesh_agent.ResetPath();
                    m_navmesh_agent.velocity = Vector3.zero;
                    m_animator.SetBool("animation_move", false);
                    m_serch_state = true;
                    m_serch_system.SerchStart(m_serch_object[m_base_state], "Enemy");
                    e_state = STATE.SERCH;
                } else if ((this.transform.position - m_target_object.transform.position).sqrMagnitude < 1.0f) {
                    if (((this.transform.position - m_serch_object[m_base_state].transform.position).sqrMagnitude) < ((m_target_object.transform.position - m_serch_object[m_base_state].transform.position).sqrMagnitude)) {
                        m_attack_switch = 2;
                        m_attack_area.tag = "Player"; // タスク5
                    } else {
                        m_attack_switch = 1;
                        m_attack_area.tag = "PlayerSkill2"; // タスク5
                    }

                    this.transform.LookAt(m_target_object.transform);
                    m_navmesh_agent.ResetPath();
                    m_navmesh_agent.velocity = Vector3.zero;
                    m_animator.SetInteger("animation_attack", m_attack_switch);
                    m_time_count = 0.0f;
                    e_state = STATE.ATTACK;
                } else {
                    m_navmesh_agent.SetDestination(m_target_object.transform.position);
                    m_animator.SetBool("animation_move", true);
                }
            break;

            case STATE.ATTACK:
                m_time_count += Time.deltaTime;

                if ((!m_attack_state) && m_time_count > 0.3f) {
                    m_time_count = 0.0f;
                    m_attack_state = true;
                    m_attack_area.enabled = true;
                }

                if ((m_attack_state) && m_time_count > 0.3f) {
                    m_attack_state = false;
                    m_attack_area.enabled = false;
                    m_animator.SetBool("animation_move", false);
                    m_animator.SetInteger("animation_attack", 0);

                    if (m_attack_switch == 1) {
                        m_navmesh_agent.SetDestination(m_serch_object[m_base_state].transform.position);
                        m_animator.SetBool("animation_move", true);
                    }

                    m_time_count = 0.0f;
                    e_state = STATE.SERCH;
                }
            break;
        }
    }
}

/*
    [規則]
    p_ 外部アクセス
    m_ メンバー変数
    l_ ローカル変数
    a_ 引数

    [説明]
    補足1 数値を下げると行動の切り替えが早くなる(強くなる)

    [バージョン]
    2021-02-25 感知エリアから一番近いオブジェクトを追いかける
    2021-03-03 2種類の攻撃を追加しスタン時は拠点に下がる
    2021-03-12 防衛拠点を配列化(防衛拠点の移行はしない)
    2021-03-17 防衛拠点変更機能を追加
    2021-03-25 攻撃判定が残る不具合を修正
    2021-07-22 プレイヤー側の本拠地システムに対応

    [タスク]
    (済)タスク1 初期化は指揮官キャラが呼び出す
    (済)タスク2 臨時の動作(対象に向かって歩くだけ)
    タスク3 攻撃キャラは自身から近い対象にする分岐を入れる
    タスク4 臨時防衛拠点数
    タスク5 臨時タグ
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AutoController : MonoBehaviour {
    [SerializeField]
    private GameObject m_defense_player;

    private List<GameObject> m_defense_prefab_list = new List<GameObject>();
    private List<DefensePlayer> m_defense_prefab_script = new List<DefensePlayer>();

    private Transform[] m_serch_object = new Transform[5];

    private NavMeshAgent m_navmesh_agent;
    private Animator m_animator;

    private int m_base_state = 3;
    private int m_attacker = 0;

    enum STATE {
        SET,
        SERCH,
    }; STATE e_state;

    public void BaseChange(int a_number) {
        m_base_state = a_number;

        if (m_attacker == 0) {
            for (int l_loop = 0; l_loop < 3; l_loop += 1) {
                m_defense_prefab_script[l_loop].BaseChange(m_base_state);
            }
        }

        m_navmesh_agent.SetDestination(m_serch_object[m_base_state].transform.position);
        m_animator.SetBool("animation_move", true);
        e_state = STATE.SET;
    }

    public void LineChange(string a_line, int a_number) {
        m_base_state = a_number;

        for (int l_loop = 0; l_loop <= a_number; l_loop += 1) {
            GameObject l_object = GameObject.Find(string.Format("{0}Base{1}", a_line, l_loop));
            m_serch_object[l_loop] = l_object.transform.Find(string.Format("SetPositionA{0}", m_attacker));
        }

        m_navmesh_agent.SetDestination(m_serch_object[m_base_state].transform.position);
        m_animator.SetBool("animation_move", true);
        e_state = STATE.SET;
    }

    public void Initialize(string a_line, int a_attacker) {
        m_navmesh_agent = GetComponent<NavMeshAgent>();
        m_animator = GetComponent<Animator>();
        m_attacker = a_attacker;

        if (m_attacker == 3) return;

        if (m_attacker == 0) {
            for (int l_loop = 0; l_loop < 3; l_loop += 1) {
                //var l_instance = Instantiate(m_defense_player, this.transform);
                var l_instance = Instantiate(m_defense_player, new Vector3(this.transform.position.x + (l_loop - 1), this.transform.position.y, this.transform.position.z + 1), Quaternion.identity);
                m_defense_prefab_list.Add(l_instance);
                m_defense_prefab_script.Add(l_instance.GetComponent<DefensePlayer>());
                m_defense_prefab_script[l_loop].Initialize(a_line, l_loop);
            }
        }

        for (int l_loop = 0; l_loop < 4; l_loop += 1) {
            GameObject l_object = GameObject.Find(string.Format("{0}Base{1}", a_line, l_loop));

            if (m_attacker == 0) {
                m_serch_object[l_loop] = l_object.transform.Find("SetPositionL");
            } else {
                m_serch_object[l_loop] = l_object.transform.Find(string.Format("SetPositionA{0}", m_attacker));
            }
        }

        m_navmesh_agent.SetDestination(m_serch_object[m_base_state].transform.position);
        m_animator.SetBool("animation_move", true);
    }

    private void FixedUpdate() {
        switch (e_state) {
            case STATE.SET:
                if (m_attacker == 3) return;

                if ((this.transform.position - m_serch_object[m_base_state].transform.position).sqrMagnitude < 1.0f) {
                    m_animator.SetBool("animation_move", false);
                    e_state = STATE.SERCH;
                }
                break;

            case STATE.SERCH:

                break;
        }
    }
}

/*
    [規則]
    p_ 外部アクセス
    m_ メンバー変数
    l_ ローカル変数
    a_ 引数

    [説明]

    [バージョン]
    2021-06-04 キャラクターごとに分けていたスクリプトを1つに
    2021-07-01 護衛キャラクターをキャラクターの子で生成しないように
    2021-07-22 プレイヤー側の本拠地システムに対応

    [タスク]
    
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
using TMPro;
using Febucci.UI;

public class AssultEnemy : MonoBehaviour {
    private const int m_const_text_array = 10;

    [SerializeField]
    private Canvas m_hitpoint_canvas;

    [SerializeField]
    private Slider m_hitpoint_bar;

    [SerializeField]
    private TextAnimatorPlayer m_damage_text;

    [SerializeField]
    private ParticleSystem m_bomb_effect;

    [SerializeField]
    private ParticleSystem m_stun_effect;

    private List<TextAnimatorPlayer> m_text_animator = new List<TextAnimatorPlayer>();

    private GameObject m_script_manager;
    private GameObject m_pop_point;
    private GameObject[] m_end_point = new GameObject[5];

    private ColliderManager m_collider_manager;

    private NavMeshAgent m_navmesh_agent;

    private Rigidbody m_rigidbody;

    private Animator m_animator;

    private BaseSystem[] m_base_system = new BaseSystem[5];

    private Vector3 m_impact_vector;
    private Vector3 m_impact_power;

    private float m_time_count = 0.0f;
    private float m_hitpoint = 0.0f;
    private float m_damagepoint = 0.0f;
    private float m_stun_set = 0.0f;
    private float m_stun_use = 0.0f;

    private int m_transition = 0;
    private int m_iskinematic_change_frame = 0;
    private int m_text_array = 0;
    private int m_base_state = 3;

    enum STATE {
        WAIT,
        MOVE,
        DIE,
        BOMB,
        STUN,
        IMPACT
    }; STATE e_state;

    public void Initialize(string a_line) {
        for (int l_loop = 0; l_loop < m_const_text_array; l_loop += 1) {
            var l_create = m_hitpoint_canvas.transform;
            var l_object = Instantiate(m_damage_text, Vector3.zero, Quaternion.identity, l_create);
            m_text_animator.Add(l_object.GetComponent<TextAnimatorPlayer>());
        }

        m_hitpoint_canvas.worldCamera = Camera.main;

        m_script_manager = GameObject.Find("ScriptManager");
        m_collider_manager = m_script_manager.GetComponent<ColliderManager>();

        m_navmesh_agent = GetComponent<NavMeshAgent>();
        m_rigidbody = GetComponent<Rigidbody>();
        m_animator = GetComponent<Animator>();
        m_pop_point = GameObject.Find(string.Format("{0}Pop", a_line));

        for (int l_loop = 0; l_loop < 4; l_loop += 1) {
            // タスク5
            m_end_point[l_loop] = GameObject.Find(string.Format("{0}Base{1}", a_line, l_loop));
            m_base_system[l_loop] = m_end_point[l_loop].GetComponent<BaseSystem>();
        }

        m_hitpoint_bar.maxValue = 1000;
    }

    public void MobState(bool a_bool) {
        if (a_bool) {
            m_hitpoint_bar.value = m_hitpoint = m_hitpoint_bar.maxValue;
            this.gameObject.transform.position = m_pop_point.transform.position; // タスク3
            m_navmesh_agent.enabled = true; // 補足1
            m_time_count = 0.0f;
            e_state = STATE.WAIT;
        } else {
            m_navmesh_agent.enabled = false; // 補足1
            this.gameObject.SetActive(false);
        }
    }

    public void BaseChange(int a_number) {
        m_base_state = a_number;
    }

    private void FixedUpdate() {
        m_hitpoint_canvas.transform.rotation = m_hitpoint_canvas.worldCamera.transform.rotation;

        switch (e_state) {
            case STATE.WAIT:
                m_time_count += Time.deltaTime;

                if (m_time_count > 1.0f) {
                    m_animator.SetBool("animation_move", true);
                    m_navmesh_agent.SetDestination(m_end_point[m_base_state].transform.position);
                    e_state = STATE.MOVE;
                }
                break;

            case STATE.MOVE:
                m_time_count += Time.deltaTime;

                if (m_time_count > 1.0f) m_navmesh_agent.SetDestination(m_end_point[m_base_state].transform.position);
                break;

            case STATE.DIE:
                m_time_count += Time.deltaTime;

                if (m_time_count > 2.0f) {
                    m_animator.SetBool("animation_die", false);
                    MobState(false);
                }
                break;

            case STATE.BOMB:
                m_time_count += Time.deltaTime;

                if (m_time_count > 0.3f) MobState(false);
                break;

            case STATE.STUN:
                m_time_count += Time.deltaTime;

                if (m_time_count > m_stun_use) {
                    m_time_count = 0.0f;
                    m_stun_effect.Stop();
                    m_navmesh_agent.enabled = true;
                    m_rigidbody.isKinematic = true;
                    m_navmesh_agent.SetDestination(m_end_point[m_base_state].transform.position);
                    e_state = STATE.MOVE;
                }
                break;

            case STATE.IMPACT:
                if (m_transition == 0) {
                    m_transition = 1;
                    m_iskinematic_change_frame = Time.frameCount; // 補足2
                    m_navmesh_agent.enabled = false;
                    m_rigidbody.isKinematic = false;
                    m_rigidbody.AddForce(m_impact_power, ForceMode.Impulse);
                }

                if ((m_transition == 1) && (m_rigidbody.IsSleeping())) {
                    m_transition = 0;
                    m_iskinematic_change_frame = Time.frameCount; // 補足2
                    m_navmesh_agent.enabled = true;
                    m_rigidbody.isKinematic = true;
                    m_navmesh_agent.SetDestination(m_end_point[m_base_state].transform.position);
                    e_state = STATE.MOVE;
                }
                break;
        }
    }

    private void OnTriggerEnter(Collider a_collider) {
        if ((e_state == STATE.BOMB) || (e_state == STATE.WAIT) || (e_state == STATE.DIE) || (m_iskinematic_change_frame == Time.frameCount)) return; // 補足2

        if (a_collider.CompareTag("Finish")) { // タスク4
            m_bomb_effect.Play();

            if(m_base_state == 0) {
                m_base_system[m_base_state].MainBaseDamage(m_hitpoint_bar.value);
            } else {
                m_base_system[m_base_state].BaseDamage(m_hitpoint_bar.value);
            }
            
            m_navmesh_agent.enabled = true;
            m_rigidbody.isKinematic = true;
            m_navmesh_agent.ResetPath();
            m_time_count = 0.0f;
            e_state = STATE.BOMB;
            return;
        }

        m_collider_manager.ColliderDataInput(a_collider, this.gameObject, ref m_impact_vector, ref m_damagepoint, ref m_stun_set);

        if (m_damagepoint != 0) {
            DamageTextShow((int)m_damagepoint);
            m_hitpoint -= (int)m_damagepoint;
            if (m_hitpoint < 0) m_hitpoint = 0;
            m_hitpoint_bar.value = m_hitpoint;
        }

        if (m_hitpoint == 0) { // タスク2
            m_navmesh_agent.enabled = true;
            m_rigidbody.isKinematic = true;
            m_navmesh_agent.ResetPath();
            m_animator.SetBool("animation_move", false);
            m_animator.SetBool("animation_die", true);
            m_time_count = 0.0f;
            e_state = STATE.DIE;
            return;
        }

        if (m_impact_vector != Vector3.zero) {
            m_impact_power = m_impact_vector; // 補足3
            m_transition = 0;
            m_stun_effect.Stop();
            e_state = STATE.IMPACT;
            return;
        }

        if ((m_stun_set != 0.0f) && (e_state != STATE.IMPACT)) {
            m_stun_use = m_stun_set;
            m_navmesh_agent.enabled = true;
            m_rigidbody.isKinematic = true;
            m_navmesh_agent.ResetPath();
            m_stun_effect.Play();
            m_time_count = 0.0f;
            e_state = STATE.STUN;
            return;
        }
    }

    private void DamageTextShow(float a_damage) {
        // タスク6
        m_text_animator[m_text_array].gameObject.transform.localPosition = new Vector3(Random.Range(-0.1f, 0.1f), Random.Range(0.9f, 1.1f), 0f);
        m_text_animator[m_text_array++].ShowText(string.Format("<FADE><BOUNCE>{0}</BOUNCE></FADE>", a_damage));

        if (m_const_text_array <= m_text_array) m_text_array = 0;
    }
}

/*
    [規則]
    p_ 外部アクセス
    m_ メンバー変数
    l_ ローカル変数
    a_ 引数

    [説明]
    補足1 真だと座標移動時に障害物が干渉して初期位置にズレが発生する
    補足2 IsKinematic切り替え時と同フレームのコライダー判定を無視する
    補足3 コライダー内で取得した吹き飛ばし変数をそのまま使用すると吹き飛ぶ前に0で上書きする事が有るため実行用変数を用意

    [バージョン]
    2020-12-12 プーリングとナビゲーション
    2021-01-03 アセットストアのモデル反映とアニメーション
    2021-01-28 吹き飛ばし機能の実装
    2021-02-03 体力システムの実装及びゲージとダメージの可視化
    2021-02-19 味方拠点へのダメージ及び自身の休息
    2021-03-03 スタン動作の追加
    2021-03-12 標的の拠点の変更機能
    2021-03-17 拠点数と沸き拠点移動対応と名前の変更(旧CenterMobEnemy)
    2021-07-22 プレイヤー側の本拠地システムに対応

    [タスク]
    (済)タスク1 拠点の占拠状態で分岐が必要
    (済)タスク2 プーリング動作確認用のため消去する
    (済)タスク3 沸く場所全ての設定が必要
    タスク4 今後タグを拠点に統一させる
    タスク5 臨時防衛拠点数
    タスク6 コルーチンを使用したオブジェクトプーリングに変更する
*/

本拠地の仕様

敵と味方共に3レーンに分かれていますが本拠地は全ての味方と敵が攻防するように見せかけています。

従来の味方基地を各レーン1つ増やし増やした拠点は本拠地の付近に透明で置いています。


public void MainBaseDamage(float a_damage) {
    m_main_base.BaseDamage(a_damage);
}

敵が最終拠点を狙っている場合に限り、どのレーンの敵が目的地に到着しても本拠地へのダメージとなるようにしています。

今回は本拠地付近に目的地を1つにまとめていますが、マップによっては3つの入口がある本拠地でどの入口に到達されても本拠地の体力を減らす仕組みとしても使用する事が出来ます


動画


記事のリンク

前回の記事

次回の記事

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です