public override async Task Execute() { animationComponent = Entity.Get <AnimationComponent>(); physicsComponent = Entity.Get <PhysicsComponent>(); rigidbodyElement = (RigidbodyElement)physicsComponent.Elements[0]; rigidbodyElement.RigidBody.LinearFactor = new Vector3(0, 1, 0); //allow only Y motion rigidbodyElement.RigidBody.AngularFactor = new Vector3(0, 0, 0); //allow no rotation await Reset(); while (Game.IsRunning) { await WaitMs((int)(enemyLife * 1000)); if (exploding != null) { await exploding; exploding = null; } await Reset(); } }
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"); } } } }