Example #1
0
        private void AddEnitityFromChar(char c, float x, float y)
        {
            switch (c)
            {
            case '1':
                container.AddStationaryEntity(
                    new StationaryShape(new Vec2F(x * Constants.WIDTH, y * Constants.HEIGHT),
                                        new Vec2F(Constants.WIDTH, Constants.BOTTOM / 2)),
                    new Image(Path.Combine("Assets", "Images", "green-square.png")));
                break;

            case '2':
                container.AddStationaryEntity(
                    new StationaryShape(new Vec2F(x * Constants.WIDTH, y * Constants.HEIGHT),
                                        new Vec2F(Constants.WIDTH, Constants.BOTTOM / 2)),
                    new Image(Path.Combine("Assets", "Images", "studio-square.png")));
                break;

            case '4':
                container.AddStationaryEntity(
                    new StationaryShape(new Vec2F(x * Constants.WIDTH, y * Constants.HEIGHT),
                                        new Vec2F(Constants.WIDTH, Constants.BOTTOM / 2)),
                    new Image(Path.Combine("Assets", "Images", "white-square.png")));
                break;
            }
        }
        public void TestIterationUnchanged()
        {
            var ent0 = new StationaryShape(0.0f, 0.0f, 0.0f, 0.0f);
            var ent1 = new StationaryShape(1.0f, 1.0f, 1.0f, 1.0f);
            var ent2 = new StationaryShape(2.0f, 2.0f, 2.0f, 2.0f);
            var ent3 = new StationaryShape(3.0f, 3.0f, 3.0f, 3.0f);
            var ent4 = new StationaryShape(4.0f, 4.0f, 4.0f, 4.0f);
            var ent5 = new StationaryShape(5.0f, 5.0f, 5.0f, 5.0f);
            var ents = new EntityContainer();

            foreach (var shp in new[] { ent0, ent1, ent2, ent3, ent4, ent5 })
            {
                ents.AddStationaryEntity(shp, null);
            }

            foreach (Entity ent in ents)
            {
                ent.Shape.Position.X *= -1.0f;
                ent.Shape.Position.Y *= -1.0f;
            }

            foreach (Entity ent in ents)
            {
                // TODO: Ask Boris for advice on safe pointers in ReadOnlyCollections
                //Assert.GreaterOrEqual(ent.Shape.Position.X, 0.0f);
                //Assert.GreaterOrEqual(ent.Shape.Position.Y, 0.0f);
            }
        }
        public void TestIterationChanged()
        {
            var ent0 = new StationaryShape(0.0f, 0.0f, 0.0f, 0.0f);
            var ent1 = new StationaryShape(1.0f, 1.0f, 1.0f, 1.0f);
            var ent2 = new StationaryShape(2.0f, 2.0f, 2.0f, 2.0f);
            var ent3 = new StationaryShape(3.0f, 3.0f, 3.0f, 3.0f);
            var ent4 = new StationaryShape(4.0f, 4.0f, 4.0f, 4.0f);
            var ent5 = new StationaryShape(5.0f, 5.0f, 5.0f, 5.0f);
            var ents = new EntityContainer();

            foreach (var shp in new[] { ent0, ent1, ent2, ent3, ent4, ent5 })
            {
                ents.AddStationaryEntity(shp, null);
            }

            ents.Iterate(entity => {
                entity.Shape.Position.X *= -1.0f;
                entity.Shape.Position.Y *= -1.0f;
            });

            foreach (Entity ent in ents)
            {
                Assert.LessOrEqual(ent.Shape.Position.X, 0.0f);
                Assert.LessOrEqual(ent.Shape.Position.Y, 0.0f);
            }
        }
Example #4
0
        public void AddEntity(StationaryShape shape, IBaseImage image)
        {
            // add to entities
            GameObjectEntities.AddStationaryEntity(shape, image);

            // update the Platform bounding box
            if (shape.Position.X < BoundingBox.Position.X)
            {
                BoundingBox.Position.X = shape.Position.X;
            }
            if (shape.Position.Y < BoundingBox.Position.Y)
            {
                BoundingBox.Position.Y = shape.Position.Y;
            }
            if (shape.Position.X + shape.Extent.X >
                BoundingBox.Position.X + BoundingBox.Extent.X)
            {
                BoundingBox.Extent.X =
                    (shape.Position.X + shape.Extent.X) - BoundingBox.Position.X;
            }
            if (shape.Position.Y + shape.Extent.Y >
                BoundingBox.Position.Y + BoundingBox.Extent.Y)
            {
                BoundingBox.Extent.Y =
                    (shape.Position.Y + shape.Extent.Y) - BoundingBox.Position.Y;
            }
        }
