Skip to content

Commit

Permalink
damage and enemyMovement
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilou Li authored and Zilou Li committed Apr 12, 2024
1 parent 0068305 commit b8139bc
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 102 deletions.
55 changes: 29 additions & 26 deletions .idea/workspace.xml

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

68 changes: 39 additions & 29 deletions Code/src/main/java/org/example/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@ public class Character extends GameLoop{
int health;
int speed;
boolean exist;
public int direction;
public static ArrayList<Player> players;
public static ArrayList<Enemy> 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;
}
Expand All @@ -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;
}

}
22 changes: 20 additions & 2 deletions Code/src/main/java/org/example/Enemy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ 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;
this.px=x;
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<Enemy> generateEnemies(PApplet parent) {
Expand All @@ -41,8 +44,23 @@ public static ArrayList<Enemy> 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(){
Expand Down
2 changes: 1 addition & 1 deletion Code/src/main/java/org/example/Flame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
27 changes: 17 additions & 10 deletions Code/src/main/java/org/example/GameLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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(){
Expand Down
3 changes: 1 addition & 2 deletions Code/src/main/java/org/example/Obstacle.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ public class Obstacle extends GameLoop{
int py;
public static ArrayList<Wall> walls;
public static ArrayList<BreakableRock> 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;
Expand Down
Loading

0 comments on commit b8139bc

Please sign in to comment.