Sign in to follow this  

Manage Multiple towers, can I write a better code?

This topic is 1972 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.

[CODE]
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);
}
}
[/CODE]

[CODE]
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);
}
}
[/CODE] Edited by FantasyVII

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. :)

Share this post


Link to post
Share on other sites
[quote name='Machaira' timestamp='1342827355' post='4961514']
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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]

thanks :D Edited by FantasyVII

Share this post


Link to post
Share on other sites

This topic is 1972 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this