Code:
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
/**
* foundation for hierarchal design
*
* @author Ethan R. Horner <[email protected]>
*/
public abstract class Node
{
/**
* name of the Node
*/
protected String name;
/**
* x coordinate for the Node
*/
protected int x;
/**
* y coordinate for the Node
*/
protected int y;
/**
* image for the Node
*/
protected Image image;
protected Node target;
/**
* used to redefine variables for the Node
*
* @param name
* @param x
* @param y
* @param image
* reference by pointer
*/
public Node(String name, int x, int y, Image image)
{
this.name = name;
this.x = x;
this.y = y;
this.image = image;
}
/**
* used to define how the image is drawn
*
* @param graphics
* used to reference by pointer
*/
public abstract void draw(Graphics2D graphics);
/**
* used for define how the Node moves
* if at all
*/
public abstract void move(ActionEvent event);
/**
* used to define what happens on collision
*
* @param target
* used to define collision based on the type of Node
*/
public abstract void handleCollision(Node target);
/**
* calculates if the Node has collided with another object
*
* @param otherX
* @param otherY
* used to input the coordinates for other Node
*/
public void collision(int otherX, int otherY)
{
int distanceFormula = (int) Math.sqrt(Math.pow((otherX - x), 2) + Math.pow((otherY - y), 2));
if (distanceFormula == 0)
{
handleCollision(target);
}
}
/**
* @return
* the name of the Node
*/
public String getName()
{
return name;
}
/**
* sets the Node's name
*
* @param name
* used to input the Node's name
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return
* a coordinate set for the Node
*/
public int[] getCoordiantes()
{
return new int[]{x, y};
}
/**
* sets the coordinates for the Node
*
* @param x
* @param y
* used to input coordinates
*/
public void setCoordinate(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* @return
* the Node's image
*/
public Image getImage()
{
return image;
}
/**
* sets the image of the Node
*
* @param image
* used to input the image for the Node
*/
public void setImage(Image image)
{
this.image = image;
}
}
Mob for representation of all character like entities:
Code:
import java.awt.Image;
import org.newpoke.node.Node;
/**
* vague representation for any character like Node
*
* @author Ethan R. Horner <[email protected]>
*/
public abstract class Mob extends Node
{
/**
* mob characters level
*/
protected double level;
/**
* mob characters health
*/
protected double health;
/**
* the amount by which the mob's x coordinate is incrementing
* used to determine the rate at which the player is moving
*/
protected int xChange;
/**
* used to plug and play information for information heigh in the hierarchal chain
*
* @param name
* @param x
* @param y
* @param image
* @param level
* @param health
* used to input value from a child class
*/
protected Mob(String name, int x, int y, Image image, int level, int health)
{
super(name, x, y, image);
this.level = level;
this.health = health;
xChange = 0;
}
/**
* method used to define how a mob character attacks
*/
public abstract void attack();
/**
* used to define the rate at which a mob character heals
* leaves door wide open for different affects on the way the mob heals
* @return
* the final formula for health regeneration
*/
public abstract double healthRegenerationFormula();
/**
* @return
* the level of the mob character
*/
public int getLevel()
{
return (int)level;
}
/**
* @return
* the health of the mob character
*/
public int getHealth()
{
return (int)health;
}
/**
* used to define or redefine the health of a mob character
*
* @param health
* used to input an integer or formula for the mobs health
*/
public void setHealth(int health)
{
this.health = health;
}
/**
* @return
* the amount by which the mob character's x coordinate is incrementing
*/
public int getXChange()
{
return xChange;
}
/**
* used to define the amount by which to increment the x coordinate of the mob character
*
* @param xChange
* used to input a new value for the xChange variable
*/
public void setXChange(int xChange)
{
this.xChange = xChange;
}
}
Mob class applied:
Code:
iimport javax.swing.ImageIcon;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import org.newpoke.core.Constants;
import org.newpoke.node.Node;
/**
*
* @author Ethan R. Horner <[email protected]>
*/
public class Player extends Mob
{
public Player()
{
super("Bob", 600, 10, new ImageIcon(Constants.CHARACTER_RIGHT_STAND).getImage(), 1, 150);
}
public Player(String name, int x, int y, int level, int health)
{
super(name, x, y, new ImageIcon(Constants.CHARACTER_RIGHT_STAND).getImage(), level, health);
}
@Override
public void attack()
{
}
@Override
public void handleCollision(Node target)
{
this.target = target;
if (target instanceof Player)
{
xChange = 0;
}
}
@Override
public void move(ActionEvent event)
{
x = x + xChange;
}
@Override
public void draw(Graphics2D graphics)
{
graphics.drawImage(image, x, y, null);
}
@Override
public double healthRegenerationFormula()
{
return (health = (health/8) * 1.3);
}
}