public static void SpriteAnimation(Group group, GameTimer gameTimer) { foreach (var entity in group.Entities) { ref var animation = ref entity.GetComponent <SpriteAnimationComponent>(); if (animation.CurrentFrame >= animation.EndFrame && animation.CurrentFrameTime <= 0 && !animation.Loop) { continue; } animation.CurrentFrameTime -= gameTimer.DeltaS; if (animation.CurrentFrameTime <= 0) { if (animation.CurrentFrame >= animation.EndFrame) { if (animation.Loop) { animation.CurrentFrame = animation.StartFrame; } else { continue; } } else { animation.CurrentFrame += 1; } animation.CurrentFrameTime = animation.BaseFrameTime; EntityUtility.SetEntitySpriteFrame(entity, animation.CurrentFrame); } }
public void Draw() { if (_drawCollisionRects) { _primitiveBatch.Begin(SamplerType.Point, _playState.Camera.GetViewMatrix()); foreach (var entity in _playState.ColliderGroup.Entities) { var rect = EntityUtility.GetEntityCollisionRect(entity); _primitiveBatch.DrawFilledRect(rect, _collisionRectColor); } _primitiveBatch.End(); } ref var transform = ref _player.GetComponent <TransformComponent>();
public override void Draw(GameTimer gameTimer) { var playerRect = EntityUtility.GetEntityDrawRect(Player); Systems.RenderBackground(DrawableBackgroundGroup, Camera); SpriteBatch.Begin(SamplerType.Point, Camera.GetViewMatrix()); Systems.Render(DrawableGroup, SpriteBatch); SpriteBatch.End(); Systems.RenderMask(DrawableMaskGroup, Camera); Systems.RenderOverlay(DrawableOverlayGroup, Camera); SpriteBatch.Begin(SamplerType.Point); //SpriteBatch.DrawText(Font, playerRect.Location.ToString(), new Vector2(25), Veldrid.RgbaByte.White, 32, 1); SpriteBatch.End(); if (ShowDebug) { DebugManager.Draw(); } }
public override void Update(GameTimer gameTimer) { Systems.Physics(PhysicsGroup, ColliderGroup, gameTimer, GRAVITY, MOVE_STEP, DeathHeight); Systems.ColliderEvents(ColliderEventGroup); Systems.Death(DeathGroup); Systems.StartMovement(StartMovementGroup); Systems.StopMovement(StopMovementGroup); Systems.SpriteAnimation(SpriteAnimationGroup, gameTimer); Systems.PlayerAnimation(PlayerAnimationGroup); Systems.MovingPlatforms(MovingPlatformGroup, gameTimer); Systems.Recordings(RecordingsGroup, Player, this); // process removal queues for entities and components Registry.SystemsFinished(); var playerRect = EntityUtility.GetEntityDrawRect(Player); Camera.Center(playerRect.Center); if (ShowDebug) { DebugManager.Update(gameTimer); } }
public static void GenerateLevel(GameStatePlay gameState) { var rng = new Random(); var platformsPerRecording = 10; var platforms = (platformsPerRecording * Recordings.Count) + platformsPerRecording; var jumpHeight = 100; var jumpLength = 200; var recordingIndex = 0; var prevPlatformPosition = Vector2.Zero; var nextPlatformPosition = new Vector2(25, 500); var prevMovingPlatform = false; for (var i = 0; i < platforms; i++) { var platform = EntityBuilder.CreatePlatform(nextPlatformPosition, PlatformType.Normal); ref var drawable = ref platform.GetComponent <DrawableMaskComponent>(); var drawRect = EntityUtility.GetEntityDrawRect(platform); if (drawRect.Bottom > gameState.DeathHeight) { gameState.DeathHeight = drawRect.Bottom; } var platformSize = drawable.AtlasRect.SizeF * drawable.Scale; var verticalDirection = rng.Next(0, 10) < 5 ? -1 : 1; var platformOffset = new Vector2(platformSize.X + jumpLength, verticalDirection * (platformSize.Y + jumpHeight)); var recordingPlatform = false; if (i > 0 && i % platformsPerRecording == 0 && recordingIndex < Recordings.Count) { var data = Recordings[recordingIndex]; var recording = EntityBuilder.CreateRecording(data.Asset, drawRect.LocationF, drawRect.LocationF + new Vector2(drawRect.Width / 2 - 200, -450f), recordingIndex == Recordings.Count - 1, recordingIndex == Recordings.Count - 2); recordingIndex += 1; drawable.Color = Veldrid.RgbaFloat.Green; recordingPlatform = true; } if (i > 3 && rng.Next(0, 10) < 3) { var secondVerticalDirection = prevPlatformPosition.Y < nextPlatformPosition.Y ? 1 : -1; var secondPlatformPosition = prevPlatformPosition + new Vector2( (platformSize.X + jumpLength) / 2, ((platformSize.Y + jumpHeight) / 2) * secondVerticalDirection); EntityBuilder.CreatePlatform(secondPlatformPosition, PlatformType.Death); prevMovingPlatform = false; } else if (i > 3 && rng.Next(0, 10) < 3 && !prevMovingPlatform && !recordingPlatform) { var endPosition = nextPlatformPosition + new Vector2(platformOffset.X * 2f, 0); platform.TryAddComponent(new MovingPlatformComponent() { StartPosition = nextPlatformPosition, EndPosition = endPosition, Destination = endPosition, MoveSpeed = 200, BaseCooldown = 2f, Cooldown = 2f, }); ref var collider = ref platform.GetComponent <ColliderComponent>(); collider.EventType = ColliderEventType.MovingPlatform; nextPlatformPosition = endPosition; prevMovingPlatform = true; }
public static Entity CreatePlayer(Vector2 position) { var player = Registry.CreateEntity(); var spritesheet = AssetManager.LoadTexture2D("adventurer.png"); player.TryAddComponent(new TransformComponent() { Position = position, Rotation = 0f, }); player.TryAddComponent(new PhysicsComponent() { MaxSpeed = new Vector2(400, 1000), MoveSpeed = 850f, JumpSpeed = 900f, Acceleration = Vector2.Zero, Velocity = Vector2.Zero, IsFalling = true, }); var scale = 8; player.TryAddComponent(new DrawableMaskComponent() { Texture = new Texture2D(50, 50, Veldrid.RgbaByte.Black), AtlasRect = new Rectangle(0, 0, 50, 37), Mask = spritesheet, Origin = Vector2.Zero, Scale = new Vector2(scale), Layer = 2, FlipType = SpriteFlipType.None, Color = RgbaFloat.White, }); player.TryAddComponent(new ColliderComponent() { EventType = ColliderEventType.None, CollisionRect = new Rectangle(20, 7, 11, 29), Scale = new Vector2(scale), }); player.TryAddComponent(new PlayerComponent() { RespawnPosition = position, }); player.TryAddComponent(new SpriteComponent() { FrameSize = new Vector2I(50, 37), Size = spritesheet.Size, }); player.TryAddComponent(new PlayerAnimationComponent()); CreateOverlay(player, "adventurer-bg-08.jpg"); CreateOverlay(player, "adventurer-bg-04.jpg"); CreateOverlay(player, "adventurer-bg-07.jpg"); CreateOverlay(player, "adventurer-bg-06.jpg"); CreateOverlay(player, "adventurer-bg-05.jpg"); CreateOverlay(player, "adventurer-bg-03.jpg"); CreateOverlay(player, "adventurer-bg-01.jpg"); CreateOverlay(player, "adventurer-bg-02.jpg"); EntityUtility.PlaySpriteAnimation(player, AnimationType.Idle); return(player); } // CreatePlayer