Пример #1
0
        static void Main(string[] args)
        {
            Log.GlobalLevel = Log.Level.Debug;

            RenderWindow app = new RenderWindow(new VideoMode(800, 600), "HP!",
                Styles.Default, new ContextSettings(0, 0, 4));
            app.SetFramerateLimit(60);
            app.Closed += delegate { app.Close(); };
            app.SetVisible(true);
            app.SetActive(true);

            L.I("Assuming assets in \"assets\"");
            Assets assets = new Assets("assets");
            LoadAnimations(assets);

            Level level1 = assets.Level("level1.json");
            Game game = new Game(assets, level1);
            var view = new View();

            int lastFrameTime = Environment.TickCount;
            while (app.IsOpen) {
                app.DispatchEvents();

                float aspect = (float)app.Size.X / app.Size.Y;
                float targetWidth = 20, targetHeight = targetWidth / aspect;
                view.Reset(new FloatRect(0, 0, targetWidth, targetHeight));
                app.SetView(view);

                int ticks = Environment.TickCount;
                float delta = (ticks - lastFrameTime) / 1000F;
                lastFrameTime = ticks;

                game.Update(delta);
                game.Render(app);

                app.Display();
            }
        }
Пример #2
0
Файл: Game.cs Проект: tilpner/hp
        public void Update(double delta, Game game) {
            Vector2f accel = v2f(0, 0);
            if (Math.Abs(Velocity.X) < MAX_WALK_SPEED) {
                if (Keyboard.IsKeyPressed(Keyboard.Key.A)) {
                    accel.X -= (float)Player.SPEED_X * HorizontalInfluence;
                }
                if (Keyboard.IsKeyPressed(Keyboard.Key.D)) {
                    accel.X += (float)Player.SPEED_X * HorizontalInfluence;
                }
            }
            
            if (Keyboard.IsKeyPressed(Keyboard.Key.Space) && NearGround) {
                state = PhysicsState.Midair;
                accel.Y -= (float) (Player.JUMP_SPEED_Y);
            }

            if (state == PhysicsState.Midair) accel += game.level.Gravity;
            // apply unproblematic amount of gravity to facilitate
            // detection of "standing on the ground" state
            else accel += DETECTION_GRAVITY;
            
            // capping
            if (Velocity.Len() > Consts.MAX_VEL) {
                Velocity = Velocity.Norm() * Consts.MAX_VEL;
            }
            
            Level.CollidedWith flags;
            Box = game.level.ResolveCollision(Box, out flags).Check();

            if ((flags & Level.CollidedWith.Bottom) == Level.CollidedWith.Bottom) {
                Velocity.Y = Math.Min(0, Velocity.Y);
                state = PhysicsState.Ground;
            }
            if ((flags & Level.CollidedWith.Left) == Level.CollidedWith.Left) {
                Velocity.X = 0;
            }
            if ((flags & Level.CollidedWith.Right) == Level.CollidedWith.Right) {
                Velocity.X = 0;
            }
            if ((flags & Level.CollidedWith.Top) == Level.CollidedWith.Top) {
                Velocity.Y = Math.Max(0, Velocity.Y);
                state = PhysicsState.Midair;
            }
            if ((int) flags == 0) {
                // No collision, assuming midair
                state = PhysicsState.Midair;
            }

            switch (state) {
                case PhysicsState.Midair:
                    HorizontalInfluence = 0.3F;
                    LastTouchedGround += delta;
                    anim.Speed = 0.3;
                    break;
                case PhysicsState.Ground:
                    Block below = game.level[(int) Math.Floor(Box.Center.X), (int) Math.Ceiling(Box.Center.Y)];
                    var friction = below.Type == null ? 32 : below.Type.Friction;

                    // If we're not walking, apply additional friction
                    if (Math.Abs(accel.X) < 0.1) Velocity.X *= (float)(1 - delta * friction).Clamp(0, 1);
                    Velocity.Y = 0;
                    HorizontalInfluence = 1F;
                    LastTouchedGround = 0;
                    anim.Speed = 1;
                    break;
            }


            // Verlet integration
            Box.Position += (float)delta * (Velocity + (float)delta * accel / 2);
            Velocity += (float)delta * accel;

            if (Math.Abs(Velocity.X) < 1) anim.Reset();
            anim.Flipped = Velocity.X < 0;
            anim.Update(delta * (Velocity.X / 4));
        }