1
Programming / I Feel Like Sharing some Code - My Unfinished AOE Class
« on: July 29, 2011, 07:25:47 PM »Code: [Select]
package eracia;
/**
*
* @author Jeff Desrosiers
*/
//imports
import java.awt.Dimension;
//This class calculates every square given within an Area of Effect
public class AreaOfEffect
{
//------------------------InClass Declarations------------------------------
public enum AOEType {CIRCLE, BEAM, CONE} //The enumeration of AOE
public AOEType Type; //The Type of Area Of Effect
private int intSize; //The Size of Area of Effect
private Dimension dimCenter; //The Center of the AOE
public AreaOfEffect(AOEType Type, int intSize, Dimension dimCenter)
{
this.Type = Type;
this.intSize = intSize;
this.dimCenter = dimCenter;
}//end of Constructor
//----------------------------Public Methods--------------------------------
//This method finds out if the current tile in question is in the Area of
//Effect's range
//ix: The X Axis value
//iy: The Y Axis value
public boolean isInAOE(int ix, int iy)
{
//--------------------InMethod Declarations-----------------------------
int intLine = 1; //What line to calculate for X axis
int ix2 = 0; //The length of the line for ix
//Checks to see if it is in the Y axis of the AOE if true go on if false
//return false
if(iy >= this.dimCenter.height - this.intSize &&
iy <= this.dimCenter.height + this.intSize)
{
//It was true so find out which line iy is in the AOE
for(int iy2 = iy; iy2 != this.dimCenter.height -
this.intSize; iy2--)
{
intLine++; //increment intLine
}//end of for(int iy2 = iy; iy2 != this.dimCenter.height -
//this.intSize; iy2--)
//Found the right line so now find the size of the line for X Axis
ix2 = this.findXNum(intLine);
//Check to see if it reaches the X Axis limit to the line in
//question
if(ix >= this.dimCenter.width - (ix2 / 2) &&
ix <= this.dimCenter.width + (ix2 / 2))
{
return true; //Both x and y are in the AOE
}//end of if(ix >= this.dimCenter.width - (ix2 / 2) &&
// ix <= this.dimCenter.width + (ix2 / 2))
}//end of if(iy >= this.dimCenter.height - this.intSize &&
// iy <= this.dimCenter.height + this.intSize)
return false; //The Tile is not in the AOE
}//end of isInAOE(int ix, int iy)
//Returns the number of Tiles from the Tile in Question to the Center of the
//AOE
//ix: The X Axis value
//iy: The Y Axis value
public int distanceFromCenter(int ix, int iy)
{
int intDistance = 0; //The Distance between Center to Tile
int iyd = Math.abs(this.dimCenter.height - iy); //The change in Y
int ixd = Math.abs(this.dimCenter.width - ix); //The change in X
//Check to see if iyd = 0 if so The Distance is ixd
if(iyd == 0)
{
intDistance = ixd;
}//end of if(iyd == 0)
//Check of ixd = 0 if so the distance is iyd
else if(ixd == 0)
{
intDistance = iyd;
}//end of else if(ixd == 0)
//Else it is the larger of the two
else
{
if(iyd > ixd)
{
intDistance = iyd;
}//end of if(iyd > ixd)
else
{
intDistance = ixd;
}//end of else
}//end of else
//If both are 0 set intDistance to 1 so there will be no division by 0
if(intDistance == 0)
{
intDistance = 1;
}//end of if(intDistance == 0)
return intDistance; //Done
}//end of distanceFromCenter(int ix, int iy)
//This method updates the Center of AOE
public void updatePosition(Dimension dimPos)
{
this.dimCenter = dimPos;
}//end of updatePosition(Dimension dimPos)
//-------------------------Private Methods----------------------------------
//This method finds the X value in a given line of the AOE
//iy: is the Y Axis of the AOE not the map
private int findXNum(int iy)
{
//------------------InMethod Declarations-------------------------------
int ix = 0; //The x value
//These variables calculate the middle of the AOE
int imStart = this.intSize - (this.intSize / 2) + 1;
int imEnd = imStart + this.intSize - 1;
//If iy is in the middle of the AOE
if(iy >= imStart && iy <= imEnd)
{
ix = (this.intSize * 2) + 1; //ix = 2 * The Size of AOE + 1
}//end of if(iy >= imStart && iy <= imEnd)
//If iy is before the middle of the AOE
else if(iy < imStart)
{
//Set ix to the The size of the middle and for each line ii travels
//minus 2 to ix
ix = (this.intSize * 2) + 1;
for(int ii = iy; ii < imStart; ii++)
{
ix -= 2;
}//end of for(int ii = iy; ii < imStart; ii++)
}//end of else if(iy < imStart)
//If iy is after the middleof the AOE
else
{
//Set ix to the size of the AOE and add two for every line ii
//travels through
ix = this.intSize;
for(int ii = (this.intSize * 2) + 1; ii > iy; ii--)
{
ix += 2;
}//end of for(int ii = (this.intSize * 2) + 1; ii > iy; ii--)
}//end of else
return ix;
}//end of findXNum(int iy)
}//end of AreaOfEffect
Post any constructive criticism that you can. I love to learn new ideas.
I also want to know how my documentation / coding style is. Any tips will be welcomed. Thankyou in advance
Taffer
eraciarl.blogspot.com