Esempio n. 1
0
        private void Joystick_Pan(Xamarin.Forms.Point point)
        {
            var newStickX = (float)(_stickX + point.X);
            var newStickY = (float)(_stickY + point.Y);

            var stickAngle = MathHelper.Angle(new SKPoint(_originX, _originY), new SKPoint(newStickX, newStickY));

            var distance = MathHelper.Distance(new SKPoint(_originX, _originY), new SKPoint(newStickX, newStickY));

            if (distance > _areaRadius * .5f)
            {
                _stickX  = (float)Math.Cos(stickAngle) * _areaRadius * .5f + _originX;
                _stickY  = (float)Math.Sin(stickAngle) * _areaRadius * .5f + _originY;
                distance = _areaRadius * .5f;
            }
            else
            {
                _stickX = newStickX;
                _stickY = newStickY;
            }

            _tiltX = (float)Math.Cos(stickAngle) * distance / (_areaRadius * .5f);
            _tiltY = (float)Math.Sin(stickAngle) * distance / (_areaRadius * .5f);
            Tilt   = new Point(_tiltX, _tiltY);

            TiltChanged?.Invoke(new SKPoint(_tiltX, _tiltY));
        }
Esempio n. 2
0
        protected override void DoWeaponFireShotgun(Vector2 startPos, Vector2 direction)
        {
            const int ShotSpread = 2;
            var       spreading  = new[] { -0.185f, -0.070f, 0f, 0.070f, 0.185f };

            for (var i = -ShotSpread; i <= ShotSpread; i++)
            {
                var angle = MathHelper.Angle(direction);
                angle += spreading[i + ShotSpread];
                var v          = 1 - (Math.Abs(i) / (float)ShotSpread);
                var speed      = MathHelper.Mix(Tuning["shotgun_speeddiff"], 1.0f, v);
                var projectile = new Projectile
                                 (
                    weapon: Weapon.Shotgun,
                    ownerId: Player.ClientId,
                    startPos: startPos,
                    direction: new Vector2(MathF.Cos(angle), MathF.Sin(angle)) * speed,
                    lifeSpan: (int)(Server.TickSpeed * Tuning["shotgun_lifetime"]),
                    damage: ServerData.Weapons.Shotgun.Damage,
                    explosive: false,
                    force: 0f,
                    soundImpact: (Sound)(-1)
                                 );
            }

            GameContext.CreateSound(Position, Sound.ShotgunFire);
        }
Esempio n. 3
0
        public override void CreateDamageIndicator(Vector2 pos, Vector2 source,
                                                   int clientId, int healthAmount, int armorAmount, bool self)
        {
            var e = Events.Create <SnapshotEventDamage>(pos);

            if (e == null)
            {
                return;
            }

            e.ClientId     = clientId;
            e.Angle        = (int)(MathHelper.Angle(source) * 256f);
            e.ArmorAmount  = armorAmount;
            e.HealthAmount = healthAmount;
            e.IsSelf       = self;
        }
Esempio n. 4
0
        private IList <SKPoint> SmoothCurve(IList <SKPoint> curve)
        {
            var newCurve = new List <SKPoint>();

            newCurve.Add(curve.First());

            for (int i = 1; i < _curve.Count - 1; i++)
            {
                var p1 = _curve[i - 1];
                var p2 = _curve[i];
                var p3 = _curve[i + 1];

                var angle = MathHelper.Angle(p1, p2, p3);

                if (Math.Abs(angle) < 7 * Math.PI / 8)
                {
                    var angle1 = MathHelper.Angle(p1, p2);

                    var d1    = MathHelper.Distance(p1, p2) * 0.7f;
                    var newP1 = new SKPoint((float)(p1.X + d1 * Math.Cos(angle1)),
                                            (float)(p1.Y + d1 * Math.Sin(angle1)));

                    var angle3 = MathHelper.Angle(p3, p2);
                    var d3     = MathHelper.Distance(p2, p3) * 0.7f;
                    var newP3  = new SKPoint((float)(p3.X + d3 * Math.Cos(angle3)),
                                             (float)(p3.Y + d3 * Math.Sin(angle3)));

                    newCurve.Add(newP1);
                    newCurve.Add(newP3);
                }
                else
                {
                    newCurve.Add(p2);
                }
            }

            newCurve.Add(curve.Last());

            return(newCurve);
        }
