public override void Start() { playerSprite = Entity.Get<SpriteComponent>(); playerController = Entity.Get<CharacterComponent>(); //Please remember that in the GameStudio element the parameter Step Height is extremely important, it not set properly it will cause the entity to snap fast to the ground playerController.JumpSpeed = 5.0f; playerController.Gravity = -10.0f; playerController.FallSpeed = 10.0f; playerController.ProcessCollisions = true; if (!IsLiveReloading) { Script.AddTask(async () => { while (Game.IsRunning) { var collision = await playerController.NewCollision(); // Stop if we collide from side foreach (var contact in collision.Contacts) { if (contact.Normal.X < -0.5f || contact.Normal.X > 0.5f) { movingToTarget = false; break; } } } }); PlayIdle(); } }
public override void Start() { spriteSheet = SpriteSheet; enemySpriteComponent = Entity.Get<SpriteComponent>(); if (!IsLiveReloading) { // Register our-self to the logic to detect collision Logic.WatchEnemy(Entity); Reset(); } }
protected override async Task LoadContent() { await base.LoadContent(); font = Content.Load<SpriteFont>("Font"); teapot = Content.Load<Model>("Teapot"); batch = new SpriteBatch(GraphicsDevice); BuildUI(); spriteComponent = new SpriteComponent { SpriteProvider = new SpriteFromSheet { Sheet = Content.Load<SpriteSheet>("SpriteSheet") } }; modelComponent = new ModelComponent { Model = teapot }; modelComponent2 = new ModelComponent { Model = teapot }; modelComponent3 = new ModelComponent { Model = teapot }; entity = new Entity { spriteComponent, modelComponent }; entity2 = new Entity { modelComponent2 }; entity3 = new Entity { modelComponent3 }; SceneSystem.SceneInstance.Scene.Entities.Add(entity); SceneSystem.SceneInstance.Scene.Entities.Add(entity2); SceneSystem.SceneInstance.Scene.Entities.Add(entity3); if (Input.Accelerometer.IsSupported) Input.Accelerometer.IsEnabled = true; if (Input.Compass.IsSupported) Input.Compass.IsEnabled = true; if (Input.Gyroscope.IsSupported) Input.Gyroscope.IsEnabled = true; if (Input.UserAcceleration.IsSupported) Input.UserAcceleration.IsEnabled = true; if (Input.Gravity.IsSupported) Input.Gravity.IsEnabled = true; if (Input.Orientation.IsSupported) Input.Orientation.IsEnabled = true; ChangeScene(0); }
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 = SpriteFromSheet.Create(spriteSheet, "bullet") }, // Will make the beam move along a direction at each frame 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; } } }
/// <summary> /// Stops the animation of the provided sprite component. /// </summary> /// <param name="spriteComponent">the sprite component to stop</param> public void Stop(SpriteComponent spriteComponent) { spriteComponent.ElapsedTime = 0; spriteComponent.CurrentIndexIndex = 0; spriteComponent.ClearAnimations(); playingSprites.Remove(spriteComponent); }
/// <summary> /// Resumes a previously paused animation. /// </summary> /// <param name="spriteComponent">the sprite component to resume</param> public void Resume(SpriteComponent spriteComponent) { spriteComponent.IsPaused = false; }
/// <summary> /// Pauses the animation of the provided sprite component. /// </summary> /// <param name="spriteComponent">the sprite component to pause</param> public void Pause(SpriteComponent spriteComponent) { spriteComponent.IsPaused = true; }
/// <summary> /// Queue the sprite animation defined by the provided sequence of indices at the end of the animation queue. /// </summary> /// <param name="spriteComponent">The sprite component containing the animation</param> /// <param name="indices">The sequence of indices defining the sprite animation</param> /// <param name="repeatMode">The value indicating how to loop the animation</param> /// <param name="framesPerSeconds">The animation speed in frames per second. 0 to use the sprite animation system default speed.</param> public void Queue(SpriteComponent spriteComponent, int[] indices, AnimationRepeatMode repeatMode, float framesPerSeconds = 0) { if (spriteComponent == null) return; var animationInfo = new SpriteComponent.AnimationInfo { ShouldLoop = repeatMode == AnimationRepeatMode.LoopInfinite, FramePerSeconds = framesPerSeconds > 0 ? framesPerSeconds : DefaultFramesPerSecond, SpriteIndices = SpriteComponent.GetNewSpriteIndicesList() }; foreach(var i in indices) animationInfo.SpriteIndices.Add(i); spriteComponent.Animations.Enqueue(animationInfo); playingSprites.Add(spriteComponent); }
/// <summary> /// Play the sprite animation defined by the provided sequence of indices. /// </summary> /// <param name="spriteComponent">The sprite component containing the animation</param> /// <param name="indices">The sequence of indices defining the sprite animation</param> /// <param name="repeatMode">The value indicating how to loop the animation</param> /// <param name="framesPerSeconds">The animation speed in frames per second. 0 to use the sprite animation system default speed.</param> /// <param name="clearQueuedAnimations">Indicate if queued animation should be cleared</param> public void Play(SpriteComponent spriteComponent, int[] indices, AnimationRepeatMode repeatMode, float framesPerSeconds = 0, bool clearQueuedAnimations = true) { if (spriteComponent == null) return; var animationInfo = new SpriteComponent.AnimationInfo { ShouldLoop = repeatMode == AnimationRepeatMode.LoopInfinite, SpriteIndices = SpriteComponent.GetNewSpriteIndicesList(), FramePerSeconds = framesPerSeconds > 0 ? framesPerSeconds : DefaultFramesPerSecond, }; foreach (var i in indices) animationInfo.SpriteIndices.Add(i); spriteComponent.RecycleFirstAnimation(); spriteComponent.Animations.Enqueue(animationInfo); var queuedAnimationsCount = spriteComponent.Animations.Count - 1; for (int i = 0; i < queuedAnimationsCount; i++) { var queuedAnimation = spriteComponent.Animations.Dequeue(); if (!clearQueuedAnimations) spriteComponent.Animations.Enqueue(queuedAnimation); } playingSprites.Add(spriteComponent); spriteComponent.ElapsedTime = 0; spriteComponent.CurrentIndexIndex = 0; spriteComponent.IsPaused = false; }
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 RigidbodyComponent { CanCollideWith = CollisionFilterGroupFlags.CustomFilter1, CollisionGroup = CollisionFilterGroups.DefaultFilter }; rb.ColliderShapes.Add(new ColliderShapeAssetDesc { Shape = bulletCS }); // Spawns a new bullet var bullet = new Entity { new SpriteComponent { SpriteProvider = SpriteFromSheet.Create(spriteSheet, "bullet") }, rb, 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.LinearFactor = new Vector3(1, 0, 0); rb.AngularFactor = new Vector3(0, 0, 0); rb.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"); } } } }