Advertisement
Guest User

MoveComponent.cs

a guest
Jun 6th, 2015
477
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ClassBasedES
  8. {
  9.     public struct vec2
  10.     {
  11.         public float x;
  12.         public float y;
  13.  
  14.         public static vec2 operator +(vec2 value1, vec2 value2)
  15.         {
  16.             return new vec2() { x = value1.x + value2.x, y = value1.y + value2.y };
  17.         }
  18.     }
  19.  
  20.     /// <summary>
  21.     /// Static component classes = no singleton needed, we just use static methods
  22.     /// </summary>
  23.     public class MoveComponent : IComponent
  24.     {
  25.         /*IComponent  begin */
  26.         public static void addComponent(entity e)
  27.         {
  28.             myEntitiesPositions[e] = new vec2() {x=0,y=0};
  29.             myEntitiesVelocities[e] = new vec2() {x=0,y=0};
  30.         }
  31.         public static void removeComponent(entity e)
  32.         {
  33.             myEntitiesPositions.Remove(e);
  34.             myEntitiesVelocities.Remove(e);
  35.         }
  36.         public static List<entity> getEntities()
  37.         {
  38.             var retval = new List<entity>();
  39.             retval.AddRange(myEntitiesPositions.Keys);
  40.             return retval;
  41.         }
  42.         /*IComponent  end */
  43.  
  44.  
  45.  
  46.  
  47.         static vec2 gravity = new vec2() { x = 0, y = 0.9f };
  48.  
  49.         //the only storage of entties (Guids) is in components
  50.         //  If we used indexed lists/arrays, the index would change = more bug prone
  51.         //  I just prefer clearer and concise code.
  52.         public static Dictionary<entity, vec2> myEntitiesPositions = new Dictionary<entity,vec2>();
  53.         public static Dictionary<entity, vec2> myEntitiesVelocities = new Dictionary<entity, vec2>();
  54.  
  55.         public static void moveEntities()
  56.         {
  57.             foreach (var entity in myEntitiesPositions)
  58.             {
  59.                 myEntitiesVelocities[entity.Key] += gravity;
  60.                 myEntitiesPositions[entity.Key] += myEntitiesVelocities[entity.Key];
  61.             }
  62.         }
  63.  
  64.  
  65.        
  66.  
  67.  
  68.     }
  69. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement