Skip to content

Commit

Permalink
BulletClass-CollisionWorking
Browse files Browse the repository at this point in the history
- Las colisiones funcionan al impactar con un alien.
- Nuevo Metodo "Detect Colission"
- A revisar el metodo "CanMoveHere"
  • Loading branch information
TvConIdeas committed Oct 30, 2024
1 parent bc46c96 commit 13c539d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/entities/Bullet.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Bullet extends Entity {
public Bullet(float x, float y, int width, int height) {
super(x, y, width, height);
active = false;
initHitbox(x, y, width, height);
initHitbox(x, y, (int) (20 * Game.SCALE), (int) (28 * Game.SCALE));
}

// ====================> GET | SET <====================
Expand Down
24 changes: 22 additions & 2 deletions src/entities/BulletManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.awt.*;
import java.util.ArrayList;

import static utilz.Constants.EnemyConstants.DEAD;
import static utilz.HelpMethods.DetectCollision;

public class BulletManager {
// ====================> ATRIBUTOS <====================
private Playing playing;
Expand All @@ -20,6 +23,10 @@ public BulletManager(Playing playing) {

// ====================> GET | SET <====================

public ArrayList<Bullet> getBulletArr() {
return bulletArr;
}

// ====================> METODOS <====================
public void update(){
move();
Expand All @@ -30,7 +37,7 @@ public void draw(Graphics g){
for (int i = 0; i < bulletArr.size(); i++) {
Bullet bullet = bulletArr.get(i);
if (!bullet.active) {
g.fillRect((int)bullet.x, (int)bullet.y, bullet.width, bullet.height);
g.fillRect((int)bullet.getHitbox().x, (int)bullet.getHitbox().y, bullet.width, bullet.height);
}
}
}
Expand All @@ -39,7 +46,20 @@ public void draw(Graphics g){
public void move() {
for (int i = 0; i < bulletArr.size(); i++) {
Bullet bullet = bulletArr.get(i);
bullet.y -= bulletSpeed;
bullet.getHitbox().y -= bulletSpeed;

for (int j = 0; j < playing.enemyManager.getEnemies().size(); j++) {
Alien1 alien = playing.enemyManager.getEnemies().get(j);
if (!bullet.active && alien.active && DetectCollision(alien, bullet)) {
bullet.active = true;
alien.state = DEAD;
alien.active = false;
}
}

while (bulletArr.size() > 0 && (bulletArr.get(0).active || bulletArr.get(0).y < 0)) {
bulletArr.remove(0);
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/entities/EnemyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public int getAlienCount() {
return alienCount;
}

public ArrayList<entities.Alien1> getEnemies() {
return enemies;
}

// ====================> METODOS <====================
public void update(){
for(Alien1 alien1 : enemies){
Expand All @@ -45,7 +49,6 @@ public void update(){

public void draw(Graphics g){
drawAlien1(g);

}

/** move() ==> Se encarga de mover la ubicacion de los aliens1. */
Expand Down Expand Up @@ -87,7 +90,6 @@ private void drawAlien1(Graphics g){
public void createAliens() {
for (int i = 0; i < alienColumns; i++) {
for (int j = 0; j < alienRows; j++) {

Alien1 alien = new Alien1(
Game.TILES_SIZE + i * Alien_WIDTH,
Game.TILES_SIZE + j * Alien_HEIGHT);
Expand Down
2 changes: 1 addition & 1 deletion src/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ else if (right && !left)
xSpeed = speed*3;


// Comprobación de Colision
// Comprobación de Colision con las Paredes
if (CanMoveHere(hitbox.x + xSpeed, hitbox.y + ySpeed, hitbox.width, hitbox.height)) {
hitbox.x += xSpeed;
hitbox.y += ySpeed;
Expand Down
8 changes: 3 additions & 5 deletions src/gameState/Playing.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package gameState;

import entities.Bullet;
import entities.BulletManager;
import entities.EnemyManager;
import entities.Player;
import entities.*;
import main.Game;

import java.awt.*;
Expand All @@ -20,8 +17,9 @@ public class Playing extends State {

// ====================> ATRIBUTOS <====================
private Player player;
private EnemyManager enemyManager;
public EnemyManager enemyManager;
private BulletManager bulletManager;
private int score;

// ====================> CONSTRUCTOR <====================
public Playing(Game game) {
Expand Down
11 changes: 11 additions & 0 deletions src/utilz/HelpMethods.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package utilz;

import entities.Entity;
import main.Game;

public class HelpMethods {
Expand All @@ -12,6 +13,8 @@ public static boolean CanMoveHere(float x, float y, float width, float height) {
- Abajo a la izquierda.
Si alguna es sólida retorna false y no se permite el movimiento.
Si todas las posiciones están libres, entonces retorna true.
Este metodo lo utilizaremos para detectar la colision CON LA PARED
(Revisar)
*/

if (!IsSolid(x, y))
Expand All @@ -22,6 +25,14 @@ public static boolean CanMoveHere(float x, float y, float width, float height) {
return false;
}

/// Metodo para detectar Colisiones con otras Entidades
public static boolean DetectCollision(Entity a, Entity b) {
return a.getHitbox().x < b.getHitbox().x + b.getHitbox().width &&
a.getHitbox().x + a.getHitbox().width > b.getHitbox().x &&
a.getHitbox().y < b.getHitbox().y + b.getHitbox().height &&
a.getHitbox().y + a.getHitbox().height > b.getHitbox().y;
}

public static boolean IsSolid(float x, float y) {
// Esta funcion se encarga de si una coordenada (x, y) está fuera de los límites del área de juego.
if (x < 0 || x >= Game.GAME_WIDTH)
Expand Down

0 comments on commit 13c539d

Please sign in to comment.