public TextBalloon(Mobile mobile, PlayerMessage playerMessage, int defaultYOffset, Action <TextBalloon> onVanishBalloon, float depthParameter) { Mobile = mobile; OnVanishBalloon = onVanishBalloon; this.defaultYOffset = defaultYOffset; TextBalloonType type = TextBalloonType.Normal; string path = $"Interface/InGame/HUD/Blue/TextBalloon/{type}/"; string framePath = $"{path}/BalloonFrame"; string hBorderPath = $"{path}/BalloonFrameHorizontalBorders"; string vBorderPath = $"{path}/BalloonFrameVerticalBorders"; string centerPath = $"{path}/BalloonCenter"; Dictionary <BalloonBorder, Rectangle> preset = textBalloonPresets[type]; spriteList = new List <Sprite>(); spriteList.Add(topLeftBorder = new Sprite(framePath, layerDepth: depthParameter, sourceRectangle: preset[BalloonBorder.TopLeft])); spriteList.Add(topRightBorder = new Sprite(framePath, layerDepth: depthParameter, sourceRectangle: preset[BalloonBorder.TopRight])); spriteList.Add(bottomLeftBorder = new Sprite(framePath, layerDepth: depthParameter, sourceRectangle: preset[BalloonBorder.BottomLeft])); spriteList.Add(bottomRightBorder = new Sprite(framePath, layerDepth: depthParameter, sourceRectangle: preset[BalloonBorder.BottomRight])); spriteList.Add(arrow = new Sprite(framePath, layerDepth: depthParameter, sourceRectangle: preset[BalloonBorder.Arrow])); spriteList.Add(topBorder = new Sprite(vBorderPath, layerDepth: depthParameter, sourceRectangle: preset[BalloonBorder.Top])); spriteList.Add(bottomBorder = new Sprite(vBorderPath, layerDepth: depthParameter, sourceRectangle: preset[BalloonBorder.Bottom])); spriteList.Add(leftBorder = new Sprite(hBorderPath, layerDepth: depthParameter, sourceRectangle: preset[BalloonBorder.Left])); spriteList.Add(rightBorder = new Sprite(hBorderPath, layerDepth: depthParameter, sourceRectangle: preset[BalloonBorder.Right])); spriteList.Add(centerSquare = new Sprite(centerPath, layerDepth: depthParameter - 0.01f, sourceRectangle: preset[BalloonBorder.Center])); spriteList.ForEach((x) => x.Pivot = Vector2.Zero); arrow.Pivot += new Vector2(arrow.SourceRectangle.Width / 2, 0).ToIntegerDomain(); spriteTextList = SpriteText.CreatePlayerTextBalloonMessage(mobile.Owner, playerMessage, 20, depthParameter); //Fix string size textSize = TextSize(); balloonDesiredSize = textSize + new Vector2( preset[BalloonBorder.TopLeft].Width * 2, preset[BalloonBorder.TopLeft].Height * 2 + textSize.Y * (spriteTextList.Count - 1)); balloonInitialSize = new Vector2(10, 10); balloonExpandedSize = balloonDesiredSize - balloonInitialSize; completeText = new string[spriteTextList.Count]; typingIndexMatrix = new int[spriteTextList.Count]; for (int i = 0; i < spriteTextList.Count; i++) { completeText[i] = spriteTextList[i].Text; spriteTextList[i].Text = ""; } textBalloonAnimation = TextBalloonAnimation.Spawning; animationTimer = 0f; expansionFactor = 0f; //Resetting text transparency spriteList.ForEach((x) => x.SetTransparency(0)); //Updating position UpdateBalloonPosition(); }
public void UpdateBalloonAnimation(GameTime gameTime) { switch (textBalloonAnimation) { case TextBalloonAnimation.Spawning: expansionFactor += (float)gameTime.ElapsedGameTime.TotalSeconds * 3; animationTimer = (float)Math.Sin(expansionFactor); Vector2 newBalloonSize = balloonInitialSize + balloonExpandedSize * animationTimer; if (expansionFactor > MathHelper.PiOver2) { textBalloonAnimation = TextBalloonAnimation.SpawningWriting; newBalloonSize = balloonInitialSize + balloonExpandedSize; expansionFactor = MathHelper.PiOver2; animationTimer = 1; } bottomBorder.Scale = topBorder.Scale = newBalloonSize * Vector2.UnitX + Vector2.UnitY; rightBorder.Scale = leftBorder.Scale = newBalloonSize * Vector2.UnitY + Vector2.UnitX; centerSquare.Scale = Vector2.Ceiling(newBalloonSize) + Vector2.One * 2; //Transparency spriteList.ForEach((x) => x.SetTransparency(animationTimer)); break; case TextBalloonAnimation.SpawningWriting: animationTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (animationTimer >= 0.02f) { animationTimer = 0f; bool hasChanged = false; for (int i = 0; i < typingIndexMatrix.Length; i++) { if (typingIndexMatrix[i] < completeText[i].Length) { spriteTextList[i].Text += completeText[i][typingIndexMatrix[i]]; typingIndexMatrix[i]++; hasChanged = true; break; } } if (!hasChanged) { animationTimer = 1f; textBalloonAnimation = TextBalloonAnimation.Spawned; } } break; case TextBalloonAnimation.Spawned: vanishTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (vanishTimer >= 5) { textBalloonAnimation = TextBalloonAnimation.Vanishing; } break; case TextBalloonAnimation.Vanishing: animationTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds; spriteList.ForEach((x) => x.SetTransparency(animationTimer)); spriteTextList.ForEach((x) => x.SetTransparency((animationTimer - 0.2f) / 2)); if (animationTimer <= 0) { textBalloonAnimation = TextBalloonAnimation.Vanished; } break; case TextBalloonAnimation.Vanished: OnVanishBalloon?.Invoke(this); break; } }