konflikt

RPG Game - Entity.java - 10/31/2011

Oct 7th, 2011
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.62 KB | None | 0 0
  1. import java.awt.Image;
  2. import java.awt.Rectangle;
  3. import javax.swing.ImageIcon;
  4.  
  5. public class Entity {
  6.  
  7.     //-----------------------------------------------------------------
  8.     // Variables
  9.     //-----------------------------------------------------------------
  10.     protected GameCanvas gc;
  11.    
  12.     protected Sprite sprite;
  13.     protected Image[] image;
  14.     protected Animation a;
  15.     protected boolean firstTime;
  16.    
  17.     protected Rectangle rect = new Rectangle();
  18.     protected Rectangle drawRect = new Rectangle();
  19.    
  20.     protected int health;
  21.     protected String imageName;
  22.     protected String entityName;
  23.    
  24.     protected int initX;
  25.     protected int initY;
  26.    
  27.     protected boolean moveUp = false;
  28.     protected boolean moveDown = false;
  29.     protected boolean moveLeft = false;
  30.     protected boolean moveRight = false;
  31.     protected int speed = 1;
  32.    
  33.     public boolean talking = false;
  34.     protected int walking = 0; // 1 = UP, 2 = DOWN, 3 = LEFT, 4 = RIGHT, 0 = FALSE
  35.     protected int facing = 2; // 1 = UP, 2 = DOWN, 3 = LEFT, 4 = RIGHT
  36.     protected int moveTo;
  37.    
  38.     protected NPC talkableNPC;
  39.     public boolean movableUp = true;
  40.     public boolean movableDown = true;
  41.     public boolean movableLeft = true;
  42.     public boolean movableRight = true;
  43.    
  44.     //-----------------------------------------------------------------
  45.     // Update Methods
  46.     //-----------------------------------------------------------------
  47.     public void update(long timePassed){
  48.         sprite.update(timePassed);
  49.         rect.setBounds(getDrawX(), getDrawY(), getWidth(), getHeight());
  50.         drawRect.setBounds(getDrawX(), getDrawY(), getWidth(), getHeight());
  51.        
  52.         if(!gc.debugNoClip){
  53.             checkCollision();
  54.         }
  55.         else{
  56.             movableUp = true;
  57.             movableDown = true;
  58.             movableLeft = true;
  59.             movableRight = true;
  60.         }
  61.        
  62.         usePortal();
  63.         move();
  64.     }
  65.    
  66.     public void loadImages(){
  67.         image = new Image[8];
  68.         for(byte i = 0; i < image.length; i++){
  69.             image[i] = new ImageIcon("images\\" + imageName + (i) + ".gif").getImage();
  70.         }
  71.         a = new Animation();
  72.         sprite = new Sprite(a);
  73.        
  74.         a.addScene(image[1], 150);
  75.        
  76.         sprite.setCoordX(initX); //Sets initial xPos
  77.         sprite.setCoordY(initY); //Sets initial yPos
  78.     }
  79.    
  80.     //-----------------------------------------------------------------
  81.     // Collision Methods
  82.     //-----------------------------------------------------------------
  83.     public boolean crashTestNPCUp() {
  84.         for (int i = 0; i < gc.currentMapNPC.length; i++) {
  85.             Entity him = gc.currentMapNPC[i];
  86.             if(getCoordY() == him.getCoordY() + 1 && getCoordX() == him.getCoordX()){
  87.                 if(facing == 1){
  88.                     talkableNPC = (NPC) him;
  89.                 }
  90.                 return true;
  91.             }
  92.         }
  93.         return false;
  94.     }
  95.    
  96.     public boolean crashTestTileUp(){
  97.         for(int i = 0; i < gc.mapTileLayer1.length; i++){
  98.             StaticTile tile = gc.mapTileLayer1[i];
  99.             if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
  100.                 if(tile.getImpassible() == true){
  101.                     if(getCoordY() == tile.getCoordY() + 1 && getCoordX() == tile.getCoordX()){
  102.                         return true;
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.        
  108.         for(int i = 0; i < gc.mapTileLayer2.length; i++){
  109.             StaticTile tile = gc.mapTileLayer2[i];
  110.             if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
  111.                 if(tile.getImpassible() == true){
  112.                     if(getCoordY() == tile.getCoordY() + 1 && getCoordX() == tile.getCoordX()){
  113.                         return true;
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.        
  119.         return false;
  120.     }
  121.    
  122.     public boolean crashTestNPCDown(){
  123.         for (int i = 0; i < gc.currentMapNPC.length; i++) {
  124.             Entity him = gc.currentMapNPC[i];
  125.             if(getCoordY() + 1 == him.getCoordY() && getCoordX() == him.getCoordX()){
  126.                 if(facing == 2){
  127.                     talkableNPC = (NPC) him;
  128.                 }
  129.                 return true;
  130.             }
  131.         }      
  132.         return false;
  133.     }
  134.    
  135.     public boolean crashTestTileDown(){
  136.         for(int i = 0; i < gc.mapTileLayer1.length; i++){
  137.             StaticTile tile = gc.mapTileLayer1[i];
  138.             if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
  139.                 if(tile.getImpassible() == true){
  140.                     if(getCoordY() + 1 == tile.getCoordY() && getCoordX() == tile.getCoordX()){
  141.                         return true;
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.        
  147.         for(int i = 0; i < gc.mapTileLayer2.length; i++){
  148.             StaticTile tile = gc.mapTileLayer2[i];
  149.             if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
  150.                 if(tile.getImpassible() == true){
  151.                     if(getCoordY() + 1 == tile.getCoordY() && getCoordX() == tile.getCoordX()){
  152.                         return true;
  153.                     }
  154.                 }
  155.             }
  156.         }
  157.        
  158.         return false;
  159.     }
  160.    
  161.     public boolean crashTestNPCLeft(){     
  162.         for (int i = 0; i < gc.currentMapNPC.length; i++) {
  163.             Entity him = gc.currentMapNPC[i];
  164.             if(getCoordX() == him.getCoordX() + 1 && getCoordY() == him.getCoordY()){
  165.                 if(facing == 3){
  166.                     talkableNPC = (NPC) him;
  167.                 }
  168.                 return true;
  169.             }
  170.         }
  171.         return false;
  172.     }
  173.    
  174.     public boolean crashTestTileLeft(){
  175.         for(int i = 0; i < gc.mapTileLayer1.length; i++){
  176.             StaticTile tile = gc.mapTileLayer1[i];
  177.             if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
  178.                 if(tile.getImpassible() == true){
  179.                     if(getCoordX() == tile.getCoordX() + 1 && getCoordY() == tile.getCoordY()){
  180.                         return true;
  181.                     }
  182.                 }
  183.             }
  184.         }
  185.        
  186.         for(int i = 0; i < gc.mapTileLayer2.length; i++){
  187.             StaticTile tile = gc.mapTileLayer2[i];
  188.             if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
  189.                 if(tile.getImpassible() == true){
  190.                     if(getCoordX() == tile.getCoordX() + 1 && getCoordY() == tile.getCoordY()){
  191.                         return true;
  192.                     }
  193.                 }
  194.             }
  195.         }
  196.        
  197.         return false;
  198.     }
  199.    
  200.     public boolean crashTestNPCRight(){    
  201.         for (int i = 0; i < gc.currentMapNPC.length; i++) {
  202.             Entity him = gc.currentMapNPC[i];
  203.             if(getCoordX() + 1 == him.getCoordX() && getCoordY() == him.getCoordY()){
  204.                 if(facing == 4){
  205.                     talkableNPC = (NPC) him;
  206.                 }
  207.                 return true;
  208.             }
  209.         }
  210.         return false;
  211.     }
  212.    
  213.     public boolean crashTestTileRight(){
  214.         for(int i = 0; i < gc.mapTileLayer1.length; i++){
  215.             StaticTile tile = gc.mapTileLayer1[i];
  216.             if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
  217.                 if(tile.getImpassible() == true){
  218.                     if(getCoordX() + 1 == tile.getCoordX() && getCoordY() == tile.getCoordY()){
  219.                         return true;
  220.                     }
  221.                 }
  222.             }
  223.         }
  224.        
  225.         for(int i = 0; i < gc.mapTileLayer2.length; i++){
  226.             StaticTile tile = gc.mapTileLayer2[i];
  227.             if(gc.checkCollisionBuffer(tile.getCoordX(), tile.getCoordY()) == true){
  228.                 if(tile.getImpassible() == true){
  229.                     if(getCoordX() + 1 == tile.getCoordX() && getCoordY() == tile.getCoordY()){
  230.                         return true;
  231.                     }
  232.                 }
  233.             }
  234.         }
  235.        
  236.         return false;
  237.     }
  238.    
  239.     public void checkCollision(){
  240.         if(crashTestNPCUp() == true || crashTestTileUp() == true){
  241.             movableUp = false;
  242.         }
  243.         else{
  244.             movableUp = true;
  245.         }
  246.        
  247.         if(crashTestNPCDown() == true || crashTestTileDown() == true){
  248.             movableDown = false;
  249.         }
  250.         else{
  251.             movableDown = true;
  252.         }
  253.        
  254.         if(crashTestNPCLeft() == true || crashTestTileLeft() == true){
  255.             movableLeft = false;
  256.         }
  257.         else{
  258.             movableLeft = true;
  259.         }
  260.        
  261.         if(crashTestNPCRight() == true || crashTestTileRight() == true){
  262.             movableRight = false;
  263.         }
  264.         else{
  265.             movableRight = true;
  266.         }
  267.    
  268.     }
  269.    
  270.     //-----------------------------------------------------------------
  271.     // Movement Methods
  272.     //-----------------------------------------------------------------
  273.     public void move(){
  274.         if(moveUp == true && walking == 0 && talking == false){
  275.             facing = 1;
  276.             if(movableUp == true){
  277.                 moveTo = (int) Math.round(getCoordY() - 1);
  278.                 walking = 1;
  279.                 sprite.setVelocityY(-speed);
  280.             }
  281.         }
  282.         else{
  283.             if(walking == 1 && moveTo >= getCoordY()){
  284.                 sprite.setVelocityY(0);
  285.                 walking = 0;
  286.             }
  287.         }
  288.        
  289.         if(moveDown == true && walking == 0 && talking == false){
  290.             facing = 2;
  291.             if(movableDown == true){
  292.                 moveTo = (int) Math.round(getCoordY() + 1);
  293.                 walking = 2;
  294.                 sprite.setVelocityY(speed);
  295.             }
  296.         }
  297.         else{
  298.             if(walking == 2 && moveTo <= getCoordY()){
  299.                 sprite.setVelocityY(0);
  300.                 walking = 0;
  301.             }
  302.         }
  303.        
  304.         if(moveLeft == true && walking == 0 && talking == false){
  305.             facing = 3;
  306.             if(movableLeft == true){
  307.                 moveTo = (int) Math.round(getCoordX() - 1);
  308.                 walking = 3;
  309.                 sprite.setVelocityX(-speed);
  310.             }
  311.         }
  312.         else{
  313.             if(walking == 3 && moveTo >= getCoordX()){
  314.                 sprite.setVelocityX(0);
  315.                 walking = 0;
  316.             }
  317.         }
  318.        
  319.         if(moveRight == true && walking == 0 && talking == false){
  320.             facing = 4;
  321.             if(movableRight == true){
  322.                 moveTo = (int) Math.round(getCoordX() + 1);
  323.                 walking = 4;
  324.                 sprite.setVelocityX(speed);
  325.             }
  326.         }
  327.         else{
  328.             if(walking == 4 && moveTo <= getCoordX()){
  329.                 sprite.setVelocityX(0);
  330.                 walking = 0;
  331.             }
  332.         }
  333.        
  334.     }
  335.    
  336.     public void animate(String direction){
  337.         if(direction.equalsIgnoreCase("up")){
  338.             if(firstTime == true){
  339.                 a.removeAllScenes();
  340.                 for(byte i = 3; i >= 2; i--){
  341.                     a.addScene(image[i], 250);
  342.                 }
  343.                 firstTime = false;
  344.             }
  345.         }
  346.     }
  347.    
  348.     public void stopAnimate(String direction){
  349.         if(direction.equalsIgnoreCase("up")){
  350.             firstTime = true;
  351.             a.removeAllScenes();
  352.             a.addScene(image[2], 150);
  353.         }
  354.     }
  355.    
  356.     public void usePortal(){
  357.         int[][] array = gc.portals;
  358.         for(int i = 0; i < array.length; i++){
  359.             int[] nestedArray = array[i];
  360.             int x1 = nestedArray[0];
  361.             int y1 = nestedArray[1];
  362.             int x2 = nestedArray[2];
  363.             int y2 = nestedArray[3];
  364.            
  365.             if(getCoordX() == x1 && getCoordY() == y1){
  366.                 walking = 0;
  367.                 moveUp = false;
  368.                 moveDown = false;
  369.                 moveLeft = false;
  370.                 moveRight = false;
  371.                 sprite.setVelocityX(0);
  372.                 sprite.setVelocityY(0);
  373.                 setCoordX(x2);
  374.                 setCoordY(y2);
  375.             }
  376.         }
  377.     }
  378.    
  379.     //-----------------------------------------------------------------
  380.     // Getter Methods
  381.     //-----------------------------------------------------------------
  382.     public Image getImage(){
  383.         return a.getImage();
  384.     }
  385.    
  386.     public String getName(){
  387.         return entityName;
  388.     }
  389.    
  390.     public int getX(){
  391.         return sprite.getX();
  392.     }
  393.    
  394.     public int getY(){
  395.         return sprite.getY();
  396.     }
  397.    
  398.     public float getCoordX(){
  399.         return sprite.getCoordX();
  400.     }
  401.    
  402.     public float getCoordY(){
  403.         return sprite.getCoordY();
  404.     }
  405.    
  406.     public int getDrawX(){
  407.         return sprite.getX() - gc.offsetX;
  408.     }
  409.    
  410.     public int getDrawY(){
  411.         return sprite.getY() - gc.offsetY;
  412.     }
  413.    
  414.     public int getWidth(){
  415.         return sprite.getWidth();
  416.     }
  417.    
  418.     public int getHeight(){
  419.         return sprite.getHeight();
  420.     }
  421.    
  422.     public Rectangle getBounds(){
  423.         return rect;
  424.     }
  425.    
  426.     //-----------------------------------------------------------------
  427.     // Setter Methods
  428.     //-----------------------------------------------------------------
  429.     public void setCoordX(int x){
  430.         sprite.setCoordX(x);
  431.     }
  432.    
  433.     public void setCoordY(int y){
  434.         sprite.setCoordY(y);
  435.     }
  436.    
  437.     public void setSpeed(int speed){
  438.         this.speed = speed;
  439.     }
  440.    
  441.    
  442. }
Advertisement
Add Comment
Please, Sign In to add comment