using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input.Touch; using Penguloon.Controls; using System; using System.Collections.Generic; namespace Penguloon.Scenes { public abstract class SceneBase { public List SnowflakeList { get; set; } = new List(); public DateTime LastSnowflakeSpawn { get; set; } public int SnowflakeSpawnDelayMS { get; set; } = 1000; public float SnowflakeRotation { get; set; } = 0f; public Main Main { get; set; } public List Controls = new List(); /// /// Base constructor. /// /// active Main object. protected SceneBase(Main main) { this.Main = main; CreateControls(); } /// /// Draw this scene. /// /// Delta time. public virtual void Draw(float deltaTime) { for (int i = 0; i < Controls.Count; i++) Controls[i].Draw(deltaTime); } /// /// Update this scene. /// /// Delta time. /// Finger touch locations. public virtual void Update(float deltaTime, TouchLocation[] touchLocations) { for (int i = 0; i < Controls.Count; i++) Controls[i].Update(deltaTime, touchLocations); SnowflakeRotation += 5f * deltaTime; if ((DateTime.Now - LastSnowflakeSpawn).TotalMilliseconds > SnowflakeSpawnDelayMS) SpawnSnowflake(); for (int i = 0; i < SnowflakeList.Count; i++) { SnowflakeList[i] = new Vector2(SnowflakeList[i].X, SnowflakeList[i].Y + 5); } DeleteOutofBoundSnowflakes(); } private void DeleteOutofBoundSnowflakes() { for (int i = 0; i < SnowflakeList.Count; i++) { if (SnowflakeList[i].Y - 100 > StaticUIValues.ScreenViewport.Y) SnowflakeList.RemoveAt(i); } } /// /// Draw text at given parameters. /// /// /// /// /// /// /// public void DrawText(SpriteFont font, string text, Vector2 position, Vector2 size, TextAllignment textAllignment, Color color, Color borderColor, int borderWidth) { if (font == null) return; Vector2 textSize = font.MeasureString(text); Vector2 pos = position; switch (textAllignment) { case TextAllignment.LeftTop: pos = position; break; case TextAllignment.CenterTop: pos.X += (size.X / 2) - (textSize.X / 2); break; case TextAllignment.RightTop: pos.X += (size.X) - (textSize.X); break; case TextAllignment.LeftMiddle: pos.Y += (size.Y / 2) - (textSize.Y / 2) + 5; break; case TextAllignment.CenterMiddle: pos.X += (size.X / 2) - (textSize.X / 2); pos.Y += (size.Y / 2) - (textSize.Y / 2) + 5; break; case TextAllignment.RightMiddle: pos.X += (size.X) - (textSize.X); pos.Y += (size.Y / 2) - (textSize.Y / 2) + 5; break; case TextAllignment.LeftBottom: pos.Y += (size.Y) - (textSize.Y); break; case TextAllignment.CenterBottom: pos.X += (size.X / 2) - (textSize.X / 2); pos.Y += (size.Y) - (textSize.Y); break; case TextAllignment.RightBottom: pos.X += (size.X) - (textSize.X); pos.Y += (size.Y) - (textSize.Y); break; } // border //leftTop Main.SpriteBatch.DrawString(font, text, new Vector2(pos.X - borderWidth, pos.Y - borderWidth), borderColor); //centerTop Main.SpriteBatch.DrawString(font, text, new Vector2(pos.X, pos.Y - borderWidth), borderColor); //rightTop Main.SpriteBatch.DrawString(font, text, new Vector2(pos.X + borderWidth, pos.Y - borderWidth), borderColor); //LeftMiddle Main.SpriteBatch.DrawString(font, text, new Vector2(pos.X - borderWidth, pos.Y), borderColor); //RightMiddle Main.SpriteBatch.DrawString(font, text, new Vector2(pos.X + borderWidth, pos.Y), borderColor); //LeftBottom Main.SpriteBatch.DrawString(font, text, new Vector2(pos.X - borderWidth, pos.Y + borderWidth), borderColor); //CenterBottom Main.SpriteBatch.DrawString(font, text, new Vector2(pos.X, pos.Y + borderWidth), borderColor); //RightBottom Main.SpriteBatch.DrawString(font, text, new Vector2(pos.X + borderWidth, pos.Y + borderWidth), borderColor); // text Main.SpriteBatch.DrawString(font, text, pos, color); } protected void DrawSnowflakes() { // draw snowflakes for (int i = 0; i < SnowflakeList.Count; i++) Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/snowflake"), destinationRectangle: new Rectangle((int)SnowflakeList[i].X, (int)SnowflakeList[i].Y, StaticUIValues.SnowflakeSize, StaticUIValues.SnowflakeSize), rotation: SnowflakeRotation, origin: new Vector2(ContentManager.GetTexture("UI/snowflake").Width / 2, ContentManager.GetTexture("UI/snowflake").Height / 2)); } protected void SpawnSnowflake() { LastSnowflakeSpawn = DateTime.Now; int minX = 100, maxX = (int)StaticUIValues.ScreenViewport.X - 200; int randomPosX = new Random().Next(minX, maxX); SnowflakeList.Add(new Vector2(randomPosX, -100)); } /// /// Create the controls for this scene. /// public abstract void CreateControls(); } }