diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2d0222e..d2f800a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,10 +6,11 @@ - + + - { - "keyToString": { - "ASKED_ADD_EXTERNAL_FILES": "true", - "Application.Bomberman.executor": "Run", - "Application.Code.org.example.GameLoop.executor": "Run", - "Application.GameLoop.executor": "Run", - "RunOnceActivity.OpenProjectViewOnStart": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "git-widget-placeholder": "enemyAI", - "kotlin-language-version-configured": "true", - "last_opened_file_path": "/Users/zilouli/Engineering Project Game/2024-group-7", - "node.js.detected.package.eslint": "true", - "node.js.detected.package.tslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "project.structure.last.edited": "SDK", - "project.structure.proportion": "0.15", - "project.structure.side.proportion": "0.2", - "settings.editor.selected.configurable": "configurable.group.editor", - "vue.rearranger.settings.migration": "true", - "应用程序.GameLoop.executor": "Run", - "应用程序.GameStarter.executor": "Run", - "应用程序.gameStarter.executor": "Run" + +}]]> diff --git a/Code/src/main/java/org/example/Character.java b/Code/src/main/java/org/example/Character.java index 22965e6..4e0365f 100644 --- a/Code/src/main/java/org/example/Character.java +++ b/Code/src/main/java/org/example/Character.java @@ -10,24 +10,26 @@ public class Character extends GameLoop{ int health; int speed; boolean exist; + public int direction; public static ArrayList players; public static ArrayList enemies; public static int enemyNumber = 5; + double damageTime = 0; public boolean collisionDetect() { - int x = 0, y = 0; - if (up) { + int x = px, y = py; + if (direction == 0) { x = this.x() + tile / 2; y = this.y() + tile / 2 - this.getSpeed(); } - if (down) { + if (direction == 2) { x = this.x() + tile / 2; y = this.y() + tile / 2 + this.getSpeed(); } - if (left) { + if (direction == 3) { x = this.x() + tile / 2 - this.getSpeed(); y = this.y() + tile / 2; } - if (right) { + if (direction == 1) { x = this.x() + tile / 2 + this.getSpeed(); y = this.y() + tile / 2; } @@ -40,53 +42,61 @@ public boolean collisionDetect() { int topY = y - height / 2; int bottomY = y + height / 2 - 1; - int topLeftGridX = (leftX - 15) / tile; - int topLeftGridY = (topY - 75) / tile; - int topRightGridX = (rightX - 15) / tile; - int bottomLeftGridY = (bottomY - 75) / tile; + int leftGridX = (leftX - 15) / tile; + int topGridY = (topY - 75) / tile; + int rightGridX = (rightX - 15) / tile; + int bottomGridY = (bottomY - 75) / tile; - if (Obstacle.obstacleGrid[topLeftGridX][topLeftGridY] || - Obstacle.obstacleGrid[topRightGridX][topLeftGridY] || - Obstacle.obstacleGrid[topLeftGridX][bottomLeftGridY] || - Obstacle.obstacleGrid[topRightGridX][bottomLeftGridY]) { - move = false; + if (Obstacle.obstacleGrid[leftGridX][topGridY] || + Obstacle.obstacleGrid[rightGridX][topGridY] || + Obstacle.obstacleGrid[leftGridX][bottomGridY] || + Obstacle.obstacleGrid[rightGridX][bottomGridY]) { return false; } + + for (Bomb bomb : Objects.bombs) { + if (bomb.showed && bomb.bombActive && + dist(x, y, bomb.x() + (float) tile / 2, bomb.y() + (float) tile / 2) <= tile) { + return false; + } + } /*for (int i = 0; i < enemyNumber; i++) { if (i < totEnemies && dist(x, y, enemies.get(i).x() + (float) tile / 2, enemies.get(i).y() + (float) tile / 2) < tile) { collision = true; } }*/ - for (Bomb bomb : Objects.bombs) { - if (bomb.showed && !bomb.bombActive && dist(x, y, bomb.x() + (float) tile / 2, bomb.y() + (float) tile / 2) >= tile) { - bomb.bombActive = true; - } - if (bomb.showed && bomb.bombActive && dist(x, y, bomb.x() + (float) tile / 2, bomb.y() + (float) tile / 2) < tile) { - return false; - } - } return true; } - /*public static void playerAttackDetect(int x, int y){ - if (bomb.showed && bomb.bombActive && dist(x, y, bomb.x() + (float) tile / 2, bomb.y() + (float) tile / 2) < tile) { - } - }*/ public void ifDamageCharacter(){ //handle the interaction between rocks and flames - if (Flame.flameCheck(this.x(), this.y())) { + if (Flame.flameCheck(px, py) && parent.millis() - damageTime > 1000) { this.health -= 1; + damageTime = parent.millis(); if (this.health == 0) { this.exist = false; } } } + void up() { + py -= speed; + } + void down() { + py += speed; + } + void left() { + px -= speed; + } + void right() { + px += speed; + } + int x(){return px;} int y(){return py;} - public int getSpeed() { - return this.speed; + int getSpeed(){ + return speed; } } diff --git a/Code/src/main/java/org/example/Enemy.java b/Code/src/main/java/org/example/Enemy.java index 871e2d2..f73522c 100644 --- a/Code/src/main/java/org/example/Enemy.java +++ b/Code/src/main/java/org/example/Enemy.java @@ -10,6 +10,8 @@ public class Enemy extends Character{ /*PApplet parent;*/ PImage enemyImage; + /*int currentDirection;// 0 = up,1 = right,2 = down, 3 = left*/ + Enemy(int x, int y, PApplet parent, PImage enemy) { /*super(x, y, parent, enemy);*/ this.parent = parent; @@ -17,8 +19,9 @@ public class Enemy extends Character{ this.py=y; this.enemyImage = enemy; this.health = 1; - this.speed = 3; + this.speed = 1; this.exist = true; + this.direction = new Random().nextInt(4); } public static ArrayList generateEnemies(PApplet parent) { @@ -41,8 +44,23 @@ public static ArrayList generateEnemies(PApplet parent) { return enemies; } - public void enemyMovement(){ + public void handleEnemyMovement(){ + if(collisionDetect()){ + switch (direction) { + case 0: this.up(); break; + case 1: this.right(); break; + case 2: this.down(); break; + case 3: this.left(); break; + } + }else{ + direction = new Random().nextInt(4); + } + } + public static void enemiesMove(){ + for(Enemy enemy : enemies){ + enemy.handleEnemyMovement(); + } } void render(){ diff --git a/Code/src/main/java/org/example/Flame.java b/Code/src/main/java/org/example/Flame.java index b15126c..e5322a0 100644 --- a/Code/src/main/java/org/example/Flame.java +++ b/Code/src/main/java/org/example/Flame.java @@ -80,7 +80,7 @@ public static void creatFlame(int x, int y) { // Check if flame exist in a position public static boolean flameCheck(int x,int y){ int col = (x - 15)/tile; - int row = (y - 15)/tile-2; + int row = (y - 75)/tile; return flames[col][row].showed; } diff --git a/Code/src/main/java/org/example/GameLoop.java b/Code/src/main/java/org/example/GameLoop.java index 6b5f2a0..e7e441b 100644 --- a/Code/src/main/java/org/example/GameLoop.java +++ b/Code/src/main/java/org/example/GameLoop.java @@ -83,13 +83,14 @@ public void draw() { fill(93, 88, 95); Wall.wallsRender(); - Enemy.enemiesRender(); + BreakableRock.rocksRender(); DoorKey.doorKeyRender(this); Door.doorRender(this); - Player.player1Render(); + Enemy.enemiesRender(); + Player.player1Render(); gameEndDetect(); @@ -102,16 +103,14 @@ public void draw() { Flame.flameRender(); Items.removeMarkedObjects(); - } - if (move) { - if(Character.players.get(0).collisionDetect()) { - Character.players.get(0).playerMove(); - } - } - Player.absorbToIntersection(); + Player.player1Movement(/*Player.players.get(0).direction*/); + Enemy.enemiesMove(); + + Player.absorbToIntersection(); - Bomb.setBombIfPossible(this); + Bomb.setBombIfPossible(this); + } } public void mouseClicked() { @@ -126,16 +125,21 @@ public void mouseClicked() { public void keyPressed() { if (key == 'w') { up = true; + Player.players.get(0).direction = 0; } else if (key == 's') { down = true; + Player.players.get(0).direction = 2; } else if (key == 'a') { left = true; + Player.players.get(0).direction = 3; } else if (key == 'd') { right = true; + Player.players.get(0).direction = 1; } else if (key == 'c' && Player.players.get(0).getMaxBombs() >= Bomb.findCurrentBombsNumber()) { Objects.bomb = true; } move = up || down || left || right; + } public void keyReleased() { @@ -146,6 +150,9 @@ public void keyReleased() { case 'd': right = false; break; } move = up || down || left || right; + /*if(key == 'w' || key == 's' || key == 'a' || key == 'd'){ + Player.players.get(0).direction = -1; + }*/ } private static void gameEndDetect(){ diff --git a/Code/src/main/java/org/example/Obstacle.java b/Code/src/main/java/org/example/Obstacle.java index aab988b..291d18a 100644 --- a/Code/src/main/java/org/example/Obstacle.java +++ b/Code/src/main/java/org/example/Obstacle.java @@ -10,10 +10,9 @@ public class Obstacle extends GameLoop{ int py; public static ArrayList walls; public static ArrayList rocks; - public static boolean[][]obstacleGrid = new boolean[50][50];//need to be fixed!!!!! + public static boolean[][]obstacleGrid = new boolean[cols][rows];//need to be fixed!!!!! public static void initializeObstacleGrid(int rows, int cols) { - /*obstacleGrid = new boolean[50][50];//need to be fixed!!!!!*/ for (BreakableRock rock : Obstacle.rocks) { int gridX = (rock.x() - 15) / tile; int gridY = (rock.y() - 75) / tile; diff --git a/Code/src/main/java/org/example/Player.java b/Code/src/main/java/org/example/Player.java index 42b5873..fd0d95d 100644 --- a/Code/src/main/java/org/example/Player.java +++ b/Code/src/main/java/org/example/Player.java @@ -10,17 +10,19 @@ public class Player extends Character { PImage playerImage; private int explosionDistance; private int maxBombs; + /*public int direction; // 0 = up,1 = right,2 = down, 3 = left*/ Player(int x, int y, PApplet parent, PImage playerImage){ this.parent =parent; this.px=x; this.py=y; this.playerImage = playerImage; - this.health = 1; + this.health = 3; this.explosionDistance = 1; this.maxBombs = 1; this.speed = 3; this.exist = true; + this.direction = -1; } public static ArrayList setPlayer1(PApplet parent) { @@ -32,18 +34,48 @@ public static ArrayList setPlayer1(PApplet parent) { } public void playerMove(){ - if (up) { - this.up(); - } - if (down) { - this.down(); - } - if (left) { - this.left(); - } - if (right) { - this.right(); + /*if (direction == 0) { + this.up(); + } + if (direction == 2) { + this.down(); + } + if (direction == 3) { + this.left(); + } + if (direction == 1) { + this.right(); + }*/ + if (up) { + this.up(); + } + if (down) { + this.down(); + } + if (left) { + this.left(); + } + if (right) { + this.right(); + } + } + + private void playerBombActivation(){ + for (Bomb bomb : Objects.bombs) { + if (bomb.showed && !bomb.bombActive && + dist(px + (float) tile / 2, py + (float) tile / 2, bomb.x() + (float) tile / 2, bomb.y() + (float) tile / 2) >= tile) { + bomb.bombActive = true; + /*System.out.println("the bomb is active");*/ } + } + } + + public static void player1Movement(){ + Character.players.get(0).playerBombActivation(); + if(Character.players.get(0).collisionDetect()) { + Character.players.get(0).playerMove(); +// System.out.println("x = " + Character.players.get(0).px + " y = " + Character.players.get(0).py); + } } public static void absorbToIntersection(){ @@ -57,26 +89,6 @@ void render(){ parent.image(playerImage,px,py,30,30); } - // 更新移动方法以设置方向标志 - void up() { - py -= speed; - } - - void down() { - py += speed; - } - - void left() { - px -= speed; - } - - void right() { - px += speed; - } - - /*int x(){return px;} - int y(){return py;}*/ - public void increasePower() { this.explosionDistance +=1; } @@ -96,8 +108,23 @@ public int getMaxBombs() { public void increaseSpeed() {this.speed +=1; } public void increaseLife() {this.health += 1;} + public void ifTouchEnemy(){ + System.out.println("?"); + for(Enemy enemy : enemies) { + if (dist(px, py, enemy.x(), enemy.y()) < (float) tile / 2 + && parent.millis() - damageTime > 1000) { + health -= 1; + damageTime = parent.millis(); + if (health == 0) { + exist = false; + } + } + } + } + public static void player1Render(){ players.get(0).ifDamageCharacter(); + players.get(0).ifTouchEnemy(); if(Character.players.get(0).exist){ Character.players.get(0).render(); }else {