Exemple #1
0
    private void GetInput()
    {
//        horizontalInput = Input.GetAxis("Horizontal");
//        verticalInput = Input.GetAxis("Vertical");
//        breaking = Input.GetButton("Brake");
        if (input != null)
        {
            CarInputs inputs = input.GetInputs(this);
            horizontalInput = inputs.horizontal;
            verticalInput   = inputs.vertical;
            braking         = inputs.braking;

//            input.GetInputs(this);
        }
    }
    public override CarInputs GetInputs(CarController cc)
    {
        CarInputs inputs = new CarInputs
        {
            horizontal = horizontalInput.useCont ? horizontalInput.InputConstant : Input.GetAxis(horizontalInput.InputName),

            vertical = verticalInput.useCont ? verticalInput.InputConstant : Input.GetAxis(verticalInput.InputName)
        };

        if (breakInput.useCont)
        {
            inputs.braking = breakInput.InputConstant >= 1;
        }
        else
        {
            inputs.braking = Input.GetButton(breakInput.InputName);
        }

        return(inputs);
    }
Exemple #3
0
        protected override void OnUpdate()
        {
            var left       = false;
            var right      = false;
            var reverse    = false;
            var accelerate = false;

            var Input = World.GetExistingSystem <InputSystem>();

            if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
            {
                reverse = true;
            }

            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
            {
                accelerate = true;
            }

            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
            {
                left = true;
            }

            if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
            {
                right = true;
            }

            if (HasSingleton <UIGameControls>())
            {
                var UIGameControls = GetSingleton <UIGameControls>();

                Entities.ForEach((Entity e, in UIState state) =>
                {
                    if (state.IsPressed)
                    {
                        if (e == UIGameControls.ButtonAccelerate)
                        {
                            accelerate = true;
                        }

                        if (e == UIGameControls.ButtonReverse)
                        {
                            reverse = true;
                        }

                        if (e == UIGameControls.ButtonLeft)
                        {
                            left = true;
                        }

                        if (e == UIGameControls.ButtonRight)
                        {
                            right = true;
                        }
                    }
                }).WithStructuralChanges().Run();

                CarInputs carInputs = default;

                if (accelerate)
                {
                    carInputs.AccelerationAxis = 1f;
                }
                else if (reverse)
                {
                    carInputs.AccelerationAxis = -1f;
                }

                if (left)
                {
                    carInputs.HorizontalAxis = -1f;
                }
                else if (right)
                {
                    carInputs.HorizontalAxis = 1f;
                }

                if (HasSingleton <CarAccelerometerSteering>())
                {
                    var carSteering = GetSingleton <CarAccelerometerSteering>();
                    if (carSteering.State == SensorState.Available && carSteering.HorizontalAxis != 0.0f)
                    {
                        carInputs.HorizontalAxis = carSteering.HorizontalAxis;
                    }
                }

                Entities.WithAll <Player>().ForEach((ref CarInputs ci) => { ci = carInputs; }).Run();
            }
        }
        protected override void OnUpdate()
        {
            var left       = false;
            var right      = false;
            var reverse    = false;
            var accelerate = false;

#if UNITY_DOTSPLAYER
            var Input = World.GetExistingSystem <InputSystem>();
#endif

            if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
            {
                reverse = true;
            }
            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
            {
                accelerate = true;
            }

            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
            {
                left = true;
            }
            if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
            {
                right = true;
            }

#if !UNITY_DOTSPLAYER
            if (Input.GetMouseButton(0))
            {
                PressAtPosition(new float2(Input.mousePosition.x, Input.mousePosition.y), ref left, ref right, ref reverse, ref accelerate);
            }

            for (int i = 0; i < Input.touchCount; i++)
            {
                var pos = Input.GetTouch(i).position;
                PressAtPosition(new float2(pos.x, pos.y), ref left, ref right, ref reverse, ref accelerate);
            }
#else
            if (Input.IsTouchSupported() && Input.TouchCount() > 0)
            {
                for (var i = 0; i < Input.TouchCount(); i++)
                {
                    var itouch = Input.GetTouch(i);
                    var pos    = new float2(itouch.x, itouch.y);
                    PressAtPosition(pos, ref left, ref right, ref reverse, ref accelerate);
                }
            }
            else
            {
                if (Input.GetMouseButton(0))
                {
                    var xpos = (int)Input.GetInputPosition().x;
                    PressAtPosition(Input.GetInputPosition(), ref left, ref right, ref reverse, ref accelerate);
                }
            }
#endif

            CarInputs inputs = default;
            if (left)
            {
                inputs.HorizontalAxis = -1f;
            }
            else if (right)
            {
                inputs.HorizontalAxis = 1f;
            }

            if (accelerate)
            {
                inputs.AccelerationAxis = 1f;
            }
            else if (reverse)
            {
                inputs.AccelerationAxis = -1f;
            }

            Entities.WithNone <AI>().ForEach((ref CarInputs iv) => { iv = inputs; });
        }