# Will this Entity Code work? [closed]

How do you create 2D entities?

Here is something i tried to write up as a base for entities but is this usable and is entites like this?:

 public abstract class Entity {
int attackRange; // out of 100
int speed; // 1-slow(walk), 2 jog, 3 sprint
int defense; // out of 100
int attack; // out of 100
int health; // out of 100
int mana; // out of 100

public abstract void render();

public void entityBoundaries(){
Rectangle chest = new Rectangle(0,0,25,25);
Rectangle arms = new Rectangle(0,0,25,25);
Rectangle legs= new Rectangle(0,0,25,25);
}
}


This is my sample class (an archer) that I have made:

public class Archer extends Entity {
public Archer() {
attackRange = setAttackRange();
speed = 2;
attack = 75;
defense = 35;
health = 100;
mana = 50;
}

public int setAttackRange() {
return 5;
}

@Override
public void render() {

}

public void trackHealth() {
System.out.println(health);//for now just out put in console, later
}

public void entityBoundaries() {
Rectangle bow = new Rectangle(0,0,25,25);
Rectangle arrow = new Rectangle(0,0,25,25);
}
}


Can the class contain boundaries and how would you check them in the main game class?

Rectangle sampleObject = new Rectangle(0,0,100,100);


As an additional issue, I am not clear with how lists work with Java but i was thinking that adding the boundaries to it would be good and then i just cycle through them.

• What exactly do you mean by boundaries? Do you mean the model size? It would help if you clarified what you're saying and asked a specific question - 'Will this entity code work' is not a valid question as it's too broad. I'm going to downvote for now, but if the question is fixed then I'll rectify that. – Polar Mar 18 '13 at 19:35
• Exikle, you still haven't answered what the boundaries are. I'm going to presume you mean the 2D shape of the object, and edit the question to make it easier to answer. Beware that it may not be exactly what you're looking for - if so, just edit it again. – Polar Mar 18 '13 at 19:47
• Oops, misread it :( You have edited it, although I've submitted a further edit for peer review and I've changed my downvote to an upvote. – Polar Mar 18 '13 at 19:51
• I think code review questions are too localized for the site. Voting to close. – MichaelHouse Mar 18 '13 at 19:56
• @Byte56 It's less of a code review and more of a specific question now, I think. If it's possibly edited so that the question is only 'Is abstracting classes good?' and 'How do I test for rectangle intersection', then it (at least to me) sounds better. Then again, you might say the first question I proposed is a discussion question, and that 'How do I test for rectangle intersection' would be best served as another question (or indeed with just a link to a duplicate; I couldn't find any on here or SO). – Polar Mar 18 '13 at 20:12

First of all, I'd ask on Stack Overflow (or get a book) about how to use lists in Java. It really is a fundamental skill, and if you can't use lists, you might want to take a crash course in Java first. Then again, learning by experience is good - it just helps to know what you're doing.

About whether there is a more efficient way to write a player class, the answer is probably - but I can't think of a way off the top of my head, and to be honest your method is the method that I would use. Using abstract classes / inheritance really ends up paying off (providing that you avoid multiple inheritance, which is a PITA - see the diamond problem), so you've made a good decision there.

Next, you need to create a list with the boundaries of the object in, unless you want to detect between a head-shot and an arrow in the knee (see what I did there, with the archer... OK, it wasn't that funny) - if you use multiple, different hit-boxes, I would either iterate through them manually, or create a new class with an array, a hit(Object obj) method and a name so you could then make a list of these and find out which part had been hit by name. The only issue with the latter method is that it would probably add unnecessary overhead - I'd go with a seperate class, but that's my OCD.

Once you have the objects in a list to be hit, you iterate through that list, and with everyone do a collision test. These get slightly more interesting with other shapes (read - irregular polygons and circles), but collision tests with rectangles are surprisingly easy. In pseudocode:

Rectangle rectA;
Rectangle rectB;

if ((rectA.leftHandSideXValue <= rectB.rightHandSideXValue) && (rectA.leftHandSideXValue >= rectB.leftHandSideXValue))
{
// Handle intersection
}

// Repeat for the other sides - that is, rectB.left <= rectA.right
// ^ && rectB.left <= rectA.left, and do the same but with top and bottom boundaries.


That probably answers your questions - I've probably failed to address something, but just comment on the OP and I'll see it, and try to edit this post accordingly.

• "more efficient way to write a player class" gamadu.com/artemis/tutorial.html This can be more effecient in some cases. But maybe not for simple games. – Kikaimaru Mar 18 '13 at 20:15
• @Kikaimaru I'll be honest, that looks more like a 'Here's some code' tutorial than a 'Here's some code, with a useful explanation'. Also, I can see no evidence for how it is more efficient - if it will contribute to the question, then post it as an answer, with circumstantial proof that it's more efficient - otherwise, there's no basis for your claims. – Polar Mar 18 '13 at 20:17