Beispiel #1
0
 public XmlWrite()
 {
     // initialise levelFolder
     StaticVariables.execFolder = AppDomain.CurrentDomain.BaseDirectory;
     StaticVariables.levelFolder = StaticVariables.execFolder;
     // create world
     this.w = new World(10, 10);
     // fill world
     this.w.FillWorld(TERRAIN.grass_normal, new Size(10, 10));
     // initialize player
     this.w.InitPlayer(new PointF(5, 5));
     this.p = w.getPlayer();
     LevelWriter.saveWorld(this.w, this.file);
     beforelist = new TerrainTile[w.getTerrain().Count];
     before = new Entity[w.getEntities().Count];
     // save beforlist
     w.getTerrain().CopyTo(beforelist);
     w.getEntities().CopyTo(before);
     world = new World(10, 10);
     world.loadLevel(this.file);
     afterlist = new TerrainTile[world.getTerrain().Count];
     after = new Entity[world.getEntities().Count];
     // save afterlist
     world.getTerrain().CopyTo(afterlist);
     world.getEntities().CopyTo(after);
 }
Beispiel #2
0
        /// <summary>
        /// Drop first item in inventory back into the world
        /// </summary>
        /// <param name="world"></param>
        public void DropItem(World world)
        {
            PointF dropLocation = GetLocationInFrontOfPlayer();
            // Check if item can be dropped on target from dropLocation
            bool isLand = world.getTerraintile(dropLocation).IsWalkable;
            bool hasSolidEntitiesOnTarget = world.checkCollision(this, dropLocation);
            List<Entity> entitiesOnTerrainTile = world.getEntitiesOnTerrainTile(location);
            List<Entity> entitiesOnTargetTerrainTile = world.getEntitiesOnTerrainTile(dropLocation);
            bool hasWaterlilyOnTarget = entitiesOnTargetTerrainTile.Exists(e => e.getSpriteID() == (int) SPRITES.waterlily);

            // If no item exists in inventory or
            //      there is a solid target on dropLocation or
            //      targettile is not land, return
            if (!Inventory.Any() || hasSolidEntitiesOnTarget ||
                (!isLand && !hasWaterlilyOnTarget))
                return;
            // If item is key, lock door
            if (Inventory[0].Entity is Key)
            {
                // If player is standing in a door, cancel action
                if (entitiesOnTerrainTile.Exists(e => e is Door))
                    return;
                world.LockDoor((Key)Inventory[0].Entity);
            }

            // Set droplocation on item
            Inventory[0].Entity.setLocation(dropLocation);
            // Push item in world objects list
            world.getEntities().Add(Inventory[0].Entity);
            // Remove item from inventory
            Inventory.RemoveAt(0);
        }
 public GuiInventory(int ID, int ScreenresX, int ScreenresY, float drawRatio, World w, Sprite[] s)
     : base(ID, ScreenresX, ScreenresY, drawRatio)
 {
     setActive(false);
     world = w;
     sprites = s;
 }
        private int xRes, yRes; //Resolution of buffer

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="KBSGame.DrawEngine"/> class.
        /// Runs all necessary code to draw a world to the screen.
        /// </summary>
        /// <param name="world">World.</param>
        /// <param name="drawingArea">Drawing area.</param>
        /// <param name="xResolution">X resolution.</param>
        /// <param name="yResolution">Y resolution.</param>
        public DrawEngine(World world, Graphics drawingArea, int xResolution, int yResolution)
        {
            this.drawingArea = drawingArea;
            xRes = xResolution;
            yRes = yResolution;

            xDrawRes = xRes / 2;
            yDrawRes = yRes / 2;

            setView(xDrawRes / StaticVariables.tileSize + 1, yDrawRes / StaticVariables.tileSize + 1);
            buffer = new Bitmap (StaticVariables.viewWidth * StaticVariables.tileSize, StaticVariables.viewHeight * StaticVariables.tileSize);
            this.world = world;

            // Load sprites
            sprites = getSprites();
            Menu menu = new Menu ((int)GUI.def, xRes, yRes, xRes / xDrawRes, world);

            Interfaces = new List<Gui> ();
            Interfaces.Insert ((int)GUI.def, menu);
            Interfaces.Insert ((int)GUI.gameover, new GameOverMenu((int)GUI.gameover, xRes, yRes, xRes / xDrawRes, world));   //Game over menu
                 //Temporary static Gui
            Interfaces.Insert ((int)GUI.finish, new FinishMenu((int)GUI.finish, xRes, yRes, xRes / xDrawRes, world));               //Create FinishedMenu GUI
            Interfaces.Insert ((int)GUI.guiinventory, new GuiInventory((int) GUI.guiinventory, xRes, yRes, xRes / xDrawRes, world, sprites)); //Inventory GUI
            Interfaces.Insert ((int)GUI.editor, menu.getEditorGui());
        }
        /// <summary>
        /// Basic Menu constructor for screen resolution
        /// </summary>
        /// <param name="ID">Menu ID</param>
        /// <param name="ScreenresX">Width of the screen</param>
        /// <param name="ScreenresY">Length of the screen</param>
        /// <param name="drawRatio"></param>
        /// <param name="map"></param>
        public GameOverMenu(int ID, int ScreenresX, int ScreenresY, float drawRatio, World map)
            : base(ID, ScreenresX, ScreenresY, drawRatio)
        {
            this.map = map;
            xRes = ScreenresX;
            yRes = ScreenresY;
            buffer = new Bitmap(xRes, yRes);

            tryAgain = Image.FromFile(StaticVariables.textFolder + "/gameover_try_again.png");;
            exit = Image.FromFile(StaticVariables.textFolder + "/menu_exit.png");
            gameOver = Image.FromFile(StaticVariables.textFolder + "/game_over.png");
        }
        /*
         * Grid simulation setup:
         * G = Grass, W = Water
         * |G|G|G|
         * |G|G|G|
         * |W|W|W|
         */
        public PlayerInventory()
        {
            // Initialize world
            this.w = new World(Size, Size);
            // Fill world with tiles of grass
            w.FillWorld(TERRAIN.grass_normal, new Size(Size, Size));
            // Fill third row with water
            for (int i = 0; i <= 2; i++)
                w.setTerraintile(new Point(i, 2), (int)SPRITES.water);

            // Initialize player
            w.InitPlayer(new PointF(1, 1));
            this.p = w.getPlayer();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="KBSGame.EditorGui"/> class.
        /// </summary>
        /// <param name="ID">I.</param>
        /// <param name="screenResX">Screen res x.</param>
        /// <param name="screenResY">Screen res y.</param>
        /// <param name="drawRatio">Draw ratio.</param>
        /// <param name="world">World.</param>
        public EditorGui(int ID, int screenResX, int screenResY, float drawRatio, World world)
            : base(ID, screenResX, screenResY, drawRatio)
        {
            this.world = world;

            width = Math.Min (StaticVariables.dpi * 2, screenResX / 2);
            tabWidth = width - tabbarWidth - margin*2;

            rowLength = (width - margin * 2 - tabbarWidth) / StaticVariables.tileSize;
            currentHover = new Point (0, 0);

            worldSize = world.getSize ();

            terrainTiles = world.getTileTypes ();
            loadEntities ();
        }
Beispiel #8
0
        /*
         * Grid simulation setup:
         * G = Grass, W = Water
         * |G|G|G|
         * |G|G|G|
         * |G|G|G|
         */
        public KeyDoor()
        {
            // Initialize world
            this.w = new World(Size, Size);
            // Fill world with tiles of grass
            w.FillWorld(TERRAIN.grass, new Size(Size, Size));

            // Initialize player
            w.InitPlayer(new PointF(0, 0));
            this.p = w.getPlayer();
            p.CurrentDirection = (int)Player.Direction.Down;

            // Initialize door and key
            this.d = new Door(new PointF(1, 0));
            w.addEntity(d);
            this.k = new Key(new PointF(0, 0));
            w.addEntity(k);
        }
Beispiel #9
0
        //Basic constructor for the resolution.
        public Menu(int ID, int ScreenresX, int ScreenresY, float drawRatio, World world)
            : base(ID, ScreenresX, ScreenresY, drawRatio)
        {
            this.world = world;
            menus = new List<List<Button>> ();
            xRes = ScreenresX;
            yRes = ScreenresY;
            buffer = new Bitmap(xRes, yRes);
            width = Math.Min (StaticVariables.dpi * 2, this.xRes / 2);
            buttonHeight = Math.Min (StaticVariables.dpi, yRes / 5);

            //ButtonList for the main menu.
            List<Button> buttonList = new List<Button> ();
            buttonList.Add(new Button("Start"));
            buttonList.Add(new Button("Editor"));
            buttonList.Add(new Button("Help"));
            buttonList.Add(new Button("Quit"));
            menus.Insert ((int)STATE.main, buttonList);

            //ButtonList for the menu when you pause the game.
            buttonList = new List<Button> ();
            buttonList.Add(new Button("Resume"));
            buttonList.Add(new Button("Help"));
            buttonList.Add(new Button("Exit"));
            menus.Insert ((int)STATE.pause, buttonList);

            //ButtonList for the menu in the editor.
            buttonList = new List<Button> ();
            buttonList.Add(new Button("Resume"));
            buttonList.Add(new Button("Help"));
            buttonList.Add(new Button("Exit"));
            menus.Insert ((int)STATE.editor, buttonList);

            //ButtonList for the menu after pressing "Start" to select a level.
            buttonList = new List<Button> ();
            buttonList.Add(new Button("Back"));
            menus.Insert ((int)STATE.levelloader, buttonList);

            editorGui = new EditorGui ((int)GUI.editor, xRes, yRes, drawRatio, world);
            loadButtonImages ();
            changeState (STATE.main);
            this.setActive (true);
        }
        public static void saveWorld(World world, String fileName)
        {
            List<TerrainTile> tiles = world.getTerrain ();
            List<Entity> entities = world.getEntities ();
            Size size = world.getSize ();

            using (XmlWriter writer = XmlWriter.Create(Path.Combine(StaticVariables.levelFolder, fileName + ".xml")))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("world");
                writer.WriteStartElement("size");
                writer.WriteElementString(xmlVar.Width, size.Width.ToString());
                writer.WriteElementString(xmlVar.Height, size.Width.ToString());
                writer.WriteEndElement();
                writer.WriteStartElement("tiles");

                foreach (TerrainTile tile in tiles)
                {
                    writer.WriteStartElement(xmlVar.Tile);
                    writer.WriteElementString(xmlVar.ID, tile.getID().ToString());
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteStartElement("entities");

                foreach (Entity e in entities) {
                    writer.WriteStartElement(xmlVar.Entity);
                    writer.WriteElementString(xmlVar.ID, e.getID().ToString());
                    writer.WriteElementString(xmlVar.Type, ((int)e.getType()).ToString());
                    writer.WriteElementString(xmlVar.SpriteID, e.getSpriteID().ToString());
                    writer.WriteElementString("x", e.getLocation().X.ToString());
                    writer.WriteElementString("y", e.getLocation().Y.ToString());
                    writer.WriteElementString(xmlVar.Solid, e.getSolid().ToString());
                    writer.WriteElementString(xmlVar.DrawOrder, e.getDrawOrder().ToString());
                    writer.WriteElementString(xmlVar.BoudingBox, e.getBoundingBox().ToString());
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
Beispiel #11
0
 /// <summary>
 /// Pickup first item on given tile
 /// </summary>
 /// <param name="w"></param>
 public void PickupItems(World w)
 {
     if (Inventory.Count >= 10) return;
     // Get a list with nonSolid entities on terrain tile and filter for bananas or keys
     List<Entity> EntitiesOnTile =
         w.getEntitiesOnTerrainTile(getLocation(), true)
             .Where(e => e.getType() == ENTITIES.fruit || e.getType() == ENTITIES.key)
             .ToList();
     // If there's nothing in the list of entities, return;
     if (EntitiesOnTile.Count == 0) return;
     // Add first item of list to inventory
     Inventory.Add(new Item(EntitiesOnTile[0]));
     // If key is picked up, unlock door
     if (EntitiesOnTile[0] is Key)
         w.UnlockDoor((Key)EntitiesOnTile[0]);
     // Remove item from world
     w.getEntities().Remove(EntitiesOnTile[0]);
 }
Beispiel #12
0
        /// <summary>
        /// Moves the player to the given relative location
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="relativeLocation"></param>
        public override void move(World sender, PointF relativeLocation)
        {
            float moveLocationX = location.X + relativeLocation.X;
            float moveLocationY = location.Y + relativeLocation.Y;

            PointF targetPoint = new PointF(moveLocationX, moveLocationY);
            TerrainTile targetTile = sender.getTerraintile (targetPoint);

            // If terrain contains solid objects OR if tile has no walkable entity on a non-walkable tile
            if (targetTile == null || sender.checkCollision(this, targetPoint) || !targetTile.IsWalkable && !sender.checkCollision(this, targetPoint, false))
                return;

            location.X = (float)Math.Round(moveLocationX, 1);
            location.Y = (float)Math.Round(moveLocationY, 1);

            //Check if the player is moving on a entity and call oncolission of that entity
            List<Entity> entities = sender.getEntities();

            for(int i = 0; i < entities.Count; i++)
            {
                if(sender.checkCollision(this, entities[i]) && entities[i].getType() != ENTITIES.player)
                {
                    entities[i].onCollision(this);
                }
            }
        }
Beispiel #13
0
 public virtual void move(World sender, PointF relativeLocation)
 {
     //Entity checks
     //Have world move the entity
     location.X += relativeLocation.X;
     location.Y += relativeLocation.Y;
 }
Beispiel #14
0
        public void reset(int screenResX, int screenResY, float drawRatio, World world)
        {
            this.world = world;
            base.resize (screenResX, screenResY, drawRatio);

            width = Math.Min (StaticVariables.dpi * 2, screenResX / 2);
            tabWidth = width - tabbarWidth - margin*2;

            rowLength = (width - margin * 2 - tabbarWidth) / StaticVariables.tileSize;
            currentHover = new Point (0, 0);

            worldSize = world.getSize ();

            terrainTiles = world.getTileTypes ();
            loadEntities ();

            selectedTab = 0;
            selected = -1;
        }
Beispiel #15
0
 public addEntity()
 {
     // Initialize world
     this.w = new World(Size, Size);
 }