summaryrefslogtreecommitdiff
path: root/Penguloon/Scenes/SceneBase.cs
diff options
context:
space:
mode:
authoraldrikboy <aldrikboy@gmail.com>2017-12-11 22:02:13 +0100
committeraldrikboy <aldrikboy@gmail.com>2017-12-11 22:02:13 +0100
commitfd6fa4e5cebbe3edb65d50c78dcc8a97ce98ce64 (patch)
tree8950f6b9023e0b47e22e1cd4869ab76de0803f4c /Penguloon/Scenes/SceneBase.cs
parentc4c0f3c887d627b6432551e96009c7aeecd4cdd8 (diff)
First commit
Diffstat (limited to 'Penguloon/Scenes/SceneBase.cs')
-rw-r--r--Penguloon/Scenes/SceneBase.cs157
1 files changed, 157 insertions, 0 deletions
diff --git a/Penguloon/Scenes/SceneBase.cs b/Penguloon/Scenes/SceneBase.cs
new file mode 100644
index 0000000..4214e97
--- /dev/null
+++ b/Penguloon/Scenes/SceneBase.cs
@@ -0,0 +1,157 @@
+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<Vector2> SnowflakeList { get; set; } = new List<Vector2>();
+ public DateTime LastSnowflakeSpawn { get; set; }
+ public int SnowflakeSpawnDelayMS { get; set; } = 1000;
+ public float SnowflakeRotation { get; set; } = 0f;
+
+ public Main Main { get; set; }
+
+ public List<ControlBase> Controls = new List<ControlBase>();
+
+ /// <summary>
+ /// Base constructor.
+ /// </summary>
+ /// <param name="main">active Main object.</param>
+ protected SceneBase(Main main)
+ {
+ this.Main = main;
+ CreateControls();
+ }
+
+ /// <summary>
+ /// Draw this scene.
+ /// </summary>
+ /// <param name="deltaTime">Delta time.</param>
+ public virtual void Draw(float deltaTime)
+ {
+ for (int i = 0; i < Controls.Count; i++)
+ Controls[i].Draw(deltaTime);
+ }
+
+ /// <summary>
+ /// Update this scene.
+ /// </summary>
+ /// <param name="deltaTime">Delta time.</param>
+ /// <param name="touchLocations">Finger touch locations.</param>
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// Draw text at given parameters.
+ /// </summary>
+ /// <param name="font"></param>
+ /// <param name="text"></param>
+ /// <param name="position"></param>
+ /// <param name="size"></param>
+ /// <param name="textAllignment"></param>
+ /// <param name="color"></param>
+ 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));
+ }
+
+ /// <summary>
+ /// Create the controls for this scene.
+ /// </summary>
+ public abstract void CreateControls();
+ }
+} \ No newline at end of file