Esempio n. 1
0
        public static void Update(
            GameObject obj
            , Dictionary <State, Animation> animations
            , ValueKeeper <State> state
            , ValueKeeper <bool> facingRight
            )
        {
            obj.RenderHandler         = animations[state];
            animations[state].flipped = !facingRight;

            //if (state.Changed)
            //    animations[state].Reset();
            //else
            animations[state].Update();
        }
Esempio n. 2
0
        public override void LoadContent(ContentManager Content)
        {
            Camera.Zoom       = 0.006f;
            Camera.Position.X = 500 * Const.Scale;

            Textures.Load(Content);

            for (var i = 0; i < 7; i++)
            {
                var currentObj = Block.Create();
                currentObj.Position.Y = 200 * Const.Scale * 2;
                currentObj.Position.X = -200 * Const.Scale + (200 * Const.Scale * i);
                AddObject(currentObj);
            }

            for (var i = 0; i < 7; i++)
            {
                var currentObj = Block.Create();
                currentObj.Position.Y = -600 * Const.Scale;
                currentObj.Position.X = -200 * Const.Scale + (200 * Const.Scale * i);
                AddObject(currentObj);
            }

            for (var i = 0; i < 6; i++)
            {
                var currentObj = Block.Create();
                currentObj.Position.Y = -600 * Const.Scale + (200 * Const.Scale * i);
                currentObj.Position.X = -400 * Const.Scale;
                AddObject(currentObj);
            }

            for (var i = 0; i < 6; i++)
            {
                var currentObj = Block.Create();
                currentObj.Position.Y = -600 * Const.Scale + (200 * Const.Scale * i);
                currentObj.Position.X = -400 * Const.Scale + (200 * Const.Scale * 8);
                AddObject(currentObj);
            }

            {
                var currentObj = Block.Create();
                currentObj.Position.Y = 200 * Const.Scale;
                currentObj.Position.X = -200 * Const.Scale + (200 * Const.Scale * 3);
                AddObject(currentObj);
            }

            {
                var currentObj = Block.Create();
                currentObj.Position.Y = 0;
                currentObj.Position.X = -200 * Const.Scale + (200 * Const.Scale * 4);
                AddObject(currentObj);
            }

            {
                var currentObj = Block.Create();
                currentObj.Position.Y = 200 * Const.Scale;
                currentObj.Position.X = -200 * Const.Scale + (200 * Const.Scale * 5);
                AddObject(currentObj);
            }

            {
                var currentObj = Block.Create();
                currentObj.Position.Y = -400 * Const.Scale;;
                currentObj.Position.X = -200 * Const.Scale + (200 * Const.Scale * 3);
                AddObject(currentObj);
            }
            var playerState = ValueKeeper <State> .Create();

            AddActiveObjects(Player.Create(Player1Inputs, playerState));

            //move to otherFile
            var fps     = GameObject.Create();
            var fpsText = Textures.text;

            fpsText.scale     = 1000;
            fps.RenderHandler = fpsText;
            fps.UpdateHandler = () => fpsText.Text = this.CurrentFramesPerSecond.ToString("N1");
            AddActiveObjects(fps);

            var playerStateDraw = GameObject.Create();

            playerStateDraw.Position.Y = 12000;
            var stateText = Textures.text2;

            stateText.scale = 1000;
            playerStateDraw.RenderHandler = stateText;
            playerStateDraw.UpdateHandler = () => stateText.Text = playerState.ToString();
            AddActiveObjects(playerStateDraw);


            {
                //var preview_head = GameObject.Create();
                //preview_head.RenderHandler = Textures.head_bump;
                //preview_head.Position.X = 200 * Const.Scale * 3;
                //AddObject(preview_head);
            }
        }
Esempio n. 3
0
 public static bool In(this ValueKeeper <State> source, params State[] targets)
 {
     return(source.GetValue().In(targets));
 }
