Пример #1
0
 private void SetRigidBodyParam(GameObjectRigidBody gameObject)
 {
     if (Motion.HasValue)
     {
         gameObject.RigidBody.Motion = Motion.Value;
     }
     if (Force.HasValue)
     {
         gameObject.RigidBody.ApplyForce(Force.Value);
     }
     if (Mass.HasValue)
     {
         gameObject.RigidBody.Mass = Mass.Value;
     }
 }
Пример #2
0
        public static void PreCollide(GameObjectRigidBody self, GameObjectRigidBody other)
        {
            switch (self)
            {
            case Star _:
            {
                var @this = self as Star;
                if (other is Mario)
                {
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, @this, new GameObjectDestroyEventArgs(@this));
                }

                break;
            }

            case Goomba _:
            {
                var @this = self as Goomba;
                if (other is Koopa koopa && koopa.Stomped)
                {
                    @this.RigidBody.CollisionLayerMask = CollisionLayer.None;
                }

                break;
            }

            case Coin _:
            {
                var @this = self as Coin;
                if (other is Mario)
                {
                    @this.RigidBody.CollisionLayerMask = CollisionLayer.None;
                    Coins.Ins.Value++;
                    Score.Ins.Value += Constants.COIN_POINTS;
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, @this, new GameObjectCreateEventArgs(new PromptingPoints(@this.Location.X, @this.Location.Y, Constants.COIN_POINTS)));
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, @this, new GameObjectDestroyEventArgs(@this));
                }

                break;
            }

            case Flag _:
            {
                var @this = self as Flag;
                if (other is Mario)
                {
                    @this.RigidBody.CollisionLayerMask = CollisionLayer.None;
                }
                break;
            }

            case Flower _:
            {
                var @this = self as Flower;
                if (other is Mario)
                {
                    @this.RigidBody.CollisionLayerMask = CollisionLayer.None;
                    Score.Ins.Value += Constants.FLOWER_SCORE;
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, @this, new GameObjectCreateEventArgs(new PromptingPoints(@this.Location.X, @this.Location.Y, Constants.FLOWER_SCORE)));
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, @this, new GameObjectDestroyEventArgs(@this));
                }

                break;
            }

            case GreenMushroom _:
            {
                var @this = self as GreenMushroom;
                if (other is Mario)
                {
                    @this.RigidBody.CollisionLayerMask = CollisionLayer.None;
                    Lives.Ins.Value++;
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, @this, new GameObjectDestroyEventArgs(@this));
                }

                break;
            }

            case RedMushroom _:
            {
                var @this = self as RedMushroom;
                if (other is Mario)
                {
                    @this.RigidBody.CollisionLayerMask = CollisionLayer.None;
                    Score.Ins.Value += Constants.REDMUSHROOM_SCORE;
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, @this, new GameObjectCreateEventArgs(new PromptingPoints(@this.Location.X, @this.Location.Y, Constants.REDMUSHROOM_SCORE)));
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, @this, new GameObjectDestroyEventArgs(@this));
                }

                break;
            }
            }
        }
