Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
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  
  Storing gameobjects, and calling them through events  (Read 661 times)
0 Members and 1 Guest are viewing this topic.
Offline waco001

Junior Newbie


Exp: 2 years



« Posted 2014-04-29 11:45:19 »

Hello Guys, New member here, but have read many posts here also  Grin.

I'm working on a 2D tile based game in which the player interacts with other game objects (chests, AI, Doors, Houses etc...). The entire map will be stored in a file which I can read. When loading the tilemap, it will find any tile with the ID that represents a gameobject and store it in a hashmap (right data structure I think?).

1  
    private static HashMap<Integer, Class<GameObject>> gameObjects = new HashMap<Integer, Class<GameObject>>();


How exactly would I go about calling, and checking for events? I figure that I would just call the update, render and input methods of each gameobject using the hashmap. Should I got towards a Minecraft/Bukkit approach (sorry only example I can think of), where the user registers an event, and it gets called whenever that event happens, and where should I go as in resources to learn about that type of programming, (Java, LWJGL).

Or should I just loop through the entire hashmap looking for an event that fits?
Thanks waco
Offline StrideColossus
« Reply #1 - Posted 2014-04-29 12:15:32 »

Couple of thoughts:

1. Tile-map

If you have a tile-map, why organise the data as a hash-map indexed by ID?

Seems more logical have a 2D array (or similar data-structure) that represents each tile and have those reference your game objects?

e.g.

1  
2  
3  
4  
5  
6  
7  
class Tile {
    private final Set<GameObject> objects = ...
}

class World {
    private final Tile[][] map = ...
}


No need for any IDs or look-ups since (presumably) you know which tile(s) you are dealing with at and the above tells you which objects are on each tile.

Or am I missing the point completely here?

2. Events

I assume you are referring to user input events here?  Or are you also talking about other events such as timers, AI, random events, etc?

In either case, presumably you know which tile(s) the event applies to so it's just a matter of handling that event in some sort of controller class and updating the state of the game object(s) on that tile?

- stride
Offline waco001

Junior Newbie


Exp: 2 years



« Reply #2 - Posted 2014-04-29 19:37:35 »

Thanks for the reply,
1. The tilemap stores the graphical code to draw the screen, as in the floor tiles, player, trees, etc (in 3 layers). Currently each different type of tile is stored in an enum. What I want is to be able have an event system that interacts with the game object manager. Each game object class will have an event like object move event. What I'm thinking of is what I think is called registration in Java?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StrideColossus
« Reply #3 - Posted 2014-04-30 08:01:07 »

Still not entirely sure what you are asking for.
Also registration isn't a Java (or OO) concept AFAIK so not sure what you mean by that.
Might be worth posting some of your code, or maybe a rough diagram to illustrate your design?
Offline waco001

Junior Newbie


Exp: 2 years



« Reply #4 - Posted 2014-04-30 11:52:35 »

Hi sorry for the confusion, basically what I want to know is the best and fastest way to store many, many game objects and call events from them, render methods, and update methods.
All gameobjects will extend the GameObject class.
For my gameObjectManagerClass, I currently have
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class GameObjectManager {
   private static ArrayList<GameObject> gameObjects = new ArrayList<GameObject>();
   public static void addGameObject(GameObject gameObject){
      gameObjects.add(gameObject);
   }
   public static void removeGameObject(GameObject gameObject){
      gameObjects.remove(gameObject);
   }

   public void update(){
      for(GameObject i : gameObjects){
         i.update();
      }
   }
   public void render(){
      for(GameObject i : gameObjects){
         i.render();
      }
   }
}

Is what I have the best way of storing gameobjects?

ALSO, I've been wanting to implement an event system similar to what bukkit has.

Ex.
1  
2  
3  
4  
@EventHandler
  public void onLogin(PlayerLoginEvent event) {
    getLogger().log(Level.INFO, "Player " + event.getPlayer().getName() + " is logging in!");
  }

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public final class LoginListener implements Listener {
    public LoginListener(LoginPlugin plugin) {
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }
 
    @EventHandler
    public void normalLogin(PlayerLoginEvent event) {
        // Some code here
    }
 
    @EventHandler(priority = EventPriority.HIGH)
    public void highLogin(PlayerLoginEvent event) {
        // Some code here
    }
}

This is a nice event system I would eventually like to work through...

And when I mentioned registering, I meant like saving the Gameobject class (Player that extends GameObject) in the gameobjectmanager class and calls methods in them.

Thanks waco
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

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

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

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

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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!