2
\$\begingroup\$

I am coding in C#. Due to limitations of the environment I will be working in, I cannot use any framework like the XNA Framework; only what is provided by default on Windows.

I want to create multiple ledges in a platform game. This is what I currently have in the object class:

class Object
{
    Bitmap _image;
    Usables.Collisions collideType;

    public Object(string image, bool collideX, bool collideY)
    {
        //sets image for picturebox BG
        _image = new Bitmap(Usables.baseDir + image);
        // Collision also set here
    }

    public Bitmap Image
    {
        get { return _image; }
    }
}

and I am assigning it to a pictureBox this way in the form:

public void initGame()
    {
        pb_ledge1.Image = World.Ledge.Image;
    }

And the World class is like this:

class World
{
    static Object Ledge1 = new Object("test.png", true, true);

    static public Object Ledge
    {
        get { return Ledge1; }
    }
}

Obviously in a finished product the World class would not be set like that. The images for the different ledge types would be set dynamically.

But my main concern is if I had 8 blocks going across the screen, I would have 8 pictureBoxes, and I would assume professional game developers do not manually assign each 8 boxes individually. Is there a way around this?

\$\endgroup\$
  • \$\begingroup\$ I think what you are looking for a level editor, such that you aren't having to code the individual positions of your platforms. Or rather, you're interested in coding a level editor. \$\endgroup\$ – Draco18s no longer trusts SE Dec 4 '15 at 21:59
  • \$\begingroup\$ I'm fine with having to code the individual positions. I'm in Visual Studio anyway, so all I have to do is drag in a pictureBox. What I don't want to do is picturebox1 = image, picturebox2 = sameImage, pictureBox3 = same image, as I'd just have this long list of pictureboxes getting assigned the exact same thing. \$\endgroup\$ – Tiernan Watson Dec 4 '15 at 22:05
  • \$\begingroup\$ Well, in some respect, you need to tell the engine "this is the image to use" some way or another. You can either do it explicitly, or you can code in some cheeky way of programatically determining it (e.g. "if the platform is made of Material.Stone, then use imageStone") but you're still going to do something. \$\endgroup\$ – Draco18s no longer trusts SE Dec 4 '15 at 22:14
1
\$\begingroup\$

Flyweight

One approach is to use a form of the flyweight pattern.

You don't give each instance of an object the image. Instead, you have one object that represents a type of thing in your game and has all common non-instance data, like the image, physics properties, etc.

The individual instances then have per-instance information, like position, and a reference to the shared data object.

Cloning

Another approach is to clone a single object. Once you configure a single instance of an object the way you want, you just create copies/clones of that object.

In this approach, you'll often have a list of object types by name somewhere in your level and then the instances in the level reference those objects by name. During level load, you just call your Clone method on the shared object to create each instance.

All the images are such are just copied from the original so there's no need to respecify them for each copy.

Reification

Similar to cloning, you might just recreate the object from encoded data.

This is very similar to cloning objects; it's mostly just a different want of implementing the technique. Instead of loading a whole object and keeping a fully instantiated object, you instead cache some efficient serialized copy of the object configuration and then deserialize that for each instance.

The advantage of this approach is that your game objects and game logic don't need to handle the special case of template objects vs instance objects. You don't need much of that logic at first but it can add up as your game gets more complex.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Just to comment: Minecraft operates on a Flyweight model for blocks and items, while entities (zombies, players, dropped items, chests, furnaces) all operate on Reification (recreated from compress/saved data) or Cloning (in some cases, such as dimensional travel). This is how it can support millions of blocks, but only a few dozen mobs or machine-blocks (typically from mods) before lag becomes noticeable very quickly. \$\endgroup\$ – Draco18s no longer trusts SE Jan 4 '16 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.