Ejemplo n.º 1
0
    public override void Show()
    {
        short    oldX, oldY, oldXMap, oldYMap;
        DateTime timeStampFromLastShot           = DateTime.Now;
        DateTime timeStampFromLastScoreIncrement = DateTime.Now;
        DateTime timeStampFromLastDamage         = DateTime.Now;

        map.BackGround.MoveTo(0, 0);

        audio.PlayMusic(0, -1);
        gameOver = false;

        isFalling          = false;
        isJumping          = true;
        verticalSpeed      = 100.0f;
        horizontalSpeed    = 0.0f;
        movement_increment = 1.2f + mainCharacter.STEP_LENGHT;

        do
        {
            if ((DateTime.Now - timeStampFromLastScoreIncrement).TotalMilliseconds
                > 1000)
            {
                timeStampFromLastScoreIncrement = DateTime.Now;
                UpdatePoints();
            }
            //1.-Draw_EveryThing

            //TO DO
            hardware.ClearScreen();

            hardware.DrawImageMap(map.BackGround, map.XMap, map.YMap);

            foreach (Wall wall in map.Walls)
            {
                hardware.DrawImageMap(wall.SpriteImage, map.XMap, map.YMap);
            }
            foreach (Platform platform in map.Platforms)
            {
                hardware.DrawImageMap(platform.SpriteImage, map.XMap, map.YMap);
            }
            foreach (DangerousFloor spikes in map.DangerousFloors)
            {
                hardware.DrawImageMap(spikes.SpriteImage, map.XMap, map.YMap);
            }
            foreach (Ladder ladder in map.Ladders)
            {
                hardware.DrawImageMap(ladder.SpriteImage, map.XMap, map.YMap);
            }
            foreach (Enemy enemy in map.Enemy)
            {
                enemy.SpriteImage.MoveTo(enemy.X, enemy.Y);
                hardware.DrawImageMap(enemy.SpriteImage, map.XMap, map.YMap);
            }

            mainCharacter.SpriteImage.MoveTo(mainCharacter.X, mainCharacter.Y);
            hardware.DrawImageMap(mainCharacter.SpriteImage, map.XMap, map.YMap);

            foreach (Weapon w in mainCharacter.Weapons)
            {
                hardware.DrawImageMap(w.SpriteImage, map.XMap, map.YMap);
            }


            foreach (Enemy Enemy in Enemies)
            {
                Enemy.SpriteImage.MoveTo(Enemy.X, Enemy.Y);
                hardware.DrawImageMap(Enemy.SpriteImage, map.XMap, map.YMap);
            }

            hardware.UpdateScreen();

            //2.-Move_Character_from_keyboard_input
            oldX    = mainCharacter.X;
            oldY    = mainCharacter.Y;
            oldXMap = map.XMap;
            oldYMap = map.YMap;
            MoveCharacter();
            if (hardware.IsKeyPressed(Hardware.KEY_C) ||
                Hardware.JoystickPressed(2))
            {
                if ((DateTime.Now - timeStampFromLastShot).TotalMilliseconds
                    > SHOT_INTERVAL)
                {
                    timeStampFromLastShot = DateTime.Now;
                    mainCharacter.AddWeapon();
                }
                if (mainCharacter.Weapons.Count > 7)
                {
                    mainCharacter.RemoveWeapon(0);
                }
            }

            //TO DO
            //3.-Move_Enemies_And_Objects
            MoveEnemy(ref timeStampFromLastDamage);
            MoveWeapon();
            //TO DO
            //4-.Check_Colisions_AndUpdateGameState
            isFalling = !isJumping;
            map.CollideWeaponsWithWalls(mainCharacter);
            short points = map.CollideWeaponsWithEnemies(mainCharacter);
            if (points > 0)
            {
                mainCharacter.Points += points;
            }

            foreach (Platform platforms in map.Platforms)
            {
                if (mainCharacter.IsOver(platforms.SpriteImage))
                {
                    mainCharacter.MoveTo(mainCharacter.X, (short)
                                         (platforms.SpriteImage.Y - mainCharacter.SPRITE_HEIGHT));
                    isFalling = false;
                    isJumping = false;
                }
            }

            foreach (Wall walls in map.Walls)
            {
                if (mainCharacter.IsOver(walls.SpriteImage))
                {
                    mainCharacter.MoveTo(mainCharacter.X, (short)
                                         (walls.SpriteImage.Y - mainCharacter.SPRITE_HEIGHT));
                    isFalling = false;
                    isJumping = false;
                }

                else if (mainCharacter.CollidesWithImage(walls.SpriteImage))
                {
                    mainCharacter.X = oldX;
                    mainCharacter.Y = oldY;
                    map.XMap        = oldXMap;
                    map.YMap        = oldYMap;
                    mainCharacter.MoveTo(mainCharacter.X, mainCharacter.Y);
                    isFalling = false;
                    isJumping = false;
                }
            }

            foreach (DangerousFloor spikes in map.DangerousFloors)
            {
                if (mainCharacter.CollidesWithImage(spikes.SpriteImage))
                {
                    mainCharacter.Lives = 0;
                }
            }

            foreach (Ladder ladder in map.Ladders)
            {
                if (mainCharacter.CollidesWithImage(ladder.SpriteImage) &&
                    hardware.IsKeyPressed(Hardware.KEY_UP))
                {
                    mainCharacter.Y -= 2;
                    isFalling        = false;
                }
            }


            foreach (Enemy Enemy in Enemies)
            {
                oldX = Enemy.X;
                oldY = Enemy.Y;
                foreach (Platform platforms in map.Platforms)
                {
                    if (Enemy.IsOver(platforms.SpriteImage))
                    {
                        Enemy.MoveTo(Enemy.X, (short)
                                     (platforms.SpriteImage.Y - Enemy.SPRITE_HEIGHT));
                        isFalling = false;
                        isJumping = false;
                    }
                }

                if (Enemy.Y >= floorPosition -
                    mainCharacter.SPRITE_HEIGHT)
                {
                    Enemy.MoveTo(Enemy.X, (short)
                                 (floorPosition - Enemy.SPRITE_HEIGHT));
                    isFalling = false;
                    isJumping = false;
                }

                foreach (Wall walls in map.Walls)
                {
                    if (Enemy.IsOver(walls.SpriteImage))
                    {
                        Enemy.MoveTo(Enemy.X, (short)
                                     (walls.SpriteImage.Y - Enemy.SPRITE_HEIGHT));
                    }

                    else if (Enemy.CollidesWithImage(walls.SpriteImage))
                    {
                        Enemy.X  = oldX;
                        Enemy.Y  = oldY;
                        map.XMap = oldXMap;
                        map.YMap = oldYMap;
                        Enemy.MoveTo(Enemy.X, Enemy.Y);
                    }
                }
            }

            if (mainCharacter.CollidesWith(map.Exit))
            {
                gameOver = true;
            }

            //5.-Puse_Game
            //TO DO
            if (hardware.IsKeyPressed(Hardware.KEY_ESC) ||
                keyPressed == Hardware.KEY_ESC)
            {
                gameOver = true;
            }
            if (mainCharacter.Lives <= 0)
            {
                gameOver = true;
            }
        } while (!gameOver);
        audio.StopMusic();
        Save();
    }
