void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities) { for (int i = 0; i < entities.Length; i++) { ECSEntity entity = entities[i]; TimeComponent tc = engine.GetComponent <TimeComponent>(entity); tc.CurrentTime -= Time.DeltaTime; if (tc.CurrentTime <= 0f) { tc.CurrentTime += tc.Time; SpawnPointComponent pc = engine.GetComponent <SpawnPointComponent>(entity); var enemy = engine.CreateEntity(); engine.AddComponent <NameComponent>(enemy, new NameComponent() { Name = "ENEMY" }); engine.AddComponent <PositionComponent>(enemy, new PositionComponent() { Position = pc.SpawnPoint }); engine.AddComponent <VelocityComponent>(enemy, new VelocityComponent() { Velocity = new Vector2(0f, 5f) }); engine.AddComponent(enemy, new BoxColliderComponent() { Size = new Vector2(1f, 1f) }); engine.AddComponent <SpriteRendererComponent>(enemy, new SpriteRendererComponent() { RenderOffset = RenderOffset.Player, Texture = TextureManager.GetTexture("EnemyBlack2"), Sprite = new Sprite(1f, 1f) { pivot = Vector2.One * 0.5f } }); engine.AddComponent <AIComponent>(enemy, new AIComponent()); } engine.SetComponent <TimeComponent>(entity, tc); } }
void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities) { for (int i = 0; i < entities.Length; i++) { ECSEntity entity = entities[i]; if (Input.IsKeyDown(KeyCode.Space)) { WeaponComponent wc = engine.GetComponent <WeaponComponent>(entity); PositionComponent pc = engine.GetComponent <PositionComponent>(entity); ECSEntity bullet = engine.CreateEntity(); engine.AddComponent <NameComponent>(bullet, new NameComponent() { Name = "BULLET" }); engine.AddComponent <PositionComponent>(bullet, new PositionComponent() { Position = pc.Position + wc.BulletSpawnOffset }); engine.AddComponent <VelocityComponent>(bullet, new VelocityComponent() { Velocity = new Vector2(0f, -10f) }); engine.AddComponent(bullet, new BoxColliderComponent() { Size = new Vector2(1f, 1f) }); engine.AddComponent <SpriteRendererComponent>(bullet, new SpriteRendererComponent() { RenderOffset = RenderOffset.Player, Texture = TextureManager.GetTexture("LaserRed01"), Sprite = new Sprite(0.1f, 1f) { pivot = new Vector2(0.1f, 0.5f) } }); engine.AddComponent <AIComponent>(bullet, new AIComponent()); } } }
static void Main(string[] args) { // create engine ECSEngine engine = new ECSEngine(); // register components (NO MORE NEEDED) //engine.RegisterComponent<PositionComponent>(); //engine.RegisterComponent<VelocityComponent>(); // add systems MoveSystem moveSystem = new MoveSystem(); engine.AddSystem(moveSystem); for (int i = 0; i < 5; i++) { // create entities ECSEntity entity = engine.CreateEntity(); // add components engine.AddComponent(entity, new PositionComponent()); engine.AddComponent(entity, new VelocityComponent() { Velocity = new Vector3(0f, -1f * (i + 1), 0f) }); } // init engine engine.Init(); while (true) { // run engine engine.Run(); System.Threading.Thread.Sleep((int)(Time.DeltaTime * 1000)); } }
void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities) { for (int i = 0; i < entities.Length - 1; i++) { ECSEntity entity1 = entities[i]; for (int j = i + 1; j < entities.Length; j++) { ECSEntity entity2 = entities[j]; if (entity1 != entity2) { PositionComponent pc1 = engine.GetComponent <PositionComponent>(entity1); BoxColliderComponent box1 = engine.GetComponent <BoxColliderComponent>(entity1); Vector2 box1ExtentMin = this.GetExtentMin(pc1, box1); Vector2 box1ExtentMax = this.GetExtentMax(pc1, box1); PositionComponent pc2 = engine.GetComponent <PositionComponent>(entity2); BoxColliderComponent box2 = engine.GetComponent <BoxColliderComponent>(entity2); Vector2 box2ExtentMin = this.GetExtentMin(pc2, box2); Vector2 box2ExtentMax = this.GetExtentMax(pc2, box2); if (this.Intersect(box1ExtentMin, box1ExtentMax, box2ExtentMin, box2ExtentMax)) { ECSEntity collisionDataEntity = engine.CreateEntity(); engine.AddComponent(collisionDataEntity, new CollisionDataComponent() { Entity1 = entity1, Entity2 = entity2 }); } } } } }
public static void Init() { // init window Game.Window.SetDefaultOrthographicSize(20f); // add textures TextureManager.AddTexture("Player", new Texture(@"Assets\Player.png")); TextureManager.AddTexture("EnemyBlack2", new Texture(@"Assets\EnemyBlack2.png")); TextureManager.AddTexture("LaserRed01", new Texture(@"Assets\LaserRed01.png")); //add systems engine.AddSystem(new PlayerMoveSystem()); engine.AddSystem(new PlayerShootSystem()); engine.AddSystem(new AISpawnSystem()); engine.AddSystem(new AIMoveSystem()); engine.AddSystem(new DestroyEntityWhenOutOfScreenSystem()); engine.AddSystem(new CollisionDetectionSystem()); engine.AddSystem(new CollisionSolverSystem()); engine.AddSystem(new SpriteRendererSystem()); //create entities & add components #region Player ECSEntity player = engine.CreateEntity(); var halfOrthoSize = Game.Window.CurrentOrthoGraphicSize * 0.5f; engine.AddComponent(player, new NameComponent() { Name = "PLAYER" }); engine.AddComponent(player, new InputReceiverComponent()); engine.AddComponent(player, new PositionComponent() { Position = new Vector2(halfOrthoSize * Window.aspectRatio, halfOrthoSize * 1.5f) }); engine.AddComponent(player, new VelocityComponent() { Velocity = new Vector2(5f, 5f) }); engine.AddComponent(player, new BoxColliderComponent() { Size = new Vector2(1f, 1f) }); engine.AddComponent(player, new SpriteRendererComponent() { RenderOffset = RenderOffset.Player, Texture = TextureManager.GetTexture("Player"), Sprite = new Sprite(1f, 1f) { pivot = Vector2.One * 0.5f } }); engine.AddComponent(player, new WeaponComponent() { BulletSpawnOffset = new Vector2(0f, -1f) }); #endregion #region EnemySpawner(s) int spawnersCount = 5; float step = Game.Window.OrthoWidth / (float)(spawnersCount + 1); float offset = -1f; float minTime = 1f; float maxTime = 3f; for (int i = 0; i < spawnersCount; i++) { var spawner = engine.CreateEntity(); engine.AddComponent(spawner, new NameComponent() { Name = "SPAWNER " + i }); engine.AddComponent(spawner, new SpawnPointComponent() { SpawnPoint = new Vector2(step * (i + 1), offset) }); var time = (float)(minTime + Game.Random.NextDouble() * (maxTime - minTime)); engine.AddComponent(spawner, new TimeComponent() { Time = time, CurrentTime = time }); } #endregion // init engine Game.engine.Init(); }