private void Reset() { IsAlive = true; Entity.Transform.Position.Y = enemyInitPositionY; var random = enemyRandomLocal; // Appearance position Entity.Transform.Position.X = (((float)(random.NextDouble())) * gameWidthX) - gameWidthHalfX; // Waiting time enemyAge = enemyTimeToWait - (((float)(random.NextDouble()))); enemySpriteComponent.SpriteProvider = new SpriteFromSheet { Sheet = spriteSheet }; SpriteAnimation.Play(enemySpriteComponent, spriteSheet.FindImageIndex("active0"), spriteSheet.FindImageIndex("active1"), AnimationRepeatMode.LoopInfinite, enemyActiveFps); }
/// <summary> /// Creates a new instance of <see cref="SpriteFromSheet"/> with the specified <see cref="SpriteSheet"/>. /// <see cref="CurrentFrame"/> is initialized according to the specified <paramref name="spriteName"/>. /// </summary> /// <param name="sheet"></param> /// <param name="spriteName">The name of the sprite.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">sheet</exception> /// <exception cref="System.Collections.Generic.KeyNotFoundException">No sprite in the sheet has the given name.</exception> /// <remarks>If two sprites have the provided name then the first sprite found is used.</remarks> public static SpriteFromSheet Create(SpriteSheet sheet, string spriteName) { if (sheet == null) { throw new ArgumentNullException(nameof(sheet)); } return(new SpriteFromSheet { Sheet = sheet, CurrentFrame = sheet.FindImageIndex(spriteName), }); }
private Entity CreateSpriteEntity(SpriteSheet sheet, string frameName) { return(new Entity(frameName) { new SpriteComponent { SpriteProvider = new SpriteFromSheet { Sheet = sheet }, CurrentFrame = sheet.FindImageIndex(frameName) } }); }
private Entity CreateSpriteEntity(SpriteSheet sheet, string frameName, bool addToScene = true) { var entity = new Entity(frameName) { new SpriteComponent { SpriteProvider = new SpriteFromSheet { Sheet = sheet }, CurrentFrame = sheet.FindImageIndex(frameName) } }; if (addToScene) { entities.Add(entity); } return(entity); }
public override async Task Execute() { spriteSheet = BulletSheet; agentSpriteComponent = Entity.Get <SpriteComponent>(); var animComponent = Entity.Get <AnimationComponent>(); PlayingAnimation playingAnimation = null; // Calculate offset of the bullet from the Agent if he is facing left and right side // TODO improve this var bulletOffset = new Vector3(1.3f, 1.65f, 0f); // Initialize game entities if (!IsLiveReloading) { shootDelayCounter = 0f; isAgentFacingRight = true; currentAgentAnimation = AgentAnimation.Idle; } CurrentAgentAnimation = currentAgentAnimation; var normalScaleX = Entity.Transform.Scale.X; var bulletCS = BulletColliderShape; Task animTask = null; while (Game.IsRunning) { await Script.NextFrame(); var inputState = GetKeyboardInputState(); if (inputState == InputState.None) { inputState = GetPointerInputState(); } if (inputState == InputState.RunLeft || inputState == InputState.RunRight) { // Update Agent's position var dt = (float)Game.UpdateTime.Elapsed.TotalSeconds; Entity.Transform.Position.X += ((inputState == InputState.RunRight) ? AgentMoveDistance : -AgentMoveDistance) * dt; if (Entity.Transform.Position.X < -gameWidthHalfX) { Entity.Transform.Position.X = -gameWidthHalfX; } if (Entity.Transform.Position.X > gameWidthHalfX) { Entity.Transform.Position.X = gameWidthHalfX; } isAgentFacingRight = inputState == InputState.RunRight; // If agent face left, flip the sprite Entity.Transform.Scale.X = isAgentFacingRight ? normalScaleX : -normalScaleX; // Update the sprite animation and state CurrentAgentAnimation = AgentAnimation.Run; if (playingAnimation == null || playingAnimation.Name != "Run") { playingAnimation = animComponent.Play("Run"); } } else if (inputState == InputState.Shoot) { if (animTask != null && !animTask.IsCompleted) { continue; } if (animTask != null && animTask.IsCompleted) { playingAnimation = null; } animTask = null; var rb = new RigidbodyElement { CanCollideWith = CollisionFilterGroupFlags.CustomFilter1, CollisionGroup = CollisionFilterGroups.DefaultFilter }; rb.ColliderShapes.Add(new ColliderShapeAssetDesc { Shape = bulletCS }); // Spawns a new bullet var bullet = new Entity { new SpriteComponent { SpriteProvider = new SpriteFromSheet { Sheet = spriteSheet }, CurrentFrame = spriteSheet.FindImageIndex("bullet") }, new PhysicsComponent { Elements = { rb } }, new ScriptComponent { Scripts = { new BeamScript() } } }; bullet.Name = "bullet"; bullet.Transform.Position = (isAgentFacingRight) ? Entity.Transform.Position + bulletOffset : Entity.Transform.Position + (bulletOffset * new Vector3(-1, 1, 1)); bullet.Transform.UpdateWorldMatrix(); SceneSystem.SceneInstance.Scene.Entities.Add(bullet); rb.RigidBody.LinearFactor = new Vector3(1, 0, 0); rb.RigidBody.AngularFactor = new Vector3(0, 0, 0); rb.RigidBody.ApplyImpulse(isAgentFacingRight ? new Vector3(25, 0, 0) : new Vector3(-25, 0, 0)); // Start animation for shooting CurrentAgentAnimation = AgentAnimation.Shoot; if (playingAnimation == null || playingAnimation.Name != "Attack") { playingAnimation = animComponent.Play("Attack"); animTask = playingAnimation.Ended(); } } else { CurrentAgentAnimation = AgentAnimation.Idle; if (playingAnimation == null || playingAnimation.Name != "Stance") { playingAnimation = animComponent.Play("Stance"); } } } }
public override async Task Execute() { spriteSheet = SpriteSheet; agentSpriteComponent = Entity.Get <SpriteComponent>(); // Calculate offset of the bullet from the Agent if he is facing left and right side // TODO improve this var bulletOffset = new Vector3(1f, 0.2f, 0f); // Initialize game entities if (!IsLiveReloading) { shootDelayCounter = 0f; isAgentFacingRight = true; currentAgentAnimation = AgentAnimation.Idle; } CurrentAgentAnimation = currentAgentAnimation; while (Game.IsRunning) { await Script.NextFrame(); var inputState = GetKeyboardInputState(); if (inputState == InputState.None) { inputState = GetPointerInputState(); } // Reset the shoot delay, if state changes if (inputState != InputState.Shoot && CurrentAgentAnimation == AgentAnimation.Shoot) { shootDelayCounter = 0; } if (inputState == InputState.RunLeft || inputState == InputState.RunRight) { // Update Agent's position var dt = (float)Game.UpdateTime.Elapsed.TotalSeconds; Entity.Transform.Position.X += ((inputState == InputState.RunRight) ? AgentMoveDistance : -AgentMoveDistance) * dt; if (Entity.Transform.Position.X < -gameWidthHalfX) { Entity.Transform.Position.X = -gameWidthHalfX; } if (Entity.Transform.Position.X > gameWidthHalfX) { Entity.Transform.Position.X = gameWidthHalfX; } isAgentFacingRight = inputState == InputState.RunRight; // If agent face left, flip the sprite Entity.Transform.Scale.X = isAgentFacingRight ? 1f : -1f; // Update the sprite animation and state CurrentAgentAnimation = AgentAnimation.Run; } else if (inputState == InputState.Shoot) { // Update shootDelayCounter, and check whether it is time to create a new bullet shootDelayCounter -= (float)Game.UpdateTime.Elapsed.TotalSeconds; if (shootDelayCounter > 0) { continue; } // Reset shoot delay shootDelayCounter = AgentShootDelay; // Spawns a new bullet var bullet = new Entity { new SpriteComponent { SpriteProvider = new SpriteFromSheet { Sheet = spriteSheet }, CurrentFrame = spriteSheet.FindImageIndex("bullet") }, // Will make the beam move along a direction at each frame new ScriptComponent { Scripts = { new BeamScript { DirectionX = isAgentFacingRight ? 1f : -1f, SpriteSheet = SpriteSheet } } } }; bullet.Transform.Position = (isAgentFacingRight) ? Entity.Transform.Position + bulletOffset : Entity.Transform.Position + (bulletOffset * new Vector3(-1, 1, 1)); SceneSystem.SceneInstance.Scene.Entities.Add(bullet); Logic.WatchBullet(bullet); // Start animation for shooting CurrentAgentAnimation = AgentAnimation.Shoot; } else { CurrentAgentAnimation = AgentAnimation.Idle; } } }