Пример #3
0
        public static void PostCollide(GameObjectRigidBody self, GameObjectRigidBody other, CollisionSide side)
        {
            switch (self)
            {
            case Block _:
            {
                var @this = self as Block;

                if (other is Mario)
                {
                    if (side == CollisionSide.Bottom && @this.RigidBody.Motion == MotionEnum.Static && [email protected])
                    {
                        if (@this.State == BlockState.Hidden)
                        {
                            @this.State = BlockState.Normal;
                        }

                        @this.RigidBody.Motion   = MotionEnum.Keyframe;
                        @this.RigidBody.Velocity = Constants.BLOCK_MARIO_COLLISION_VELOCITY;
                    }
                }

                if (self is BrickBlock thisBrickBlock)
                {
                    if (other is Mario mario && side == CollisionSide.Bottom)
                    {
                        if (thisBrickBlock.Powerup != null)
                        {
                            thisBrickBlock.State = BlockState.Used;

                            var powerupObj = new GameObjectParam
                            {
                                TypeName = thisBrickBlock.Powerup,
                                Location = new int[2] {
                                    (int)thisBrickBlock.Location.X, (int)thisBrickBlock.Location.Y - Constants.BLOCK_HEIGHT * 2
                                },
                                Motion = MotionEnum.Dynamic,
                            }.ToGameObject();
                            EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, thisBrickBlock, new GameObjectCreateEventArgs(powerupObj));
                            if (thisBrickBlock.Powerup == Constants.COIN_TYPE_NAME)
                            {
                                EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, thisBrickBlock, new GameObjectDestroyEventArgs(powerupObj), Constants.BLOCK_COLLISION_EVENT_DT);
                            }
                            thisBrickBlock.Powerup = null;
                        }
                        else if (thisBrickBlock.State == BlockState.Normal && !(mario.State.IsSmall || mario.State.IsDead))
                        {
                            EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, thisBrickBlock, new GameObjectDestroyEventArgs(thisBrickBlock));

                            for (var i = 0; i < Constants.BRICK_BLOCK_COLLISION_POSITIONS.Length; i++)
                            {
                                var debris = new GameObjectParam
                                {
                                    TypeName = Constants.BRICK_DEBRIS_TYPE_NAME,
                                    Location = new int[] { (int)thisBrickBlock.Location.X + Constants.BRICK_BLOCK_COLLISION_OFFSETS[i, 0], (int)thisBrickBlock.Location.Y + Constants.BRICK_BLOCK_COLLISION_OFFSETS[i, 1] },
                                    Motion   = MotionEnum.Dynamic,
                                    Params   = new Dictionary <string, string> {
                                        { Constants.POSITION_PARAM, Constants.BRICK_BLOCK_COLLISION_POSITIONS[i] }
                                    },
                                }.ToGameObject();
                                (debris as GameObjectRigidBody).RigidBody.Velocity = Constants.BRICK_BLOCK_COLLISION_VELOCITIES[i];
                                EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, thisBrickBlock, new GameObjectCreateEventArgs(debris));
                                EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, thisBrickBlock, new GameObjectDestroyEventArgs(debris), Constants.BLOCK_COLLISION_EVENT_DT);
                            }
                        }
                    }
                }

                else if (self is BlueBrickBlock thisBlueBrickBlock)
                {
                    if (other is Mario mario && side == CollisionSide.Bottom)
                    {
                        if (thisBlueBrickBlock.Powerup != null)
                        {
                            thisBlueBrickBlock.State = BlockState.Used;

                            var powerupObj = new GameObjectParam
                            {
                                TypeName = thisBlueBrickBlock.Powerup,
                                Location = new int[2] {
                                    (int)thisBlueBrickBlock.Location.X, (int)thisBlueBrickBlock.Location.Y - Constants.BLOCK_HEIGHT * 2
                                },
                                Motion = MotionEnum.Dynamic,
                            }.ToGameObject();
                            EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, thisBlueBrickBlock, new GameObjectCreateEventArgs(powerupObj));
                            if (thisBlueBrickBlock.Powerup == Constants.COIN_TYPE_NAME)
                            {
                                EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, thisBlueBrickBlock, new GameObjectDestroyEventArgs(powerupObj), Constants.BLOCK_COLLISION_EVENT_DT);
                            }
                            thisBlueBrickBlock.Powerup = null;
                        }
                        else if (thisBlueBrickBlock.State == BlockState.Normal && !(mario.State.IsSmall || mario.State.IsDead))
                        {
                            EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, thisBlueBrickBlock, new GameObjectDestroyEventArgs(thisBlueBrickBlock));

                            for (var i = 0; i < Constants.BRICK_BLOCK_COLLISION_POSITIONS.Length; i++)
                            {
                                var debris = new GameObjectParam
                                {
                                    TypeName = Constants.BLUE_BRICK_DEBRIS_TYPE_NAME,
                                    Location = new int[] { (int)thisBlueBrickBlock.Location.X + Constants.BRICK_BLOCK_COLLISION_OFFSETS[i, 0], (int)thisBlueBrickBlock.Location.Y + Constants.BRICK_BLOCK_COLLISION_OFFSETS[i, 1] },
                                    Motion   = MotionEnum.Dynamic,
                                    Params   = new Dictionary <string, string> {
                                        { Constants.POSITION_PARAM, Constants.BRICK_BLOCK_COLLISION_POSITIONS[i] }
                                    },
                                }.ToGameObject();
                                (debris as GameObjectRigidBody).RigidBody.Velocity = Constants.BRICK_BLOCK_COLLISION_VELOCITIES[i];
                                EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, thisBlueBrickBlock, new GameObjectCreateEventArgs(debris));
                                EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, thisBlueBrickBlock, new GameObjectDestroyEventArgs(debris), Constants.BLOCK_COLLISION_EVENT_DT);
                            }
                        }
                    }
                }

                else if (self is QuestionBlock thisQuestionBlock)
                {
                    if (other is Mario)
                    {
                        if (thisQuestionBlock.State == BlockState.Normal && side == CollisionSide.Bottom)
                        {
                            thisQuestionBlock.State = BlockState.Used;

                            if (thisQuestionBlock.Powerup != null)
                            {
                                var powerupObj = new GameObjectParam
                                {
                                    TypeName = thisQuestionBlock.Powerup,
                                    Location = new int[2] {
                                        (int)thisQuestionBlock.Location.X, (int)thisQuestionBlock.Location.Y - 2 * Constants.BLOCK_HEIGHT
                                    },
                                    Motion = MotionEnum.Dynamic,
                                }.ToGameObject();
                                EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, thisQuestionBlock, new GameObjectCreateEventArgs(powerupObj));

                                if (thisQuestionBlock.Powerup == Constants.COIN_TYPE_NAME)
                                {
                                    Coins.Ins.Value++;
                                    Score.Ins.Value += Constants.COIN_POINTS;
                                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, @this, new GameObjectCreateEventArgs(new PromptingPoints(@this.Location.X, @this.Location.Y, Constants.COIN_POINTS)));
                                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, thisQuestionBlock, new GameObjectDestroyEventArgs(powerupObj), Constants.BLOCK_COLLISION_EVENT_DT);
                                    AudioManager.Ins.PowerupCoin();
                                }
                                else
                                {
                                    AudioManager.Ins.ItemAppear();
                                }
                            }
                        }
                    }
                }

                break;
            }

            case Goomba _:
            {
                var @this = self as Goomba;

                if ((other is Mario mario && (side == CollisionSide.Top || mario.State.IsInvincible)) || other is Fireball)
                {
                    @this.Sprite = SpriteFactory.Ins.CreateSprite(Constants.GOOMBA_STOMPED_SPRITE);
                    @this.RigidBody.CollisionLayerMask = CollisionLayer.None;
                    @this.RigidBody.Velocity           = Vector2.Zero;
                    Score.Ins.Value += Constants.GOOMBA_POINTS;
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectCreate, @this, new GameObjectCreateEventArgs(new PromptingPoints(@this.Location.X, @this.Location.Y, Constants.GOOMBA_POINTS)));
                    EventManager.Ins.RaiseEvent(EventEnum.GameObjectDestroy, @this, new GameObjectDestroyEventArgs(@this), Constants.ENEMY_COLLISION_EVENT_DT);
                }
Пример #4
0
 public void Remove(GameObjectRigidBody o)
 {
     Apply(o.RigidBody.Bound, k => buckets.ContainsKey(k).Then(() => buckets[k].Remove(o)));
     objects.Remove(o);
 }
Пример #5
0
 public void Add(GameObjectRigidBody o)
 {
     objects.Add(o);
 }