Repos

MELHARFI-2D-Game-Engine.

Got to repo.

16. Tag

The Tag object is not something new in programming and i think you already know the purpose, if it's so just go next chapter, otherwise I’ll explain for someone who ignore that context.

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, "manager");
            manager.Background = Color.Blue;

            Bmp supermario = new Bmp(@"supermario.png", new Point(100, 0), "supermario", Manager.TypeGfx.Object, true, manager);
            manager.ObjectLayer.Add(supermario);

            // new instance of stats class
            Stats supermarioStats = new Stats();
            supermarioStats.LifePoint = 5;
            supermarioStats.Shield = 10;

        }

        public class Stats
        {
            public int LifePoint;
            public int Shield;
        }
    }
}
                          
                        
                      

And now let's say that you want to show the player's stats on the board but for that you need to attach thoses stats to the player somehow, and gess what ?!! this is the job of the Tag object.

Tag property is an object that you can associate to any object, including instance of any class that hold many information inside itself and we are going to use a Stats instace to attache it to the Tag object of the "supermario" object.

                        
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, "manager");
            manager.Background = Color.Blue;

            Bmp supermario = new Bmp(@"supermario.png", new Point(100, 100), "supermario", Manager.TypeGfx.Object, true, manager);
            manager.ObjectLayer.Add(supermario);

            // new instance of stats class
            Stats supermarioStats = new Stats();
            supermarioStats.LifePoints = 5;
            supermarioStats.Shield = 10;

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

            ShowStats();
        }

        private void ShowStats()
        {
            Manager manager = Manager.Managers.First(f => f.Name == "manager");
            Stats playerStats = manager.ObjectLayer.First(f => f.Name == "supermario").Tag as Stats;

            string lifePoints = "Life points : " + playerStats.LifePoints;
            string shield = "Shield : " + playerStats.Shield;

            Txt lifePointsTxt = new Txt(lifePoints, new Point(5, 5), "lifePointsTxt", Manager.TypeGfx.Top, true, new Font("Verdana", 10, FontStyle.Bold), Brushes.Pink, manager);
            manager.TopLayer.Add(lifePointsTxt);

            Txt lifeShieldsTxt = new Txt(shield, new Point(5, 25), "shieldTxt", Manager.TypeGfx.Top, true, new Font("Verdana", 10, FontStyle.Bold), Brushes.Pink, manager);
            manager.TopLayer.Add(lifeShieldsTxt);
        }

        public class Stats
        {
            public int LifePoints;
            public int Shield;
        }
    }
}
                          
                        
                      

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