Exemplo n.º 1
0
        bool GrabRope(Miner miner)
        {
            List <GameObject> ropes = new List <GameObject>();

            foreach (GameObject c in GameState.NonSolids)
            {
                if (c is HangingRope)
                {
                    ropes.Add(c);
                }
            }
            AxisAllignedBoundingBox tmpBox = new AxisAllignedBoundingBox(miner.Position,
                                                                         miner.Position + miner.SpriteSize + new Vector2(0, 10));
            List <GameObject> onRope = CollisionDetector.FindCollisions(tmpBox, ropes);

            if (onRope.Count > 0)
            {
                miner.ClimbingRope = !miner.ClimbingRope;
                miner.Climbing     = !miner.Climbing;
                miner.Falling      = !miner.Falling;
                miner.Speed        = Vector2.Zero;
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        public bool pickUpCrateLeftSide(GameObject c, GameState gs)
        {
            AxisAllignedBoundingBox BBox = new AxisAllignedBoundingBox(
                new Vector2(Position.X - c.SpriteSize.X, Position.Y), new Vector2(Position.X, Position.Y + c.SpriteSize.Y));
            List <GameObject> tmpSolids = gs.GetObjects(GameState.Handling.Solid);
            // tmpSolids.Remove(c);
            List <GameObject> crateCollisions = CollisionDetector.FindCollisions(BBox, tmpSolids);

            if (crateCollisions.Count == 0)
            {
                c.Position = new Vector2(Position.X - c.SpriteSize.X, Position.Y);
                gs.Add(c, GameState.Handling.None);
                gs.Remove(c, GameState.Handling.Solid);

                HeldObj         = c;
                HeldObj.Falling = false;
                Holding         = true;
                return(true);
            }
            else
            {
                // gs.Add(c, GameState.Handling.Solid);
                MyDebugger.WriteLine("crate hits something as it is picked up");
                return(false);
            }
        }
Exemplo n.º 3
0
        public override void Use(Miner user, GameState gamestate)
        {
            if (UsesLeft <= 0)
            {
                return;
            }
            List <GameObject> hooks = new List <GameObject>();

            foreach (GameObject q in gamestate.GetObjects(GameState.Handling.Solid))
            {
                if (q is RockHook)
                {
                    hooks.Add(q);
                }
            }

            Vector2 offset = new Vector2(100, 0);
            AxisAllignedBoundingBox interactionBox = new AxisAllignedBoundingBox(
                new Vector2(user.Position.X, user.Position.Y - 750) - offset,
                user.Position + user.SpriteSize + offset);

            List <GameObject> collisions = CollisionDetector.FindCollisions(interactionBox, hooks);

            foreach (GameObject c in collisions)
            {
                bool used = (c as RockHook).HangOrTakeRope(gamestate);
                CanUseAgain = !((c as RockHook).isRope);
                if (used && !CanUseAgain)
                {
                    --UsesLeft;
                    break;
                }
            }
            CanUseAgain = UsesLeft > 0;
        }
Exemplo n.º 4
0
        public void AxisAllignesBoundingBoxes()
        {
            AxisAllignedBoundingBox a = new AxisAllignedBoundingBox(new Vector2(0, 0), new Vector2(2, 2));
            AxisAllignedBoundingBox b = new AxisAllignedBoundingBox(new Vector2(1, 1), new Vector2(3, 3));
            AxisAllignedBoundingBox c = new AxisAllignedBoundingBox(new Vector2(3, 3), new Vector2(4, 4));

            AxisAllignedBoundingBox d = new AxisAllignedBoundingBox(new Vector2(0.5f, 0.5f), new Vector2(1.5f, 1.5f));


            Assert.AreEqual(true, a.Intersects(b));
            Assert.AreEqual(true, b.Intersects(a));
            Assert.AreEqual(false, a.Intersects(c));
            Assert.AreEqual(false, c.Intersects(a));

            Assert.AreEqual(true, a.Intersects(d));
            Assert.AreEqual(true, d.Intersects(a));
        }
Exemplo n.º 5
0
        public void Draw(GameTime gameTime, int width, int height, Mode mode, Camera camera)
        {
            List <Light> lights     = new List <Light>();
            Texture2D    background = _gameState.GetBackground();

            // Render the scene
            _graphicsDevice.SetRenderTarget(_renderTargetScene);
            _graphicsDevice.Clear(Color.Gray);
            _spriteBatch.Begin(
                SpriteSortMode.Deferred,
                mode == Mode.DebugView ? BlendState.Opaque : null,
                null, null, null, null, camera.view);

            _spriteBatch.Draw(background, camera.GetCameraRectangle(background.Width, background.Height), Color.White);

            var cameraRect    = camera.GetCameraRectangle(0, 0);
            var cameraPolygon = new AxisAllignedBoundingBox(
                new Vector2(cameraRect.Left, cameraRect.Top),
                new Vector2(cameraRect.Right, cameraRect.Bottom));

            foreach (GameObject obj in _gameState.GetAll())
            {
                if (!obj.Active || !obj.Visible)
                {
                    continue;
                }
                if (!obj.BBox.Intersects(cameraPolygon))
                {
                    if (!(obj is RockHook))
                    {
                        continue;
                    }
                }
                if (obj is Miner)
                {
                    Miner     m           = obj as Miner;
                    Vector2   motionSize  = obj.SpriteSize * new Vector2(m.CurrMotion.Scale.X, m.CurrMotion.Scale.Y);
                    Rectangle source      = m.CurrMotion.SourceRectangle;
                    Rectangle destination = new Rectangle((int)obj.Position.X, (int)obj.Position.Y, (int)motionSize.X,
                                                          (int)motionSize.Y);
                    _spriteBatch.Draw(m.CurrMotion.Image, destination, source, Color.White, 0f, Vector2.Zero, m.Orientation, 0f);

                    Tool tool = m.Tool;
                    if (tool != null)
                    {
                        destination.Width  = tool.GetTexture().Width / 20;
                        destination.Height = tool.GetTexture().Height / 20;
                        destination.Y     -= 100;
                        destination.X     -= (destination.Width - (int)motionSize.X) / 2;
                        _spriteBatch.Draw(tool.GetTexture(), destination, Color.White);
                    }
                }
                else
                {
                    if (obj is RockHook)
                    {
                        HangingRope hangingRope = (obj as RockHook).GetRope();
                        _spriteBatch.Draw(
                            mode == Mode.DebugView ? _debugBox : hangingRope.SecondTexture,
                            new Rectangle(
                                (int)hangingRope.Position.X,
                                (int)hangingRope.Position.Y,
                                (int)hangingRope.SpriteSize.X,
                                (int)hangingRope.SpriteSize.Y),
                            Color.White);
                    }
                    if (obj?.Texture != null)
                    {
                        _spriteBatch.Draw(
                            mode == Mode.DebugView ? _debugBox : obj.Texture,
                            new Rectangle(
                                (int)obj.Position.X,
                                (int)obj.Position.Y,
                                (int)obj.SpriteSize.X,
                                (int)obj.SpriteSize.Y),
                            Color.White);
                    }
                }

                if (obj.Lights is List <Light> )
                {
                    lights.AddRange(obj.Lights);
                }
            }

            _spriteBatch.End();


            foreach (Miner miner in _gameState.GetActors())
            {
                if (miner.Active)
                {
                    miner.CurrMotion.Update(gameTime);
                }
            }

            // Render the Lights
            _renderTargetLights = _lightRenderer.Draw(gameTime, width, height, lights, camera.view);

            _graphicsDevice.SetRenderTarget(null);

            _graphicsDevice.Clear(Color.Black);

            _lightingEffect.CurrentTechnique.Passes[0].Apply();

            if (GameManager.RenderDark)
            {
                _spriteBatch.Begin(effect: _lightingEffect);
            }
            else
            {
                _spriteBatch.Begin();
            }

            _lightingEffect.Parameters["LightMask"].SetValue(_renderTargetLights);


            _spriteBatch.Draw(_renderTargetScene, new Rectangle(0, 0, width, height), Color.White);

            _spriteBatch.End();

            // Draw the UI interface that displays the number of miners still available in the game
            _spriteBatch.Begin();
            int i = 0;

            foreach (var tool in _gameState.Resources)
            {
                Enum.TryParse(tool.Key, out ExistingTools et);
                Texture2D toolTexture = _gameState.Tools[et].GetTexture();
                Vector2   textureSize = new Vector2(toolTexture.Width / 20, toolTexture.Height / 20);

                _spriteBatch.Draw(toolTexture, new Rectangle(100, 100 + i, (int)textureSize.X, (int)textureSize.Y), Color.White);
                _spriteBatch.DrawString(_gameState.GameFont, tool.Value.Count.ToString(), new Vector2(100 + textureSize.X + 50, 100 + i + textureSize.Y / 3)
                                        , Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                i += (int)textureSize.Y;
            }

            _spriteBatch.End();
        }
Exemplo n.º 6
0
        void CalculateAndSetNewPosition(GameObject obj, Vector2 direction)
        {
            int leftrightminer = 0; // rightminer then 1, leftminer then -1, not a miner(or they are close) then 0

            if (obj is Miner && (DistBetweenMiners() > 2500))
            {
                foreach (Miner m in GameState.GetActors())
                {
                    if (obj != m)
                    {
                        if (obj.Position.X < m.Position.X)
                        {
                            leftrightminer = -1;
                        }
                        else
                        {
                            leftrightminer = 1;
                        }
                    }
                }
            }

            // 1. calulate position without any obstacles
            if (obj.Falling)
            {
                obj.Speed += GRAVITY * (float)(gameTime - obj.LastUpdated).TotalSeconds;
            }
            direction += obj.Speed * (float)(gameTime - obj.LastUpdated).TotalSeconds;

            // 2. check for collisions in the X-axis, the Y-axis (falling and jumping against something) and the intersection of the movement
            AxisAllignedBoundingBox xBox, yBox;

            // TODO: Move into a separate function
            if (direction.X > 0) // we are moving right
            {
                xBox = new AxisAllignedBoundingBox(
                    new Vector2(obj.BBox.Max.X, obj.BBox.Min.Y),
                    new Vector2(obj.BBox.Max.X + direction.X, obj.BBox.Max.Y)
                    );
                if (leftrightminer == 1)
                {
                    direction.X = 0;
                }
            }
            else
            {
                xBox = new AxisAllignedBoundingBox(
                    new Vector2(obj.BBox.Min.X + direction.X, obj.BBox.Min.Y),
                    new Vector2(obj.BBox.Min.X, obj.BBox.Max.Y)
                    );
                if (leftrightminer == -1)
                {
                    direction.X = 0;
                }
            }


            if (direction.Y > 0) // we are moving downwards
            {
                yBox = new AxisAllignedBoundingBox(
                    new Vector2(obj.BBox.Min.X, obj.BBox.Max.Y),
                    new Vector2(obj.BBox.Max.X, obj.BBox.Max.Y + direction.Y)
                    );
            }
            else
            {
                yBox = new AxisAllignedBoundingBox(
                    new Vector2(obj.BBox.Min.X, obj.BBox.Min.Y + direction.Y),
                    new Vector2(obj.BBox.Max.X, obj.BBox.Min.Y)
                    );
            }

            // 3. check, if there are any collisions in the X-axis and correct position
            // TODO: Use GameState function instead
            List <GameObject> collisions = CollisionDetector.FindCollisions(
                xBox, GameState.Solids);

            // if obj is a miner holding an object, that object can also limit the miners movement
            List <GameObject> boxCollisions = new List <GameObject>();

            if (obj is Miner)
            {
                if ((obj as Miner).Holding)
                {
                    AxisAllignedBoundingBox xCrate;
                    Miner actor = obj as Miner;
                    if (direction.X > 0) // we are moving right
                    {
                        xCrate = new AxisAllignedBoundingBox(
                            new Vector2(actor.HeldObj.BBox.Max.X, actor.HeldObj.BBox.Min.Y),
                            new Vector2(actor.HeldObj.BBox.Max.X + direction.X, actor.HeldObj.BBox.Max.Y)
                            );
                    }
                    else
                    {
                        xCrate = new AxisAllignedBoundingBox(
                            new Vector2(actor.HeldObj.BBox.Min.X + direction.X, actor.HeldObj.BBox.Min.Y),
                            new Vector2(actor.HeldObj.BBox.Min.X, actor.HeldObj.BBox.Max.Y)
                            );
                    }
                    boxCollisions = CollisionDetector.FindCollisions(xCrate, GameState.Solids);
                }
            }

            if (collisions.Count > 0 || boxCollisions.Count > 0)
            {
                MyDebugger.WriteLine("collided with x-axis");
                direction.X = 0;
            }

            bool climbingMiner = false;

            if (obj is Miner)
            {
                climbingMiner = (obj as Miner).Climbing;
            }

            bool heldObjFalling = false;

            foreach (Miner m in GameState.GetActors())
            {
                if (m.Holding && m.HeldObj == obj)
                {
                    heldObjFalling = true;
                    break;
                }
            }

            // We only need to check things in y-axis (including intersection), if we are actually moving in it
            if (obj.Falling || climbingMiner || heldObjFalling)
            {
                // TODO: Use GameState functions instead
                collisions = CollisionDetector.FindCollisions(yBox, GameState.Solids);
                if (collisions.Count > 0)
                {
                    MyDebugger.WriteLine("collided with y-axis");

                    float lowestPoint = float.MaxValue;
                    foreach (GameObject collision in collisions)
                    {
                        lowestPoint = Math.Min(lowestPoint, collision.BBox.Min.Y);
                    }

                    obj.Speed = Vector2.Zero;
                    // TODO: Perhaps move this to the Miner class
                    if (obj is Miner && (obj as Miner).Holding)
                    {
                        (obj as Miner).HeldObj.Speed = Vector2.Zero;
                    }

                    if (direction.Y > 0) // hitting floor
                    {
                        direction.Y = (lowestPoint - obj.BBox.Max.Y) - 0.1f;
                        obj.Falling = false;

                        if (obj.ShouldDie)
                        {
                            GameState.Remove(obj);
                        }
                        if (obj is Miner && (obj as Miner).Holding)
                        {
                            (obj as Miner).HeldObj.Falling = false;
                        }
                    }

                    // if the object is being held by a miner and the objects has a collision, that miner drops the object
                    foreach (Miner m in GameState.GetActors())
                    {
                        if (m.Holding && m.HeldObj == obj)
                        {
                            MyDebugger.WriteLine("held object hits y axis");
                            GameState.Remove(obj, GameState.Handling.None);

                            obj.Falling = true;
                            GameState.Add(obj, GameState.Handling.Solid);

                            m.HeldObj = null;
                            m.Holding = false;
                        }
                    }
                }
            }


            if (!obj.Falling)
            {
                // Next, we need to check if we are falling, i.e. walking over an edge to store it to the character, to calculate the difference in height for the next iteration
                AxisAllignedBoundingBox tempBox = new AxisAllignedBoundingBox(
                    new Vector2(obj.BBox.Min.X, obj.BBox.Max.Y),
                    new Vector2(obj.BBox.Max.X, obj.BBox.Max.Y + 0.5f)
                    );

                collisions = CollisionDetector.FindCollisions(tempBox, GameState.Solids);
                if (collisions.Count == 0)
                {
                    obj.Falling = true;

                    // do not drop if object is being held by a miner
                    List <GameObject> allObjects = GameState.GetAll();
                    foreach (Miner c in GameState.GetActors())
                    {
                        if (c.Holding && c.HeldObj == obj)
                        {
                            obj.Falling = false;
                            if (c.Position.Y != obj.Position.Y)
                            {
                                obj.Position = new Vector2(obj.Position.X, c.Position.Y);
                            }
                        }
                    }

                    // correct if object is miner and is climbing a ladder
                    if (obj is Miner && (obj as Miner).Climbing)
                    {
                        obj.Falling = false;
                    }
                }
            }

            if (obj is Miner)
            {
                List <GameObject> ladders = new List <GameObject>();
                foreach (GameObject c in GameState.NonSolids)
                {
                    if (c is Ladder)
                    {
                        ladders.Add(c);
                    }
                }
                AxisAllignedBoundingBox Box = new AxisAllignedBoundingBox(
                    new Vector2(obj.BBox.Min.X, obj.BBox.Max.Y),
                    new Vector2(obj.BBox.Max.X, obj.BBox.Max.Y + 10)
                    );
                List <GameObject> onLadders = CollisionDetector.FindCollisions(Box, ladders);
                if (onLadders.Count == 0)
                {
                    (obj as Miner).Climbing = false;
                }
                else
                {
                    (obj as Miner).Climbing = true;
                    (obj as Miner).Falling  = false;
                    obj.Speed = direction;
                }

                if ((obj as Miner).ClimbingRope)
                {
                    obj.Falling = false;
                    obj.Speed   = direction;
                }

                (obj as Miner).xVel = direction.X;
                (obj as Miner).ChangeCurrentMotion();
            }

            obj.Position   += direction;
            obj.LastUpdated = gameTime;
        }
Exemplo n.º 7
0
        public void MovePlatform(Platform platform)
        {
            float offset = 0.4f;
            AxisAllignedBoundingBox interactionBBox = new AxisAllignedBoundingBox(
                new Vector2(platform.BBox.Min.X, platform.BBox.Min.Y - offset),
                new Vector2(platform.BBox.Max.X, platform.BBox.Min.Y));

            List <GameObject> actorsAndSolids = new List <GameObject>();

            foreach (GameObject g in GameState.GetObjects(GameState.Handling.Solid))
            {
                actorsAndSolids.Add(g);
            }
            foreach (Miner g in GameState.GetActors())
            {
                actorsAndSolids.Add(g as GameObject);
            }

            List <GameObject> collisions = CollisionDetector.FindCollisions(interactionBBox, actorsAndSolids);

            if (!platform.Activate)
            {
                // moving down or left
                if ((platform.IsMovingInY() && platform.Position.Y < platform.MinHeight) ||
                    (!platform.IsMovingInY() && platform.Position.X > platform.MinHeight))
                {
                    platform.Position = platform.Position + platform.DisplacementStep;
                    foreach (GameObject c in collisions)
                    {
                        if (c is Platform)
                        {
                            continue;
                        }
                        if (platform.IsMovingInY() && (c.Position.Y + c.SpriteSize.Y > platform.Position.Y + platform.SpriteSize.Y))
                        {
                            continue;
                        }
                        if (!platform.IsMovingInY() && (c.Position.X > platform.Position.X + platform.SpriteSize.X))
                        {
                            continue;
                        }

                        CalculateAndSetNewPosition(c, platform.DisplacementStep);
                        c.Falling = false;
                        c.Speed   = Vector2.Zero;
                        if ((c is Miner) && (c as Miner).Holding)
                        {
                            CalculateAndSetNewPosition((c as Miner).HeldObj, platform.DisplacementStep);
                        }
                    }
                }
            }
            else
            {
                // moving up or right
                if ((platform.IsMovingInY() && platform.Position.Y > platform.MaxHeight) ||
                    (!platform.IsMovingInY() && platform.Position.X < platform.MaxHeight))
                {
                    platform.Position = platform.Position - platform.DisplacementStep;

                    foreach (GameObject c in collisions)
                    {
                        if (c is Platform)
                        {
                            continue;
                        }
                        if (platform.IsMovingInY() && (c.Position.Y + c.SpriteSize.Y > platform.Position.Y + platform.SpriteSize.Y))
                        {
                            continue;
                        }
                        if (!platform.IsMovingInY() && (c.Position.X + c.SpriteSize.X < platform.Position.X))
                        {
                            continue;
                        }

                        CalculateAndSetNewPosition(c, -platform.DisplacementStep);
                        c.Falling = false;
                        c.Speed   = Vector2.Zero;
                        if ((c is Miner) && (c as Miner).Holding)
                        {
                            CalculateAndSetNewPosition((c as Miner).HeldObj, -platform.DisplacementStep);
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        void TryToClimb(Miner miner, Vector2 direction)
        {
            if (miner.ClimbingRope)
            {
                List <GameObject> ropes = new List <GameObject>();
                foreach (GameObject c in GameState.NonSolids)
                {
                    if (c is HangingRope)
                    {
                        ropes.Add(c);
                    }
                }

                AxisAllignedBoundingBox BBBox = new AxisAllignedBoundingBox(
                    new Vector2(miner.BBox.Min.X, miner.BBox.Max.Y),
                    new Vector2(miner.BBox.Max.X, miner.BBox.Max.Y + direction.Y)
                    );

                List <GameObject> onRope = CollisionDetector.FindCollisions(BBBox, ropes);
                if (onRope.Count > 0)
                {
                    miner.Speed    = Vector2.Zero;
                    miner.Climbing = true;
                    miner.Falling  = false;
                    CalculateAndSetNewPosition(miner, direction);
                }
                return;
            }
            List <GameObject> ladders = new List <GameObject>();

            foreach (GameObject c in GameState.NonSolids)
            {
                if (c is Ladder)
                {
                    ladders.Add(c);
                }
            }
            if (ladders.Count == 0)
            {
                return;
            }

            AxisAllignedBoundingBox Box = new AxisAllignedBoundingBox(
                new Vector2(miner.BBox.Min.X, miner.BBox.Max.Y),
                new Vector2(miner.BBox.Max.X, miner.BBox.Max.Y + direction.Y)
                );

            List <GameObject> onClimbableObj = CollisionDetector.FindCollisions(Box, ladders);

            if (onClimbableObj.Count > 0)
            {
                miner.Speed    = Vector2.Zero;
                miner.Climbing = true;
                miner.Falling  = false;
                CalculateAndSetNewPosition(miner, direction);
            }
            else
            {
                miner.Climbing = false;
            }
        }