Ejemplo n.º 2
0
        private static void Main()
        {
            ConsoleSettings.PrepareConsole();
            KeyboardInterface keyboard = new KeyboardInterface();

            Opponent enemy = new Opponent(new MatrixCoords(3, 3), new char[, ] {
                { '@' }
            }, null);

            ConsoleRenderer renderer = new ConsoleRenderer(ConsoleSettings.ConsoleHeight, ConsoleSettings.ConsoleWidth);

            IList <WorldObject> map = MapParser.ParseMap("../../WorldMaps/map.txt");

            GameEngine.GameEngine gameEngine = new GameEngine.GameEngine(renderer, keyboard);

            int heroChosen = 0;

            Console.WriteLine("Please select your Hero: \nPress 1 for  Mage\nPress 2 for  Thief\nPress 3 for  Warrior");

            // validates hero choice and let's player choose correctly
            do
            {
                try
                {
                    heroChosen = int.Parse(Console.ReadLine());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine("Please choose 1, 2 or 3");
                }
            } while (!true || heroChosen < 1 || heroChosen > 3);

            Console.Clear();

            // TODO implement interface for the choice of type of character
            MainCharacter hero = HeroChoice(heroChosen);

            hero.AddWeapon(new Knife("Steel knife")
            {
                MinDmg = 20, MaxDmg = 30
            });
            //hero.AddWeapon(new Knife("mnogo qk knife") { MaxDmg = 30, MinDmg = 20 });
            gameEngine.AddObject(hero);
            gameEngine.AddObject(enemy);
            Opponent newOpponent = new Opponent(new MatrixCoords(2, 35), new char[, ] {
                { '@' }
            }, new BattleAxe("Battle Axe"));
            Opponent newOpponent2 = new Opponent(new MatrixCoords(7, 30), new char[, ] {
                { '@' }
            }, new Knife("Steel knife"));
            Opponent newOpponent3 = new Opponent(new MatrixCoords(10, 10), new char[, ] {
                { '@' }
            }, new BattleAxe("Battle Axe"));
            Opponent newOpponent4 = new Opponent(new MatrixCoords(15, 15), new char[, ] {
                { '@' }
            }, new BattleAxe("Battle Axe"));

            gameEngine.AddObject(newOpponent);
            gameEngine.AddObject(newOpponent2);
            gameEngine.AddObject(newOpponent3);
            gameEngine.AddObject(newOpponent4);
            foreach (var item in map)
            {
                gameEngine.AddObject(item);
            }

            keyboard.OnDownPressed   += (sender, eventInfo) => { hero.Move(Direction.Down); };
            keyboard.OnLeftPressed   += (sender, eventInfo) => { hero.Move(Direction.Left); };
            keyboard.OnRightPressed  += (sender, eventInfo) => { hero.Move(Direction.Right); };
            keyboard.OnUpPressed     += (sender, eventInfo) => { hero.Move(Direction.Top); };
            keyboard.onPotionPressed += (sencer, eventInfo) => { hero.Health += 5; };

            gameEngine.Run();
        }