My current approach - hashtable

Started by
-1 comments, last by flashinpan 18 years, 3 months ago
I would like some feedback on my use of a hashtable to store Image types and Images for retreival at runtime when it comes time to draw the image for the object. Is there a better way to do what I am doing?

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace robotsgame
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.PictureBox pictureBoxWorld;
		private System.ComponentModel.IContainer components;
		private World new_world = new World();

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.pictureBoxWorld = new System.Windows.Forms.PictureBox();
			this.SuspendLayout();
			// 
			// pictureBoxWorld
			// 
			this.pictureBoxWorld.Location = new System.Drawing.Point(24, 32);
			this.pictureBoxWorld.Name = "pictureBoxWorld";
			this.pictureBoxWorld.Size = new System.Drawing.Size(424, 384);
			this.pictureBoxWorld.TabIndex = 0;
			this.pictureBoxWorld.TabStop = false;
			this.pictureBoxWorld.Click += new System.EventHandler(this.pictureBoxWorld_Click);
			this.pictureBoxWorld.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxWorld_Paint);
			this.pictureBoxWorld.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBoxWorld_MouseDown);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 478);
			this.Controls.Add(this.pictureBoxWorld);
			this.Name = "MainForm";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.MainForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			
		}

		private void pictureBoxWorld_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			new_world.DisplayWorld(e.Graphics);
		}

		private void pictureBoxWorld_Click(object sender, System.EventArgs e)
		{
			Debug.WriteLine(e.GetType());
		}

		private void pictureBoxWorld_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			int x;
			int y;

			x = e.X	;
			Debug.WriteLine(e.X.ToString() + "," + e.Y.ToString());
		}
	}

	public class World
	{
		public const int MAX_ROWS = 10;
		public const int MAX_COLS = 10;

		public Hashtable images = new Hashtable();

		public Object[,] world_objects = new Object[MAX_ROWS,MAX_COLS];

		public World()
		{
			InitImagesHash();

			for(int row = 0;row < MAX_ROWS;row++)
			{
				for(int col = 0;col < MAX_COLS;col++)
				{
					world_objects[row,col] = new MasterRobot();
				}
			}

			world_objects[5,5] = new Wall();
		}

		public void InitImagesHash()
		{
			images.Add(ITypes.FriendlyMasterRobot,Image.FromFile(MasterImageList.FriendlyMasterRobot));
			images.Add(ITypes.EnemyMasterRobot,Image.FromFile(MasterImageList.EnemyMasterRobot));
			images.Add(ITypes.FriendlyMedicRobot,Image.FromFile(MasterImageList.FriendlyMedicRobot));
			images.Add(ITypes.EnemyMedicRobot,Image.FromFile(MasterImageList.EnemyMedicRobot));
			images.Add(ITypes.FriendlyEngineerRobot,Image.FromFile(MasterImageList.FriendlyEngineerRobot));
			images.Add(ITypes.EnemyEngineerRobot,Image.FromFile(MasterImageList.EnemyEngineerRobot));
			images.Add(ITypes.FriendlyDestroyerRobot,Image.FromFile(MasterImageList.FriendlyDestroyerRobot));
			images.Add(ITypes.EnemyDestroyerRobot,Image.FromFile(MasterImageList.EnemyDestroyerRobot));
			images.Add(ITypes.Wall,Image.FromFile(MasterImageList.Wall));
		}


		public void LoadGame()
		{

		}

		public void DisplayWorld(Graphics gr)
		{
			for(int row = 0;row < MAX_ROWS;row++)
			{
				for(int col = 0;col < MAX_COLS;col++)
				{
					gr.DrawImage(((Image)images[world_objects[row,col].icon]),col * 32, row* 32);					
				}
			}
		}		
	}

	public class Object
	{
		public ITypes icon;

		public Object()
		{
		}		
	}

	public class Robot : Object
	{
		public string owner;
		public bool is_enemy;
		
		public Robot()
		{
		}

	}

	public class MasterRobot : Robot
	{
		public MasterRobot()
		{
			if(!is_enemy)
			{
				icon = ITypes.FriendlyMasterRobot;
			}
			else
			{
				icon = ITypes.EnemyMasterRobot;
			}
		}
	}	

	public class Medic : Robot
	{
		public Medic()
		{
			if(!is_enemy)
			{
				icon = ITypes.FriendlyMedicRobot;
			}
			else
			{
				icon = ITypes.EnemyMedicRobot;
			}
		}	
	}

	public class Engineer : Robot
	{
		public Engineer()
		{
			if(!is_enemy)
			{
				icon = ITypes.FriendlyEngineerRobot;
			}
			else
			{
				icon = ITypes.EnemyEngineerRobot;
			}
		}	
	}

	public class Destroyer : Robot
	{
		public Destroyer()
		{
			if(!is_enemy)
			{
				icon = ITypes.FriendlyDestroyerRobot;
			}
			else
			{
				icon = ITypes.EnemyDestroyerRobot;
			}
		}	
	}

	public class Wall : Object
	{
		public Wall()
		{
			icon = ITypes.Wall;		
		}
	}

	struct MasterImageList
	{
		public const string FriendlyMasterRobot = "images\\mr_friendly.bmp";
		public const string EnemyMasterRobot = "images\\mr_enemy.bmp";
 
		public const string FriendlyEngineerRobot = "images\\eng_friendly.bmp";
		public const string EnemyEngineerRobot = "images\\eng_enemy.bmp";

		public const string FriendlyDestroyerRobot = "images\\dest_friendly.bmp";
		public const string EnemyDestroyerRobot = "images\\dest_enemy.bmp";

		public const string FriendlyMedicRobot = "images\\med_friendly.bmp";
		public const string EnemyMedicRobot = "images\\med_enemy.bmp";		

		public const string Wall = "images\\wall.bmp";		
	}


	public enum ITypes
	{
		FriendlyMasterRobot = 0,
		EnemyMasterRobot,
		FriendlyEngineerRobot,
		EnemyEngineerRobot,
		FriendlyDestroyerRobot,
		EnemyDestroyerRobot,
		FriendlyMedicRobot,
		EnemyMedicRobot,
		Wall
	};

}

This topic is closed to new replies.

Advertisement