Jump to content
  • Advertisement
Sign in to follow this  
FantasyVII

Manage Multiple towers, can I write a better code?

This topic is 2186 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.LoadContent(Content);

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

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

Share this post


Link to post
Share on other sites
Advertisement
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. :)

Share this post


Link to post
Share on other sites

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. smile.png


thanks :D Edited by FantasyVII

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!