Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (515)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Level tree method?  (Read 2529 times)
0 Members and 1 Guest are viewing this topic.
Offline marcuiulian13
« Posted 2012-04-14 17:43:09 »

Hello everyone!
I am still developing my game and i got another problem. Is there any way to create a level "tree" like the one i have in image? A am loading every level from another image. Here is the example i need.


http://imageshack.us/photo/my-images/812/85362882.png/

The green zones are the zones i already linked. The linking between chambers are some "portals" that load a specific level (for example, the left portal on "start" map teleports you to the left map(the linking is done manually) and the right portal in the left map teleports you to the "start" map).

Getting a project done is by far the most hard thing in game development.
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-04-14 18:34:43 »

Your "chambers" class could have 4 references to "portals", one for each side. This way, it's very dynamic.

Offline marcuiulian13
« Reply #2 - Posted 2012-04-14 19:19:21 »

I am already doing this, but i don't know how to make it dynamic. Now i use these to make portals:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
                if(currentLevel == levels.get(0)) {
                    Level.others.add(new Teleporter((w-1)/2*32, (h-2)*32, (Level) levels.get(1)));
                   Level.others.add(new Teleporter(w/2*32, (h-2)*32, (Level) levels.get(1)));
                   
                   Level.others.add(new Teleporter(32, (h-1)/2*32, (Level) levels.get(2)));
                   Level.others.add(new Teleporter(32, h/2*32, (Level) levels.get(2)));

                   Level.others.add(new Teleporter((w-1)/2*32, 32, (Level) levels.get(3)));
                   Level.others.add(new Teleporter(w/2*32, 32, (Level) levels.get(3)));
                   
                   Level.others.add(new Teleporter((w-2)*32, (h-1)/2*32, (Level) levels.get(4)));
                   Level.others.add(new Teleporter((w-2)*32, h/2*32, (Level) levels.get(4)));
                } else if(currentLevel == levels.get(1)) {
                   Level.others.add(new Teleporter((w-1)/2*32, 32, (Level) levels.get(0)));
                   Level.others.add(new Teleporter(w/2*32, 32, (Level) levels.get(0)));
                } else if(currentLevel == levels.get(2)) {
                   Level.others.add(new Teleporter((w-2)*32, (h-1)/2*32, (Level) levels.get(0)));
                   Level.others.add(new Teleporter((w-2)*32, h/2*32, (Level) levels.get(0)));
                } else if(currentLevel == levels.get(3)) {
                   Level.others.add(new Teleporter((w-1)/2*32, (h-2)*32, (Level) levels.get(0)));
                   Level.others.add(new Teleporter(w/2*32, (h-2)*32, (Level) levels.get(0)));
                } else if(currentLevel == levels.get(4)) {
                   Level.others.add(new Teleporter(32, (h-1)/2*32, (Level) levels.get(0)));
                   Level.others.add(new Teleporter(32, h/2*32, (Level) levels.get(0)));
                }

Getting a project done is by far the most hard thing in game development.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-04-14 19:55:49 »

Why is "others" static?!

Also, I see you are casting when retrieving from an ArrayList; you're not using generics properly.

Offline marcuiulian13
« Reply #4 - Posted 2012-04-14 21:32:44 »

I'm not using ArrayList, i implemented a bag to store everything.

Getting a project done is by far the most hard thing in game development.
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-04-14 23:09:46 »

Then use generics on that Bag so you won't have to cast everywhere.

Why do you have a static "others" bag in Level? As I mentioned before, you should have 4 Teleporter variables named north,south,east,west in Level and this way you can dynamically create Level.

Offline ReBirth
« Reply #6 - Posted 2012-04-15 03:04:40 »

What is the static class Level?

Offline marcuiulian13
« Reply #7 - Posted 2012-04-15 10:07:52 »

In my game, Level is an object that contains some Bags of tiles, levelImage and levelTexture. I am going to change the statics to normal variables.

EDIT: Okay, so i think that i don't know when to use static. When a player steps into a portal, i use my levelLoader to load a new level. Now my question: When i want to refer to this levelLoader class in..let's say..entity class, is it okay to make a new reference to levelLoader in this class? I think, no, so i have to make a static variable in my GameLauncher class with the current level. I am right?

Getting a project done is by far the most hard thing in game development.
Offline atombrot
« Reply #8 - Posted 2012-04-15 10:53:12 »

You could apply the singleton pattern, when you wan't to have exactly one instance of the class... I use something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
class LevelLoader
{
  private static LevelLoader levelLoader;

  public static LevelLoader get()
  {
    if(levelLoader == null)
      levelLoader = new LevelLoader();
    return levelLoader;
  }

  private LevelLoader()
  {
  }
}


with the private constructor, you make sure that the class cannot be constructed anywhere else. Then If you want to use the loader, you simply call LevelLoader.get() to get the instance. You could also add some initialization code into the if block in the get method, so the loader is initialized with the correct parameters... hope that helps
Offline marcuiulian13
« Reply #9 - Posted 2012-04-15 14:10:21 »

Thanks!

So, how could i store the levels so i can dynamically create the linkings to portals?Huh

Currently i have this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
package com.game.level;

import java.awt.image.BufferedImage;

import com.game.entity.Player;
import com.game.level.buildings.Teleporter;
import com.game.level.tiles.Tile;
import com.game.util.Bag;

public class Level {
   int levelWidth, levelHeight;
   Tile[] tiles;
   int maxEnemy;
   Bag enemy, unpassableTiles, passableTiles, others;
   Player player;
   BufferedImage levelImage;
   String levelName;
   BufferedImage[][] levelTexture;
   public Teleporter north, south, west, east;
   
   public Level(BufferedImage image, int enemies, BufferedImage[][] texture) {
      levelImage = image;
      maxEnemy = enemies;
      levelTexture = texture;
      levelWidth = levelImage.getWidth();
      levelHeight = levelImage.getHeight();
     
      north = new Teleporter((levelWidth-1)/2*32, 32, 64, 32, null/*WHERE???*/);
      south = new Teleporter((levelWidth-1)/2*32, (levelHeight-2)*32, 64, 32, null/*WHERE???*/);
      west = new Teleporter(32, (levelHeight-1)/2*32, 32, 64, null/*WHERE???*/);
      east = new Teleporter((levelWidth-2)*32, (levelHeight-1)/2*32, 32, 64, null/*WHERE???*/);
   }
   
   //GETTERS AND SETTERS
   public int getMaxEnemy() { return maxEnemy; }
   public int getLevelWidth() { return levelWidth; }
   public int getLevelHeight() { return levelHeight; }
   public Tile[] getTiles() { return tiles; }
   public Bag getEnemy() { return enemy; }
   public Bag getUnpassableTiles() { return unpassableTiles; }
   public Bag getPassableTiles() { return passableTiles; }
   public Bag getOthers() { return others; }
   public Player getPlayer() { return player; }
   public BufferedImage[][] getLevelTexture() { return levelTexture; }
   //GETTERS AND SETTERS
}

Getting a project done is by far the most hard thing in game development.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #10 - Posted 2012-04-15 14:35:16 »

either specifie for each level only a strict nummber of portals to other levels, like ranking said.
and then just do something like
1  
2  
3  
4  
5  
Level current;

...found out that the player wants to travel to east..

current = current.getEastLevel();


or you could do it totaly generic where you just place portal entitys in your levels and when a player enters/uses one teleporter ask that teleporter were to go.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
class Portal
{
  Level destination;

  use()
  {
    game.setCurrentLevel(destination);
  }

}



In either case you somehow must setup this Level graph at the beginnning of the game, normally you would want to create your levels with some level editor and save them in some configuration file.
This file can be read at the start of the game and set up all the levels and their connections.
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2012-04-15 14:38:51 »

I'm not using ArrayList, i implemented a bag to store everything.
sorry accidental appreciate. Enjoy! Tongue

Concerning levels, your game manager holds the reference of the current level. When the user goes east, it loads the level that is reference in the current level's east variable.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public class Level {
    private Level north, south, east, west;

    /* getters and setters for levels */
}

/* in player class's update method */
If goes north
    gameManager.load(Direction.NORTH); //Direction would be an enumerated type
....

/* in GameManager.load */
public void load(Direction dir) {
    switch(dir) {
        case NORTH: currentLevel = currentLevel.getNorth(); break;
        case SOUTH: currentLevel =  currentLevel.getSouth(); break;
        .....
        default: return;
    }
   
    currentLevel.init();
}

Offline Danny02
« Reply #12 - Posted 2012-04-15 14:43:56 »

sry ranking,
but i think , this is not such a good idea.

You are outsourceing resposibility and implementations details out of the level class to the gameloader.

just get the needed next level from the current level and initialize it.
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-04-15 14:47:58 »

The manager is responsible for managing the levels. The only thing in Level should only concern Level.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (25 views)
2014-10-26 10:14:04

theagentd (31 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!