Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ClassBasedES
- {
- public struct vec2
- {
- public float x;
- public float y;
- public static vec2 operator +(vec2 value1, vec2 value2)
- {
- return new vec2() { x = value1.x + value2.x, y = value1.y + value2.y };
- }
- }
- /// <summary>
- /// Static component classes = no singleton needed, we just use static methods
- /// </summary>
- public class MoveComponent : IComponent
- {
- /*IComponent begin */
- public static void addComponent(entity e)
- {
- myEntitiesPositions[e] = new vec2() {x=0,y=0};
- myEntitiesVelocities[e] = new vec2() {x=0,y=0};
- }
- public static void removeComponent(entity e)
- {
- myEntitiesPositions.Remove(e);
- myEntitiesVelocities.Remove(e);
- }
- public static List<entity> getEntities()
- {
- var retval = new List<entity>();
- retval.AddRange(myEntitiesPositions.Keys);
- return retval;
- }
- /*IComponent end */
- static vec2 gravity = new vec2() { x = 0, y = 0.9f };
- //the only storage of entties (Guids) is in components
- // If we used indexed lists/arrays, the index would change = more bug prone
- // I just prefer clearer and concise code.
- public static Dictionary<entity, vec2> myEntitiesPositions = new Dictionary<entity,vec2>();
- public static Dictionary<entity, vec2> myEntitiesVelocities = new Dictionary<entity, vec2>();
- public static void moveEntities()
- {
- foreach (var entity in myEntitiesPositions)
- {
- myEntitiesVelocities[entity.Key] += gravity;
- myEntitiesPositions[entity.Key] += myEntitiesVelocities[entity.Key];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement