示例#1
0
文件: Ground.cs 项目: laurence6/Mario
        public Ground(int dstX, int dstY, Dictionary <string, string> Params) : base(dstX, dstY, 0, 0)
        {
            var blockType = Params["BlockType"];
            var count     = int.Parse(Params["Count"]);
            var param     = new GameObjectParam
            {
                TypeName = blockType,
                Location = new int[] { dstX, dstY },
                Motion   = MotionEnum.Static,
                Params   = new Dictionary <string, string> {
                    { "State", "Normal" }
                },
            };

            for (var i = 0; i < count; i++)
            {
                var blockInstance = param.ToGameObject();
                param.Location[0] += blockInstance.Size.X;
                if (blockInstance is Block block)
                {
                    blocks.Add(block);
                }
            }
            Size = new Point(param.Location[0] - dstX, Constants.BLOCK_HEIGHT);
        }
示例#2
0
文件: Model.cs 项目: laurence6/Mario
        public IGameObject AddGameObject(GameObjectParam param)
        {
            var obj = param.ToGameObject();

            Data.Objects.Add(param);
            Objects.Add(obj);
            return(obj);
        }
示例#3
0
文件: Scene.cs 项目: laurence6/Mario
        public void Reset()
        {
            Player?.Dispose();
            scenes.Clear();
            var param = new GameObjectParam
            {
                TypeName = "Mario",
                Location = new int[] { (int)Constants.MARIO_DEFAULT_LOCATION.X, (int)Constants.MARIO_DEFAULT_LOCATION.Y },
                Motion   = MotionEnum.Dynamic,
                Force    = WorldForce.Gravity | WorldForce.Friction,
                Mass     = 1,
            };

            Player      = (Mario)param.ToGameObject();
            ActiveScene = null;
            unsubscribe = null;
            Constants.AVAILABLE_SCENES.ForEach(level => scenes.Add(level, new SceneData(level, Player)));
            scenes.ForEach((name, scene) => scene.Reset());
        }
示例#4
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);
                }