Skip to content

Commit

Permalink
forced change
Browse files Browse the repository at this point in the history
  • Loading branch information
bv23164 committed Apr 14, 2024
1 parent 7f08a77 commit 6aac445
Show file tree
Hide file tree
Showing 12 changed files with 438 additions and 141 deletions.
23 changes: 19 additions & 4 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 54 additions & 11 deletions Code/src/main/java/org/example/Bomb.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import processing.core.PApplet;
import processing.core.PImage;

import java.util.ArrayList;
import java.util.Iterator;

public class Bomb extends Objects{
Expand All @@ -10,7 +11,7 @@ public class Bomb extends Objects{
boolean bombActive;
double timer;
double setupTime;
int duration = 3000;
public static int duration = 3000;
int attack = 1;
boolean showed;
Bomb(int x, int y, PApplet parent, PImage bombImage){
Expand All @@ -29,17 +30,45 @@ void render(){
parent.image(bombImage,x,y,30,30);
}

public static void setBombIfPossible(PApplet parent){
if (bomb) {
if (findCurrentBombsNumber() < Character.players.get(0).getMaxBombs()) {
public static void setBombIfPossible1(PApplet parent){
if(parent.millis() - Player.players.get(0).setBombTime < 310){
return;
}
if (Player.players.get(0).bomb) {
if (findCurrentBombsNumber1() < Character.players.get(0).getMaxBombs()) {
int playerCenterX = Character.players.get(0).x() + tile / 2 - 15;
int playerCenterY = Character.players.get(0).y() + tile / 2 - 15;
bombs.add(new Bomb(playerCenterX, playerCenterY, parent, ResourceManager.basicBomb));
Character.players.get(0).bombs.add(new Bomb(playerCenterX, playerCenterY, parent, ResourceManager.basicBomb));
Player.players.get(0).setBombTime = parent.millis();
}
Player.players.get(0).bomb = false;
}
if (findCurrentBombsNumber1()>0) {
for (Bomb bomb : Character.players.get(0).bombs) {
if (!bomb.hasExpired()) {
bomb.render();
} else {
bomb.showed = false;
}
}
}
}

public static void setBombIfPossible2(PApplet parent){
if(parent.millis() - Player.players.get(1).setBombTime < 310){
return;
}
if (Player.players.get(1).bomb) {
if (findCurrentBombsNumber2() < Character.players.get(1).getMaxBombs()) {
int playerCenterX = Character.players.get(1).x() + tile / 2 - 15;
int playerCenterY = Character.players.get(1).y() + tile / 2 - 15;
Character.players.get(1).bombs.add(new Bomb(playerCenterX, playerCenterY, parent, ResourceManager.basicBomb));
Player.players.get(0).setBombTime = parent.millis();
}
bomb = false;
Player.players.get(1).bomb = false;
}
if (findCurrentBombsNumber()>0) {
for (Bomb bomb : bombs) {
if (findCurrentBombsNumber2()>0) {
for (Bomb bomb : Character.players.get(1).bombs) {
if (!bomb.hasExpired()) {
bomb.render();
} else {
Expand Down Expand Up @@ -73,7 +102,11 @@ public static int encodeCoordinate(int x, int y) {
}

public static void bombRender(){
Iterator<Bomb> iterator = bombs.iterator();
ArrayList<Bomb> list = new ArrayList<>();
list.addAll(Player.players.get(0).bombs);
list.addAll(Player.players.get(1).bombs);

Iterator<Bomb> iterator = list.iterator();
while (iterator.hasNext()) {
Bomb bomb = iterator.next();
int packedNumber = bomb.update();
Expand All @@ -90,9 +123,19 @@ public static void bombRender(){
}
}

public static int findCurrentBombsNumber(){
public static int findCurrentBombsNumber1(){
int number =0;
for(Bomb bomb : Player.players.get(0).bombs){
if(bomb.showed){
number++;
}
}
return number;
}

public static int findCurrentBombsNumber2(){
int number =0;
for(Bomb bomb : bombs){
for(Bomb bomb : Player.players.get(1).bombs){
if(bomb.showed){
number++;
}
Expand Down
57 changes: 53 additions & 4 deletions Code/src/main/java/org/example/BreakableRock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import processing.core.PImage;

import java.util.ArrayList;
import java.util.Iterator;

public class BreakableRock extends Obstacle{
PImage rock;
Expand All @@ -30,14 +31,13 @@ public class BreakableRock extends Obstacle{
this.rockExist = true;
}

public static ArrayList<BreakableRock> generateRocks(int rows, int cols, PApplet parent) {
float chanceOfRock = 0.5F;
ArrayList<BreakableRock> rocks = new ArrayList<>();
public static ArrayList<BreakableRock> generateRocks(int rows, int cols, PApplet parent, float chanceOfRock) {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// Randomly decide whether to place a rock in this grid cell
// Exclude the area around where player initially stand
if (parent.random(1) < chanceOfRock && !Wall.isWallAt(i, j) && !(i <4 && j <4)) {
if (parent.random(1) < chanceOfRock && !Wall.isWallAt(i, j) &&
!(i <= 4 && j <= 4) && !(i >= cols - 4 && j >= rows - 4)) {
int x = 15 + i * tile;
int y = 75 + j * tile;
rocks.add(new BreakableRock(x, y, parent, ResourceManager.rock));
Expand All @@ -47,6 +47,46 @@ public static ArrayList<BreakableRock> generateRocks(int rows, int cols, PApplet
return rocks;
}

public static ArrayList<BreakableRock> generateLessRocks(int rows, int cols, PApplet parent, float chanceOfRock) {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// Randomly decide whether to place a rock in this grid cell
// Exclude the area around where player initially stand
if (parent.random(1) < chanceOfRock && !Wall.isWallAt(i, j) &&
!(i <= 4 && j <= 4) && !(i >= cols - 4 && j >= rows - 4)) {
int x = 15 + i * tile;
int y = 75 + j * tile;
lessRocks.add(new BreakableRock(x, y, parent, ResourceManager.rock));
}
}
}
return lessRocks;
}

// 移除被標記的岩石
public static void removeRocks() {
Iterator<BreakableRock> rockIterator = Obstacle.rocks.iterator();
while (rockIterator.hasNext()) {
BreakableRock rock = rockIterator.next();
if (rock.isMarkedForRemoval()) {
Obstacle.removeRockFromObstacleGrid(rock);
rockIterator.remove();
}
}
}

public static void removeLessRocks() {
Iterator<BreakableRock> rockIterator = Obstacle.lessRocks.iterator();
while (rockIterator.hasNext()) {
BreakableRock rock = rockIterator.next();
if (rock.isMarkedForRemoval()) {
Obstacle.removeRockFromObstacleGridPVP(rock);
rockIterator.remove();
}
}
}


public void ifDestroyRock(){
//handle the interaction between rocks and flames
if (Flame.flameCheck(this.x(), this.y())) {
Expand Down Expand Up @@ -129,4 +169,13 @@ public static void rocksRender(){
rock.ifDestroyRock();
}
}

public static void lessRocksRender(){
for (BreakableRock rock : lessRocks) {
if (rock.rockExist) {
rock.render();
}
rock.ifDestroyRock();
}
}
}
66 changes: 63 additions & 3 deletions Code/src/main/java/org/example/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class Character extends GameLoop{
int speed;
boolean exist;
public int direction;
public static ArrayList<Player> players;
public static ArrayList<Enemy> enemies;
public static ArrayList<Player> players = new ArrayList<>();
public static ArrayList<Enemy> enemies = new ArrayList<>();
public static int enemyNumber = 5;
double damageTime = 0;
public boolean collisionDetect() {
Expand Down Expand Up @@ -54,7 +54,66 @@ public boolean collisionDetect() {
return false;
}

for (Bomb bomb : Objects.bombs) {
for (Bomb bomb : Player.players.get(0).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;
}
}*/
return true;
}

public boolean collisionDetectPVP() {
int x = px, y = py;
if (direction == 0) {
x = this.x() + tile / 2;
y = this.y() + tile / 2 - this.getSpeed();
}
if (direction == 2) {
x = this.x() + tile / 2;
y = this.y() + tile / 2 + this.getSpeed();
}
if (direction == 3) {
x = this.x() + tile / 2 - this.getSpeed();
y = this.y() + tile / 2;
}
if (direction == 1) {
x = this.x() + tile / 2 + this.getSpeed();
y = this.y() + tile / 2;
}

int width = tile;
int height = tile;

int leftX = x - width / 2;
int rightX = x + width / 2 - 1;
int topY = y - height / 2;
int bottomY = y + height / 2 - 1;

int leftGridX = (leftX - 15) / tile;
int topGridY = (topY - 75) / tile;
int rightGridX = (rightX - 15) / tile;
int bottomGridY = (bottomY - 75) / tile;

if (Obstacle.obstacleGridPVP[leftGridX][topGridY] ||
Obstacle.obstacleGridPVP[rightGridX][topGridY] ||
Obstacle.obstacleGridPVP[leftGridX][bottomGridY] ||
Obstacle.obstacleGridPVP[rightGridX][bottomGridY]) {
return false;
}

for (Bomb bomb : Character.players.get(0).bombs) {
if (bomb.showed && bomb.bombActive &&
dist(x, y, bomb.x() + (float) tile / 2, bomb.y() + (float) tile / 2) <= tile) {
return false;
}
}
for (Bomb bomb : Character.players.get(1).bombs) {
if (bomb.showed && bomb.bombActive &&
dist(x, y, bomb.x() + (float) tile / 2, bomb.y() + (float) tile / 2) <= tile) {
return false;
Expand All @@ -71,6 +130,7 @@ public boolean collisionDetect() {
public void ifDamageCharacter(){
//handle the interaction between rocks and flames
if (Flame.flameCheck(px, py) && parent.millis() - damageTime > 1000) {
System.out.println(this.health);
this.health -= 1;
damageTime = parent.millis();
if (this.health == 0) {
Expand Down
1 change: 0 additions & 1 deletion Code/src/main/java/org/example/Enemy.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class Enemy extends Character{
}

public static ArrayList<Enemy> generateEnemies(PApplet parent) {
ArrayList<Enemy> enemies = new ArrayList<>();
int number =0;
Random random = new Random();
while(number < enemyNumber){
Expand Down
4 changes: 2 additions & 2 deletions Code/src/main/java/org/example/Flame.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public class Flame extends Objects{
PApplet parent;
PImage frameImage;
boolean showed;
int startTime, duration;
public int startTime, duration;
Flame(int x, int y, PApplet parent, PImage frameImage){
this.x = x;
this.y = y;
this.parent =parent;
this.frameImage = frameImage;
this.showed = false;
this.duration = 1000;
this.duration = 200;
}

void render(){
Expand Down
Loading

0 comments on commit 6aac445

Please sign in to comment.