Пример #1
0
        public static void DropEntityInventory(ECSContainer ecsContainer, Guid ownerId)
        {
            Inventory ownerInventory = ecsContainer.Inventories[ownerId];
            Position  ownerPosition  = ecsContainer.Positions[ownerId];

            foreach (Guid id in ownerInventory.EntitiesOwned)
            {
                BaseEntity itemChange = new BaseEntity(ComponentFlags.POSITION, ComponentFlags.MOVEMENT, ComponentFlags.COLLISION);
                itemChange.Position = new Position()
                {
                    OriginPosition = ownerPosition.OriginPosition
                };
                int velocity = Constants.Random.Next(Constants.Loot.MinimumDropFling, Constants.Loot.MaximumDropFling);
                itemChange.Movement = new Movement()
                {
                    BaseVelocity   = velocity,
                    MovementType   = MovementType.DIRECTED,
                    Velocity       = velocity * Constants.Random.Next(2, 6),
                    TargetPosition = ownerPosition.OriginPosition + new Vector2(Constants.Random.Next(-velocity, velocity), Constants.Random.Next(-velocity, velocity))
                };
                itemChange.Collision = new Collision()
                {
                    CollisionType   = CollisionType.ITEM,
                    Solid           = false,
                    CollisionRadius = Constants.Sprites.ItemGrid * 3
                };
                ecsContainer.AppendEntity(itemChange, id);
            }
            ownerInventory.EntitiesOwned.Clear();
        }
Пример #2
0
 public static void ResetCollisions(ref ECSContainer ecsContainer)
 {
     foreach (Entity entity in ecsContainer.Entities.Where(x => x.IsCollidable()))
     {
         ecsContainer.Collisions[entity.Id].CollidedEntities.Clear();
         ecsContainer.Collisions[entity.Id].CheckedEntities.Clear();
     }
 }
Пример #3
0
        public static void UpdateCameraTarget(ECSContainer components, Camera camera)
        {
            Entity cameraTarget = components.Entities.Where(x => x.Id == camera.TargetEntity).FirstOrDefault();

            if (cameraTarget != null && components.Positions.ContainsKey(cameraTarget.Id))
            {
                SetCameraPosition(components.Positions[cameraTarget.Id], camera);
            }
        }
Пример #4
0
        public static Guid CreateEntityFromFile(string xmlFilePath, ref ECSContainer ecsContainer)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(BaseEntity));

            using (FileStream fileStream = new FileStream(xmlFilePath, FileMode.Open))
            {
                BaseEntity entity = (BaseEntity)serializer.Deserialize(fileStream);
                return(ecsContainer.AddEntity(entity));
            }
        }
Пример #5
0
        public static Guid SpawnEntityWithOverrides(string xmlFilePath, ref ECSContainer ecsContainer, BaseEntity additions = null)
        {
            Guid id = ArkCreation.CreateEntityFromFile(xmlFilePath, ref ecsContainer);

            if (additions != null)
            {
                ecsContainer.AppendEntity(additions, id);
            }
            return(id);
        }
Пример #6
0
        public static void GenerateRandomInventoryItemsForEntity(ECSContainer ecsContainer, Guid ownerId)
        {
            List <Guid> itemIds       = new List <Guid>();
            int         numberOfItems = Constants.Random.Next(1, 15);

            for (int i = 0; i <= numberOfItems; i++)
            {
                BaseEntity item = new BaseEntity(ComponentFlags.LABEL, ComponentFlags.DISPLAY);
                item.Label = new Label()
                {
                    Color                = Color.Black,
                    Displacement         = new Vector2(0, -4),
                    WhenToShow           = WhenToShowLabel.NEVER,
                    Origin               = Vector2.Zero,
                    Rotation             = 0f,
                    Scale                = 1f,
                    DistanceRenderBuffer = 200,
                    SpriteEffect         = SpriteEffects.None,
                    Text = "item"
                };
                item.Display = new Display()
                {
                    Color           = Color.White,
                    Layer           = DisplayLayer.FOREGROUND,
                    Opacity         = 1f,
                    Origin          = new Vector2(Constants.Sprites.ItemGrid / 2, Constants.Sprites.ItemGrid / 2),
                    Rotation        = 0f,
                    Scale           = 2f,
                    SpriteEffect    = SpriteEffects.None,
                    SpriteSheetSize = Constants.Sprites.ItemGrid,
                    SpriteSheetKey  = Constants.Sprites.ItemSheetKey,
                    SpriteSheetCol  = Constants.Random.Next(1, 22),
                    SpriteSheetRow  = Constants.Random.Next(1, 14)
                };
                Guid id = ecsContainer.AddEntity(item);
                itemIds.Add(id);
            }
            BaseEntity ownerAddition = new BaseEntity(ComponentFlags.INVENTORY);

            ownerAddition.Inventory = new Inventory()
            {
                EntitiesOwned = itemIds
            };
            ecsContainer.AppendEntity(ownerAddition, ownerId);
        }
Пример #7
0
        public static void HandleCollisions(ECSContainer ecsContainer)
        {
            IEnumerable <Entity> collidableEntities = ecsContainer.Entities.Where(x => x.IsCollidable());

            foreach (Entity entity in collidableEntities)
            {
                Collision collision = ecsContainer.Collisions[entity.Id];
                if (collision.CollisionType == CollisionType.REACTOR)
                {
                    foreach (Guid collidedEntity in collision.CollidedEntities)
                    {
                        Collision collided = ecsContainer.Collisions[collidedEntity];
                        switch (collided.CollisionType)
                        {
                        case CollisionType.ITEM:
                            if (entity.HasComponents(ComponentFlags.IS_PLAYER))
                            {
                                ecsContainer.DelayedActions.Add(new Action(() =>
                                {
                                    if (entity.HasComponents(ComponentFlags.INVENTORY))
                                    {
                                        ecsContainer.Inventories[entity.Id].EntitiesOwned.Add(collidedEntity);
                                    }
                                    Entity collidedEntityObject = ecsContainer.Entities.Where(x => x.Id == collidedEntity).First();
                                    collidedEntityObject.AddComponentFlags(ComponentFlags.MOVEMENT);
                                    ecsContainer.Movements[collidedEntity] = new Movement()
                                    {
                                        BaseVelocity          = 800,
                                        MovementType          = MovementType.DIRECTED,
                                        TargetReachedBehavior = TargetReachedBehavior.HIDE,
                                        Velocity       = 800,
                                        TargetPosition = ecsContainer.Positions[entity.Id].OriginPosition
                                    };
                                }));
                            }
                            break;
                        }
                    }
                }
            }
        }
Пример #8
0
        public TestLevel(ContentManager content, Camera camera)
        {
            _content   = new ContentManager(content.ServiceProvider, content.RootDirectory);
            _labelFont = _content.Load <SpriteFont>(Constants.Fonts.TelegramaSmall);
            _spriteSheets.Add(Constants.Sprites.MonsterSheetKey, _content.Load <Texture2D>(Constants.Sprites.MonsterSheet));
            _spriteSheets.Add(Constants.Sprites.ItemSheetKey, _content.Load <Texture2D>(Constants.Sprites.ItemSheet));
            _spriteSheets.Add(Constants.Sprites.TileSheetKey, _content.Load <Texture2D>(Constants.Sprites.TileSheet));
            _spriteSheets.Add(Constants.Sprites.PlaceHolderKey, _content.Load <Texture2D>(Constants.Sprites.Placeholder));
            this._components         = new ECSContainer();
            this._dungeonGrid        = DungeonGenerationSystem.GenerateDungeon(50, 100);
            this._gridCols           = this._dungeonGrid.GetLength(0);
            this._gridRows           = this._dungeonGrid.GetLength(1);
            this._collisionPartition = CollisionSystem.CreatePartitionGrid(this._gridCols, this._gridRows);
            drawSequence[DisplayLayer.BACKGROUND] = new List <Action>();
            drawSequence[DisplayLayer.FLOOR]      = new List <Action>();
            drawSequence[DisplayLayer.FOREGROUND] = new List <Action>();
            drawSequence[DisplayLayer.NORMAL]     = new List <Action>();
            drawSequence[DisplayLayer.SUPER]      = new List <Action>();
            drawSequence[DisplayLayer.TOP]        = new List <Action>();

            #region Debug Creation
            Guid playerId = ArkCreation.SpawnEntityWithOverrides(Constants.Ark.Monsters.Player, ref this._components, new BaseEntity(ComponentFlags.POSITION)
            {
                Position = new Position()
                {
                    OriginPosition = new Vector2(20, 20)
                }
            });
            Guid testId = ArkCreation.SpawnEntityWithOverrides(Constants.Ark.Monsters.TestNpc, ref this._components, new BaseEntity(ComponentFlags.POSITION)
            {
                Position = new Position()
                {
                    OriginPosition = new Vector2(20, 20)
                }
            });
            InventorySystem.GenerateRandomInventoryItemsForEntity(this._components, testId);
            camera.TargetEntity = playerId;
            #endregion
        }
Пример #9
0
        public static void UpdateMovingEntities(Movement movement, Position position, GameTime gameTime, ECSContainer ecsContainer, Entity entity)
        {
            Vector2 Direction = Vector2.Normalize(movement.TargetPosition - position.OriginPosition);
            float   distance  = Math.Abs(Vector2.Distance(position.OriginPosition, movement.TargetPosition));

            if (distance > 10)
            {
                position.OriginPosition += Direction * new Vector2((float)movement.Velocity) * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            else
            {
                ecsContainer.DelayedActions.Add(new Action(() =>
                {
                    entity.RemoveComponentFlags(ComponentFlags.MOVEMENT);
                    switch (movement.TargetReachedBehavior)
                    {
                    case TargetReachedBehavior.HIDE:
                        entity.RemoveComponentFlags(ComponentFlags.DISPLAY);
                        entity.RemoveComponentFlags(ComponentFlags.POSITION);
                        break;

                    case TargetReachedBehavior.DESTROY:
                        ecsContainer.DestroyEntity(entity);
                        break;
                    }
                }));
            }
        }
Пример #10
0
        public static void CheckForCollisions(ECSContainer ecsContainer, List <Entity>[,] partitionGrid)
        {
            IEnumerable <Entity> collidableEntities = ecsContainer.Entities.Where(x => x.IsCollidable());

            foreach (Entity entity in collidableEntities)
            {
                //Check all 4 corners of the entity, place in grid for each if it's not already in there.
                Collision entityCollision = ecsContainer.Collisions[entity.Id];
                Position  entityPosition  = ecsContainer.Positions[entity.Id];
                Vector2   nw = new Vector2(entityPosition.OriginPosition.X - entityCollision.CollisionRadius, entityPosition.OriginPosition.Y - entityCollision.CollisionRadius);
                Vector2   ne = new Vector2(entityPosition.OriginPosition.X + entityCollision.CollisionRadius, entityPosition.OriginPosition.Y - entityCollision.CollisionRadius);
                Vector2   sw = new Vector2(entityPosition.OriginPosition.X + entityCollision.CollisionRadius, entityPosition.OriginPosition.Y + entityCollision.CollisionRadius);
                Vector2   se = new Vector2(entityPosition.OriginPosition.X - entityCollision.CollisionRadius, entityPosition.OriginPosition.Y + entityCollision.CollisionRadius);

                Vector2 gridNW = SpatialGridPosition(nw);
                Vector2 gridNE = SpatialGridPosition(ne);
                Vector2 gridSW = SpatialGridPosition(sw);
                Vector2 gridSE = SpatialGridPosition(se);

                AddToBucket(ref partitionGrid, gridNW, entity);
                if (gridNE != gridNW)
                {
                    AddToBucket(ref partitionGrid, gridNE, entity);
                }
                if (gridSE != gridNW && gridSE != gridNE)
                {
                    AddToBucket(ref partitionGrid, gridSE, entity);
                }
                if (gridSW != gridSE && gridSW != gridNW && gridSW != gridNE)
                {
                    AddToBucket(ref partitionGrid, gridSW, entity);
                }
            }

            foreach (List <Entity> entitiesInCell in partitionGrid)
            {
                if (entitiesInCell != null)
                {
                    foreach (Entity entityOne in entitiesInCell)
                    {
                        Collision collisionOne = ecsContainer.Collisions[entityOne.Id];
                        Position  positionOne  = ecsContainer.Positions[entityOne.Id];
                        Rectangle one          = new Rectangle((int)positionOne.OriginPosition.X - collisionOne.CollisionRadius, (int)positionOne.OriginPosition.Y - collisionOne.CollisionRadius, collisionOne.CollisionRadius * 2, collisionOne.CollisionRadius * 2);

                        foreach (Entity entityTwo in entitiesInCell)
                        {
                            if (entityOne != entityTwo)
                            {
                                Collision collisionTwo = ecsContainer.Collisions[entityTwo.Id];
                                Position  positionTwo  = ecsContainer.Positions[entityTwo.Id];

                                if (!collisionOne.CheckedEntities.Contains(entityTwo.Id) && (collisionOne.CollisionType == CollisionType.REACTOR || collisionTwo.CollisionType == CollisionType.REACTOR))
                                {
                                    Rectangle two = new Rectangle((int)positionTwo.OriginPosition.X - collisionTwo.CollisionRadius, (int)positionTwo.OriginPosition.Y - collisionTwo.CollisionRadius, collisionTwo.CollisionRadius * 2, collisionTwo.CollisionRadius * 2);
                                    if (one.Intersects(two))
                                    {
                                        collisionOne.CollidedEntities.Add(entityTwo.Id);
                                        collisionTwo.CollidedEntities.Add(entityOne.Id);
                                    }
                                    collisionOne.CheckedEntities.Add(entityTwo.Id);
                                    collisionTwo.CheckedEntities.Add(entityOne.Id);
                                }
                            }
                        }
                    }
                    entitiesInCell.Clear();
                }
            }
        }