Beispiel #1
0
 public void onTouchPressed(TouchLocation touch)
 {
     if (this.InMoveMode && this.MovingWithDevice == null && CollisionChecking.Check(touch.Position, this.CurrentTrackpadRect))
     {
         this.MovingWithDevice    = InputDevices.Touch;
         this.MoveModeStartOffset = touch.Position.ToPoint() - this.CurrentTrackpadRect.Center;
         this.CurrentTouchID      = touch.Id;
     }
 }
Beispiel #2
0
 public override void onMouseDown(MouseEventArgs e)
 {
     base.onMouseDown(e);
     if (this.InMoveMode && this.MovingWithDevice == null && CollisionChecking.Check(e.Position, this.CurrentTrackpadRect))
     {
         this.MovingWithDevice    = InputDevices.Mouse;
         this.MoveModeStartOffset = e.Position - this.CurrentTrackpadRect.Center;
     }
 }
Beispiel #3
0
        private void onInputDown(Point position)
        {
            if (!this.Active)
            {
                return;
            }

            var trackpad_rect = this.Trackpad.CurrentTrackpadRect;

            this.IsCurrentClickValid     = CollisionChecking.Check(position, trackpad_rect);
            this.IsLockedToInputPosition = false;
        }
        private bool IsClickOnSelf(Point click_position)
        {
            var collider = this.GetCollider("main");

            switch (collider)
            {
            case ColliderRectangle rectangle:
                return(CollisionChecking.Check(click_position, rectangle.Rectangle));

            case ColliderCircle circle:
                return(CollisionChecking.Check(click_position, circle.Circle));

            default:
                return(false);
            }
        }
        private bool IsPositionInCollision(int x, int y)
        {
            var rect = new Rectangle(x - Food.Size / 2, y - Food.Size / 2, Food.Size, Food.Size);

            var entities = Engine.GetAllInstances <Entity>();

            for (int i = 0; i < entities.Count; i++)
            {
                if (entities[i].IsExpired)
                {
                    continue;
                }
                ;

                for (int j = 0; j < entities[i].Colliders.Count; j++)
                {
                    var collider = entities[i].Colliders[j];
                    if (!collider.Enabled)
                    {
                        continue;
                    }

                    if (collider is ColliderCircle)
                    {
                        if (CollisionChecking.CircleRect(((ColliderCircle)collider).Circle, rect))
                        {
                            return(true);
                        }
                    }
                    else if (collider is ColliderRectangle)
                    {
                        if (CollisionChecking.RectRect(((ColliderRectangle)collider).Rectangle, rect))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Beispiel #6
0
        public override void onCollision(Collider collider, Collider other_collider, Entity other_instance)
        {
            base.onCollision(collider, other_collider, other_instance);

            // If the snake is not alive simply return, no collisions should be processed
            if (this.State != States.Alive)
            {
                return;
            }

            // If collided with food add to snake, play sound, post event, and destroy food.
            if (other_instance is Food && collider.Name == this.MouthColliderName)
            {
                this.AddToSnake();
                SFXPlayer.Play(AvailableSounds.eat, 0.7f);
                Engine.PostGameEvent(new FoodEatenEvent((int)other_instance.Position.X, (int)other_instance.Position.Y));
                other_instance.IsExpired = true;

                // Else if collided with tail stop music, play sound, and begin death sequence
            }
            else if ((other_instance is SnakeTail || other_instance is Wall) && collider.Name != this.MouthColliderName)
            {
                MediaPlayer.Stop();
                SFXPlayer.Play(AvailableSounds.death_hit, 0.75f);
                Engine.SpawnInstance(new TimedExecution(1000, () => SFXPlayer.Play(AvailableSounds.death, 0.75f)));
                this.BeginDeath();

                // Else if collided with Portal handle porting to new location
            }
            else if (other_instance is Portal && collider.Name != this.MouthColliderName)
            {
                var start_pos          = this.CurrentLocation.ToVector2();
                var entrance_portal    = (Portal)other_instance;
                var exit_portal        = entrance_portal.GetDestination(this.Direction);
                var original_direction = this.Direction;
                this.Direction = entrance_portal.GetDirection(this.Direction);

                // Move the snake to the portal exit and reverse direction if necessary
                float travel_x = exit_portal.Position.X - entrance_portal.Position.X;
                float travel_y = exit_portal.Position.Y - entrance_portal.Position.Y;
                if (this.Direction == original_direction)
                {
                    if (this.Direction == Directions.Up)
                    {
                        travel_y -= Portal.Size + Snake.Size;
                    }
                    else if (this.Direction == Directions.Down)
                    {
                        travel_y += Portal.Size + Snake.Size;
                    }
                    else if (this.Direction == Directions.Left)
                    {
                        travel_x -= Portal.Size + Snake.Size;
                    }
                    else if (this.Direction == Directions.Right)
                    {
                        travel_x += Portal.Size + Snake.Size;
                    }
                }
                else
                {
                    foreach (var c in this.Colliders)
                    {
                        c.Enabled = (c.Name == this.Direction.ToString() || c.Name == this.MouthColliderName);
                    }
                }

                this.InternalLocation += new Vector2(travel_x, travel_y);
                this.Position          = this.InternalLocation;
                this.CurrentLocation   = this.InternalLocation.ToPoint();

                // Do extra checking to see if snake is immediately colliding with wall when exiting portal and adjust if so
                var head_collider = (ColliderRectangle)this.GetCollider(this.Direction.ToString());
                var walls         = Engine.GetAllInstances <Wall>();
                for (int i = 0; i < walls.Count; i++)
                {
                    var wall_collider = (ColliderRectangle)walls[i].GetCollider("main");
                    if (CollisionChecking.Check(head_collider.Rectangle, wall_collider.Rectangle))
                    {
                        if (this.Direction == Directions.Right || this.Direction == Directions.Left)
                        {
                            if (wall_collider.Rectangle.Center.Y < exit_portal.Position.Y)
                            {
                                this.InternalLocation.Y = wall_collider.Rectangle.Bottom + Snake.Size / 2;
                            }
                            else
                            {
                                this.InternalLocation.Y = wall_collider.Rectangle.Top - Snake.Size / 2;
                            }
                        }
                        else
                        {
                            if (wall_collider.Rectangle.Center.X < exit_portal.Position.X)
                            {
                                this.InternalLocation.X = wall_collider.Rectangle.Right + Snake.Size / 2;
                            }
                            else
                            {
                                this.InternalLocation.X = wall_collider.Rectangle.Left - Snake.Size / 2;
                            }
                        }

                        this.Position        = this.InternalLocation;
                        this.CurrentLocation = this.InternalLocation.ToPoint();
                        break;
                    }
                }

                // Spawn a new snake portal traversal animation entity
                var portal_animation = new SnakePortalAnimation(
                    start_pos,
                    this.CurrentLocation.ToVector2(),
                    entrance_portal.Position + new Vector2(Portal.Size / 2f),
                    exit_portal.Position + new Vector2(Portal.Size / 2f),
                    this.Tail.Count);
                Engine.SpawnInstance(portal_animation);
            }
        }
Beispiel #7
0
 public void Rectangles_Collide()
 {
     Assert.True(CollisionChecking.Rectangles(rectangleBase.X, rectangleBase.Y, rectangleBase.Width, rectangleBase.Height, rectangleCollide.X, rectangleCollide.Y, rectangleCollide.Width, rectangleCollide.Height));
 }
Beispiel #8
0
 public void Rectangles_Seperate()
 {
     Assert.False(CollisionChecking.Rectangles(rectangleBase.X, rectangleBase.Y, rectangleBase.Width, rectangleBase.Height, rectangleSeperate.X, rectangleSeperate.Y, rectangleSeperate.Width, rectangleSeperate.Height));
 }
Beispiel #9
0
 public void Circles()
 {
     Assert.True(CollisionChecking.Circles(10, 10, 5, 17, 10, 5));
     Assert.False(CollisionChecking.Circles(10, 10, 5, 20, 10, 5));
 }