Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: Saving world

  1. #1
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Saving world

    Hey! I's like a little help with saving my world. in fact,i have no idea how to do it myself.And also loading,if you can get it.just add two methods for saving/loading. if you can.

    World code:
    Java Code:
    package net.ulixava.RetroHunter;
    
    import java.awt.*;
    import java.util.*;
    
    public class Level {
    	public static int worldW= 100, worldH = 100;
    	public static Block [][] block = new Block[worldW][worldH];
    	
    	public Level() {
    		for(int x=0;x<block.length;x++) {
    			for(int y=0;y<block[0].length;y++) {
    				block[x][y] = new Block(new Rectangle(x * Tile.tileSize, y * Tile.tileSize, Tile.tileSize, Tile.tileSize), Tile.air);
    			}
    		}
    		
    		
    		generateLevel();
    	}
    	
    	public void generateLevel() {
    		//Generating Mountains, Earth, etc.
    		for(int y=0;y<block.length;y++) {
    			for(int x=0;x<block[0].length;x++) {
    	            if(y > worldH / 5.5) {
            			if(new Random().nextInt(100) > 15) {
    		            	try{
    		            		if(block[x-1][y-1].id == Tile.earth) {
    		            			block[x][y].id = Tile.earth;
    		            		}
    		            	} catch (Exception e) {
    		            		
    		            	}
            			}
            			if(new Random().nextInt(100) > 20) {
    		                try{
    		                	if(block[x+1][y-1].id == Tile.earth) {
    		            			block[x][y].id = Tile.earth;
    		            		}
    		            	} catch (Exception e) {
    		            		
    		            	}
            			}
            			
    						try{
    							if(block[x][y-1].id == Tile.earth) {
    		            			block[x][y].id = Tile.earth;
    		            		}
    						} catch (Exception e) {
    							
    						}
            			if(new Random().nextInt(100) > 30) {
    						try{
    							if(block[x][y].id == Tile.earth) {
    		            			block[x][y].id = Tile.earth;
    		            		}
    						} catch (Exception e) {
    							
    						}
            			}
    
    	            	
    	            		 	
    	            	if(new Random().nextInt(100) < 2) {
    	            		block[x][y].id = Tile.earth;
    	            	}
    	            }
    			}
    		}
    		//placing grass blocks
    		for(int y=0;y<block.length;y++) {
    			for(int x=0;x<block[0].length;x++) {
    				if(block[x][y].id == Tile.earth && block[x][y-1].id == Tile.air) {
    					block[x][y].id = Tile.grass;
    				}
    			}
    		}
    	}
    	public static void building(int camX, int camY, int renW, int renH) {
    		if(Component.isMouseLeft || Component.isMouseRight){
    			for(int x=(camX / Tile.tileSize);x<(camX / Tile.tileSize) + renW;x++) {
    				for(int y=(camY / Tile.tileSize);y<(camY / Tile.tileSize)+ renH;y++) {
    					if(x >= 0 && y >= 0 && x <worldW && y <worldH) {
    						if(block[x][y].contains(new Point((Component.mse.x ) / Component.pixelSize + (int) Component.sX, (Component.mse.y) / Component.pixelSize + (int) Component.sY))){
    							int sid[] = Inventory.invBar[Inventory.selected].id;
    							
    							if (Component.isMouseLeft){
    								block[x][y].id = Tile.air;
    							} else if (Component.isMouseRight) {
    								if(block[x][y].id == Tile.air){
    									if (sid != Tile.air){
    										block[x][y].id = sid;
    									}
    									if(block[x][y+1].id == Tile.grass) {
    										block[x][y+1].id = Tile.earth;
    									}
    									
    									
    									
    								}
    							}
    							break;
    						}
    					}
    				}
    			}
    		}
    	}
    		
    	public void tick(int camX, int camY, int renW, int renH) {
    		if(!Inventory.isOpen){
    			building(camX, camY, renW, renH);
    		}
    	}
    	public void render(Graphics g, int camX, int camY, int renW, int renH) {
    		for(int x=(camX / Tile.tileSize);x<(camX / Tile.tileSize) + renW;x++) {
    			for(int y=(camY / Tile.tileSize);y<(camY / Tile.tileSize)+ renH;y++) {
    				if(x >= 0 && y >= 0 && x <worldW && y <worldH) {
    					block[x][y].render(g);
    					
    					if(block[x][y].id != Tile.air && !Inventory.isOpen){
    						if(block[x][y].contains(new Point((Component.mse.x ) / Component.pixelSize + (int) Component.sX, (Component.mse.y) / Component.pixelSize + (int) Component.sY))){
    							g.setColor(new Color(255, 255, 255, 60));
    							g.fillRect(block[x][y].x - camX, block[x][y].y - camY, block[x][y].width, block[x][y].height);
    						}
    					}
    				}
    			}
    		}
    	}
    }
    Thanks for any help!~

  2. #2
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Saving world

    what did you envision this being saved to and loaded from?

  3. #3
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    If you mean the format,preferably .txt format. if that isnt what you meant,can you be more clear,please?

  4. #4
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    Well,if you meant like the way it saves,sometimeg like 0,0,0,1,0,0,0,0 for 0 being air,and 1 being earth. except a lot longer.

  5. #5
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Saving world

    Well you just need to create a toString() method to represent how you want it saved, and then create a readin method to recreate the objects from the txt file. After that its as simple as reading and writing to the text file with a filewriter and filereader.

  6. #6
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    Yes,i know. but i'd like to know how to do that. I'm not experienced in writing/reading in java,and i got most of my level code off of a tutorial.

  7. #7
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Saving world

    Ok well look at your code a decide what information you need to create the instance of the game where you are. Put that into a string representation. Once you get that far we can discuss how to save and read from a txt file

  8. #8
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    Yes,i know. but i don't know HOW to do that. i'm not very experienced in java.if you can link me to a tutorial of some sort or something,then i'll be fine.

  9. #9
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Saving world

    Its just a toString method. for instance lets say I have a Person class:
    Java Code:
    class Person{
    public Person(){
    this("","",0,"");
    }
    /* Fully Specified Constructor*/
    public Person(String fName, String lName, int sSecurity, String gender){
    this.fName=fName;
    this.lName= lName;
    this.sSecurity= sSecurity;
    this.gender= gender;
    }
    //Getters
    public String getLName(){
    return lName;
    }
    public String getFName(){
    return fName;
    }
    public String getGender(){
    return gender;
    }
    public int getSSecurity(){
    return sSecurity;
    }
    //Setters
    public void setFName(String fName){
    this.fName=fName;
    }
    
    public void setLName(String lName){
    this.lName=lName;
    }
    
    public void setSSecurity(int sSecurity){
    this.sSecurity=sSecurity;
    }
    public void setGender(String gender){
    this.gender=gender;
    }
    //This is a toString method. 
    public String toString(){
    return "Name: " + fName + " " + lName + " Gender: " + gender + " Social Security #:  " + sSecurity;
    }
    String fName;
    String lName;
    int sSecurity;
    String gender;
    }
    A toString Method is a way that I can record all the information in a string representation I need to recreate the object in this instance. This is what you need to do to start with.

  10. #10
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    Okay,i'll try something like this.

  11. #11
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    I was thinking of using something like this,but i don't know if i just messed it up.I was thinking it could read each line of the world separately,and then write to the file or whatever.And just go up until it maxed out at the world height.
    Did i even do this correctly?


    Java Code:
    package net.ulixava.RetroHunter;
    
    class SaveLoadGame{
    public SaveLoadGame(){
    this(0,0,"");
    }
    /* Fully Specified Constructor*/
    public SaveLoadGame(int WorldW, int WorldH, String LineData){
    this.WorldW=Level.worldW;
    this.WorldH=Level.worldH;
    this.LineData= LineData;
    }
    //Getters
    public int getWorldW(){
    return WorldW;
    }
    public int getWorldH(){
    return WorldH;
    }
    public String getLineData(){
    return LineData;
    }
    //Setters
    public void setWorldH(int WorldH){
    this.WorldH=WorldH;
    }
     
    public void setWorldW(int WorldW){
    this.WorldW=WorldW;
    }
     
    public void setLineData(String LineData){
    this.LineData=LineData;
    }
    
    //This is a toString method. 
    public String toString(){
    return "World Height " + WorldH + " World Width" + WorldW + " Current Line Data " + LineData;
    }
    int WorldH;
    int WorldW;
    String LineData;
    }
    Last edited by RiokuTheSlayer; 06-01-2013 at 06:46 AM.

  12. #12
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    um..what do i do now? how do i get the blocks and all that? i already gave you the level code,and that's all it is. I use this for all blocks :

    Java Code:
     for(int x=(camX / Tile.tileSize);x<(camX / Tile.tileSize) + renW;x++) {
    				for(int y=(camY / Tile.tileSize);y<(camY / Tile.tileSize)+ renH;y++) {
    Then use block[x][y].id =(=) Tile.(Id of block) to do whatever,and Level.block[x][y] =(=) to refrence the blocks...

  13. #13
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    Hello?I'm really stuck at this point,and i'd really like to have this as a feature! If anyone can help me,please!

  14. #14
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    Someone? anyone?

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Saving world

    Have you thought about serialization instead? It doesn't produce a human readable file but the actual reading/writing can be a breeze if you do it correctly.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    Personally,i don't really care HOW i do it,lol. i just want it done. I've been looking around,but all i really need to know it how to put all the data into one file,then also be able to read it. the world isn't that big,either.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Saving world

    Quote Originally Posted by RiokuTheSlayer View Post
    Personally,i don't really care HOW i do it,lol. i just want it done. I've been looking around,but all i really need to know it how to put all the data into one file,then also be able to read it. the world isn't that big,either.
    Then have a look at some serialization tutorials. Another option if you want the data to be readable is to use JAXB to save properties to an XML file. I think XStream can do this as well.

  18. #18
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    I have no idea what you just said. all i want is for it to read the world,write it to a file,then be able to read the file,and load the world.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Saving world

    Quote Originally Posted by RiokuTheSlayer View Post
    I have no idea what you just said. all i want is for it to read the world,write it to a file,then be able to read the file,and load the world.
    What don't you understand about "look at some xxx tutorials"? I'm sure that with Google and some effort you can figure this out. Come on now, no one is going to write this code for you.

  20. #20
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: Saving world

    Quote Originally Posted by Fubarable View Post
    Come on now, no one is going to write this code for you.
    Haha,i know. but thats what a team is for. but it's just me. i've already looked at tutorials,and searched for it. but that's why i posted here,was because i couldnt find what i need. What i need,is a way to put the level into a string of code or whatever. I'm not very expirienced,and that's why i'm asking for help.

Page 1 of 2 12 LastLast

Similar Threads

  1. Java world generator-real world with people and stuff
    By conker5295 in forum Jobs Offered
    Replies: 2
    Last Post: 02-23-2012, 01:22 PM
  2. Hello World.... now what!?
    By JoshNerd557 in forum New To Java
    Replies: 11
    Last Post: 03-07-2011, 03:22 AM
  3. Hello World in AOP
    By lheebhee in forum Eclipse
    Replies: 0
    Last Post: 11-18-2009, 11:30 AM
  4. hello world
    By wayne.seymour in forum Introductions
    Replies: 2
    Last Post: 02-11-2009, 01:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •