0
\$\begingroup\$

I just finished to code the most important parts of my game engine (not a tile-map one), such as lights, shadows, objects update/draw, input, ecc. Now I'm making a level editor to simplify the creation of levels. I have a form that does only simple things: place the player, place a block, choose a background, place an enemy... These two components are divided between them (sorry for my bad english).

My question relies on the relation between the form (level editor) and the game engine (the game itself)

These are my thoughts: My thoughs


For a better explaination, take this example:

  • My game_engine project holds a Light class, a Player class, a Background class, an Enemy class. My engine (in abstract) is like this:
namespace game_engine 
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        enum GameStates { TitleScreen, Playing, PlayerDead, GameOver };
        GameStates gameState = GameStates.Playing;

        Enemy zombie1;
        Enemy zombie2;
        Light sun;
        Light lamp1;
        Player player;
        Background mountains;
    }

     public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        this.graphics.PreferredBackBufferWidth = 800;
        this.graphics.PreferredBackBufferHeight = 600;
        this.graphics.ApplyChanges();

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        Camera.ViewPortWidth = 800;
        Camera.ViewPortHeight = 600;
        Camera.Position = new Vector2(0, 0);

        mountains = new Background(...);
        zombie1 = new Enemy(...);
        zombie2 = new Enemy(...);
        sun = new Light(...);
        lamp1 = new Light(...);

        enemy_manager.Add(zombie1);
        enemy_manager.Add(zombie2);
        light_manager.Add(sun);
        light_manager.Add(lamp1);
    }

     protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            switch (gameState)
            {
                case GameStates.TitleScreen:
                    //Something
                    break;
                case GameStates.Playing:
                    enemy_manager.Update(gameTime);
                    light_manager.Update(gameTime);
                    mountains.Update(gameTime);
                    player.Update(gameTime);                    
                    break;
                case GameStates.PlayerDead:
                    //Something
                    break;
                case GameStates.GameOver:
                    //Something
                    break;
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            switch (gameState)
            {
                case GameStates.TitleScreen:
                    //Something
                    break;
                case GameStates.Playing:
                    enemy_manager.Draw(spriteBatch);
                    light_manager.Draw(spriteBatch);
                    mountains.Draw(spriteBatch);
                    player.Draw(spriteBatch);                    
                    break;
                case GameStates.PlayerDead:
                    //Something
                    break;
                case GameStates.GameOver:
                    //Something
                    break;
            }

            base.Draw(gameTime);
        }

Then I have another project which is a form (Level editor) with some function/tool to help me out not putting manually every entity (enemy, light, ecc ...) when initializing the game:

My map editor

At this point I have to "link" the form and my game engine. To make my tests I just did copy and paste game engine code into form project. But this is the wrong idea, because I want my code to be written once. Here there are my doubts: is it possible to create a link between them, considering that:

  • Inside the form there is a PictureBox where I want to render my game for full (With lights, shadows, etc)
  • I want to write once the game engine so that if I modify something for example inside "Enemy" class, when editing both Level editor and Game itself would run the new code

I miss that logical link between my two entities, I need some hint to go on, something to think about in order to clear things up. I put some code, please give me some code.

\$\endgroup\$

closed as too broad by Josh Dec 5 '13 at 15:19

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ My question is really explicit: with the entities described above, I need just one logical idea to implement the connections between level editor and game engine, in order to use modularity. If this answer can't stay here, consider the possibility to move it to another section of this big forum. \$\endgroup\$ – Francesco Bonizzi Dec 5 '13 at 15:48
  • \$\begingroup\$ The problem is that there are many valid ways to handle that kind of communication and your question doesn't offer enough details about what you have tried or more specific requirements that would allow that huge array of options to be narrowed down. \$\endgroup\$ – Josh Dec 5 '13 at 18:58
  • \$\begingroup\$ "Simple and clean" are subjective terms. Please describe what you want in technical and non-subjective language. \$\endgroup\$ – MichaelHouse Dec 5 '13 at 19:02
2
\$\begingroup\$

I would recommend you to take a look at the model-view-controller concept.

The model would be your map with all the objects on it.

The view would be your graphic engine which visualizes the current state of the model.

The controllers are the parts of the code which implement the game logic or the map editing capabilities.

When your application is in "play mode", the game mechanic controllers would be active. They would cause the objects in your game to behave according to the game rules and manage the user input as commands for the game objects they control.

When your application is in "edit mode", the game mechanic controllers would be inactive. The input controller for the edit mode would interprete the user input as commands to change the model directly.

Your visualization in edit-mode and play-mode will likely also have some difference. The play-mode view might show some HUD information about the game-state (like how much life the player has left), while the edit-mode view shows some help-lines, additional menus and so on and will likely also visualize certain objects which are invisible in play-mode. But the general rendering-engine would likely be the same. When you work object-oriented I would write an abstract base-class GraphicEngine which implements the general rendering engine with two extending classes GameGraphicEngine and EditorGraphicEngine which implement the game-specific or editor-specific graphic engine features.

When you do this well, you might be able to implement seamless switching between play-mode and edit-mode at any time. This would provide you with a very powerful test-tool.

\$\endgroup\$

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