Esempio n. 5
0
    public static float ToAng(this Vector2 v, V2AngleMode angMode = V2AngleMode.Angle)
    {
        switch (angMode)
        {
        case V2AngleMode.Angle:
            return(MathHelper.Angle(v));

        case V2AngleMode.AngleAlt:
            return(MathHelper.AngleAlt(v));

        case V2AngleMode.CCW_AngleAlt:
            return(MathHelper.UnAngleAlt(v));

        case V2AngleMode.ThirdType:
            var vector2 = v - new Vector2();
            var vector1 = new Vector2(1, 0);     // 12 o'clock == 0°, assuming that y goes from bottom to top
            return(Mathf.Rad2Deg * (Mathf.Atan2(v.y, v.x) - Mathf.Atan2(vector1.y, vector1.x)));

        default:
            Debug.Log("Unhandled switch, returning normal angle mode");
            goto case V2AngleMode.Angle;
        }
    }
Esempio n. 6
0
        public override void Tick(SnapshotPlayerInput input)
        {
            TriggeredEvents = CoreEvents.None;

            var isGrounded = false;

            if (MapCollision.IsTileSolid(Position.x + TeeSize / 2, Position.y + TeeSize / 2 + 5))
            {
                isGrounded = true;
            }
            else if (MapCollision.IsTileSolid(Position.x - TeeSize / 2, Position.y + TeeSize / 2 + 5))
            {
                isGrounded = true;
            }

            var vel = Velocity;

            vel.y += World.Tuning["gravity"];

            if (input != null)
            {
                Direction = input.Direction;
                Angle     = (int)(MathHelper.Angle(new Vector2(input.TargetX, input.TargetY)) * 256f);

                if (input.IsJump)
                {
                    if ((Jumped & 1) == 0)
                    {
                        if (isGrounded)
                        {
                            TriggeredEvents |= CoreEvents.GroundJump;
                            vel.y            = -World.Tuning["ground_jump_impulse"];
                            Jumped          |= 1;
                        }
                        else if ((Jumped & 2) == 0)
                        {
                            TriggeredEvents |= CoreEvents.AirJump;
                            vel.y            = -World.Tuning["air_jump_impulse"];
                            Jumped          |= 3;
                        }
                    }
                }
                else
                {
                    Jumped &= ~1;
                }

                if (input.IsHook)
                {
                    if (HookState == HookState.Idle)
                    {
                        var targetDirection = new Vector2(input.TargetX, input.TargetY).Normalized;
                        HookState     = HookState.Flying;
                        HookPosition  = Position + targetDirection * TeeSize * 1.5f;
                        HookDirection = targetDirection;
                        HookedPlayer  = -1;
                        HookTick      = 0;
                        //TriggeredEvents |= CoreEvents.HookLaunch;
                    }
                }
                else
                {
                    HookedPlayer = -1;
                    HookState    = HookState.Idle;
                    HookPosition = Position;
                }
            }

            float maxSpeed = isGrounded ? World.Tuning["ground_control_speed"] : World.Tuning["air_control_speed"];
            float accel    = isGrounded ? World.Tuning["ground_control_accel"] : World.Tuning["air_control_accel"];
            float friction = isGrounded ? World.Tuning["ground_friction"] : World.Tuning["air_friction"];

            if (Direction < 0)
            {
                vel.x = MathHelper.SaturatedAdd(-maxSpeed, maxSpeed, vel.x, -accel);
            }
            else if (Direction > 0)
            {
                vel.x = MathHelper.SaturatedAdd(-maxSpeed, maxSpeed, vel.x, +accel);
            }
            else
            {
                vel.x *= friction;
            }

            if (isGrounded)
            {
                Jumped &= ~2;
            }

            if (HookState == HookState.Idle)
            {
                HookedPlayer = -1;
                HookPosition = Position;
            }
            else if (HookState >= HookState.RetractStart && HookState < HookState.RetractEnd)
            {
                HookState++;
            }
            else if (HookState == HookState.RetractEnd)
            {
                HookState = HookState.Retracted;
                //TriggeredEvents |= CoreEvents.HOOK_RETRACT;
            }
            else if (HookState == HookState.Flying)
            {
                var newHookPos = HookPosition + HookDirection * World.Tuning["hook_fire_speed"];
                if (MathHelper.Distance(Position, newHookPos) > World.Tuning["hook_length"])
                {
                    HookState  = HookState.RetractStart;
                    newHookPos = Position + (newHookPos - Position).Normalized * World.Tuning["hook_length"];
                }

                var goingToHitGround = false;
                var goingToRetract   = false;
                var hitFlags         = MapCollision.IntersectLine(HookPosition, newHookPos,
                                                                  out newHookPos, out _);

                if (hitFlags != CollisionFlags.None)
                {
                    if (hitFlags.HasFlag(CollisionFlags.NoHook))
                    {
                        goingToRetract = true;
                    }
                    else
                    {
                        goingToHitGround = true;
                    }
                }

                if (World.Tuning["player_hooking"] > 0)
                {
                    var distance = 0f;
                    for (var i = 0; i < World.CharacterCores.Length; i++)
                    {
                        var characterCore = World.CharacterCores[i];
                        if (characterCore == null || characterCore == this)
                        {
                            continue;
                        }

                        var closestPoint = MathHelper.ClosestPointOnLine(HookPosition, newHookPos,
                                                                         characterCore.Position);
                        if (MathHelper.Distance(characterCore.Position, closestPoint) < TeeSize + 2f)
                        {
                            if (HookedPlayer == -1 || MathHelper.Distance(HookPosition, characterCore.Position) < distance)
                            {
                                TriggeredEvents |= CoreEvents.HookAttachPlayer;
                                HookState        = HookState.Grabbed;
                                HookedPlayer     = i;
                                distance         = MathHelper.Distance(HookPosition, characterCore.Position);
                                break;
                            }
                        }
                    }
                }

                if (HookState == HookState.Flying)
                {
                    if (goingToHitGround)
                    {
                        TriggeredEvents |= CoreEvents.HookAttachGround;
                        HookState        = HookState.Grabbed;
                    }
                    else if (goingToRetract)
                    {
                        TriggeredEvents |= CoreEvents.HookHitNoHook;
                        HookState        = HookState.RetractStart;
                    }

                    HookPosition = newHookPos;
                }
            }

            if (HookState == HookState.Grabbed)
            {
                if (HookedPlayer != -1)
                {
                    var characterCore = World.CharacterCores[HookedPlayer];
                    if (characterCore != null)
                    {
                        HookPosition = characterCore.Position;
                    }
                    else
                    {
                        HookedPlayer = -1;
                        HookState    = HookState.Retracted;
                        HookPosition = Position;
                    }
                }

                if (HookedPlayer == -1 && MathHelper.Distance(HookPosition, Position) > 46.0f)
                {
                    var hookVel = (HookPosition - Position).Normalized * World.Tuning["hook_drag_accel"];
                    if (hookVel.y > 0)
                    {
                        hookVel.y *= 0.3f;
                    }

                    if (hookVel.x < 0 && Direction < 0 || hookVel.x > 0 && Direction > 0)
                    {
                        hookVel.x *= 0.95f;
                    }
                    else
                    {
                        hookVel.x *= 0.75f;
                    }

                    var newVel = vel + hookVel;
                    if (newVel.Length < World.Tuning["hook_drag_speed"] || newVel.Length < vel.Length)
                    {
                        vel = newVel;
                    }
                }

                HookTick++;

                // 60 = 1.25s
                if (HookedPlayer != -1 && (HookTick > 60 || World.CharacterCores[HookedPlayer] == null))
                {
                    HookedPlayer = -1;
                    HookState    = HookState.Retracted;
                    HookPosition = Position;
                }
            }

            if (World.Tuning["player_collision"] > 0 ||
                World.Tuning["player_hooking"] > 0)
            {
                for (var i = 0; i < World.CharacterCores.Length; i++)
                {
                    var characterCore = World.CharacterCores[i];
                    if (characterCore == null || characterCore == this)
                    {
                        continue;
                    }

                    var distance  = MathHelper.Distance(Position, characterCore.Position);
                    var direction = (Position - characterCore.Position).Normalized;

                    if (World.Tuning["player_collision"] > 0 &&
                        distance < TeeSize * 1.25f &&
                        distance > 0)
                    {
                        var a        = (TeeSize * 1.45f - distance);
                        var velocity = 0.5f;

                        if (vel.Length > 0.0001f)
                        {
                            velocity = 1 - (MathHelper.Dot(vel.Normalized, direction) + 1) / 2;
                        }

                        vel += direction * a * (velocity * 0.75f);
                        vel *= 0.85f;
                    }

                    if (World.Tuning["player_hooking"] > 0 &&
                        HookedPlayer == i)
                    {
                        if (distance > TeeSize * 1.50f)
                        {
                            var hookAccelerate = World.Tuning["hook_drag_accel"] *
                                                 (distance / World.Tuning["hook_length"]);
                            float dragSpeed = World.Tuning["hook_drag_speed"];

                            characterCore.Velocity = new Vector2(
                                MathHelper.SaturatedAdd(-dragSpeed, dragSpeed,
                                                        characterCore.Velocity.x, hookAccelerate * direction.x * 1.5f),
                                MathHelper.SaturatedAdd(-dragSpeed, dragSpeed,
                                                        characterCore.Velocity.y, hookAccelerate * direction.y * 1.5f)
                                );

                            vel.x = MathHelper.SaturatedAdd(-dragSpeed, dragSpeed, vel.x,
                                                            -hookAccelerate * direction.x * 0.25f);
                            vel.y = MathHelper.SaturatedAdd(-dragSpeed, dragSpeed, vel.y,
                                                            -hookAccelerate * direction.y * 0.25f);
                        }
                    }
                }
            }

            if (vel.Length > 6000)
            {
                vel = vel.Normalized * 6000;
            }
            Velocity = vel;
        }