Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.80 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.scene.canvas.Canvas;
  4. import javafx.scene.canvas.GraphicsContext;
  5. import javafx.scene.image.Image;
  6. import javafx.scene.input.KeyCode;
  7. import javafx.scene.input.KeyEvent;
  8. import javafx.scene.layout.BorderPane;
  9.  
  10.  
  11. public class Player implements Paintable, Destructable {
  12.  
  13.     private int x;
  14.     private int y;
  15.     private int index_x;
  16.     private int index_y;
  17.     private int last_index_x;
  18.     private int last_index_y;
  19.     private int size;
  20.  
  21.     private int score;
  22.  
  23.     private BorderPane pane;
  24.     private GraphicsContext gc;
  25.     private Canvas canvas;
  26.  
  27.     private Map map;
  28.  
  29.     private String facing;
  30.  
  31.     private boolean alive = true;
  32.  
  33.     private int explosion_range;
  34.     private int max_bombs;
  35.     private int bomb_count;
  36.     private boolean bomb_planted = false;
  37.  
  38.  
  39.  
  40.  
  41.     public Player(BorderPane pane, Map map, int x, int y, int size){
  42.         this.pane = pane;
  43.         this.x = x;
  44.         this.y = y;
  45.         this.size = size;
  46.         this.explosion_range = 2;
  47.         this.bomb_count = 0;
  48.         this.max_bombs = 3;
  49.         this.map = map;
  50.         this.score = 0;
  51.  
  52.     }
  53.  
  54.     public int getX(){ return this.x; }
  55.     public int getY(){ return this.y; }
  56.     public Map getMap(){ return this.map; }
  57.  
  58.     public void increaseScore(int score){ this.score+= score; }
  59.  
  60.     public void bombCountDecrease(){ if(this.bomb_count != 0) bomb_count--;}
  61.  
  62.     public void addToCanvas(){
  63.         this.canvas = new Canvas(Constants.WIDTH, Constants.HEIGHT);
  64.         this.gc = canvas.getGraphicsContext2D();
  65.         this.gc.drawImage(Constants.BOMBERMAN, x, y, size, size);    // tady do gc. vykreslis nejaky geometricky utvar treba ctverec, nebo treba texturu
  66.         this.pane.getChildren().add(canvas);  // pridas do struktury
  67.         this.index_x = x/Constants.UNIT_SIZE;
  68.         this.index_y = y/Constants.UNIT_SIZE;
  69.         this.last_index_x = index_x;
  70.         this.last_index_y = index_y;
  71.         this.map.addPlayerToMapOfDestructible(this,index_x,index_y);
  72.  
  73.     }
  74.  
  75.     public void redraw(){
  76.         this.map.removePlayerFromMapOfDestructible(last_index_x,last_index_y);
  77.         this.pane.getChildren().remove(canvas);
  78.         addToCanvas();
  79.  
  80.     }
  81.  
  82.     public void control(KeyEvent event){
  83.         if(alive) {
  84.             int index_x2 = this.x / Constants.UNIT_SIZE;
  85.             int index_x1 = (this.x + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
  86.             int index_y2 = this.y / Constants.UNIT_SIZE;
  87.             int index_y1 = (this.y + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
  88.  
  89.             if (event.getCode() == KeyCode.LEFT) {
  90.                 int new_x = this.x - Constants.UNIT_SPEED;
  91.                 index_x1 = (new_x + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
  92.                 index_x2 = (new_x) / Constants.UNIT_SIZE;
  93.                 if (((this.map.getMap()[index_x1][index_y1] <= 1 && this.map.getMap()[index_x2][index_y2] <= 1) && (this.map.getMap()[index_x2][index_y1] <= 1 && this.map.getMap()[index_x1][index_y2] <= 1)) && this.bomb_planted) {
  94.                     this.x -= Constants.UNIT_SPEED;
  95.                     this.bomb_planted = false;
  96.                     this.facing = "LEFT";
  97.                 } else if ((this.map.getMap()[index_x1][index_y1] == 0) && (this.map.getMap()[index_x2][index_y2] == 0 && this.map.getMap()[index_x2][index_y1] == 0) && (this.map.getMap()[index_x1][index_y2] == 0)) {
  98.                     this.x -= Constants.UNIT_SPEED;
  99.                     this.facing = "LEFT";
  100.                 }
  101.             }
  102.             if (event.getCode() == KeyCode.RIGHT) {
  103.                 int new_x = this.x + Constants.UNIT_SPEED;
  104.                 index_x1 = (new_x + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
  105.                 index_x2 = (new_x) / Constants.UNIT_SIZE;
  106.                 if (((this.map.getMap()[index_x1][index_y1] <= 1) && (this.map.getMap()[index_x2][index_y2] <= 1 && this.map.getMap()[index_x2][index_y1] <= 1) && (this.map.getMap()[index_x1][index_y2] <= 1)) && this.bomb_planted) {
  107.                     this.x += Constants.UNIT_SPEED;
  108.                     this.bomb_planted = false;
  109.                     this.facing = "RIGHT";
  110.  
  111.                 } else if ((this.map.getMap()[index_x1][index_y1] == 0) && (this.map.getMap()[index_x2][index_y2] == 0 && this.map.getMap()[index_x2][index_y1] == 0) && (this.map.getMap()[index_x1][index_y2] == 0)) {
  112.                     this.x += Constants.UNIT_SPEED;
  113.                     this.facing = "RIGHT";
  114.  
  115.                 }
  116.  
  117.             }
  118.             if (event.getCode() == KeyCode.UP) {
  119.                 int new_y = this.y - Constants.UNIT_SPEED;
  120.                 index_y1 = (new_y + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
  121.                 index_y2 = (new_y) / Constants.UNIT_SIZE;
  122.                 if (((this.map.getMap()[index_x1][index_y1] <= 1 && this.map.getMap()[index_x2][index_y2] <= 1) && (this.map.getMap()[index_x2][index_y1] <= 1 && this.map.getMap()[index_x1][index_y2] <= 1)) && this.bomb_planted) {
  123.                     this.y -= Constants.UNIT_SPEED;
  124.                     this.bomb_planted = false;
  125.                     this.facing = "UP";
  126.                 } else if ((this.map.getMap()[index_x1][index_y1] == 0) && (this.map.getMap()[index_x2][index_y2] == 0 && this.map.getMap()[index_x2][index_y1] == 0) && (this.map.getMap()[index_x1][index_y2] == 0)) {
  127.                     this.y -= Constants.UNIT_SPEED;
  128.                     this.facing = "UP";
  129.                 }
  130.             }
  131.             if (event.getCode() == KeyCode.DOWN) {
  132.                 int new_y = this.y + Constants.UNIT_SPEED;
  133.                 index_y1 = (new_y + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
  134.                 index_y2 = (new_y) / Constants.UNIT_SIZE;
  135.                 if ((((this.map.getMap()[index_x2][index_y2] <= 1 && this.map.getMap()[index_x1][index_y1] <= 1)) && ((this.map.getMap()[index_x1][index_y2] <= 1) && (this.map.getMap()[index_x2][index_y1] <= 1))) && this.bomb_planted) {
  136.                     this.y += Constants.UNIT_SPEED;
  137.                     this.bomb_planted = false;
  138.                     this.facing = "DOWN";
  139.                 } else if (((this.map.getMap()[index_x2][index_y2] == 0 && this.map.getMap()[index_x1][index_y1] == 0)) && ((this.map.getMap()[index_x1][index_y2] == 0) && (this.map.getMap()[index_x2][index_y1] == 0))) {
  140.                     this.y += Constants.UNIT_SPEED;
  141.                     this.facing = "DOWN";
  142.                 }
  143.             }
  144.             if (event.getCode() == KeyCode.SPACE && !bomb_planted) {
  145.                 this.bomb_planted = true;
  146.                 plant_bomb();
  147.  
  148.             }
  149.         }
  150.     }
  151.  
  152.     public void plant_bomb(){
  153.         int index_x = 0;
  154.         int index_y = 0;
  155.         if(this.bomb_count < this.max_bombs) {
  156.             if(this.x % 50 != 0){
  157.                 if(this.facing == "LEFT"){ index_x = this.x/Constants.UNIT_SIZE; }
  158.                 else if(this.facing == "RIGHT"){ index_x = this.x/Constants.UNIT_SIZE+1; }
  159.             }
  160.             else { index_x = this.x/Constants.UNIT_SIZE;}
  161.             if(this.y % 50 != 0){
  162.                 if(this.facing == "UP"){ index_y = this.y/Constants.UNIT_SIZE; }
  163.                 else if(this.facing == "DOWN"){ index_y = this.y/Constants.UNIT_SIZE + 1; }
  164.             }
  165.             else { index_y = this.y/Constants.UNIT_SIZE; }
  166.             System.out.println(index_x + "  " + index_y);
  167.             this.bomb_count++;
  168.             Bomb bomb = new Bomb(this, this.pane,this.map, index_x, index_y, this.explosion_range);
  169.             bomb.addToCanvas();
  170.             bomb.activate();
  171.         }
  172.  
  173.     }
  174.  
  175.     @Override
  176.     public void Destroy() {
  177.         System.out.println("You died!");
  178.         /*this.alive = false;
  179.         this.gc.drawImage(Constants.DEATH,x,y,Constants.UNIT_SIZE,Constants.UNIT_SIZE);
  180.        // this.pane.getChildren().remove(canvas);
  181.     */
  182.  
  183.     }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement