Created
February 24, 2024 15:43
-
-
Save waquwex/5c50b338e0aa204f92838536523c0be1 to your computer and use it in GitHub Desktop.
Stride SpriteSheet anim with AsyncScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Stride.Core.Mathematics; | |
using Stride.Input; | |
using Stride.Engine; | |
using System.Diagnostics; | |
using Stride.Graphics; | |
using Stride.Rendering.Sprites; | |
namespace PlatformerGame.Assets.Scripts | |
{ | |
public class SpriteAnimator : AsyncScript | |
{ | |
public int DelayBetweenFramesMS { get; set; } = 150; | |
public SpriteSheet _SpriteSheet; | |
private SpriteComponent spriteComponent { get; set; } | |
const string FLY_SPRITE_GROUP = "batFly"; | |
public override async Task Execute() | |
{ | |
spriteComponent = new SpriteComponent(); | |
Entity.Add(spriteComponent); | |
var flyStartIndex = -1; | |
var flyEndIndex = -1; | |
for (var i = 0; i < _SpriteSheet.Sprites.Count; i++) | |
{ | |
if (_SpriteSheet.Sprites[i].Name == FLY_SPRITE_GROUP) | |
{ | |
if (flyStartIndex == -1) // Occurs single time | |
{ | |
flyStartIndex = i; | |
} | |
flyEndIndex = i; | |
} | |
} | |
for (var i = flyStartIndex; i <= flyEndIndex ; i++) | |
{ | |
spriteComponent.SpriteProvider = new SpriteFromSheet() | |
{ | |
Sheet = _SpriteSheet, | |
CurrentFrame = i | |
}; | |
if (i == flyEndIndex) | |
{ | |
i = 0; | |
} | |
await Task.Delay(DelayBetweenFramesMS); | |
} | |
await Script.NextFrame(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment