Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.image.Image;
- import javafx.scene.input.KeyCode;
- import javafx.scene.input.KeyEvent;
- import javafx.scene.layout.BorderPane;
- public class Player implements Paintable, Destructable {
- private int x;
- private int y;
- private int index_x;
- private int index_y;
- private int last_index_x;
- private int last_index_y;
- private int size;
- private int score;
- private BorderPane pane;
- private GraphicsContext gc;
- private Canvas canvas;
- private Map map;
- private String facing;
- private boolean alive = true;
- private int explosion_range;
- private int max_bombs;
- private int bomb_count;
- private boolean bomb_planted = false;
- public Player(BorderPane pane, Map map, int x, int y, int size){
- this.pane = pane;
- this.x = x;
- this.y = y;
- this.size = size;
- this.explosion_range = 2;
- this.bomb_count = 0;
- this.max_bombs = 3;
- this.map = map;
- this.score = 0;
- }
- public int getX(){ return this.x; }
- public int getY(){ return this.y; }
- public Map getMap(){ return this.map; }
- public void increaseScore(int score){ this.score+= score; }
- public void bombCountDecrease(){ if(this.bomb_count != 0) bomb_count--;}
- public void addToCanvas(){
- this.canvas = new Canvas(Constants.WIDTH, Constants.HEIGHT);
- this.gc = canvas.getGraphicsContext2D();
- this.gc.drawImage(Constants.BOMBERMAN, x, y, size, size); // tady do gc. vykreslis nejaky geometricky utvar treba ctverec, nebo treba texturu
- this.pane.getChildren().add(canvas); // pridas do struktury
- this.index_x = x/Constants.UNIT_SIZE;
- this.index_y = y/Constants.UNIT_SIZE;
- this.last_index_x = index_x;
- this.last_index_y = index_y;
- this.map.addPlayerToMapOfDestructible(this,index_x,index_y);
- }
- public void redraw(){
- this.map.removePlayerFromMapOfDestructible(last_index_x,last_index_y);
- this.pane.getChildren().remove(canvas);
- addToCanvas();
- }
- public void control(KeyEvent event){
- if(alive) {
- int index_x2 = this.x / Constants.UNIT_SIZE;
- int index_x1 = (this.x + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
- int index_y2 = this.y / Constants.UNIT_SIZE;
- int index_y1 = (this.y + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
- if (event.getCode() == KeyCode.LEFT) {
- int new_x = this.x - Constants.UNIT_SPEED;
- index_x1 = (new_x + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
- index_x2 = (new_x) / Constants.UNIT_SIZE;
- 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) {
- this.x -= Constants.UNIT_SPEED;
- this.bomb_planted = false;
- this.facing = "LEFT";
- } 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)) {
- this.x -= Constants.UNIT_SPEED;
- this.facing = "LEFT";
- }
- }
- if (event.getCode() == KeyCode.RIGHT) {
- int new_x = this.x + Constants.UNIT_SPEED;
- index_x1 = (new_x + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
- index_x2 = (new_x) / Constants.UNIT_SIZE;
- 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) {
- this.x += Constants.UNIT_SPEED;
- this.bomb_planted = false;
- this.facing = "RIGHT";
- } 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)) {
- this.x += Constants.UNIT_SPEED;
- this.facing = "RIGHT";
- }
- }
- if (event.getCode() == KeyCode.UP) {
- int new_y = this.y - Constants.UNIT_SPEED;
- index_y1 = (new_y + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
- index_y2 = (new_y) / Constants.UNIT_SIZE;
- 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) {
- this.y -= Constants.UNIT_SPEED;
- this.bomb_planted = false;
- this.facing = "UP";
- } 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)) {
- this.y -= Constants.UNIT_SPEED;
- this.facing = "UP";
- }
- }
- if (event.getCode() == KeyCode.DOWN) {
- int new_y = this.y + Constants.UNIT_SPEED;
- index_y1 = (new_y + Constants.UNIT_SIZE - 1) / Constants.UNIT_SIZE;
- index_y2 = (new_y) / Constants.UNIT_SIZE;
- 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) {
- this.y += Constants.UNIT_SPEED;
- this.bomb_planted = false;
- this.facing = "DOWN";
- } 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))) {
- this.y += Constants.UNIT_SPEED;
- this.facing = "DOWN";
- }
- }
- if (event.getCode() == KeyCode.SPACE && !bomb_planted) {
- this.bomb_planted = true;
- plant_bomb();
- }
- }
- }
- public void plant_bomb(){
- int index_x = 0;
- int index_y = 0;
- if(this.bomb_count < this.max_bombs) {
- if(this.x % 50 != 0){
- if(this.facing == "LEFT"){ index_x = this.x/Constants.UNIT_SIZE; }
- else if(this.facing == "RIGHT"){ index_x = this.x/Constants.UNIT_SIZE+1; }
- }
- else { index_x = this.x/Constants.UNIT_SIZE;}
- if(this.y % 50 != 0){
- if(this.facing == "UP"){ index_y = this.y/Constants.UNIT_SIZE; }
- else if(this.facing == "DOWN"){ index_y = this.y/Constants.UNIT_SIZE + 1; }
- }
- else { index_y = this.y/Constants.UNIT_SIZE; }
- System.out.println(index_x + " " + index_y);
- this.bomb_count++;
- Bomb bomb = new Bomb(this, this.pane,this.map, index_x, index_y, this.explosion_range);
- bomb.addToCanvas();
- bomb.activate();
- }
- }
- @Override
- public void Destroy() {
- System.out.println("You died!");
- /*this.alive = false;
- this.gc.drawImage(Constants.DEATH,x,y,Constants.UNIT_SIZE,Constants.UNIT_SIZE);
- // this.pane.getChildren().remove(canvas);
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement