Пример #1
0
        //Pre: crate is not null
        //Post: N/A
        //Description: Handle's the player's response to colliding with a crate
        public override void CollisionWith(Crate crate)
        {
            //Checks if player is on top of door, if so set isGrounded to true
            if (TypeOfCollision(crate) == CollisionType.Bottom)
            {
                isGrounded = true;
            }

            //Checks if player reached his target spot, if so invoke ResetCollideCrate event
            if (TruePos.X == targetSpot)
            {
                ResetCollideCrate.Invoke();
            }

            //Checks if crate still collides with player, if so block the player from moving into it
            if (BoxCollision(crate))
            {
                BlockedCollision(crate);
            }
        }
Пример #2
0
        //Pre: N/A
        //Post: N/A
        //Description: Adds all the gameObjects in the level file to the gameObjects list and stores their appropiate positions and types
        public void LoadLevel()
        {
            //Stores current line in file and its row number
            string line;
            int    row = 0;

            //Iterates while the end of the file isn't reached
            while (!inFile.EndOfStream)
            {
                //Sets line to current line in level file
                line = inFile.ReadLine();

                //Increments through all characters in current line
                for (int i = 0; i < line.Length; ++i)
                {
                    //Based off the character, the appropiate gameobject is added to the list
                    switch (Convert.ToInt32(line[i]) - '0')
                    {
                    case MainGame.PLAYER:
                        player = new Player(MainGame.gameObjectsImg[MainGame.PLAYER].Width * i,
                                            MainGame.gameObjectsImg[MainGame.PLAYER].Height * row);
                        break;

                    case MainGame.WALL:
                        gameObjects.Add(new Wall(MainGame.gameObjectsImg[MainGame.PLAYER].Width * i,
                                                 MainGame.gameObjectsImg[MainGame.PLAYER].Height * row));
                        break;

                    case MainGame.CRATE:
                        Crate crate = new Crate(MainGame.gameObjectsImg[MainGame.PLAYER].Width * i,
                                                MainGame.gameObjectsImg[MainGame.PLAYER].Height * row);
                        //Subsciribes to crate's CratCollectableCol and excecutes CrateCollectableCollision with crate when event happens
                        crate.CrateCollectableCol += () => CrateCollectableCollision(crate);

                        //Subsciribes to crate's CollectableCollision and excecutes CollectCollectable with c when event happens
                        crate.CollectableCollision += c => player.CollectCollectable(c);

                        //Adds crate to gameObjects
                        gameObjects.Add(crate);
                        break;

                    case MainGame.GOAL:
                        gameObjects.Add(new Goal(MainGame.gameObjectsImg[MainGame.PLAYER].Width * i,
                                                 MainGame.gameObjectsImg[MainGame.PLAYER].Height * row));
                        goalIndex = gameObjects.Count - 1;
                        break;

                    case MainGame.DOOR:
                        gameObjects.Add(new Door(MainGame.gameObjectsImg[MainGame.PLAYER].Width * i,
                                                 MainGame.gameObjectsImg[MainGame.PLAYER].Height * row));
                        break;

                    case MainGame.SPIKES:
                        gameObjects.Add(new Spikes(MainGame.gameObjectsImg[MainGame.PLAYER].Width * i,
                                                   MainGame.gameObjectsImg[MainGame.PLAYER].Height * row));
                        break;

                    case MainGame.GEM:
                        gameObjects.Add(new Gem(MainGame.gameObjectsImg[MainGame.PLAYER].Width * i,
                                                MainGame.gameObjectsImg[MainGame.PLAYER].Height * row));
                        //Increments totalGems
                        ++totalGems;
                        break;

                    case MainGame.KEY:
                        gameObjects.Add(new Key(MainGame.gameObjectsImg[MainGame.PLAYER].Width * i,
                                                MainGame.gameObjectsImg[MainGame.PLAYER].Height * row));
                        //Increments totalKeys
                        ++totalKeys;
                        break;
                    }
                }

                //Increments row
                ++row;
            }

            //Closes file
            inFile.Close();


            //Iterates through each gameObject and subscribes to its Move event and calls GameObjectCollisions with o when it sets off
            foreach (GameObject obj in gameObjects)
            {
                obj.Move += o => GameObjectCollisions(o);

                //Subsribes to player's ResetCollideCrate event and excecutes object's ResetCollisions when event goes off
                player.ResetCollideCrate += () => obj.ResetCollisions();
            }
        }
Пример #3
0
 //Pre: crate is not null
 //Post: N/A
 //Description: Calculates type of collision for both crates and sets the appropiate direction of collision to true, as well as block crate from moving
 public override void CollisionWith(Crate crate)
 {
     collideCrate[(int)TypeOfCollision(crate)]            = true;
     crate.collideCrate[(int)crate.TypeOfCollision(this)] = true;
     BlockedCollision(crate);
 }