Jump to content

  • Log In with Google      Sign In   
  • Create Account

Manage Multiple towers, can I write a better code?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 FantasyVII   Members   -  Reputation: 299

Like
0Likes
Like

Posted 20 July 2012 - 04:18 AM

Hello everyone,

I'm working on a tower defence game. I have tower class which has all the things a tower needs and ManageTower class that makes muliple towers when the mouse is click. is there a way to make my code better, more organized? cuz i think there is but don't know what.

class Tower
	{
		Texture2D TowerTexture;
		Vector2 Position;
		int Damage, FireRate, Cost, RefundAmount, CurrentTarget;
		float Center, Radius, ElapsedTime, TimeIn_ms;
		String TexturePath;

		public Tower(String TexturePath, int Damage, float Range, int FireRate, int Cost, int RefundAmount)
		{
			this.TexturePath	= TexturePath;
		 this.Damage		 = Damage;
		 this.Radius		 = Range;
		 this.FireRate	   = FireRate;
		 this.Cost		   = Cost;
		 this.RefundAmount   = RefundAmount;
		 CurrentTarget  = 0;
		}

		public void LoadContent(ContentManager Content)
		{
			TowerTexture = Content.Load<Texture2D>(TexturePath);
		}

		public void Update(Vector2 Position)
		{
			this.Position.X = (int)Position.X / 32;
			this.Position.Y = (int)Position.Y / 32;
		}

		public void Draw(SpriteBatch spriteBatch)
		{
			spriteBatch.Draw(TowerTexture, Position*32, Color.White);
		}
	}

class TowerManager
    {
	    MouseState lastMouseState, currentMouseState;

	    List<Tower> Towers = new List<Tower>();

	    void AddTower()
	    {
		    Towers.Add(new Tower("Tiles/Tower", 100, 1000, 1000, 10, 10));
	    }

	    public void Update(Vector2 MousePos, ContentManager Content)
	    {
		    lastMouseState = currentMouseState;
		    currentMouseState = Mouse.GetState();

		    if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
		    {
			    AddTower();

			    for (int i = 0; i < Towers.Count; i++)
				    Towers[i].LoadContent(Content);

			    for (int i = Towers.Count-1; i < Towers.Count; i++)
				    Towers[i].Update(MousePos);
		    }
	    }

	    public void Draw(SpriteBatch spriteBatch)
	    {
		    if (Towers.Count >= 1)
			    for (int i = 0; i < Towers.Count; i++)
				    Towers[i].Draw(spriteBatch);
	    }
    }

Edited by FantasyVII, 20 July 2012 - 03:38 PM.


Sponsor:

#2 Machaira   Moderators   -  Reputation: 1028

Like
1Likes
Like

Posted 20 July 2012 - 05:35 PM

One thing - you shouldn't be passing the ContentManager every Update. Just keep a reference to it.

Other than that, if it ain't broke don't fix it. :)
Microsoft XNA MVP | Check out my blog for random ramblings on XNA game development

#3 FantasyVII   Members   -  Reputation: 299

Like
0Likes
Like

Posted 21 July 2012 - 03:25 AM

One thing - you shouldn't be passing the ContentManager every Update. Just keep a reference to it.

Other than that, if it ain't broke don't fix it. Posted Image


thanks :D

Edited by FantasyVII, 21 July 2012 - 03:26 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS