Repos

MELHARFI-2D-Game-Engine.

Got to repo.

17. Childs

Child is the ability to add graphic object inside thatobject itself, as a picture inside a picture will be then superposed, and to illustrate this let's say that there's a Rectangle object as a Conteiner or parent and inside of it there is a bunch of items Child like some informations as the lifepoints and Shield.

The good thing to store objects inside another object is that you don't worry about moving them one by one as you have to move only the parent and the others will be moved depending on their relative position of the parent like a player moving and it's name above hime move same time.

Assuming this code.

                        
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using MELHARFI.Manager;
using MELHARFI.Manager.Gfx;

namespace _2dProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Manager manager = new Manager(this, "CORE_ENGINE");
            manager.Background = Color.Blue;

            Anim player = new Anim(150, manager);
            player.AddCell("narutoStep1.png", 0, 0, 0);
            player.AddCell("narutoStep2.png", 1, 0, 0);
            player.AddCell("narutoStep3.png", 2, 0, 0);
            player.AddCell("narutoStep4.png", 3, 0, 0);
            player.Ini(Manager.TypeGfx.Object, "naruto", false);
            player.Bmp.Point = new Point(100, 200);
            manager.ObjectLayer.Add(player);
            player.PointOfParent = true;            // necessary to not depend on relativ position of offsets
            
            // new instance of stats class
            Stats playerStats = new Stats();
            playerStats.Name = "Naruto";
            playerStats.LifePoints = 5;

            // Link Stats instance to the Tag of the player
            player.Tag = playerStats;

            Txt playerName = new Txt(playerStats.Name, new Point(0, -40), "playerName", Manager.TypeGfx.Object, true, new Font("Verdana", 10, FontStyle.Bold), Brushes.Pink, manager);
            player.Childs.Add(playerName);

            Txt playerLifePoints = new Txt(playerStats.LifePoints.ToString(), new Point(0, -25), "playerLifePoints", Manager.TypeGfx.Object, true, new Font("Verdana", 10, FontStyle.Bold), Brushes.Pink, manager);
            player.Childs.Add(playerLifePoints);

            KeyDown += Form1_KeyDown;
            KeyUp += Form1_KeyUp;
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            Manager manager = Manager.Managers.First(f => f.Name == "CORE_ENGINE");
            Anim player = manager.ObjectLayer.First(f => f.Name == "naruto") as Anim;

            player.Stop();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Manager manager = Manager.Managers.First(f => f.Name == "CORE_ENGINE");
            Anim player = manager.ObjectLayer.First(f => f.Name == "naruto") as Anim;

            if (player.AnimState == Anim.State.STOPED)
                player.Start();

            player.Point = new Point(player.Point.X + 5, player.Point.Y);
        }

        public class Stats
        {
            public string Name;
            public int LifePoints;
        }
    }
}

Services

App Developpement

Want me to dev an app for you ? dont hesitate to contact me.

Programming

Are you looking for a coder/teammate for your project ? Let's give it a try.

Job Invitation

Have a proposal for me ? we can discuss about it.

Donation maybe ?!

You want to buy me a coffe ? m.elharfi@gmail.com