Advertisement
Guest User

Player.java

a guest
Oct 28th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. package genericzombieshooter.actors;
  2.  
  3. import java.awt.Shape;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.geom.Rectangle2D;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import javax.imageio.ImageIO;
  10.  
  11. /**
  12.  *
  13.  * @author packetpirate
  14.  */
  15. public class Player extends Rectangle2D.Double {
  16.     // Constant variables.
  17.     private static final double MOVE_SPEED = 2; // How many pixels per tick the player moves.
  18.     private static final int WEAPON_CD = 12;
  19.     // Member variables.
  20.     private AffineTransform af;
  21.     private BufferedImage img;
  22.    
  23.     private int health;
  24.     private int cooldown; // How many ticks before gun can be fired again.
  25.    
  26.     public Player() {
  27.         super();
  28.         af = new AffineTransform();
  29.         LoadImage();
  30.        
  31.         health = 100;
  32.         cooldown = WEAPON_CD;
  33.     }
  34.    
  35.     public Player(double x_, double y_, double w_, double h_) {
  36.         super(x_, y_, w_, h_);
  37.         af = new AffineTransform();
  38.         LoadImage();
  39.        
  40.         health = 100;
  41.         cooldown = WEAPON_CD;
  42.     }
  43.    
  44.     public Player(Rectangle2D.Double rect_) {
  45.         super(rect_.x, rect_.y, rect_.width, rect_.height);
  46.         af = new AffineTransform();
  47.         LoadImage();
  48.        
  49.         health = 100;
  50.         cooldown = WEAPON_CD;
  51.     }
  52.    
  53.     private void LoadImage() {
  54.         try {
  55.             img = ImageIO.read(new File("images/GZS_Player.png"));
  56.         } catch(IOException io) {
  57.             System.out.println("File not found!");
  58.         }
  59.     }
  60.    
  61.     // Getter/Setter methods.
  62.     public double getMoveSpeed() { return MOVE_SPEED; }
  63.     public AffineTransform getTransform() { return af; }
  64.     public BufferedImage getImage() { return img; }
  65.     public Shape createTransformedShape() { return af.createTransformedShape(this); }
  66.    
  67.     public int getHealth() { return health; }
  68.     public int getCooldown() { return cooldown; }
  69.    
  70.     public void decCooldown() { if(cooldown > 0) cooldown--; }
  71.     public boolean isOnCooldown() {
  72.         if(cooldown == 0) {
  73.             cooldown = WEAPON_CD;
  74.             return false;
  75.         } else return true;
  76.     }
  77.    
  78.     // Shape manipulation.
  79.     public void rotate(double theta_) { af.setToRotation(theta_, getCenterX(), getCenterY()); }
  80.    
  81.     // Player manipulation.
  82.     /*
  83.      * @param direction The direction to move in.
  84.      */
  85.     public void move(int direction) {
  86.         switch(direction) {
  87.             case 0: // Up
  88.                 y -= MOVE_SPEED;
  89.                 break;
  90.             case 1: // Right
  91.                 x += MOVE_SPEED;
  92.                 break;
  93.             case 2: // Down
  94.                 y += MOVE_SPEED;
  95.                 break;
  96.             case 3: // Left
  97.                 x -= MOVE_SPEED;
  98.                 break;
  99.             default: // No direction.
  100.                 break;
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement