Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        private void LoadTools(GameState gameState)
        {
            String      toolSpritePath = "Sprites/Tools/";
            ToolFactory toolFactory    = new ToolFactory();

            foreach (ExistingTools et in Enum.GetValues(typeof(ExistingTools)))
            {
                Texture2D toolSprite = ContentManager.Load <Texture2D>(toolSpritePath + et.ToString());
                switch (et)
                {
                case ExistingTools.pickaxe:
                    Pickaxe.ToolSprite = toolSprite;
                    break;

                case ExistingTools.rope:
                    Rope.ToolSprite = toolSprite;
                    break;

                default:
                    MyDebugger.WriteLine(
                        string.Format("GameObject '{0}' cannot be created", true));
                    break;
                }

                Tool tool = toolFactory.Create(new Obj {
                    Type = et.ToString()
                });
                gameState.AddTool(et, tool);
            }
        }
Esempio n. 3
0
 private void HandleMouse(MouseState ms)
 {
     if (ms.LeftButton == ButtonState.Pressed)
     {
         MyDebugger.WriteLine(ms.Position.X);
         MyDebugger.WriteLine(ms.Position.Y);
     }
 }
Esempio n. 4
0
 private void HandleMouse()
 {
     if (_mouseState.LeftButton == ButtonState.Pressed)
     {
         MyDebugger.Write("(" + _mouseState.Position.X + ", ", true);
         MyDebugger.WriteLine(_mouseState.Position.Y + ")", true);
     }
 }
Esempio n. 5
0
        public override GameObject Create(
            Object obj)
        {
            GameObject instance;
            Obj        entity = obj as Obj;

            switch (entity.Type.ToLower())
            {
            case "miner":
                instance = new Miner(
                    entity.Position,
                    entity.SpriteSize)
                {
                    Speed         = entity.Velocity,
                    Mass          = entity.Mass,
                    TextureString = entity?.TextureString,
                    Handling      = GameState.Handling.Actor,
                    Tool          = _toolFactory.Create(new Obj {
                        Type = entity.Tool
                    })
                };
                break;

            case "ground":
                instance = new Ground(
                    entity.Position,
                    entity.SpriteSize)
                {
                    TextureString = entity?.TextureString,
                    Handling      = GameState.Handling.Solid
                };
                break;

            case "rock":
                instance = new Rock(
                    entity.Position,
                    entity.SpriteSize)
                {
                    // compute the mass of the rock depending on the sprite size
                    // and consider the density as being 1/750
                    Mass          = entity.SpriteSize.X * entity.SpriteSize.Y / 750f,
                    TextureString = entity?.TextureString,
                    Handling      = GameState.Handling.Solid
                };
                break;

            case "door":
                instance = new Door(
                    entity.Position,
                    entity.SpriteSize,
                    entity.TextureString)
                {
                    Handling      = GameState.Handling.Interact,
                    RequiresKey   = entity.Requirement,
                    KeyId         = entity.Id,
                    TextureString = entity.TextureString,
                    Id            = currentDoor++
                };

                bool unlocked = !entity.Requirement;
                if (entity.Requirement)
                {
                    (instance as Door).AddKey(entity.Id);
                }
                break;

            case "crate":
                instance = new Crate(
                    entity.Position,
                    entity.SpriteSize,
                    entity.TextureString)
                {
                    Handling = GameState.Handling.Solid
                };
                break;

            case "ladder":
                instance = new Ladder(
                    entity.Position,
                    entity.SpriteSize,
                    entity.TextureString)
                {
                    Handling = GameState.Handling.None
                };
                break;

            case "platform":
                instance = new Platform(
                    entity.Position,
                    entity.SpriteSize,
                    entity.TextureString,
                    entity.Displacement,
                    entity.Direction,
                    entity.ActivationKey,
                    entity.SecondTexture)
                {
                    Handling = GameState.Handling.Solid
                };
                break;

            case "lever":
                instance = new Lever(
                    entity.Position,
                    entity.SpriteSize,
                    entity.TextureString,
                    entity.ActivationKey)
                {
                    Handling          = GameState.Handling.None,
                    RightleverTexture = entity?.SecondTexture
                };
                //(instance as Lever).RightleverTexture = entity?.SecondTexture;
                break;

            case "button":
                instance = new Button(
                    entity.Position,
                    entity.SpriteSize,
                    entity.TextureString,
                    entity.ActivationKey)
                {
                    Handling = GameState.Handling.None
                };
                activationKeys++;
                break;

            case "rockandhook":
                entity.SecondTexture = "Sprites/Misc/Rope";
                instance             = new RockHook(
                    entity.Position,
                    entity.SpriteSize,
                    entity.TextureString,
                    entity.SecondTexture,
                    entity.RopeLength)
                {
                    Handling = GameState.Handling.Solid
                };
                break;

            case "secondary":
                instance = new PlatformBackground(
                    entity.Position,
                    entity.SpriteSize,
                    entity.TextureString);
                break;

            case "key":
                instance = new Key(
                    entity.Position,
                    entity.SpriteSize)
                {
                    TextureString = entity.TextureString,
                    Handling      = GameState.Handling.Collect,
                    Id            = entity.Id
                };
                currentKey++;
                break;

            case "sign":
                instance = new Sign(
                    entity.Position,
                    entity.SpriteSize,
                    entity.TextureString)
                {
                    Handling = GameState.Handling.None
                };
                break;

            default:
                instance = null;
                MyDebugger.WriteLine(
                    string.Format("GameObject '{0}' cannot be created",
                                  entity?.Type), true);
                break;
            }
            if (instance != null)
            {
                instance.Visible = true;
                instance.Active  = true;
            }
            return(instance);
        }
Esempio 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;
        }