Beispiel #1
0
    void Start()
    {
        statisticsModule = new Statistics();
        AIInputHandler   = new AIInputHandler(speedUpDown);

        UnityThread.initUnityThread();

        InitializeNetworking();
    }
Beispiel #2
0
 private void UseAI()
 {
     if (_aiController != null)
     {
         _aiController.Restart();
     }
     else
     {
         _aiController = Instantiate(Resources.Load <AIInputHandler>("AIController"), this.transform.parent);
         _inputSourceController.SetSingleSource(_aiController);
     }
 }
    public override void _Ready()
    {
        _xInicial  = Position.x;
        _xOriginal = _xInicial;
        _textLabel = GetNode(TextPath) as RichTextLabel;
        _input     = new AIInputHandler <InputActions>();
        _fsm       = new FiniteStateMachine <PlayerState, Malo>(equalizer: (current, captured) => current == captured)
        {
            InitialState = PlayerState.OnAir
        };
        _enemyState = new FiniteStateMachine <EnemyState, Malo>(equalizer: (current, captured) => current == captured)
        {
            InitialState = EnemyState.MoviendoDerecha
        };
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (!IsOnFloor() && !IsOnWall())
            {
                GlobalVelocity += GravityVector;
                return(PlayerState.OnAir);
            }
            return(IsOnFloor() && !IsOnWall() ? PlayerState.OnGround : current);
        });
        _fsm.Add(PlayerState.OnGround, (current, player) =>
        {
            if (_input.IsActionPressed(InputActions.Jump))
            {
                var jump       = new Vector2(GlobalVelocity.x, -JumpForce);
                GlobalVelocity = jump;
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnGround, (current, player) =>
        {
            var horizontal = Vector2.Zero;
            if (_input.IsActionPressed(InputActions.MoveLeft))
            {
                horizontal = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
            }
            if (_input.IsActionPressed(InputActions.MoveRight))
            {
                horizontal = new Vector2((HorizontalSpeed), GlobalVelocity.y);
            }
            GlobalVelocity = horizontal;
            return(current);
        });
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (_wallTime > 0)
            {
                return(current);
            }
            var horizontal = Vector2.Zero;
            if (_input.IsActionPressed(InputActions.MoveLeft))
            {
                horizontal     = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
                GlobalVelocity = horizontal;
            }
            if (_input.IsActionPressed(InputActions.MoveRight))
            {
                horizontal     = new Vector2((HorizontalSpeed), GlobalVelocity.y);
                GlobalVelocity = horizontal;
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (IsOnFloor())
            {
                GlobalVelocity.x = 0;
                return(PlayerState.OnGround);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (IsOnWall())
            {
                GlobalVelocity = GravityVector;
                var isLef      = _getCollisionNormal() == Vector2.Left;
                return(isLef ? PlayerState.OnRightWall : PlayerState.OnLeftWall);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnLeftWall, (current, delta) => IsOnFloor() ? PlayerState.OnGround : current);
        _fsm.Add(PlayerState.OnRightWall, (current, delta) => IsOnFloor() ? PlayerState.OnGround : current);
        _fsm.Add(PlayerState.OnLeftWall, (current, delta) =>
        {
            if (_input.IsActionPressed(InputActions.MoveRight))
            {
                var jump       = new Vector2(HorizontalSpeed, -JumpForce);
                GlobalVelocity = jump;
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnRightWall, (current, player) =>
        {
            if (_input.IsActionPressed(InputActions.MoveLeft))
            {
                var jump       = new Vector2(HorizontalSpeed * -1, -JumpForce);
                GlobalVelocity = jump;
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnLeftWall, (current, player) =>
        {
            if (_input.IsActionJustPressed(InputActions.Jump))
            {
                var jump       = new Vector2(WallJumpHorizontalForce, -JumpForce);
                GlobalVelocity = jump;
                _wallTime      = WallTime;
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnRightWall, (current, player) =>
        {
            if (_input.IsActionJustPressed(InputActions.Jump))
            {
                var jump       = new Vector2(WallJumpHorizontalForce * -1, -JumpForce);
                GlobalVelocity = jump;
                _wallTime      = WallTime;
                return(PlayerState.OnAir);
            }
            return(current);
        });

        _enemyState.Add(EnemyState.MoviendoDerecha, (current, enemy) =>
        {
            _estadoAnterior = current;
            Player jugador  = DetectarYPerseguir();

            if (_caminarHasta != 0)
            {
                HorizontalSpeed = _velocidadNormal * 4;
            }
            if (Mathf.Abs(Position.x - _caminarHasta) <= 10)
            {
                HorizontalSpeed = _velocidadNormal;

                if (_caminarHasta != 0)
                {
                    _caminarHasta = 0;
                    _xInicial     = Position.x;
                    _tiempoAlerta = 30;
                    return(EnemyState.AlertaDerecha);
                }
            }

            if (jugador != null && Mathf.Abs(Position.x - jugador.Position.x) <= 50)
            {
                _input.SetActionReleased(InputActions.MoveRight);
                return(EnemyState.Atacando);
            }

            if (IsOnWall())
            {
                _input.SetActionPressed(InputActions.MoveLeft);
                _input.SetActionReleased(InputActions.MoveRight);
                return(EnemyState.MoviendoIzquierda);
            }
            _input.SetActionPressed(InputActions.MoveRight);
            _input.SetActionReleased(InputActions.MoveLeft);

            var bodies = GetNode <Area2D>("Area2D2")?.GetOverlappingBodies() ?? new Godot.Collections.Array();

            foreach (var body in bodies)
            {
                jugador = body as Player;
                if (jugador != null && !jugador.Cubierto)
                {
                    GD.Print($"{DateTime.Now.ToString("G")}");
                    //_input.SetActionPressed(InputActions.MoveRight);
                    //_input.SetActionReleased(InputActions.MoveLeft);
                    return(EnemyState.MoviendoIzquierda);
                }
            }
            return(current);
        });

        _enemyState.Add(EnemyState.MoviendoIzquierda, (current, enemy) =>
        {
            _estadoAnterior = current;
            Player jugador  = DetectarYPerseguir();

            if (_caminarHasta != 0)
            {
                HorizontalSpeed = _velocidadNormal * 4;
            }
            if (Mathf.Abs(Position.x - _caminarHasta) <= 10)
            {
                HorizontalSpeed = _velocidadNormal;

                if (_caminarHasta != 0)
                {
                    _caminarHasta = 0;
                    _xInicial     = Position.x;
                    _tiempoAlerta = 30;
                    return(EnemyState.AlertaIzquierda);
                }
            }

            if (jugador != null && Mathf.Abs(Position.x - jugador.Position.x) <= 50)
            {
                _input.SetActionReleased(InputActions.MoveLeft);
                return(EnemyState.Atacando);
            }

            if (IsOnWall())
            {
                _input.SetActionPressed(InputActions.MoveRight);
                _input.SetActionReleased(InputActions.MoveLeft);
                return(EnemyState.MoviendoDerecha);
            }
            _input.SetActionPressed(InputActions.MoveLeft);
            _input.SetActionReleased(InputActions.MoveRight);

            var bodies = GetNode <Area2D>("Area2D2")?.GetOverlappingBodies() ?? new Godot.Collections.Array();

            foreach (var body in bodies)
            {
                jugador = body as Player;
                if (jugador != null && !jugador.Cubierto)
                {
                    GD.Print($"{DateTime.Now.ToString("G")}");
                    //_input.SetActionPressed(InputActions.MoveLeft);
                    //_input.SetActionReleased(InputActions.MoveRight);
                    return(EnemyState.MoviendoDerecha);
                }
            }
            return(current);
        });

        _enemyState.Add(EnemyState.AlertaIzquierda, (current, enemy) =>
        {
            _ = DetectarYPerseguir();

            var bodies = GetNode <Area2D>("Area2D2")?.GetOverlappingBodies() ?? new Godot.Collections.Array();

            foreach (var body in bodies)
            {
                if (body is Player jugador && !jugador.Cubierto)
                {
                    GD.Print($"{DateTime.Now:G}");
                    _input.SetActionPressed(InputActions.MoveRight);
                    _input.SetActionReleased(InputActions.MoveLeft);
                    return(EnemyState.AlertaDerecha);
                }
            }
Beispiel #4
0
 public override void _Ready()
 {
     _textLabel = GetNode(TextPath) as RichTextLabel;
     _input     = new AIInputHandler <InputActions>();
     _fsm       = new FiniteStateMachine <PlayerState, Malo>(equalizer: (current, captured) => current == captured)
     {
         InitialState = PlayerState.OnAir
     };
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (!IsOnFloor() && !IsOnWall())
         {
             GlobalVelocity += GravityVector;
             return(PlayerState.OnAir);
         }
         return(IsOnFloor() && !IsOnWall() ? PlayerState.OnGround : current);
     });
     _fsm.Add(PlayerState.OnGround, (current, player) =>
     {
         if (_input.IsActionPressed(InputActions.Jump))
         {
             var jump       = new Vector2(GlobalVelocity.x, -JumpForce);
             GlobalVelocity = jump;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnGround, (current, player) =>
     {
         var horizontal = Vector2.Zero;
         if (_input.IsActionPressed(InputActions.MoveLeft))
         {
             horizontal = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
         }
         if (_input.IsActionPressed(InputActions.MoveRight))
         {
             horizontal = new Vector2((HorizontalSpeed), GlobalVelocity.y);
         }
         GlobalVelocity = horizontal;
         return(current);
     });
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (_wallTime > 0)
         {
             return(current);
         }
         var horizontal = Vector2.Zero;
         if (_input.IsActionPressed(InputActions.MoveLeft))
         {
             horizontal     = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
             GlobalVelocity = horizontal;
         }
         if (_input.IsActionPressed(InputActions.MoveRight))
         {
             horizontal     = new Vector2((HorizontalSpeed), GlobalVelocity.y);
             GlobalVelocity = horizontal;
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (IsOnFloor())
         {
             GlobalVelocity.x = 0;
             return(PlayerState.OnGround);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (IsOnWall())
         {
             GlobalVelocity = GravityVector;
             var isLef      = _getCollisionNormal() == Vector2.Left;
             return(isLef ? PlayerState.OnRightWall : PlayerState.OnLeftWall);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnLeftWall, (current, delta) => IsOnFloor() ? PlayerState.OnGround : current);
     _fsm.Add(PlayerState.OnRightWall, (current, delta) => IsOnFloor() ? PlayerState.OnGround : current);
     _fsm.Add(PlayerState.OnLeftWall, (current, delta) =>
     {
         if (_input.IsActionPressed(InputActions.MoveRight))
         {
             var jump       = new Vector2(HorizontalSpeed, -JumpForce);
             GlobalVelocity = jump;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnRightWall, (current, player) =>
     {
         if (_input.IsActionPressed(InputActions.MoveLeft))
         {
             var jump       = new Vector2(HorizontalSpeed * -1, -JumpForce);
             GlobalVelocity = jump;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnLeftWall, (current, player) =>
     {
         if (_input.IsActionJustPressed(InputActions.Jump))
         {
             var jump       = new Vector2(WallJumpHorizontalForce, -JumpForce);
             GlobalVelocity = jump;
             _wallTime      = WallTime;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnRightWall, (current, player) =>
     {
         if (_input.IsActionJustPressed(InputActions.Jump))
         {
             var jump       = new Vector2(WallJumpHorizontalForce * -1, -JumpForce);
             GlobalVelocity = jump;
             _wallTime      = WallTime;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _bt = Bt.Root(
         _bt.Function(x => {
         if (!x.IsOnWall() || x.IsOnWall() && x._getCollisionNormal() == Vector2.Left)
         {
             x._input.SetActionPressed(InputActions.MoveLeft);
             return(Node <Malo> .Status.Prossess);
         }
         else
         {
             x._input.SetActionReleased(InputActions.MoveLeft);
             return(Node <Malo> .Status.Success);
         }
     }),
         _bt.Wait(5f),
         _bt.Function(x => {
         if (!x.IsOnWall() || x.IsOnWall() && x._getCollisionNormal() == Vector2.Right)
         {
             x._input.SetActionPressed(InputActions.MoveRight);
             return(Node <Malo> .Status.Prossess);
         }
         else
         {
             x._input.SetActionReleased(InputActions.MoveRight);
             return(Node <Malo> .Status.Success);
         }
     }),
         _bt.Wait(5f)
         );
 }