Esempio n. 4
0
        public static GameObject Create(InputController input, ValueKeeper <State> state)
        {
            var obj = GameObject.Create();

            obj.Position.Y = -14000;
            obj.Identifier = Identifier.Player;

            var grounded = ValueKeeper <int> .Create();

            var hittingHead = ValueKeeper <int> .Create();

            var facingRight = ValueKeeper <bool> .Create();

            {
                var collider = Collider.Create(obj);
                collider.Area = new Rectangle(
                    50 * Const.Scale
                    , 60 * Const.Scale
                    , 100 * Const.Scale
                    , 200 * Const.Scale - 60 * Const.Scale);
                collider.Handler = StopsWhenHitingBlocks.Create();
            }

            {
                var collider = Collider.Create(obj);
                collider.Area    = new Rectangle(50 * Const.Scale, 210 * Const.Scale, 100 * Const.Scale, 10 * Const.Scale);
                collider.Handler = TimerTrigger.Create(grounded, Const.Grounded_Timer);
            }

            {
                var collider = Collider.Create(obj);
                collider.Area    = new Rectangle(50 * Const.Scale, 40 * Const.Scale, 100 * Const.Scale, 10 * Const.Scale);
                collider.Handler = TimerTrigger.Create(hittingHead, 1);
            }

            var animationDic = new Dictionary <State, Animation> {
                { State.Idle, PlayerAnimation.Idle() }
                , { State.Walking, PlayerAnimation.Walk() }
                , { State.Fall, PlayerAnimation.Fall() }
                , { State.JumpStart, PlayerAnimation.Jump() }
                , { State.Jump, PlayerAnimation.Jump() }
                , { State.JumpBreak, PlayerAnimation.Fall() }
                , { State.HeadBump, PlayerAnimation.HeadBump() }
            };

            var stateMachine = StateMachine.Create();

            stateMachine.Add(
                State.Idle
                , stateChange: () =>
            {
                ChangeToFallingState.Try(obj, grounded, hittingHead, state);
                ChangeToWalking.Try(input, grounded, state, obj);
                ChangeToJumpStart.Try(input, grounded, state);
            }
                , update: () =>
            {
                UpdateGravity.Update(obj);
                PlayerAnimation.Update(obj, animationDic, state, facingRight);
                Friction.Apply(obj, input);
                hittingHead.SetValue(hittingHead.GetValue().DecrementUntil(0));
                grounded.SetValue(grounded.GetValue().DecrementUntil(0));
            });

            stateMachine.Add(
                State.Walking
                , stateChange: () =>
            {
                ChangeFacingDirection.Change(input, facingRight);
                ChangeToFallingState.Try(obj, grounded, hittingHead, state);
                ChangeToIdle.Try(input, grounded, state);
                ChangeToWalking.Try(input, grounded, state, obj);
                ChangeToJumpStart.Try(input, grounded, state);
            }
                , update: () =>
            {
                UpdateGravity.Update(obj);
                UpdateVelocityUsingInputs.Update(obj, input);
                PlayerAnimation.Update(obj, animationDic, state, facingRight);
                hittingHead.SetValue(hittingHead.GetValue().DecrementUntil(0));
                grounded.SetValue(grounded.GetValue().DecrementUntil(0));
            });

            stateMachine.Add(
                State.JumpStart
                , stateChange: () =>
            {
                ChangeFacingDirection.Change(input, facingRight);
                ChangeToJumpState.Try(state);
            }
                , update: () =>
            {
                UpdateGravity.Update(obj);
                UpdateVelocityUsingInputs.Update(obj, input);
                UpdateJumpStart.Update(obj);
                PlayerAnimation.Update(obj, animationDic, state, facingRight);
                Friction.Apply(obj, input);

                hittingHead.SetValue(hittingHead.GetValue().DecrementUntil(0));
                grounded.SetValue(grounded.GetValue().DecrementUntil(0));
            });

            stateMachine.Add(
                State.Jump
                , stateChange: () =>
            {
                ChangeFacingDirection.Change(input, facingRight);
                ChangeToFallingState.Try(obj, grounded, hittingHead, state);
                ChangeToJumpBreak.Try(obj, input, grounded, state);

                ChangeToIdle.Try(input, grounded, state);
                ChangeToWalking.Try(input, grounded, state, obj);
                ChangeToBumpHead.Try(state, obj, hittingHead);
                //ChangeToJumpStart.Try(obj, input, grounded, state);
            }
                , update: () =>
            {
                UpdateGravity.Update(obj);
                UpdateVelocityUsingInputs.Update(obj, input);
                PlayerAnimation.Update(obj, animationDic, state, facingRight);
                Friction.Apply(obj, input);

                hittingHead.SetValue(hittingHead.GetValue().DecrementUntil(0));
                grounded.SetValue(grounded.GetValue().DecrementUntil(0));
            });

            stateMachine.Add(
                State.HeadBump
                , stateChange: () =>
            {
                ChangeFacingDirection.Change(input, facingRight);
                ChangeToFallingState.Try(obj, grounded, hittingHead, state);
                //ChangeToJumpBreak.Try(obj, input, grounded, state);

                ChangeToIdle.Try(input, grounded, state);
                ChangeToWalking.Try(input, grounded, state, obj);
                //ChangeToBumpHead.Try(state, obj, hittingHead);
                //ChangeToJumpStart.Try(obj, input, grounded, state);
            }
                , update: () =>
            {
                UpdateGravity.Update(obj);
                UpdateVelocityUsingInputs.Update(obj, input);
                PlayerAnimation.Update(obj, animationDic, state, facingRight);
                Friction.Apply(obj, input);

                grounded.SetValue(grounded.GetValue().DecrementUntil(0));
                hittingHead.SetValue(hittingHead.GetValue().DecrementUntil(0));
            });

            stateMachine.Add(
                State.JumpBreak
                , stateChange: () =>
            {
                ChangeFacingDirection.Change(input, facingRight);
                ChangeToFallingState.Try(obj, grounded, hittingHead, state);
                ChangeToWalking.Try(input, grounded, state, obj);
                ChangeToIdle.Try(input, grounded, state);
            }
                , update: () =>
            {
                UpdateGravity.Update(obj);
                UpdateVelocityUsingInputs.Update(obj, input);
                UpdateJumpBreak.Update(obj);
                PlayerAnimation.Update(obj, animationDic, state, facingRight);
                Friction.Apply(obj, input);

                hittingHead.SetValue(hittingHead.GetValue().DecrementUntil(0));
                grounded.SetValue(grounded.GetValue().DecrementUntil(0));
            });

            stateMachine.Add(
                State.Fall
                , stateChange: () =>
            {
                ChangeFacingDirection.Change(input, facingRight);
                ChangeToIdle.Try(input, grounded, state);
                ChangeToWalking.Try(input, grounded, state, obj);
                ChangeToJumpStart.Try(input, grounded, state);
            }
                , update: () =>
            {
                UpdateGravity.Update(obj);

                PlayerAnimation.Update(obj, animationDic, state, facingRight);
                UpdateVelocityUsingInputs.Update(obj, input);

                Friction.Apply(obj, input);
                hittingHead.SetValue(hittingHead.GetValue().DecrementUntil(0));
                grounded.SetValue(grounded.GetValue().DecrementUntil(0));
            });

            obj.RenderHandler = PlayerAnimation.Idle();
            obj.UpdateHandler = () => stateMachine.Update(state);

            obj.OnDestroy = () =>
            {
                grounded.Destroy();
            };

            return(obj);
        }
Esempio n. 5
0
 public void Update(ValueKeeper <State> state)
 {
     stateChanges[state]();
     updates[state]();
 }