Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: saywhatyousee (posted 2013-12-17 13:25:11, viewed 124 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334  
package learningAid.Entity;

import uwe.ac.uk.s2Vora.learningAid.TileMap.LoadGameMap.*;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import javax.imageio.ImageIO;


public class Player extends MapObject{
    
    private int health;
    private int maxHealth = 5;
    private int fire;
    private int maxFire = 2500;
    
    
    //Scratch
    private boolean scratching;

    
    
    //animations
    private ArrayList<BufferedImage[]> sprites;
    private final int[] numFrames = {2, 8, 1, 2, 4, 2, 5};
    
    //animation indexes
    private static final int IDLE = 0;
    private static final int WALKING = 1;
    private static final int SCRATCHING = 6;
   
    
    public int getHealth(){ return health;}
    public int getMaxHealth(){ return maxHealth;}
    public int getFire(){return fire;}
    public int getMaxFire(){return maxFire;}
    
    private double moveAmt = 40;
    
    
    
    private static Player playerInstance;
    
    public static Player getPlayerInstance(){
        
        if(playerInstance == null){
            playerInstance = new Player();
        }
        return playerInstance;
    }
    
    
    
    
    private Player(){
        
        width = 30;
        height = 30;
        cwidth = 20;
        cheight = 20;
        
        moveSpeed = 1;
        maxSpeed = 10;
        stopSpeed = 0.4;
        
        health = (maxHealth);
        fire = maxFire;

        
        //Load Sprites
        try{
            
            Properties prop = new Properties();
            InputStream propertiesInputStream = getClass().getResourceAsStream("/Resources/config.properties");
            prop.load(propertiesInputStream);
            
            BufferedImage spriteSheet = ImageIO.read(getClass().getResourceAsStream(prop.getProperty("playerSprites")));
            
            sprites = new ArrayList<BufferedImage[]>();
            
            for (int i = 0; i < 2; i++) {
                BufferedImage[] imageArray = new BufferedImage[numFrames[i]];
                
                for (int j = 0; j < numFrames[i]; j++) {
                    imageArray[j] = spriteSheet.getSubimage(j * width, i * height, width, height);
                }
                
                sprites.add(imageArray);
            }

        }catch(Exception e){
            e.printStackTrace();
        }
        
        animation = new Animation();
        
        //Start the player as idle
        currentAction = IDLE;
        animation.setFrames(sprites.get(IDLE));
        animation.setDelay(400);
        
        playerMapPosision = new double[2];
    }
    
    //Is this creating a memory Leak?
   private double[] playerMapPosision = null;
    
   public void resetPlayer(){
           isColliding = false;
           playerReachedEnd = true;
           playerMapPosision[0] = 100;
           playerMapPosision[1] = 100;
           commandsHopper = new  ArrayList<String>();
           moveCharacter(100, 100);
   }

   private boolean pause = false;
    
   
   public void setGamePause(boolean pauseGame){
        pause = (pauseGame == true) ? true : false;
    }
 
    private void moveCharacter(double x, double y){
        this.x = x;
        this.y = y;
    }
   
    private boolean isMoving = false;
    
    public boolean isPlayerMoving(){
        return isMoving;
    }
   
    private boolean playerReachedEnd = true;
    
    public boolean hasPlayerReachedTheEnd(){
        return playerReachedEnd;
    }
   
    public void setPlayerReachedEnd(boolean reachedTheEnd){
        this.playerReachedEnd = reachedTheEnd;
    }
    
    public void checkObstacleCollision(ArrayList<Obstacle> obs){
        
        for (int i = 0; i < obs.size(); i++) {
            
            Obstacle obstacle = obs.get(i);
            
            if(intersects(obstacle)){
                 System.out.println("hitting Obstacle in Player Class");
            }
            
        }
    }
    
   
    public void addCommand(String command){
        commandsHopper.add(command); //commands added at the top of arraylist
    }   
 
    private void setMovementCommands(String moveDirection, int moveAmount){

       for (int i = 0; i < moveAmount; i++) {

            if(moveDirection.equals("up")){
                
                 addCommand("up");

            } else if(moveDirection.equals("down")){
                addCommand("down");

            } else if(moveDirection.equals("left")){
                addCommand("left");
                
            } else if(moveDirection.equals("right")){
                addCommand("right");
            }
       }
       
     getNextTarget();  
       
    }
    
   
    public void moveUp(int tilesToMove){
        setMovementCommands("up", tilesToMove);
    }
    
    public void moveDown(int tilesToMove){
        setMovementCommands("down", tilesToMove);
    }
    
    public void moveRight(int tilesToMove){
        setMovementCommands("right", tilesToMove);
    }
   
    public void moveLeft(int tilesToMove){
        setMovementCommands("left", tilesToMove);
    }
    
    public boolean isColliding(){
        return isColliding;
    }
    

    private double[] determineTargetFromCommand(String command){

        double[] tempArray = new double[2];

        if(command.equals("up")){
            tempArray[0] = x;
            tempArray[1] = y - moveAmt;

        } else if(command.equals("down")){

            tempArray[0] = x;
            tempArray[1] = y + moveAmt;

        } else if(command.equals("left")){

           tempArray[0] = x - moveAmt;
           tempArray[1] = y;

        } else if(command.equals("right")){

            tempArray[0] = x  + moveAmt;
            tempArray[1] = y;
        }

        return tempArray;

    }

   
    ArrayList<String> commandsHopper = new  ArrayList<String>(); 

    
    public void getNextTarget(){
        if (!commandsHopper.isEmpty()){
            playerMapPosision = determineTargetFromCommand(commandsHopper.get(0));
        }else{
            playerReachedEnd = true;
        }
    }    
    
   public void updatePosition(){

       double targetX = playerMapPosision[0];
       double targetY = playerMapPosision[1];

       if(targetX > 0 && targetY > 0 && !commandsHopper.isEmpty() && pause == false){

           isMoving = true;
           
            if(y > targetY) {
                
                moveCharacter(x, y - moveSpeed);

            }else if(y < targetY) {

                 moveCharacter(x, y + moveSpeed);

            }else if(x > targetX) {

                 moveCharacter(x - moveSpeed, y);

            }else if(x < targetX) {
                 moveCharacter(x + moveSpeed, y); 
            }

            if(y <= targetY && y >= targetY && x >= targetX && x <= targetX && playerReachedEnd == false || isColliding == true){
                commandsHopper.remove(0);
                getNextTarget();
            }
       }else{
            isMoving = false;
       }

    }
    
    
    public void update(){
        
        //update position
        checkTileMapCollision();
        updatePosition();
        
        //set Animations
        if(scratching){
            if(currentAction != SCRATCHING){
                currentAction = SCRATCHING;
                animation.setFrames(sprites.get(SCRATCHING));
                animation.setDelay(50);
                width = 60;
            }
        }else if(left || right){
            if(currentAction != WALKING){
                currentAction = WALKING;
                animation.setFrames(sprites.get(WALKING));
                animation.setDelay(40);
                width = 30;
            }
        }else{
            if(currentAction != IDLE){
                currentAction = IDLE;
                animation.setFrames(sprites.get(IDLE));
                animation.setDelay(400);
                width = 30;
            }
        }
        
        
        animation.update();
    }
    
    
    
    public void draw(Graphics2D g){
        setMapPosition();
        
        //draw Player
        g.drawImage(animation.getImage(), 
                (int)(x + xmap - width / 2), 
                (int)(y + ymap - height / 2),
                null);
        
        
    }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (81 views)
2014-04-15 18:08:23

BurntPizza (73 views)
2014-04-15 03:46:01

UprightPath (84 views)
2014-04-14 17:39:50

UprightPath (67 views)
2014-04-14 17:35:47

Porlus (83 views)
2014-04-14 15:48:38

tom_mai78101 (107 views)
2014-04-10 04:04:31

BurntPizza (167 views)
2014-04-08 23:06:04

tom_mai78101 (263 views)
2014-04-05 13:34:39

trollwarrior1 (213 views)
2014-04-04 12:06:45

CJLetsGame (223 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!