Is this a good structure of instance variable for my Pig class

Started by
3 comments, last by rip-off 9 years, 7 months ago
I have a 2D game that have 9 pigs moving left and right in a map. May you please take a look to see if the code design is sufficient judging from the private instance declared in the Pig class?
Code is in Java.

 
public class Pig extends Sprite implements Collidable{
 
// animation object that renders the action state of the pig
// this allows flexible for rendering certain animations for different state.
// object oriented composition
private PigMoveLeft pigMoveLeft;
private PigMoveRight pigMoveRight;
private PigIdleLeft pigIdleLeft;
private PigIdleRight pigIdleRight;
private PigHitLeft pigHitLeft;
private PigHitRight pigHitRight;
private PigDeadLeft pigDeadLeft;
private PigDeadRight pigDeadRight;
 
// direction enum that specifies left, right 
private Direction direction;
 
// actionstate enum that specifies hit, dead
private ActionState state;
 
// how fast is the pig moving
private int speed = 1;
 
// a timer counter that specifies what does the pig do
private int counter;
 
// bounding box of the pig. colliding with pig causes main character to lose health
private Rectangle rectangle;
 
// maximum health of the pig
private int maxLife = 2;
 
// life is the current life of the pig
private int life = maxLife;
 
// is the pig dead?
private boolean isDead;
 
// is the pig alive?
private boolean alive;
 
// pig gets hit. hit causes it to stop. pig waits for a bit and starts to move again
private int hitCount;
 
// experience points for defeating the pig
private int exp = 3;
 
// is the pig killed?
private boolean killed;
 
// loot from the pig
private Gold gold;
private Leather leather;
 
// gold amount for killing the pig
private int goldValue = 5;
 
Advertisement

No.

A pig has a sprite, not is a sprite.

Most likely, a pig is a character/actor, and a character/actor has most of the private members you want to question, and may also have the sprite.

A pig adds on top of a character/actor things specific to pigs, which will mostly likely be AI routines, not data. Every character will have “exp” and “isDead”. The only changes are what these values have when the pig/cow/shark gets created, which would be data stored in a separate location and fed to the class during its creation.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Looking at the members of your class, they look like they could be put into an "Actor" interface (whatever you want to call it), which the pig class could implement, along with any other entities. The pig would also contain a member of type Sprite, which is what Spiro was saying.

I think you might want it a little more generic. You have a sprite class, so pig could just be one instance of it. It would have a moveLeft, moveRight, speed, etc. That way you could just put all your sprites in one container and do collisions easily using a for loop. Collision reactions might be unique, so then you might want to use an extended class and do an override. You want as much standard behavior in your base class as possible. For instance, if it falls downward on a static object, it stops falling. You probably also want a static sprite and a movable sprite class, for testing purposes. You only need to test a moving against a static or another moving.

You have two sets of sprites, one set that faces left and another that faces right. You could have a two element array of objects, indexed by "direction" which represent just the set of sprite states. It might even be possible to represent the set of sprite states with another array indexed by an enum of the Sprites current display state - which could be derived from the sprites ActionState + whether they have moved recently.

I'm confused by the following set of variables: "life", "isDead", "alive", "killed". Most or all of these could be collapsed together.

Does "maxLife", "goldValue" or "exp" vary per Pig instance? If not consider making them class constants rather than instance variables.

The "counter" variable doesn't have much meaning, and the comment isn't helping much. I'd worry that the mention of "time" in relation to a "counter" means that you don't have frame rate independent logic here.

I'd prefer if "exp" wasn't contracted - something like "experiencePoints". Similarly, "state" doesn't convey a lot of meaning in isolation. One of your booleans is called "isDead", the others don't have an "is" prefix - strive for consistency.

This topic is closed to new replies.

Advertisement