XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Stuck on a MapObject class :(

Last post 05-03-2008 8:16 PM by mtnPhil. 5 replies.
  • 04-08-2008 4:56 PM

    Stuck on a MapObject class :(

    Hi, I've been working on a learning project for a little bit, and I've come to a problem.

    Before I explain, let me kind of draw out how my mini-game is supposed to work to maybe help understand a little bit more.

    I am using an altered version of the GameStateManagement from the Tutorials as a base for the project(besides a few differences like how the input class is set up and some other minor things, it pretty much works the same way).

    The point of the game is you have a little 2D character on a 2D field (overhead view), and beat up little turnips who are out to get you.  The way I have it set up is there is a Map class which derives from GameScreen.  Because the controls, HUD, etc are all the same on each level, I use that for all of the stuff that gets done for each level.  Each individual level derives from the Map class.  That is the one that holds stuff like which enemies are on the map, and the scenery that gets placed.

    The part where I am stuck though is putting things onto the map.  I wanted to make a MapObject class and have everything that gets drawn derive from that (Monsters, Scenery, the Player, Powerups, etc).  That way, I could put it all into one list and draw/update them all at the same time.  The only problem is, I don't know where the best place would be to Load/Store the texture for the monsters.  Since there might be more than one of each monster, if I made each instance of "Monster" load the texture, would there be memory set aside for each one's texture?  Should I load all the monster textures in the level's LoadContent function and just have a variable that references which texture it should be?

    I'm still pretty new to programming (around a year of experience) so if I might be looking at this the wrong way, any advice would be good to have.
  • 04-08-2008 5:28 PM In reply to

    Re: Stuck on a MapObject class :(

    Answer
    Yami M:
    I don't know where the best place would be to Load/Store the texture for the monsters.  Since there might be more than one of each monster, if I made each instance of "Monster" load the texture, would there be memory set aside for each one's texture? 


    Based on my understanding... there's only 1 real "copy" of the texture. Each monster would get a reference to that copy. The catch is your ContentManager. If you use the same ContentManager to load all of the same textures, then there's only 1 copy and bunches of references. If you used a new ContentManager for each monster, then there'd be a unique copy of the texture per monster.

    So it's just based on where you load it into. If you use the same ContentManager to Load<Texture2D>(filename), then it will share between the monsters.

    -----------------------------------------------
    Jim Welch
  • 04-08-2008 9:14 PM In reply to

    Re: Stuck on a MapObject class :(

    I think I understand.  I'll see how that works.

    (Side note: I accidentally figured out how to use the GamerServices while I was working on that.  There's a lot of stuff in there that could be fun to use for later...)
  • 04-09-2008 2:36 AM In reply to

    Re: Stuck on a MapObject class :(

    Now that I've got that mostly squared away, I moved onto some basic "AI".  By basic, I mean that the turnips will target the closest player and hop towards them.  Then they'll wait for a second and repeat :P.  It's all coming together really well actually.  Except I have been slacking off and playing around letting them chase my character all over.  Now I won't be able to get anything done since I'm just playing around on it....

    But I guess that is a whole other problem on it's own......
  • 05-03-2008 7:59 PM In reply to

    Re: Stuck on a MapObject class :(

    I am by far no expert on the matter, so consider this a suggestion from one newb to another.

    I think what you would want to do for the MapObject is to make that an Interface, rather than a class and have your monsters, items, whatever implement the IMapObject interface.

    The interface would define the methods (such as Draw and Update) and properties (position, active state, etc) that any class MUST have to interact with the map. Make your List of type IMapObject and add the classes that implement the interface to it.

    A lot of code I've looked at also shows a seperate list for each class of object on the map, which you may have to do if there's not enough "common ground" between objects to make an interface with.

  • 05-03-2008 8:16 PM In reply to

    Re: Stuck on a MapObject class :(

    Re IMapObject:

     The framework already defines IDrawable, IGameComponent and IUpdateable (and the GameComponent and DrawableGameComponent classes that implement them). 

    If you add an IGameComponent to Game.Components, its update (if it implements IUpdateable) and draw (if it implements IDrawable) methods will be called automatically by the framework.

Page 1 of 1 (6 items) Previous Next