Exemple #1
0
    // Use this for initialization
    void Start()
    {
        state = SkillBridge.Message.CharacterState.Idle;
        if (this.character == null)
        {
            DataManager.Instance.Load();
            NCharacterInfo cinfo = new NCharacterInfo();
            cinfo.Id                 = 1;
            cinfo.Name               = "Test";
            cinfo.ConfigId           = 1;
            cinfo.Entity             = new NEntity();
            cinfo.Entity.Position    = new NVector3();
            cinfo.Entity.Direction   = new NVector3();
            cinfo.Entity.Direction.X = 0;
            cinfo.Entity.Direction.Y = 100;
            cinfo.Entity.Direction.Z = 0;
            cinfo.attrDynamic        = new NAttributeDynamic();
            this.character           = new Character(cinfo);

            if (entityController != null)
            {
                entityController.entity = this.character;
            }
        }
        if (agent == null)
        {
            agent = this.gameObject.AddComponent <NavMeshAgent>();
            agent.stoppingDistance = 0.3f;
            agent.updatePosition   = false;//---
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        //get and init fields
        mState            = CharacterState.Idle;
        mRigidbody        = GetComponentInChildren <Rigidbody>();
        mEntityController = GetComponent <EntityController>();

        //if mcharacter hasnt been assigned value, load data and create a temp cha
        if (character == null)
        {
            DataManager.Instance.LoadData();
            NCharacterInfo cinfo = new NCharacterInfo();
            cinfo.Id                 = 1;
            cinfo.Name               = "Test";
            cinfo.Tid                = 1;
            cinfo.Entity             = new NEntity();
            cinfo.Entity.Position    = new NVector3();
            cinfo.Entity.Direction   = new NVector3();
            cinfo.Entity.Direction.X = 0;
            cinfo.Entity.Direction.Y = 100;
            cinfo.Entity.Direction.Z = 0;
            character                = new Character(cinfo);
        }

        mLastPos = mRigidbody.transform.position;
    }
 public void StopNav()
 {
     autoNav = false;
     agent.ResetPath();
     if (state != SkillBridge.Message.CharacterState.Idle)
     {
         state            = SkillBridge.Message.CharacterState.Idle;
         this.rb.velocity = Vector3.zero;
         this.character.Stop();
         this.SendEntityEvent(EntityEvent.Idle);
     }
     NavPathRenderer.Instance.SetPath(null, Vector3.zero);
 }
    IEnumerator  BeginNav(Vector3 target)
    {
        agent.SetDestination(target);
        yield return(null);

        autoNav = true;
        if (state != SkillBridge.Message.CharacterState.Move)
        {
            state = SkillBridge.Message.CharacterState.Move;
            this.character.MoveForward();
            this.SendEntityEvent(EntityEvent.MoveFwd);
            agent.speed = this.character.speed * 0.01f;
        }
    }
    // Use this for initialization
    void Start()
    {
        state = SkillBridge.Message.CharacterState.Idle;
        if (this.character == null)
        {
            DataManager.Instance.Load();
            NCharacterInfo cinfo = new NCharacterInfo();
            cinfo.Id                 = 1;
            cinfo.Name               = "Test";
            cinfo.Tid                = 1;
            cinfo.Entity             = new NEntity();
            cinfo.Entity.Position    = new NVector3();
            cinfo.Entity.Direction   = new NVector3();
            cinfo.Entity.Direction.X = 0;
            cinfo.Entity.Direction.Y = 100;
            cinfo.Entity.Direction.Z = 0;
            this.character           = new Character(cinfo);

            if (entityController != null)
            {
                entityController.entity = this.character;
            }
        }
    }
    void FixedUpdate()
    {
        if (character == null)
        {
            return;
        }


        float v = Input.GetAxis("Vertical");

        if (v > 0.01)
        {
            if (state != SkillBridge.Message.CharacterState.Move)
            {
                state = SkillBridge.Message.CharacterState.Move;
                this.character.MoveForward();
                this.SendEntityEvent(EntityEvent.MoveFwd);
            }
            this.rb.velocity = this.rb.velocity.y * Vector3.up + GameObjectTool.LogicToWorld(character.direction) * (this.character.speed + 9.81f) / 100f;
        }
        else if (v < -0.01)
        {
            if (state != SkillBridge.Message.CharacterState.Move)
            {
                state = SkillBridge.Message.CharacterState.Move;
                this.character.MoveBack();
                this.SendEntityEvent(EntityEvent.MoveBack);
            }
            this.rb.velocity = this.rb.velocity.y * Vector3.up + GameObjectTool.LogicToWorld(character.direction) * (this.character.speed + 9.81f) / 100f;
        }
        else
        {
            if (state != SkillBridge.Message.CharacterState.Idle)
            {
                state            = SkillBridge.Message.CharacterState.Idle;
                this.rb.velocity = Vector3.zero;
                this.character.Stop();
                this.SendEntityEvent(EntityEvent.Idle);
            }
        }

        if (Input.GetButtonDown("Jump"))
        {
            this.SendEntityEvent(EntityEvent.Jump);
        }

        float h = Input.GetAxis("Horizontal");

        if (h < -0.1 || h > 0.1)
        {
            this.transform.Rotate(0, h * rotateSpeed, 0);
            Vector3    dir = GameObjectTool.LogicToWorld(character.direction);
            Quaternion rot = new Quaternion();
            rot.SetFromToRotation(dir, this.transform.forward);

            if (rot.eulerAngles.y > this.turnAngle && rot.eulerAngles.y < (360 - this.turnAngle))
            {
                character.SetDirection(GameObjectTool.WorldToLogic(this.transform.forward));
                rb.transform.forward = this.transform.forward;
                this.SendEntityEvent(EntityEvent.None);
            }
        }
        //Debug.LogFormat("velocity {0}", this.rb.velocity.magnitude);
    }
    /// <summary>
    /// use fixed update to deal with user inputs
    /// When receive input, update 3 things: (1)Character data, (2)rigidbody, (3)animator
    /// </summary>
    void Update()
    {
        //if controller hasnt been assigned with a character, dont read user input
        if (character == null)
        {
            return;
        }

        //get user input on vertical axis
        float v = Input.GetAxis("Vertical");

        //if user wants to move forward
        if (v > 0.01f)
        {
            //send synchronization message only when state changes
            if (mState != SkillBridge.Message.CharacterState.Move)
            {
                mState = CharacterState.Move;
                character.MoveForward();

                SendEntityEvent(EntityEvent.MoveFwd);
            }

            mRigidbody.velocity = this.mRigidbody.velocity.y * Vector3.up + GameObjectTool.LogicToWorld(character.direction) * (character.speed + 9.81f) / 100f;
        }

        //if user wants to move backward
        else if (v < -0.01f)
        {
            if (mState != SkillBridge.Message.CharacterState.Move)
            {
                mState = CharacterState.Move;
                character.MoveBackward();

                SendEntityEvent(EntityEvent.MoveBack);
            }

            mRigidbody.velocity = this.mRigidbody.velocity.y * Vector3.up + GameObjectTool.LogicToWorld(character.direction) * (this.character.speed + 9.81f) / 100f;
        }

        //if user is not moving
        else
        {
            character.Stop();
            if (mState != SkillBridge.Message.CharacterState.Idle)
            {
                mState = CharacterState.Idle;
                mRigidbody.velocity = Vector3.zero;

                SendEntityEvent(EntityEvent.Idle);
            }
        }

        //player jump input detection
        //jump is not considered a movement, only display animation
        if (Input.GetButtonDown("Jump"))
        {
            SendEntityEvent(EntityEvent.Jump);
        }

        //horizontal input are consider as rotation
        float h = Input.GetAxis("Horizontal");

        if (h > 0.1f || h < -0.1f)
        {
            //perform rotation on gameobject
            this.transform.Rotate(0, h * mRotateSpeed, 0);
            Vector3 dir = GameObjectTool.LogicToWorld(character.direction);

            //update the data stored in Character
            Quaternion rotation = new Quaternion();
            rotation.SetFromToRotation(dir, this.transform.forward);

            //if offset is too small, do not update
            if (rotation.eulerAngles.y > this.mTurnAngle && rotation.eulerAngles.y < (360 - mTurnAngle))
            {
                character.SetDirection(GameObjectTool.WorldToLogic(this.transform.forward));
                mRigidbody.transform.forward = this.transform.forward;
                SendEntityEvent(EntityEvent.None);
            }
        }
    }