Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

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

This topic is 1472 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

Share this post

Link to post
Share on other sites

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.

Edited by iedoc

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!