Пример #1
0
        /// <summary>
        /// Handle the collision of an entity compared to an other (identified by its components)
        /// </summary>
        /// <param name="entity1">The entity</param>
        /// <param name="positionComponent2">The position of the other entity</param>
        /// <param name="boxCollisionComponent2">The box of the other entity</param>
        /// <returns>The collision side, ie where entity1 collides entity2</returns>
        private CollisionSide HandleCollision(Entity entity1, PositionComponent positionComponent2, BoxCollisionComponent boxCollisionComponent2)
        {
            PositionComponent     positionComponent1     = (PositionComponent)entity1.GetComponentOfType(typeof(PositionComponent));
            BoxCollisionComponent boxCollisionComponent1 = (BoxCollisionComponent)entity1.GetComponentOfType(typeof(BoxCollisionComponent));
            VelocityComponent     velocityComponent1     = (VelocityComponent)entity1.GetComponentOfType(typeof(VelocityComponent));

            CollisionSide collisionSide; // Where entity1 collides entity2

            // The 4 distances which indicate how the entity1 is inside entity2
            // dx1, dx2, dy1 & dy2 are respectively the ditance between the left, right, up & down side of entity1
            // and the right, left, down & up side of entity2
            float dx1 = positionComponent2.position.X + boxCollisionComponent2.size.X - positionComponent1.position.X;
            float dx2 = positionComponent1.position.X + boxCollisionComponent1.size.X - positionComponent2.position.X;
            float dy1 = positionComponent2.position.Y + boxCollisionComponent2.size.Y - positionComponent1.position.Y;
            float dy2 = positionComponent1.position.Y + boxCollisionComponent1.size.Y - positionComponent2.position.Y;

            // Determinate where the entity1 collides entity2
            if (Math.Min(dx1, dx2) < Math.Min(dy1, dy2))
            {
                if (dx1 > dx2)
                {
                    // Collision by the left
                    positionComponent1.position.X -= dx2;
                    collisionSide = CollisionSide.FROM_LEFT_SIDE;
                }
                else
                {
                    // Collision by the right
                    positionComponent1.position.X += dx1;
                    collisionSide = CollisionSide.FROM_RIGHT_SIDE;
                }
                // Negate the Y velocity
                velocityComponent1.velocity.X = 0;
            }
            else
            {
                if (dy1 > dy2)
                {
                    // Collision by the up
                    positionComponent1.position.Y -= dy2;
                    collisionSide = CollisionSide.FROM_TOP_SIDE;
                }
                else
                {
                    // Collision by the down
                    positionComponent1.position.Y += dy1;
                    collisionSide = CollisionSide.FROM_BOTTOM_SIDE;
                }

                // Negate the X velocity
                velocityComponent1.velocity.Y = 0;
            }
            return(collisionSide);
        }
Пример #2
0
        public void LoadComponent(Entity currentEntity, XElement component)
        {
            //Component
            switch (component.Attribute("Type")?.Value)
            {
            case "Physics":
                PhysicsComponent physicsComponent = new PhysicsComponent();
                physicsComponent.masse              = int.Parse(component.Element("masse")?.Value);
                physicsComponent.useGravity         = bool.Parse(component.Element("useGravity")?.Value);
                physicsComponent.useAirFriction     = bool.Parse(component.Element("useAirFriction")?.Value);
                physicsComponent.airFrictionTweaker = float.Parse(component.Element("airFrictionTweaker")?.Value, CultureInfo.InvariantCulture);
                currentEntity.AddComponent(physicsComponent);
                break;

            case "BoxCollision":
                BoxCollisionComponent boxCollisionComponent = new BoxCollisionComponent();
                boxCollisionComponent.size.X    = float.Parse(component.Element("sizeX")?.Value);
                boxCollisionComponent.size.Y    = float.Parse(component.Element("sizeY")?.Value);
                boxCollisionComponent.isTrigger = bool.Parse(component.Element("isTrigger")?.Value);
                currentEntity.AddComponent(boxCollisionComponent);
                break;

            case "Position":
                PositionComponent positionComponent = new PositionComponent();
                positionComponent.position.X  = float.Parse(component.Element("posX")?.Value);
                positionComponent.position.Y  = float.Parse(component.Element("posY")?.Value);
                positionComponent.orientation = float.Parse(component.Element("orientation")?.Value);
                currentEntity.AddComponent(positionComponent);
                break;

            case "Velocity":
                VelocityComponent velocityComponent = new VelocityComponent();
                velocityComponent.maxVelocity = float.Parse(component.Element("maxVelocity")?.Value);
                currentEntity.AddComponent(velocityComponent);
                break;

            case "Render":
                RenderComponent renderComponent = new RenderComponent();
                renderComponent.image  = component.Element("image")?.Value;
                renderComponent.size.X = int.Parse(component.Element("sizeX")?.Value);
                renderComponent.size.Y = int.Parse(component.Element("sizeY")?.Value);
                currentEntity.AddComponent(renderComponent);
                break;

            case "Script":
                ScriptComponent scriptComponent = new ScriptComponent();
                scriptComponent.Script = CreateScriptInstance(component.Element("scriptName")?.Value);
                currentEntity.AddComponent(scriptComponent);
                break;

            default:
                throw new Exception("Undefined Component");
            }
        }
Пример #3
0
        public void Update(float deltaTime)
        {
            for (int i = 0; i < _entities.Count; i++)
            {
                PositionComponent     positionComponent1     = (PositionComponent)_entities[i].GetComponentOfType(typeof(PositionComponent));
                BoxCollisionComponent boxCollisionComponent1 = (BoxCollisionComponent)_entities[i].GetComponentOfType(typeof(BoxCollisionComponent));
                for (int j = i + 1; j < _entities.Count; j++)
                {
                    PositionComponent     positionComponent2     = (PositionComponent)_entities[j].GetComponentOfType(typeof(PositionComponent));
                    BoxCollisionComponent boxCollisionComponent2 = (BoxCollisionComponent)_entities[j].GetComponentOfType(typeof(BoxCollisionComponent));

                    if (positionComponent2.position.X + boxCollisionComponent2.size.X > positionComponent1.position.X &&
                        positionComponent1.position.X + boxCollisionComponent1.size.X > positionComponent2.position.X &&
                        positionComponent2.position.Y + boxCollisionComponent2.size.Y > positionComponent1.position.Y &&
                        positionComponent1.position.Y + boxCollisionComponent1.size.Y >
                        positionComponent2.position.Y)
                    {
                        // Collision detected
                        CollisionSide collisionSide1 = CollisionSide.UNKNOWN;
                        CollisionSide collisionSide2 = CollisionSide.UNKNOWN;

                        if (!boxCollisionComponent1.isTrigger && !boxCollisionComponent2.isTrigger)
                        {
                            if (_entities[i].GetComponentOfType(typeof(VelocityComponent)) != null)
                            {
                                collisionSide1 = HandleCollision(_entities[i], positionComponent2,
                                                                 boxCollisionComponent2);
                            }
                            if (_entities[j].GetComponentOfType(typeof(VelocityComponent)) != null)
                            {
                                collisionSide2 = HandleCollision(_entities[j], positionComponent1,
                                                                 boxCollisionComponent1);
                            }
                        }
                        // Create the CollisionEvent for entity 1 :
                        CollisionEvent gameEvent1 = new CollisionEvent(
                            _entities[i],
                            _entities[j],
                            collisionSide1);
                        _gameEngine.GetEventManager().AddEvent(gameEvent1);
                        // Create the CollisionEvent for entity 2 :
                        CollisionEvent gameEvent2 = new CollisionEvent(
                            _entities[j],
                            _entities[i],
                            collisionSide2);
                        _gameEngine.GetEventManager().AddEvent(gameEvent2);
                    }
                }
            }
        }
Пример #4
0
        public Hero(AbstractScene scene, Vector2 position) : base(scene.LayerManager.EntityLayer, null, position)
        {
            //DEBUG_SHOW_PIVOT = true;

            AddTag("Hero");
            AddCollisionAgainst("Enemy");
            AddCollisionAgainst("Spikes");
            AddCollisionAgainst("Trap");

            CanFireTriggers = true;

            DrawPriority = 10;

            AddCollisionAgainst("Pickup");
            AddCollisionAgainst("Door", false);
            AddCollisionAgainst("Key");
            AddCollisionAgainst("MountedGunBullet");

            SetupController();

            CollisionOffsetBottom = 1;
            CollisionOffsetLeft   = 0.5f;
            CollisionOffsetRight  = 0.5f;
            CollisionOffsetTop    = 1;

            CurrentFaceDirection = Direction.EAST;

            collisionComponent = new BoxCollisionComponent(this, 18, 30, new Vector2(-8, -30));
            AddComponent(collisionComponent);
            //DEBUG_SHOW_COLLIDER = true;

            AnimationStateMachine animations = new AnimationStateMachine();

            animations.Offset = offset;
            AddComponent(animations);

            SpriteSheetAnimation idleLeft = new SpriteSheetAnimation(this, Assets.GetTexture("HeroIdle"), 24);

            /*idleLeft.AnimationSwitchCallback = () => { if (CurrentWeapon != null) CurrentWeapon.SetAnimationBreathingOffset(Vector2.Zero); };
             * idleLeft.EveryFrameAction = (frame) =>
             *  {
             *      if (CurrentWeapon == null) return;
             *      float unit = 0.5f;
             *      float offsetY = 0;
             *      if (frame == 2 || frame == 3 || frame == 4)
             *      {
             *          offsetY += unit;
             *      }
             *      else if (frame == 6 || frame == 7)
             *      {
             *          offsetY -= unit;
             *      }
             *      CurrentWeapon.SetAnimationBreathingOffset(new Vector2(0, offsetY));
             *  };*/
            idleLeft.StartedCallback = () =>
            {
                if (CurrentWeapon == null)
                {
                    return;
                }
                if (CurrentWeapon is Shotgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Machinegun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Handgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
            };
            animations.RegisterAnimation("IdleLeft", idleLeft, () => previousPosition == Transform.Position && CurrentFaceDirection == Direction.WEST);

            SpriteSheetAnimation idleRight = idleLeft.CopyFlipped();

            animations.RegisterAnimation("IdleRight", idleRight, () => previousPosition == Transform.Position && CurrentFaceDirection == Direction.EAST);

            SpriteSheetAnimation runLeft = new SpriteSheetAnimation(this, Assets.GetTexture("HeroRun"), 40);

            runLeft.StartedCallback = () =>
            {
                if (CurrentWeapon == null)
                {
                    return;
                }
                if (CurrentWeapon is Shotgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Machinegun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Handgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(1, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(-1, -17));
                }
            };
            animations.RegisterAnimation("RunLeft", runLeft, () => Velocity.X < 0 && CurrentFaceDirection == Direction.WEST);

            SpriteSheetAnimation runRight = runLeft.CopyFlipped();

            animations.RegisterAnimation("RunRight", runRight, () => Velocity.X > 0 && CurrentFaceDirection == Direction.EAST);

            SpriteSheetAnimation runBackwardsLeft = new SpriteSheetAnimation(this, Assets.GetTexture("HeroRunBackwards"), 40);

            runBackwardsLeft.StartedCallback = () =>
            {
                if (CurrentWeapon == null)
                {
                    return;
                }
                if (CurrentWeapon is Shotgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Machinegun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Handgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(1, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(-1, -17));
                }
            };
            animations.RegisterAnimation("RunBackwardsLeft", runBackwardsLeft, () => Velocity.X > 0 && CurrentFaceDirection == Direction.WEST);

            SpriteSheetAnimation runRighrunBackwardsRight = runBackwardsLeft.CopyFlipped();

            animations.RegisterAnimation("RunBackwardsRight", runRighrunBackwardsRight, () => Velocity.X < 0 && CurrentFaceDirection == Direction.EAST);

            SpriteSheetAnimation jumpLeft = new SpriteSheetAnimation(this, Assets.GetTexture("HeroRun"), 1);

            jumpLeft.StartFrame = 6;
            jumpLeft.EndFrame   = 7;
            animations.RegisterAnimation("JumpLeft", jumpLeft, () => !IsOnGround && CurrentFaceDirection == Direction.WEST, 1);

            SpriteSheetAnimation jumpRight = jumpLeft.CopyFlipped();

            animations.RegisterAnimation("JumpRight", jumpRight, () => !IsOnGround && CurrentFaceDirection == Direction.EAST, 1);

            SpriteSheetAnimation jetpackLeft = new SpriteSheetAnimation(this, Assets.GetTexture("Jetpacking"), 40);

            animations.RegisterAnimation("JetpackLeft", jetpackLeft, () => flying && CurrentFaceDirection == Direction.WEST, 2);

            SpriteSheetAnimation jetpackRight = jetpackLeft.CopyFlipped();

            animations.RegisterAnimation("JetpackRight", jetpackRight, () => flying && CurrentFaceDirection == Direction.EAST, 2);

            SpriteSheetAnimation kickLeft = new SpriteSheetAnimation(this, Assets.GetTexture("Kick"), 40);

            kickLeft.Looping = false;
            kickLeft.AddFrameAction(5, (frame) =>
            {
                AudioEngine.Play("Kick");
                IsKicking = true;
                if (THIS_IS_SPARTAAAAA && collidingWith.Count > 0)
                {
                    THIS_IS_SPARTAAAAA    = false;
                    Vector2 canSpawnPos   = collidingWith[0].Transform.Position;
                    FuelCan can           = new FuelCan(scene, canSpawnPos + new Vector2(0, -20), TankCapacity);
                    can.Velocity         += new Vector2(-3, -1);
                    can.CollisionsEnabled = false;

                    Ammo ammo              = new Ammo(scene, canSpawnPos + new Vector2(0, -20), 20, typeof(Handgun));
                    ammo.Velocity         += new Vector2(-4f, -1.5f);
                    ammo.CollisionsEnabled = false;
                    Timer.TriggerAfter(2000, () =>
                    {
                        can.CollisionsEnabled  = true;
                        ammo.CollisionsEnabled = true;

                        new TextPopup(Scene, Assets.GetTexture("FuelAmmoText"), Transform.Position + new Vector2(-20, -80), 0.3f, 3000);
                    });
                }
            });
            kickLeft.AddFrameAction(6, (frame) =>
            {
                IsKicking = false;
            });
            kickLeft.StartedCallback = () =>
            {
                if (THIS_IS_SPARTAAAAA && collidingWith.Count > 0)
                {
                    THIS_IS_SPARTA();
                    Globals.FixedUpdateMultiplier = 0.1f;
                    Timer.TriggerAfter(3000, () =>
                    {
                        Globals.FixedUpdateMultiplier = 0.5f;

                        Timer.Repeat(1000, (elapsedTime) =>
                        {
                            Scene.Camera.Zoom -= 0.002f * elapsedTime;
                        });
                    });
                }
                //IsKicking = true;
                if (IsOnGround)
                {
                    MovementSpeed = 0;
                }

                if (CurrentWeapon == null)
                {
                    return;
                }
                if (CurrentWeapon is Shotgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Machinegun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Handgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(1, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(-1, -17));
                }
            };

            kickLeft.AnimationSwitchCallback = () =>
            {
                IsKicking     = false;
                MovementSpeed = Config.CHARACTER_SPEED;
                foreach (AbstractEnemy enemy in collidingWith)
                {
                    enemy.IsKicked = false;
                }
            };
            animations.RegisterAnimation("KickLeft", kickLeft, () => false);

            SpriteSheetAnimation kickRight = kickLeft.CopyFlipped();

            animations.RegisterAnimation("KickRight", kickRight, () => false);

            SpriteSheetAnimation kickJetpackLeft = new SpriteSheetAnimation(this, Assets.GetTexture("KickJetpacking"), 40);

            kickJetpackLeft.Looping = false;
            kickJetpackLeft.AddFrameAction(5, (frame) =>
            {
                AudioEngine.Play("Kick");
                IsKicking = true;
            });
            kickJetpackLeft.AddFrameAction(6, (frame) =>
            {
                IsKicking = false;
            });
            kickJetpackLeft.StartedCallback = () =>
            {
                //IsKicking = true;
                if (CurrentWeapon == null)
                {
                    return;
                }
                if (CurrentWeapon is Shotgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Machinegun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17));
                }
                else if (CurrentWeapon is Handgun)
                {
                    (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(1, -17));
                    (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(-1, -17));
                }
            };
            kickJetpackLeft.AnimationSwitchCallback = () =>
            {
                IsKicking = false;
            };
            animations.RegisterAnimation("KickJetpackLeft", kickJetpackLeft, () => false);

            SpriteSheetAnimation kickJetpackRight = kickJetpackLeft.CopyFlipped();

            animations.RegisterAnimation("KickJetpackRight", kickJetpackRight, () => false);

            animations.AddFrameTransition("RunLeft", "RunBackwardsLeft");
            animations.AddFrameTransition("RunRight", "RunBackwardsRight");
            animations.AddFrameTransition("RunLeft", "RunRight");
            animations.AddFrameTransition("RunBackwardsLeft", "RunBackwardsRight");
            animations.AddFrameTransition("JetpackLeft", "JetpackRight");

            Visible = true;
            Active  = true;

            /*DebugFunction = () =>
             * {
             *  return "Fuel: " + (int)(((float)(Fuel / TankCapacity)) * 100) + " %";
             * };*/

            CurrentWeapon = new Handgun(scene, this);
            weapons.Add(CurrentWeapon);
            Machinegun mg = new Machinegun(scene, this);

            mg.Visible = false;
            Shotgun sg = new Shotgun(scene, this);

            sg.Visible = false;
            weapons.Add(mg);
            weapons.Add(sg);
        }