Пример #1
0
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        var data = new EnemySpawnComponent
        {
            Prefab = conversionSystem.GetPrimaryEntity(Prefab),
            Count  = Count
        };

        dstManager.AddComponentData(entity, data);
    }
Пример #2
0
        private void SpawnEnemy(EnemySpawnComponent c, int index)
        {
            ComponentManager cm = ComponentManager.GetInstance();
            Random           rand = new Random();
            int x, y;
            PositionComponent p        = new PositionComponent();
            List <IComponent> template = new List <IComponent>(c.EnemyTemplate);
            int    entity;
            Random r = new Random();
            int    shouldEnemyHaveItems = r.Next(0, 3);



            for (int i = 0; i < template.Count; i++)
            {
                template[i] = (IComponent)template[i].Clone();
            }

            for (int i = 0; i < MaxSpawnTries; i++)
            {
                x = rand.Next() % c.SpawnMaxRadius;
                y = rand.Next() % c.SpawnMaxRadius;

                if (c.CollisionComponent == null)
                {
                    p.Position = new Vector2(x, y) + c.SpawnLocation.ToVector2();
                    template.Add(p);
                    entity = EntityManager.GetEntityId();
                    cm.AddComponentsToEntity(entity, template.ToArray());
                    c.EnemyEntities[index] = entity;

                    if (shouldEnemyHaveItems > 1)
                    {
                        cm.AddComponentsToEntity(entity, new IComponent[] {
                            new InteractComponent(InteractType.Loot),
                            new ItemComponent(AddHealth, "Bread",
                                              ItemType.Consumable),
                        });
                    }
                    break;
                }
                else
                {
                    Rectangle bb = c.CollisionComponent.CollisionBox;
                    bb.Location  = c.SpawnLocation;
                    bb.Location += new Point(x, y);
                    if (CollisionSystem.DetectAreaCollision(bb).Count == 0)
                    {
                        p.Position = new Vector2(x, y) + c.SpawnLocation.ToVector2();
                        template.Add(p);
                        entity = EntityManager.GetEntityId();
                        cm.AddComponentsToEntity(entity, template.ToArray());
                        c.EnemyEntities[index] = entity;

                        if (shouldEnemyHaveItems > 1)
                        {
                            cm.AddComponentsToEntity(entity, new IComponent[] {
                                new InteractComponent(InteractType.Loot),
                                new ItemComponent(AddHealth, "Bread",
                                                  ItemType.Consumable),
                            });
                        }
                        break;
                    }
                }
            }
        }