Exemplo n.º 1
0
        /// <summary>
        /// Creates a player from a given user object - this is an entity ready to be sent into a simulation game.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static Entity CreatePlayer(User user, Vector2 location)
        {
            var entity = new Entity();

            var transformComponent = new TransformComponent(location, new Vector2(50, 70));
            var nameComponent      = new NameComponent(user.Name);
            var skinComponent      = new SkinComponent(user.SessionConfig.Skin);
            var playerComponent    = new PlayerComponent();
            var bombModifier       = new BombCountModifierComponent();
            var rangeModifier      = new RangeModifierComponent();
            var movementModifier   = new MovementModifierComponent();

            // Add modifier components to the sprite

            playerComponent.Connection  = user.Connection;
            playerComponent.SecureToken = user.SecureToken;

            entity.AddComponent(transformComponent);
            entity.AddComponent(nameComponent);
            entity.AddComponent(skinComponent);
            entity.AddComponent(playerComponent);
            entity.AddComponent(bombModifier);
            entity.AddComponent(rangeModifier);
            entity.AddComponent(movementModifier);

            return(entity);
        }
Exemplo n.º 2
0
        public override void ApplyTo(SkinComponent skinComponent)
        {
            var rotate = skinComponent.GetComponent <AutoRotate>();

            rotate.xAngle = xSpeed;
            rotate.yAngle = ySpeed;
            rotate.zAngle = zSpeed;
        }
Exemplo n.º 3
0
        public static Entity CreateBreakingBlock(Vector2 position, string spriteName)
        {
            var entity = new Entity();

            var transformComponent = new TransformComponent(position, new Vector2(32, 32));
            var skinComponent      = new SkinComponent("StandardBlock");

            entity.AddComponent(transformComponent);
            entity.AddComponent(skinComponent);
            entity.AddComponent(new OneShotAnimationComponent());

            return(entity);
        }
Exemplo n.º 4
0
        public static Entity CreateBomb(Vector2 location, Entity user)
        {
            var entity = new Entity();

            var rangeModifier = (RangeModifierComponent)user.GetComponent(typeof(RangeModifierComponent));

            var transformComponent = new TransformComponent(location, new Vector2(32, 32));
            var skinComponent      = new SkinComponent("BombSprite");
            var explosiveComponent = new ExplosiveComponent(3f, rangeModifier.Amount, user.ID);

            entity.AddComponent(transformComponent);
            entity.AddComponent(skinComponent);
            entity.AddComponent(explosiveComponent);

            return(entity);
        }
Exemplo n.º 5
0
        public static Entity CreateBombCountUpPackage(Vector2 location)
        {
            var entity = new Entity();

            var rangeModifier = new BombCountModifierComponent();
            var list          = new List <PowerUpComponent>();
            var transform     = new TransformComponent(location, new Vector2(32, 32));

            list.Add(rangeModifier);
            var powerupPackage = new PowerUpCollectionComponent(list);
            var skinComponent  = new SkinComponent(rangeModifier.SkinName);

            entity.AddComponent(powerupPackage);
            entity.AddComponent(skinComponent);
            entity.AddComponent(transform);

            return(entity);
        }
Exemplo n.º 6
0
        public static Entity CreateExplosionSprite(Vector2 position, ExplosiveType explosiveType)
        {
            var entity = new Entity();

            var transformComponent       = new TransformComponent(position, new Vector2(32, 32));
            var skinComponent            = new SkinComponent("ExplosiveSprite");
            var explosiveSpriteComponent = new ExplosionSpriteComponent();

            explosiveSpriteComponent.ExplosiveType = explosiveType;
            explosiveSpriteComponent.TimeRemaining = 1000;

            entity.AddComponent(transformComponent);
            entity.AddComponent(skinComponent);
            entity.AddComponent(explosiveSpriteComponent);
            entity.AddComponent(new OneShotAnimationComponent());

            return(entity);
        }
Exemplo n.º 7
0
        public static Entity CreateRangeModifierMaxPowerupPackage(Vector2 location)
        {
            var entity = new Entity();

            var rangeModifier = new RangeModifierComponent();

            rangeModifier.Strength = 50;
            var list      = new List <PowerUpComponent>();
            var transform = new TransformComponent(location, new Vector2(32, 32));

            list.Add(rangeModifier);
            var powerupPackage = new PowerUpCollectionComponent(list);
            var skinComponent  = new SkinComponent(rangeModifier.SkinName.Replace("Up", "Max"));

            entity.AddComponent(powerupPackage);
            entity.AddComponent(skinComponent);
            entity.AddComponent(transform);

            return(entity);
        }
Exemplo n.º 8
0
        public static Entity CreateMovementModifierPackage(Vector2 location)
        {
            var entity = new Entity();

            var movementModifier = new MovementModifierComponent();

            movementModifier.Strength = 1;

            var list      = new List <PowerUpComponent>();
            var transform = new TransformComponent(location, new Vector2(32, 32));

            list.Add(movementModifier);
            var movementPackage = new PowerUpCollectionComponent(list);
            var skinComponent   = new SkinComponent(movementModifier.SkinName);

            entity.AddComponent(movementPackage);
            entity.AddComponent(skinComponent);
            entity.AddComponent(transform);

            return(entity);
        }
Exemplo n.º 9
0
        private void ProcessLocalPlayer(Entity entity, GameTime gameTime)
        {
            // Local players can be moved automatically, then report their status if needed
            var playerTransform        = (TransformComponent)entity.GetComponent(typeof(TransformComponent));
            var playerMovementModifier = (MovementModifierComponent)entity.GetComponent(typeof(MovementModifierComponent));

            // Get the skin information so we can calculate bounding box collisions
            var playerSkin       = (SkinComponent)entity.GetComponent(typeof(SkinComponent));
            var playerDescriptor = SpriteDescriptorLookup[playerSkin.SpriteDescriptorName];

            // Move the camera
            ServiceManager.Camera.Move(-lastTransformVector);

            // Determine the movement bonus multiplier
            float movementBonus = 1.0f;

            if (playerMovementModifier != null)
            {
                movementBonus += (float)playerMovementModifier.Amount;
            }

            // Apply the multiplier to the velocity and move the position
            Vector2 nextPosition = playerTransform.LocalPosition;

            nextPosition += playerTransform.Velocity * movementBonus;

            // Clamp the x and y so the player won't keep walking offscreen
            float nextX = MathHelper.Clamp(nextPosition.X + playerDescriptor.BoundingBox.X, 0, ServiceManager.Map.WorldSizePixels.X / 2 - playerDescriptor.BoundingBox.Width);
            float nextY = MathHelper.Clamp(nextPosition.Y + playerDescriptor.BoundingBox.Y, 0, ServiceManager.Map.WorldSizePixels.Y / 2 - playerDescriptor.BoundingBox.Height);

            // TODO: This is shitty. Needs to be redone.
            if (playerTransform.Velocity.LengthSquared() != 0)
            {
                // Check Entity Collision
                if (ServiceManager.Entities != null && ServiceManager.Entities.Count() > 0)
                {
                    foreach (Entity e in ServiceManager.Entities)
                    {
                        // TODO: Bugfix! Currently 2 arrow keys are needed to get off of bombs. Fix it bitches.
                        if (e != entity && e.HasComponent(typeof(ExplosiveComponent)))
                        {
                            TransformComponent bombTransform  = (TransformComponent)e.GetComponent(typeof(TransformComponent));
                            SkinComponent      bombSkin       = (SkinComponent)e.GetComponent(typeof(SkinComponent));
                            SpriteDescriptor   bombDescriptor = SpriteDescriptorLookup[bombSkin.SpriteDescriptorName];

                            Vector2 playerOrigin = playerTransform.LocalPosition + new Vector2(playerDescriptor.BoundingBox.X, playerDescriptor.BoundingBox.Y) + new Vector2(playerDescriptor.BoundingBox.Width, playerDescriptor.BoundingBox.Height) / 2;
                            Vector2 bombOrigin   = bombTransform.LocalPosition + new Vector2(bombDescriptor.BoundingBox.X, bombDescriptor.BoundingBox.Y) + new Vector2(bombDescriptor.BoundingBox.Width, bombDescriptor.BoundingBox.Height) / 2;

                            Vector2 relativePosition = playerOrigin - bombOrigin;
                            if (Math.Abs(relativePosition.X) >= (bombDescriptor.BoundingBox.Width / 2) || Math.Abs(relativePosition.Y) >= (bombDescriptor.BoundingBox.Height / 2))
                            {
                                if ((Math.Sign(playerTransform.Velocity.X) != 0 && Math.Sign(relativePosition.X) != Math.Sign(playerTransform.Velocity.X)) || (Math.Sign(playerTransform.Velocity.Y) != 0 && Math.Sign(relativePosition.Y) != Math.Sign(playerTransform.Velocity.Y)))
                                {
                                    Rectangle bombRect = new Rectangle(
                                        (int)bombTransform.LocalPosition.X + bombDescriptor.BoundingBox.X,
                                        (int)bombTransform.LocalPosition.Y + bombDescriptor.BoundingBox.Y,
                                        (int)bombDescriptor.BoundingBox.Width,
                                        (int)bombDescriptor.BoundingBox.Height);

                                    Rectangle xBBox = new Rectangle((int)nextX,
                                                                    (int)playerTransform.LocalPosition.Y + playerDescriptor.BoundingBox.Y,
                                                                    (int)playerDescriptor.BoundingBox.Width,
                                                                    (int)playerDescriptor.BoundingBox.Height);

                                    if (bombRect.Intersects(xBBox))
                                    {
                                        nextX = playerTransform.LocalPosition.X + playerDescriptor.BoundingBox.X;
                                    }

                                    Rectangle yBBox = new Rectangle((int)nextX, (int)nextY,
                                                                    (int)playerDescriptor.BoundingBox.Width,
                                                                    (int)playerDescriptor.BoundingBox.Height);

                                    if (bombRect.Intersects(yBBox))
                                    {
                                        nextY = playerTransform.LocalPosition.Y + playerDescriptor.BoundingBox.Y;
                                    }
                                }
                            }
                        }
                    }
                }

                // Check Tile Collision
                if (ServiceManager.Map != null)
                {
                    foreach (TmxLayer layer in ServiceManager.Map.Layers)
                    {
                        foreach (TmxLayerTile tile in layer.Tiles)
                        {
                            var          set = (TmxTileset)ServiceManager.Map.Tilesets[0];
                            PropertyDict dict;

                            set.Tiles.TryGetValue((int)tile.GID, out dict);


                            // TODO: Sucks. Temporary, incomplete code. Needs to get fixed.
                            if (MapUtility.IsSolid(ServiceManager.Map.TmxMap, tile.X, tile.Y))
                            {
                                Rectangle tileRect = new Rectangle(tile.X * 32, tile.Y * 32, 32, 32);

                                Rectangle xBBox = new Rectangle((int)nextX,
                                                                (int)playerTransform.LocalPosition.Y + playerDescriptor.BoundingBox.Y,
                                                                (int)playerDescriptor.BoundingBox.Width,
                                                                (int)playerDescriptor.BoundingBox.Height);

                                if (tileRect.Intersects(xBBox))
                                {
                                    nextX = playerTransform.LocalPosition.X + playerDescriptor.BoundingBox.X;
                                }

                                Rectangle yBBox = new Rectangle((int)nextX, (int)nextY,
                                                                (int)playerDescriptor.BoundingBox.Width,
                                                                (int)playerDescriptor.BoundingBox.Height);

                                if (tileRect.Intersects(yBBox))
                                {
                                    nextY = playerTransform.LocalPosition.Y + playerDescriptor.BoundingBox.Y;
                                }
                            }
                        }
                    }
                }
            }

            playerTransform.LocalPosition = new Vector2(nextX - playerDescriptor.BoundingBox.X, nextY - playerDescriptor.BoundingBox.Y);

            float transformX = playerTransform.LocalPosition.X;
            float transformY = playerTransform.LocalPosition.Y;

            float offsetX = playerDescriptor.BoundingBox.Left + playerDescriptor.BoundingBox.Width / 2;
            float offsetY = playerDescriptor.BoundingBox.Bottom;

            transformX = Math.Max(transformX - 319 + offsetX, 0);
            transformX = Math.Min(transformX, ServiceManager.Map.WorldSizePixels.X / 2 - 638);
            transformY = Math.Max(transformY - 283 + offsetY, 0);
            transformY = Math.Min(transformY, ServiceManager.Map.WorldSizePixels.Y / 2 - 566);

            // Move the camera back
            lastTransformVector = new Vector2(transformX, transformY);
            ServiceManager.Camera.Move(lastTransformVector);

            if (playerTransform.Velocity.X != playerTransform.Velocity.Y)
            {
                if (playerTransform.Velocity.X < 0)
                {
                    playerTransform.DirectionalCache = DirectionalCache.Left;
                }

                if (playerTransform.Velocity.X > 0)
                {
                    playerTransform.DirectionalCache = DirectionalCache.Right;
                }

                if (playerTransform.Velocity.Y > 0)
                {
                    playerTransform.DirectionalCache = DirectionalCache.Down;
                }

                if (playerTransform.Velocity.Y < 0)
                {
                    playerTransform.DirectionalCache = DirectionalCache.Up;
                }
            }

            var directionalChange = (playerTransform.Velocity != playerTransform.LastVelocity &&
                                     playerTransform.Velocity != Vector2.Zero);

            directionalChange = false;

            if ((_lastReaction > MovementRate && playerTransform.Velocity != Vector2.Zero) || directionalChange)
            {
                // Alert the server out this change in events if needed
                var packet = new NotifyMovementPacket(playerTransform.Velocity, playerTransform.LocalPosition);
                NetworkManager.Instance.SendPacket(packet);

                // Reset reaction timer
                _lastReaction = 0f;
            }

            // Increment reaction timer
            _lastReaction += (float)gameTime.ElapsedGameTime.TotalSeconds;
        }
Exemplo n.º 10
0
 public override bool CanApplyTo(SkinComponent skinComponent)
 {
     return(skinComponent.GetComponent <AutoRotate>() != null);
 }