Manage Multiple towers, can I write a better code?

Started by
1 comment, last by FantasyVII 11 years, 9 months ago
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);
}
}
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. :)

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development


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

This topic is closed to new replies.

Advertisement