Example #5
0
        /// <summary>
        /// Parser creates a container of Map entities
        /// </summary>
        /// <param name="mapStrings"></param>
        /// <param name="keymap"></param>
        /// <param name="platforms"></param>
        /// <returns></returns>

        public static EntityContainer Parser(List <string> mapStrings, Dictionary <string, string> keymap, List <string> platforms)
        {
            EntityContainer returnContainer = new EntityContainer();

            List <string> map = ASCIIParser.RemoveEmptyLines(mapStrings);

            int row = 1;
            int col = 0;

            float tileHeight = 1.0f / map.Count;

            foreach (string line in map)
            {
                float tileWidth = 1.0f / line.Length;
                foreach (char key in line)
                {
                    if (keymap.ContainsKey(key.ToString()))
                    {
                        returnContainer.AddStationaryEntity(
                            new StationaryShape(new Vec2F(col * tileWidth, 1.0f - row * tileHeight),
                                                new Vec2F(tileWidth, tileHeight)),
                            new Image(keymap[key.ToString()]));
                    }
                    col++;
                }
                row++;
                col = 0;
            }

            return(returnContainer);
        }
Example #6
0
        [Test] // Testing too much speed collision with platform results in death.
        public void CollisionWithPlatformDeathTest()
        {
            _testCollisionObstacle = new EntityContainer();
            _testCollisionPlatform = new EntityContainer();
            _testSpecifiedPlatform = new List <Dictionary <char, List <Entity> > >();
            _testPassengerList     = new List <Passenger>();

            var stationaryShape = new StationaryShape(new Vec2F(0.0f, 0.0f), new Vec2F(1.0f, 0.5f));
            var image           = new Image(Path.Combine("Assets", "Images", "Taxi_Thrust_None.png"));

            _testPlayer = new Player();
            _testPlayer.SetPosition(0.5f, 0.6f);
            _testPlayer.GetsShape().Direction.Y = -0.005f;

            _testCollisionPlatform.AddStationaryEntity(stationaryShape, image);

            CollisionChecker collisionTest = new CollisionChecker(_testCollisionObstacle, _testCollisionPlatform,
                                                                  _testPlayer, _testPassengerList, _testSpecifiedPlatform);

            while (!collisionTest.GetGameOverChecker())
            {
                _testPlayer.PlayerMove();
                collisionTest.CheckCollsion();
            }
            Assert.AreEqual(collisionTest.GetGameOverChecker(), true);
        }
Example #7
0
        public EntityContainer <Platform> GetPlatforms()
        {
            var plist = new EntityContainer <Platform>();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    var key = map[y][x];
                    if (platforms.Contains(key))
                    {
                        float x1 = x / ((float)width),
                              y1 = 1.0f - (1.0f + y) / height;
                        var acc  = 1;
                        while (map[y][x + acc] == key)
                        {
                            acc++;
                        }
                        plist.AddStationaryEntity(
                            new Platform(
                                // "y1 - yscale" because we wish to lay the platform entity just
                                // above where it is to use collisionstrategy to check for landing.
                                new StationaryShape(x1, y1, acc * xscale, yscale),
                                images[key], key));



                        // jump to the index after the platform.
                        x += acc + 1;
                    }
                }
            }

            return(plist);
        }
Example #8
0
        /// <summary>
        /// A method which return all objects of the level
        /// </summary>
        /// <returns> A list of obstacles on the levelmap </returns>
        public EntityContainer <Obstacle> GetObstacles()
        {
            var obstacles = new EntityContainer <Obstacle>();

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    if (map[y][x] != ' ' && map[y][x] != '^' && map[y][x] != '<' &&
                        map[y][x] != '>' && !platforms.Contains(map[y][x]))
                    {
                        float x1 = x / ((float)width),
                              y1 = 1.0f - (1.0f + y) / ((float)height);

                        var key = map[y][x];
                        obstacles.AddStationaryEntity(
                            new Obstacle(
                                new StationaryShape(x1, y1, xscale, yscale),
                                images[key]));
                    }
                }
            }

            return(obstacles);
        }
Example #9
0
        public void CreateEnemiesSpot(List <Image> enemyStrides)
        {
            float initValue = 0.8f;

            Enemies = new EntityContainer <Enemy>(8);

            for (int i = 0; i < 8; i++)
            {
                enemies.Add(new Enemy(new DynamicShape(new Vec2F(initValue, 0.9f),
                                                       new Vec2F(0.1f, 0.1f)), new ImageStride(80, enemyStrides)));
            }

            foreach (var elem in enemies)
            {
                Enemies.AddStationaryEntity(elem);
            }
        }
Example #10
0
        /// <summary>
        /// Processes incoming events. Right now only timed events are processed
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="gameEvent"></param>
        public void ProcessEvent(GameEventType eventType, GameEvent <object> gameEvent)
        {
            switch (eventType)
            {
            case GameEventType.TimedEvent:
                if (gameEvent.Message == "LEVEL")
                {
                    switch (gameEvent.Parameter1)
                    {
                    case "SPAWN_CUSTOMER": //Finding correct customer in allCustomersInLevel
                        foreach (Customer customer in allCustomersInLevel)
                        {
                            if (customer.Name != gameEvent.Parameter2)
                            {
                                continue;
                            }
                            //get platform that the customer should spawn on
                            var platform =
                                platformDictionary[customer.PlatformId].GetBoundingBox();
                            var xVal = platform.Position.X + platform.Extent.X / 2;

                            //set customer position to center of platform
                            customer.Shape.Position.X = xVal;
                            customer.Shape.Position.Y = platform.Position.Y + Constants.EXTENT_Y;
                            activeCustomersInlevel.AddStationaryEntity(customer.Copy());
                        }
                        break;

                    case "DESPAWN_CUSTOMER":
                        spaceTaxibus.RegisterEvent(
                            GameEventFactory <object> .CreateGameEventForAllProcessors(
                                GameEventType.GameStateEvent,
                                this,
                                "CHANGE_STATE",
                                "GAME_OVER",
                                ""));
                        break;
                    }
                }

                break;
            }
        }
        public void CreateEnemies(List <Image> enemyStrides)
        {
            float initValueX = 0.0f;
            float initValueY = 0.7f;

            Enemies = new EntityContainer <Enemy>(8);
            for (int i = 0; i < 8; i++)
            {
                initValueX += 0.1f;
                initValueY += 0.02f;
                enemies.Add(new Enemy(game, new DynamicShape(new Vec2F(initValueX, initValueY),
                                                             new Vec2F(0.1f, 0.1f)), new ImageStride(80, enemyStrides)));
            }

            foreach (var elem in enemies)
            {
                Enemies.AddStationaryEntity(elem);
            }
        }
Example #12
0
        public void ExitObstacleCollisionTest()
        {
            var shape = new StationaryShape(new Vec2F(0.0F, 0.5F),
                                            new Vec2F(1F, 0.5F));
            var file = Path.Combine(Utils.GetImageFilePath("obstacle.png"));

            player = new Player();
            player.SpawnPos(new Vec2F(0.5F, 0.4F));
            entityContainer = new EntityContainer();
            entityContainer.AddStationaryEntity(shape, new Image(file));
            bool col = false;

            while (player.Shape.Position.Y < 0.55F && col == false)
            {
                player.MoveUp = true;
                player.Move();
                col = Collision.ExitCollision(player, entityContainer);
                Console.WriteLine(col);
            }
            Assert.True(col);
        }
