Exemplo n.º 1
0
        public override void Update(Game0 game)
        {
            base.Update(game);

            if (Fade)
            {
                FadeFrame += game.Player.Alive ? -FadeStep : FadeStep;

                if (FadeFrame < FadeMin || FadeMax < FadeFrame)
                {
                    FadeFrame = Math.Min(FadeMax, Math.Max(FadeMin, FadeFrame));
                    Fade      = false;

                    if (!game.Player.Alive)
                    {
                        game.ChangeState(new LoadingLevel(game.CurrentLevel.Name));
                    }
                }
            }

            if (game.Player.Alive)
            {
                // remove obsolete entities
                for (var i = game.Entities.Count - 1; i >= 0; i--)
                {
                    if ((game.Entities[i] as IDestroyable)?.Obsolete() == true)
                    {
                        game.Entities.RemoveAt(i);
                    }
                }

                // update all entities
                for (var i = game.Entities.Count - 1; i >= 0; i--)
                {
                    game.Entities[i].Update();
                }

                // player standing on obstacle (moving platform)
                if (game.Player.ObstacleAt(Side.Down))
                {
                    var bb          = game.Player.BoundingBox + game.Player.Position;
                    var underPlayer = new AABB(new Vector2f(bb.Left + 1, bb.Bottom - 1), new Vector2f(bb.Right - 1, bb.Bottom));
                    if (game.Entities.SingleOrDefault(e => e is Platform p && underPlayer.Overlap(p.BoundingBox + p.Position)) is Platform platformDown)
                    {
                        game.Player.Velocity += platformDown.Velocity;
                    }
                }

                // platform pushing player
                var obstacles = game.Entities.Where(e => (e as Obstacle) != null).Select(o => o as Obstacle).ToArray();
                foreach (var obstacle in obstacles)
                {
                    var playerAABB = game.Player.BoundingBox + game.Player.Position;
                    if (obstacle is Platform platform)
                    {
                        var platformAABB = platform.BoundingBox + platform.Position;
                        if (playerAABB.Overlap(platformAABB))
                        {
                            var overlap = AABB.Overlap(playerAABB, platformAABB);
                            game.Player.Velocity += overlap.Size * platform.Velocity.Normalize();
                        }
                    }
                }

                // solve collisions of dynamic entities
                foreach (var entity in game.Entities)
                {
                    if (entity is DynamicEntity dynamicEntity)
                    {
                        // if entity is oblivious to environment solve its collisions
                        if (dynamicEntity.Responsive)
                        {
                            Solve(game, dynamicEntity, out Vector2f position, out Vector2f velocity);
                            dynamicEntity.Position = position;
                            dynamicEntity.Velocity = velocity;
                        }
                        else
                        {
                            dynamicEntity.Position += dynamicEntity.Velocity;
                        }

                        //if (entity as Player != null || entity as Platform != null)
                        //	Log.Add(entity.ToString());
                    }
                }

                foreach (var entity in game.Entities)
                {
                    DebugData.TrackEntity(entity as ITrack);
                }

                // update camera with respect to world/level borders
                game.Camera.Update(game.Player, new AABB(Vector2f.Zero, game.CurrentLevel.Size));

                // if player is still alive
                if (!CheckPlayerDeath(game))
                {
                    // update player awarness of surrounding objects
                    game.Player.UpdateSurroundings(game.Entities.Where(e => (e as Obstacle) != null).Select(o => o as Obstacle).ToArray());

                    if (game.Entities.SingleOrDefault(e => e is LevelEndZone) is LevelEndZone levelEndZone && levelEndZone.VsEntity(game.Player))
                    {
                        game.ChangeState(new LoadingLevel(game.CurrentLevel.NextLevel));
                    }
                }
            }
        }