Where to include map data etc.

Started by
5 comments, last by Eruadan 9 years, 9 months ago

Ok so I am currently in the process of learning java and my game that I am making is all console based, meaning no graphics are involved. I was kind of turned off by the idea of just a text adventure or guess the word game, so I am trying to make a game that might be kind of fun to play. The best way to describe it would be a console version of pokemon. I know this is challenging for a beginner, but I want to challenge myself and learn java at the same time. So my question is where should I include all of the information for my map objects. I have a map class that includes the name, type, etc. but I am not sure as to where I need to setup all of my maps if the player is going to be able to go back and forth between maps. I hope this question makes sense and thank you for any help you can give me!

Advertisement

Make a universal World class that stores the maps that is currently selected, and and other world-related things. This class will handle which entities are currently in the map, which room is selected, and other world-related details. That way you can avoid using global variables and everything is packed together in a nice, neat OO way.

http://docs.oracle.com/javase/tutorial/jaxp/

Java has built in classes to process XML files. You can store your maps in whatever format you need, and easily parse them. For example, each map can have references to the other maps that are connected. This gives you flexibility but is easy to adjust to your needs.


<?xml version="1.0"?>

<maps>
  <map id="start">
    <north id="map1"/>
    <south id="map1"/>

  <!-- etc  -->
  </map>
</maps>

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

So are you saying to make a class that includes methods to build all of my objects? Like a class called Setup that has methods like Setup.maps(); and so forth? If this is the right way of going about this, then should all of these methods be called as soon as the player selects play from the menu?

Rick, there is no "right" way to do this stuff. If you ask five different people to code an application, you'll get five completely different results. But it doesn't matter if they all get the job done. If you're the only one working on this, then do what makes sense to you. You will make mistakes, and you'll design stuff that doesn't work. That is just part of learning how to make games.

My first game, Tetris, had to be completely re-written because I designed the game loop so that I couldn't add sounds. It is all part of the process.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Yes, I suppose this is true. I guess a better question would be if this is a logical way to attempt to do this. If it doesn't seem logical I would really rather spend another day researching instead of programming a nice chunk only to realize that it isn't going to work many days in the future. But anyways I think I will give it a shot. Thanks for the help guys and you will probably be seeing me again on here. :p

Ok so I am currently in the process of learning java and my game that I am making is all console based, meaning no graphics are involved. I was kind of turned off by the idea of just a text adventure or guess the word game, so I am trying to make a game that might be kind of fun to play. The best way to describe it would be a console version of pokemon. I know this is challenging for a beginner, but I want to challenge myself and learn java at the same time. So my question is where should I include all of the information for my map objects. I have a map class that includes the name, type, etc. but I am not sure as to where I need to setup all of my maps if the player is going to be able to go back and forth between maps. I hope this question makes sense and thank you for any help you can give me!

So I started replying to this, but I'm not sure what you mean by maps... How are you going to have a text based map? What exactly do you want to store?

I recently coded a 2D RPG in C# and stored 234 16x16 maps inside a text file. At the start of the game (or when a player loaded the game) the program would read all the map data to the RAM by storing it in an array of strings. My map consisted of 16x16 (256) strings for each tile pointer to a tile image, along with values that indicate it's map number, and adjacent maps.

Here is an example of what would get stored:


228 //Map Number
220 //North Map
229 //East Map
999 //South Map (999 = No adjacent map, edge of game world)
227 //West Map
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/gedge5.bmp
GFX/brock1.bmp
GFX/brock1.bmp
GFX/brock1.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/gedge2.bmp
GFX/gedge1.bmp
GFX/beach.bmp
GFX/brock2.bmp
GFX/brock2.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/gedge2.bmp
GFX/gedge1.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/gedge2.bmp
GFX/gedge3.bmp
GFX/gedge3.bmp
GFX/gedge1.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/grass.bmp
GFX/gedge2.bmp
GFX/gedge1.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/gedge3.bmp
GFX/gedge3.bmp
GFX/gedge3.bmp
GFX/gedge3.bmp
GFX/gedge3.bmp
GFX/gedge3.bmp
GFX/gedge1.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/bedge1.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/bedge1.bmp
GFX/bedge2.bmp
GFX/bedge3.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/bedge1.bmp
GFX/bedge2.bmp
GFX/bedge3.bmp
GFX/water.bmp
GFX/water.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/beach.bmp
GFX/bedge1.bmp
GFX/bedge3.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/bedge2.bmp
GFX/bedge2.bmp
GFX/bedge2.bmp
GFX/bedge2.bmp
GFX/bedge2.bmp
GFX/bedge2.bmp
GFX/bedge2.bmp
GFX/bedge2.bmp
GFX/bedge2.bmp
GFX/bedge2.bmp
GFX/bedge3.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp
GFX/water.bmp

And here is the MapData class


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace Arborea
{
    class MapData
    {
        public int NumOfMap;
        public int MapNumber;
        public int AdjacentMapN;
        public int AdjacentMapE;
        public int AdjacentMapS;
        public int AdjacentMapW;
        public int[,] MapTile = new int[16, 16];
        public int[,] MapCode = new int[16, 16];
        public bool Paint;
        public string CurrentPaint;
        public string CurrentCode;
        public int CurrentTileBox;
        public bool CheckNPC;
        public int NPCNumber;
        public Label[,] CodeLabel = new Label[16, 16];
        public string[] MapLines = new string[65250];
        public string[] MapCodeLines = new string[65250];

        public void CreateMap()
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"DATA/maps.txt", true))
            {
                int blankline;
                blankline = 0;
                while (blankline < 261)
                {
                    file.WriteLine(".");
                    blankline++;
                }

            }
            var lines = File.ReadAllLines(@"DATA/maps.txt");
            lines[0] = Convert.ToString(NumOfMap);
            lines[(MapNumber - 1) * 261 + 0 + 1] = Convert.ToString(MapNumber);
            lines[(MapNumber - 1) * 261 + 1 + 1] = Convert.ToString(AdjacentMapN);
            lines[(MapNumber - 1) * 261 + 2 + 1] = Convert.ToString(AdjacentMapE);
            lines[(MapNumber - 1) * 261 + 3 + 1] = Convert.ToString(AdjacentMapS);
            lines[(MapNumber - 1) * 261 + 4 + 1] = Convert.ToString(AdjacentMapW);
            int x = 5;
            while (x <= 260)
            {
                lines[(MapNumber - 1) * 261 + x + 1] = "GFX/grass.bmp";
                x++;
            }
            File.WriteAllLines(@"DATA/maps.txt", lines);

            // WRITE DEFAULT MAP CODE TO MAPCODE.TXT
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"DATA/mapcode.txt", true))
            {
                int blankline;
                blankline = 0;
                while (blankline < 261)
                {
                    file.WriteLine(".");
                    blankline++;
                }

            }
            var codelines = File.ReadAllLines(@"DATA/mapcode.txt");
            codelines[0] = Convert.ToString(NumOfMap);
            codelines[(MapNumber - 1) * 261 + 0 + 1] = Convert.ToString(MapNumber);
            codelines[(MapNumber - 1) * 261 + 1 + 1] = Convert.ToString(AdjacentMapN);
            codelines[(MapNumber - 1) * 261 + 2 + 1] = Convert.ToString(AdjacentMapE);
            codelines[(MapNumber - 1) * 261 + 3 + 1] = Convert.ToString(AdjacentMapS);
            codelines[(MapNumber - 1) * 261 + 4 + 1] = Convert.ToString(AdjacentMapW);
            int y = 5;
            while (y <= 260)
            {
                codelines[(MapNumber - 1) * 261 + y + 1] = "N";
                y++;
            }
            File.WriteAllLines(@"DATA/mapcode.txt", codelines);
        }
        public void LoadAllMaps()
        {
            MapLines = File.ReadAllLines(@"DATA/maps.txt");
            MapCodeLines = File.ReadAllLines(@"DATA/mapcode.txt");
        }
        public System.Drawing.Image DrawMap(int N, System.Windows.Forms.PictureBox MapBox)
        {
            Bitmap MapBMP = new Bitmap (512, 512);
            MapBox.Invalidate();
            Graphics g = Graphics.FromImage(MapBMP);
            int x = 0;
            int y = 0;
            while (y <= 15)
            {
                while (x <= 15)
                {
                    Bitmap Piece = new Bitmap(MapLines[(N - 1) * 261 + (y * 16 + x) + 6]);
                    g.DrawImageUnscaled(Piece, x * 32, y * 32);
                    x++;
                }
                x = 0;
                y++;
            }
            MapBox.Image = MapBMP;
            MapBox.Refresh();
            return MapBox.Image;
        }
        public System.Drawing.Image DrawTileBox(int N, System.Windows.Forms.PictureBox TileBox)
        {
            Bitmap TileBMP = new Bitmap(128, 512);
            TileBox.Invalidate();
            Graphics g = Graphics.FromImage(TileBMP);
            int x = 0;
            int y = 0;
            while (y <= 15)
            {
                while (x <= 3)
                {
                    Bitmap Piece = new Bitmap(File.ReadLines(@"DATA/tiles.txt").Skip((N - 1) * 64 + (y * 4 + x)).Take(1).First());
                    g.DrawImageUnscaled(Piece, x * 32, y * 32);
                    x++;
                }
                x = 0;
                y++;
            }
            TileBox.Image = TileBMP;
            TileBox.Refresh();
            return TileBox.Image;
        }
        public void SelectPaint (int X, int Y)
        {
            int iX = X/32;
            int iY = Y/32;
            CurrentPaint = File.ReadLines(@"DATA/tiles.txt").Skip((CurrentTileBox - 1) * 64 + (iY * 4 + iX)).Take(1).First();
        }
        public void PaintMap (int X, int Y)
        {
            int iX = X / 32;
            int iY = Y / 32;
            MapLines[((MapNumber-1)*261 + (iY*16 + iX) + 6)] = CurrentPaint;
            File.WriteAllLines(@"DATA/maps.txt", MapLines);
        }
        public void CodeMap (int X, int Y)
        {
            int iX = X / 32;
            int iY = Y / 32;
            MapCodeLines[((MapNumber - 1) * 261 + (iY * 16 + iX) + 6)] = CurrentCode;
            File.WriteAllLines(@"DATA/mapcode.txt", MapCodeLines);
        }
        public void DrawCodeMap(int N, System.Windows.Forms.PictureBox MapBox)
        {

            int iX = 0;
            int iY = 0;
            while (iY <= 15)
            {
                while (iX <= 15)
                {
                    if (CodeLabel[iX, iY] != null)
                    {
                        CodeLabel[iX, iY].Dispose();
                    }
                    if (MapCodeLines[(N - 1) * 261 + (iY * 16 + iX) + 6] == "B")
                    {
                        CodeLabel[iX, iY] = new Label();
                        CodeLabel[iX, iY].Parent = MapBox;
                        CodeLabel[iX, iY].BackColor = Color.Transparent;
                        CodeLabel[iX, iY].Text = MapCodeLines[(N - 1) * 261 + (iY * 16 + iX) + 6];
                        CodeLabel[iX, iY].Location = new Point(iX * 32 + 10, iY * 32 + 10);
                        CodeLabel[iX, iY].Width = 20;
                    }
                    iX++;
                }
                iX = 0;
                iY++;
            }
        }

        public void SetAdjacentMap(string North, string East, string South, string West)
        {
            MapLines[(MapNumber - 1) * 261 + 1 + 1] = North;
            MapLines[(MapNumber - 1) * 261 + 2 + 1] = East;
            MapLines[(MapNumber - 1) * 261 + 3 + 1] = South;
            MapLines[(MapNumber - 1) * 261 + 4 + 1] = West;
            File.WriteAllLines(@"DATA/maps.txt", MapLines);
        }
        public void LoadMap(int NewMap)
        {
            MapNumber = NewMap;
            AdjacentMapN = Convert.ToInt32(MapLines[(MapNumber - 1) * 261 + 1 + 1]);
            AdjacentMapE = Convert.ToInt32(MapLines[(MapNumber - 1) * 261 + 2 + 1]);
            AdjacentMapS = Convert.ToInt32(MapLines[(MapNumber - 1) * 261 + 3 + 1]);
            AdjacentMapW = Convert.ToInt32(MapLines[(MapNumber - 1) * 261 + 4 + 1]);
        }
        public void LoadNPC(PictureBox [] NPCPicBox, Character [] NPC, PictureBox MapPictureBox)
        {
            int n = 0;
            while (n <= 15)
            {
                if (NPC[n] != null)
                {
                    NPCPicBox[NPC[n].ID].Hide();
                    if (NPC[n].Z_Location == MapNumber && NPC[n].Alive == true)
                    {
                        NPC[n].DrawCharacter(NPCPicBox[NPC[n].ID], MapPictureBox);
                    }
                }
                n++;
            }
        }
        public bool CollisionCheck(int direction, int MapNum, Character Player, Character[] NPC)
        {
            CheckNPC = false;
            int iX, iY;
            if (direction == 0)
            {
                iX = Player.X_Location;
                iY = Player.Y_Location + 1;
                if (File.ReadLines(@"DATA/mapcode.txt").Skip((MapNum - 1) * 261 + (iY * 16 + iX) + 6).Take(1).First() == "B")
                {
                    return false;
                }
                int n = 0;
                while (n <= 15)
                {
                    if (NPC[n] != null && NPC[n].Alive == true)
                    {
                        if (NPC[n].Z_Location == MapNum && NPC[n].Y_Location == iY && NPC[n].X_Location == iX)
                        {
                            CheckNPC = true;
                            NPCNumber = n;
                            return false;
                        }
                    }
                    n++;
                }
                return true;
            }
            if (direction == 1)
            {
                iX = Player.X_Location - 1;
                iY = Player.Y_Location;
                if (File.ReadLines(@"DATA/mapcode.txt").Skip((MapNum - 1) * 261 + (iY * 16 + iX) + 6).Take(1).First() == "B")
                {
                    return false;
                }
                int n = 0;
                while (n <= 15)
                {
                    if (NPC[n] != null && NPC[n].Alive == true)
                    {
                        if (NPC[n].Z_Location == MapNum && NPC[n].Y_Location == iY && NPC[n].X_Location == iX)
                        {
                            CheckNPC = true;
                            NPCNumber = n;
                            return false;
                        }
                    }
                    n++;
                }
                return true;
            }
            if (direction == 2)
            {
                iX = Player.X_Location + 1;
                iY = Player.Y_Location;
                if (File.ReadLines(@"DATA/mapcode.txt").Skip((MapNum - 1) * 261 + (iY * 16 + iX) + 6).Take(1).First() == "B")
                {
                    return false;
                }
                int n = 0;
                while (n <= 15)
                {
                    if (NPC[n] != null && NPC[n].Alive == true)
                    {
                        if (NPC[n].Z_Location == MapNum && NPC[n].Y_Location == iY && NPC[n].X_Location == iX)
                        {
                            CheckNPC = true;
                            NPCNumber = n;
                            return false;
                        }
                    }
                    n++;
                }
                return true;
            }
            if (direction == 3)
            {
                iX = Player.X_Location;
                iY = Player.Y_Location - 1;
                if (File.ReadLines(@"DATA/mapcode.txt").Skip((MapNum - 1) * 261 + (iY * 16 + iX) + 6).Take(1).First() == "B")
                {
                    return false;
                }
                int n = 0;
                while (n <= 15)
                {
                    if (NPC[n] != null && NPC[n].Alive == true)
                    {
                        if (NPC[n].Z_Location == MapNum && NPC[n].Y_Location == iY && NPC[n].X_Location == iX)
                        {
                            CheckNPC = true;
                            NPCNumber = n;
                            return false;
                        }
                    }
                    n++;
                }
                return true;
            }
            return true;
        }
        public bool AggroCheck(Character Player, Character[] NPC)
        {
            int n = 0;
            while(n <= 15)
            {
                if(NPC[n] != null && NPC[n].Alive == true && NPC[n].Z_Location == Player.Z_Location && NPC[n].AggresiveState == true)
                {
                    if(NPC[n].Direction == 0 && Player.X_Location == NPC[n].X_Location && Player.Y_Location > NPC[n].Y_Location)
                    {
                        NPCNumber = n;
                        return true;
                    }
                    if (NPC[n].Direction == 1 && Player.X_Location < NPC[n].X_Location && Player.Y_Location == NPC[n].Y_Location)
                    {
                        NPCNumber = n;
                        return true;
                    }
                    if (NPC[n].Direction == 2 && Player.X_Location > NPC[n].X_Location && Player.Y_Location == NPC[n].Y_Location)
                    {
                        NPCNumber = n;
                        return true;
                    }
                    if (NPC[n].Direction == 3 && Player.X_Location == NPC[n].X_Location && Player.Y_Location < NPC[n].Y_Location)
                    {
                        NPCNumber = n;
                        return true;
                    }
                }
                n++;
            }
            return false;
        }
    }
}

This is just an example, and its for a C# 2D RPG, but hopefully it gives you an idea of how my process works. Hopefully that helps some. If you got any questions or need clarification on something, just ask! :D

Best of luck with your game

This topic is closed to new replies.

Advertisement