Example #13
0
        /// <summary>
        /// Add a new entity to the instance's platEntity EntityContainer. Also updates the
        /// boundingBox shape if the position and dimensions of the new shape exceed the ones of
        /// boundingBox. Also re-centers the platform's text label to the new center of the
        /// bounding box
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="image"></param>
        public void AddPlatform(StationaryShape shape, IBaseImage image)
        {
            if (platEntity.CountEntities() < 1)
            {
                boundingBox.Position = shape.Position.Copy();
                boundingBox.Extent   = shape.Extent.Copy();
            }

            platEntity.AddStationaryEntity(shape, image);
            if (shape.Position.X < boundingBox.Position.X)
            {
                boundingBox.Position.X = shape.Position.X;
            }

            if (shape.Position.Y < boundingBox.Position.Y)
            {
                boundingBox.Position.Y = boundingBox.Position.Y;
            }

            if (shape.Position.X + shape.Extent.X >
                boundingBox.Position.X + boundingBox.Extent.X)
            {
                boundingBox.Extent.X =
                    (shape.Position.X + shape.Extent.X) - boundingBox.Position.X;
            }

            if (shape.Position.Y + shape.Extent.Y >
                boundingBox.Position.Y + boundingBox.Extent.Y)
            {
                boundingBox.Extent.Y =
                    (shape.Position.Y + shape.Extent.Y) - boundingBox.Position.Y;
            }

            text.GetShape().Position = new Vec2F(
                boundingBox.Position.X + boundingBox.Extent.X / 2 - Constants.EXTENT_X / 2,
                boundingBox.Position.Y);
        }
        /// <summary>
        /// Builds a level object from the given level file path.
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public Level GetLevelFromFile(string filepath)
        {
            var levelContent = levelLoader.ReadFileContents(filepath);

            //parsing map
            var levelMap = levelContent[Constants.MAP];

            if (levelMap.Count != 23 || levelMap[0].Length != 40)
            {
                throw new Exception(
                          "Error building level. Level must be 40 lines x 23 characters.");
            }

            var platformIds = levelParser.ParsePlatformChars(levelContent[Constants.PLATFORMS]);

            if (platformIds.Count < 1)
            {
                throw new Exception("Error building level. Level must contain platforms.");
            }

            var levelImages = levelParser.ParseImages(levelContent[Constants.IMAGES]);

            if (levelImages.Count < 1)
            {
                throw new Exception("Error building level. Level must contain image mapping.");
            }

            Dictionary <char, Platform> platformDictionary = new Dictionary <char, Platform>();

            foreach (char platformChar in platformIds)
            {
                platformDictionary.Add(platformChar, new Platform(platformChar));
            }
            EntityContainer <Obstacle> obstacles = new EntityContainer <Obstacle>();
            EntityContainer            portals   = new EntityContainer();

            Vec2F extent = new Vec2F(Constants.EXTENT_X, Constants.EXTENT_Y);

            Vec2F  playerStartingPosition = null;
            Player player = new Player(null, null);

            //parsing level map
            int i = 0;

            foreach (string mapString in levelMap)
            {
                for (int j = 0; j < mapString.Length; j++)
                {
                    char  mapChar  = mapString[j];
                    Vec2F position =
                        new Vec2F(
                            j * Constants.EXTENT_X,
                            1 - i * Constants.EXTENT_Y - Constants.EXTENT_Y
                            );

                    StationaryShape shape = new StationaryShape(position, extent);
                    if (mapChar == ' ')
                    {
                        continue;
                    }

                    //checking the individual map chars
                    if (platformIds.Contains(mapString[j]))
                    {
                        platformDictionary[mapChar].AddPlatform(
                            shape, imageContainer.GetImageByName(levelImages[mapChar]));
                    }
                    else if (mapChar == '^')
                    {
                        portals.AddStationaryEntity(shape, null);
                    }
                    else if (mapChar == '<' || mapChar == '>')
                    {
                        playerStartingPosition =
                            new Vec2F(j * Constants.EXTENT_X, 1 - i * Constants.EXTENT_Y);
                        player.Shape.Position = playerStartingPosition;
                    }
                    else
                    {
                        var newObstacle = new Obstacle(shape, null)
                        {
                            Image = imageContainer.GetImageByName(levelImages[mapChar])
                        };
                        obstacles.AddStationaryEntity(newObstacle);
                    }
                }
                i++;
            }

            if (playerStartingPosition == null)
            {
                throw new Exception(
                          "Error building level. Level map must contain a starting position for player.");
            }

            if (portals.CountEntities() < 1)
            {
                throw new Exception(
                          "Error building level. Level map must contain portal entities.");
            }

            var levelCustomers =
                levelParser.ParseCustomerStrings(levelContent[Constants.CUSTOMERS]);

            var name = levelParser.ParseLevelName(levelContent[Constants.NAME]);

            return(new Level(
                       name,
                       portals,
                       obstacles,
                       platformDictionary,
                       levelCustomers,
                       player,
                       playerStartingPosition));
        }