//TODO: MODIFY! NOT WORKING IF LINE SEGMENT INSIDE POLYGON

        private static bool IsLineAndPolygonIntersects(Vector2 lineSegmentVertex1, Vector2 lineSegmentVertex2, Shape shape, Vector2 shapePos, long shapeRot)
        {
            var polygonShape = (PolygonShape)shape;

            var cos = FixedMath.Cos(shapeRot);
            var sin = FixedMath.Sin(shapeRot);

            //TODO: replace to better algorithm

            var prevPolygonVertex = Rotate(polygonShape.vertices[0], cos, sin) + shapePos;

            for (var i = polygonShape.vertices.Length - 1; i >= 0; i--)
            {
                var curPolygonVertex = Rotate(polygonShape.vertices[i], cos, sin) + shapePos;

                if (LineSegmentIntersection.IsLineSegmentsIntersects(lineSegmentVertex1, lineSegmentVertex2, prevPolygonVertex, curPolygonVertex))
                {
                    return(true);
                }

                prevPolygonVertex = curPolygonVertex;
            }

            return(false);
        }
        private static bool IsPolygonAndPolygonIntersects(Shape first, Vector2 firstPos, long firstRot, Shape second, Vector2 secondPos, long secondRot)
        {
            var polygonShapeFirst  = (PolygonShape)first;
            var polygonShapeSecond = (PolygonShape)second;

            var firstCos = FixedMath.Cos(firstRot);
            var firstSin = FixedMath.Sin(firstRot);

            var secondCos = FixedMath.Cos(secondRot);
            var secondSin = FixedMath.Sin(secondRot);

            for (var i = 0; i < polygonShapeFirst.triangleCount; i++)
            {
                var fa = Rotate(polygonShapeFirst.triangleVertices[i * 3], firstCos, firstSin) + firstPos;
                var fb = Rotate(polygonShapeFirst.triangleVertices[i * 3 + 1], firstCos, firstSin) + firstPos;
                var fc = Rotate(polygonShapeFirst.triangleVertices[i * 3 + 2], firstCos, firstSin) + firstPos;

                for (var j = 0; j < polygonShapeSecond.triangleCount; j++)
                {
                    var sa = Rotate(polygonShapeSecond.triangleVertices[j * 3], secondCos, secondSin) + secondPos;
                    var sb = Rotate(polygonShapeSecond.triangleVertices[j * 3 + 1], secondCos, secondSin) + secondPos;
                    var sc = Rotate(polygonShapeSecond.triangleVertices[j * 3 + 2], secondCos, secondSin) + secondPos;

                    if (TriangleIntersection.IsTrianglesIntersects(fa, fb, fc, sa, sb, sc))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Beispiel #3
0
        internal override void Update()
        {
            Vector2 moveDir;

            if (entity.entityManager.GetEntityPerNameCount("Spaceship") == 0)
            {
                moveDir = new Vector2(FixedMath.Cos(transform.Rotation), FixedMath.Sin(transform.Rotation));
            }
            else
            {
                var spaceship = entity.entityManager.Find(e => e.name == "Spaceship");

                moveDir = (spaceship.GetComponent <Transform>().Position - transform.Position).Normalized();
            }

            moveSpeed += moveDir * Acceleration;

            var moveSpeedNormalized = moveSpeed.Normalized(out var mag);

            if (mag > MaxMoveSpeed)
            {
                moveSpeed = moveSpeedNormalized * MaxMoveSpeed;
            }

            transform.Position += moveSpeed;

            transform.Rotation += RotationSpeed;
        }
        private void OnAdvancedShot()
        {
            var laser = entity.entityManager.CreateEntity("Laser", entity.Owner);

            var spaceshipMoveDir = new Vector2(FixedMath.Cos(transform.Rotation), FixedMath.Sin(transform.Rotation));

            laser.GetComponent <LaserController>().Init(entity.Pointer, spaceshipMoveDir);
        }
        private void OnShot()
        {
            var bullet = entity.entityManager.CreateEntity("Bullet", entity.Owner);

            bullet.GetComponent <Collider>().SetCollisionLayer(CollisionLayersConfig.WeaponLayer);

            var bulletTransform = bullet.GetComponent <Transform>();

            var spaceshipMoveDir = new Vector2(FixedMath.Cos(transform.Rotation), FixedMath.Sin(transform.Rotation));

            //pos with small forward offset
            bulletTransform.Rotation = transform.Rotation;
            bulletTransform.Position = transform.Position + spaceshipMoveDir * FixedMath.One / 120;

            bullet.GetComponent <BulletController>().RecalculateMoveSpeed();
        }
Beispiel #6
0
        internal override void OnCreate()
        {
            var rnd = entity.entityManager.matchManager.random;

            transform.Rotation = rnd.GetRandom((int)(FixedMath.Pi * 2 + 1));

            var diff = maxRotationSpeed - minRotationSpeed;

            rotationSpeed = minRotationSpeed + rnd.GetRandom((int)diff);

            diff = maxMoveSpeed - minMoveSpeed;
            var moveSpeedMultiplier = minMoveSpeed + rnd.GetRandom((int)diff);

            var dirAngle = rnd.GetRandom((int)(FixedMath.Pi * 2 + 1));

            moveSpeed = new Vector2(FixedMath.Cos(dirAngle), FixedMath.Sin(dirAngle)) * moveSpeedMultiplier;
        }
 internal void RecalculateMoveSpeed()
 {
     moveSpeed = new Vector2(FixedMath.Cos(transform.Rotation), FixedMath.Sin(transform.Rotation)) * MoveSpeed;
 }
        internal override void Update()
        {
            if ((lastControls & MoveForwardMask) != 0)
            {
                if (moveVelocity >= 0)
                {
                    ApplyAcceleration(1, MoveAcceleration, MaxForwardSpeed, ref moveVelocity);
                }
                else
                {
                    ApplyBraking(-1, MoveDeceleration, ref moveVelocity);
                }
            }
            else if ((lastControls & MoveBackwardMask) != 0)
            {
                if (moveVelocity <= 0)
                {
                    ApplyAcceleration(-1, MoveAcceleration, MaxBackwardSpeed, ref moveVelocity);
                }
                else
                {
                    ApplyBraking(1, MoveDeceleration, ref moveVelocity);
                }
            }
            else
            {
                ApplyBraking(moveVelocity >= 0 ? 1 : -1, MoveDamping, ref moveVelocity);
            }

            if ((lastControls & RotateRightMask) != 0)
            {
                if (rotationVelocity <= 0)
                {
                    ApplyAcceleration(-1, RotationAcceleration, MaxRotationSpeed, ref rotationVelocity);
                }
                else
                {
                    ApplyBraking(1, RotationDeceleration, ref rotationVelocity);
                }
            }
            else if ((lastControls & RotateLeftMask) != 0)
            {
                if (rotationVelocity >= 0)
                {
                    ApplyAcceleration(1, RotationAcceleration, MaxRotationSpeed, ref rotationVelocity);
                }
                else
                {
                    ApplyBraking(-1, RotationDeceleration, ref rotationVelocity);
                }
            }
            else
            {
                ApplyBraking(rotationVelocity >= 0 ? 1 : -1, RotationDamping, ref rotationVelocity);
            }

            if ((lastControls & SimpleShotMask) != 0 && entity.entityManager.matchManager.CurrentTick - lastShotTick > SimpleShotCooldown)
            {
                lastShotTick = entity.entityManager.matchManager.CurrentTick;

                OnShot();
            }

            if ((lastControls & AdvancedShotMask) != 0 && entity.entityManager.matchManager.CurrentTick - lastAdvancedShotTick > AdvancedShotCooldown)
            {
                lastAdvancedShotTick = entity.entityManager.matchManager.CurrentTick;

                OnAdvancedShot();
            }

            transform.Position += new Vector2(FixedMath.Cos(transform.Rotation), FixedMath.Sin(transform.Rotation)) * moveVelocity;

            transform.Rotation += rotationVelocity;
        }