Guest User

Untitled

a guest
Jan 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace GeneratorTest
  13. {
  14.     public class Entity
  15.     {
  16.         public Vector2 Position;
  17.     }
  18.  
  19.     /// <summary>
  20.     /// This is the main type for your game
  21.     /// </summary>
  22.     public class Game1 : Microsoft.Xna.Framework.Game
  23.     {
  24.         GraphicsDeviceManager graphics;
  25.         SpriteBatch spriteBatch;
  26.         float currentDelta;
  27.         List<Entity> entities = new List<Entity>();
  28.         Texture2D tex;
  29.         Stack<IEnumerator<object>> stack = new Stack<IEnumerator<object>>();
  30.  
  31.         public Game1()
  32.         {
  33.             graphics = new GraphicsDeviceManager(this);
  34.             Content.RootDirectory = "Content";
  35.         }
  36.  
  37.         /// <summary>
  38.         /// Allows the game to perform any initialization it needs to before starting to run.
  39.         /// This is where it can query for any required services and load any non-graphic
  40.         /// related content.  Calling base.Initialize will enumerate through any components
  41.         /// and initialize them as well.
  42.         /// </summary>
  43.         protected override void Initialize()
  44.         {
  45.             base.Initialize();
  46.  
  47.             entities.Add(new Entity() { Position = new Vector2(100,100) });
  48.             entities.Add(new Entity() { Position = new Vector2(300,100) });
  49.  
  50.             // Begin the cinematic.
  51.             stack.Push(Cinematic().GetEnumerator());
  52.         }
  53.  
  54.         /// <summary>
  55.         /// LoadContent will be called once per game and is the place to load
  56.         /// all of your content.
  57.         /// </summary>
  58.         protected override void LoadContent()
  59.         {
  60.             // Create a new SpriteBatch, which can be used to draw textures.
  61.             spriteBatch = new SpriteBatch(GraphicsDevice);
  62.            
  63.             tex = Content.Load<Texture2D>("Sprite");
  64.         }
  65.        
  66.         /// <summary>
  67.         /// Allows the game to run logic such as updating the world,
  68.         /// checking for collisions, gathering input, and playing audio.
  69.         /// </summary>
  70.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  71.         protected override void Update(GameTime gameTime)
  72.         {
  73.             // Allows the game to exit
  74.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  75.                 this.Exit();
  76.            
  77.             currentDelta = (float)gameTime.ElapsedGameTime.Ticks / (float)TimeSpan.TicksPerSecond;
  78.  
  79.             // This is the complicated part.
  80.             // When you run a generator you get an IEnumerable<>, then you have to call GetEnumerator()
  81.             // to get an enumerator, then for an enumerator you can do the following to step through it:
  82.             //
  83.             // while(enumerator.MoveNext())
  84.             // {
  85.             //     do something with enumerator.Current
  86.             // }
  87.             //
  88.             // In our case, we actually wait a frame before calling MoveNext again.
  89.             // Also, as a nicety, if the generator returns another IEnumerable<>, treat it like a "subroutine"
  90.             // and begin stepping it until it's over, and then return to stepping the caller.
  91.             // This is why this resembles a programming language stack behavior so much.
  92.             if (stack.Count > 0)
  93.             {
  94.                 IEnumerator<object> e = stack.Peek();
  95.                 if (e.MoveNext())
  96.                 {
  97.                     if (e.Current is IEnumerable<object>)
  98.                     {
  99.                         var newFrame = (IEnumerable<object>)e.Current;
  100.                         stack.Push(newFrame.GetEnumerator());
  101.                     }
  102.                     // otherwise it's probably null, ignore it
  103.                 }
  104.                 else
  105.                 {
  106.                     stack.Pop();
  107.                 }
  108.             }
  109.  
  110.             base.Update(gameTime);
  111.         }
  112.  
  113.         /// <summary>
  114.         /// This is called when the game should draw itself.
  115.         /// </summary>
  116.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  117.         protected override void Draw(GameTime gameTime)
  118.         {
  119.             GraphicsDevice.Clear(Color.CornflowerBlue);
  120.  
  121.             // Draw stuff
  122.             spriteBatch.Begin();
  123.             foreach (Entity ent in entities)
  124.             {
  125.                 spriteBatch.Draw(tex, new Rectangle((int)ent.Position.X, (int)ent.Position.Y, 50, 50), Color.White);
  126.             }
  127.             spriteBatch.End();
  128.  
  129.             base.Draw(gameTime);
  130.         }
  131.  
  132.         // Function that lets you wait for N seconds
  133.         public IEnumerable<object> Wait(float seconds)
  134.         {
  135.             float t = 0;
  136.             while (t < seconds)
  137.             {
  138.                 t += currentDelta;
  139.                 yield return null;
  140.             }
  141.         }
  142.  
  143.         // Lerps an entity to the target position over N seconds
  144.         public IEnumerable<object> MoveEntity(Entity ent, Vector2 targetPosition, float seconds)
  145.         {
  146.             Vector2 startPos = ent.Position;
  147.             float t = 0;
  148.  
  149.             while (t < seconds)
  150.             {
  151.                 ent.Position = Vector2.Lerp(startPos, targetPosition, t / seconds);
  152.                 t += currentDelta;
  153.                 yield return null;
  154.             }
  155.  
  156.             ent.Position = targetPosition;
  157.         }
  158.  
  159.         // This is the "cinematic" script
  160.         // Notice how it almost looks like it just blocks, but it actually runs over several frames
  161.         // Easier than dealing with an FSM
  162.         public IEnumerable<object> Cinematic()
  163.         {
  164.             Entity a = entities[0];
  165.             Entity b = entities[1];
  166.  
  167.             while (true)
  168.             {
  169.                 yield return MoveEntity(a, new Vector2(100, 200), 1.0f);
  170.                 yield return Wait(0.25f);
  171.                 yield return MoveEntity(b, new Vector2(200, 200), 1.0f);
  172.                 yield return Wait(0.5f);
  173.                 yield return MoveEntity(b, new Vector2(100, 100), 0.5f);
  174.                 yield return MoveEntity(a, new Vector2(400, 200), 0.5f);
  175.                 yield return Wait(1.0f);
  176.                 yield return MoveEntity(a, new Vector2(100, 100), 0.2f);
  177.                 yield return MoveEntity(b, new Vector2(300, 100), 0.2f);
  178.             }
  179.         }
  180.     }
  181. }
Add Comment
Please, Sign In to add comment