Skip to content

Commit

Permalink
bigUpdate
Browse files Browse the repository at this point in the history
- Hacer interfaz para metodos draw() y update()
- Implementar interfaz (draw y update) en las clases necesarias
- Modificar Enemy Manager
  • Loading branch information
delfi-fenoy committed Nov 10, 2024
1 parent 3f9183c commit 2d56f1f
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 58 deletions.
Binary file added res/alien2_sprites.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/entities/Alien1.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package entities;

import main.Game;
import utilz.LoadSave;

import java.awt.*;
import java.awt.image.BufferedImage;

import static utilz.Constants.EnemyConstants.*;

Expand All @@ -13,10 +17,30 @@ public class Alien1 extends Enemy{
public Alien1(float x, float y) {
super(x, y, Alien_WIDTH, Alien_HEIGHT, Alien1);
initHitbox(x, y, (int) (20 * Game.SCALE), (int) (20 * Game.SCALE));
loadEnemyImgs();
}

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


// ====================> METODOS <====================
public void draw(Graphics g){
g.drawImage(
animations[state][getAniIndex()],
(int)(hitbox.x - xDrawOffset),
(int)(hitbox.y - yDrawOffset),
Alien_WIDTH,
Alien_HEIGHT,
null);
} // Dibuja en Alien, UTILIZANDO SU HITBOX

/** loadEnemyImgs() ==> Separa el SpriteSheat y los ubica en una matriz. */
private void loadEnemyImgs() {
animations = new BufferedImage[2][7];
BufferedImage temp = LoadSave.GetSpritesAtlas(LoadSave.Alien1_ATLAS);
for (int j = 0; j < animations.length; j++)
for (int i = 0; i < animations[j].length; i++)
animations[j][i] = temp.getSubimage(i * Alien_WIDHT_DEFAULT, j * Alien_HEIGHT_DEFAULT, Alien_WIDHT_DEFAULT, Alien_HEIGHT_DEFAULT);
}
}

29 changes: 27 additions & 2 deletions src/entities/Alien2.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package entities;

import main.Game;
import utilz.LoadSave;

import java.awt.*;
import java.awt.image.BufferedImage;

import static utilz.Constants.EnemyConstants.*;

public class Alien2 extends Enemy{
Expand All @@ -9,12 +15,31 @@ public class Alien2 extends Enemy{

// ====================> CONTRUCTOR <====================
public Alien2(float x, float y) {
super(x, y, Alien_WIDTH, Alien_HEIGHT, Alien1);
super(x, y, Alien_WIDTH, Alien_HEIGHT, Alien2);
initHitbox(x, y, (int) (20 * Game.SCALE), (int) (20 * Game.SCALE));
loadEnemyImgs();
}

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


// ====================> METODOS <====================

public void draw(Graphics g){
g.drawImage(
animations[state][getAniIndex()],
(int)(hitbox.x - xDrawOffset),
(int)(hitbox.y - yDrawOffset),
Alien_WIDTH,
Alien_HEIGHT,
null);
} // Dibuja en Alien, UTILIZANDO SU HITBOX

/** loadEnemyImgs() ==> Separa el SpriteSheat y los ubica en una matriz. */
private void loadEnemyImgs() {
animations = new BufferedImage[2][5];
BufferedImage temp = LoadSave.GetSpritesAtlas(LoadSave.Alien2_ATLAS);
for (int j = 0; j < animations.length; j++)
for (int i = 0; i < animations[j].length; i++)
animations[j][i] = temp.getSubimage(i * Alien_WIDHT_DEFAULT, j * Alien_HEIGHT_DEFAULT, Alien_WIDHT_DEFAULT, Alien_HEIGHT_DEFAULT);
}
}
5 changes: 3 additions & 2 deletions src/entities/BulletManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import gameState.Playing;
import main.Game;
import utilz.IRenderable;

import java.awt.*;
import java.util.ArrayList;

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

public class BulletManager {
public class BulletManager implements IRenderable {
// ====================> ATRIBUTOS <====================
private Playing playing;
private ArrayList<Bullet> bulletArr = new ArrayList<>(); // Arraylist con las balas
Expand Down Expand Up @@ -46,7 +47,7 @@ public void move() {

// Detecta la Colision con Enemigos
for (int j = 0; j < playing.enemyManager.getEnemies().size(); j++) {
Alien1 alien = playing.enemyManager.getEnemies().get(j);
Enemy alien = (Enemy) playing.enemyManager.getEnemies().get(j);

if (!bullet.active && alien.active && DetectCollision(alien, bullet)) {
alien.newState(DEAD); // Metodo para hacer que empiece la animacion de DEAD y
Expand Down
7 changes: 6 additions & 1 deletion src/entities/Enemy.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package entities;

import utilz.IRenderable;

import java.awt.image.BufferedImage;

import static utilz.Constants.ANI_SPEED;
import static utilz.Constants.EnemyConstants.*;

public abstract class Enemy extends Entity{
public abstract class Enemy extends Entity implements IRenderable {

// ====================> ATRIBUTOS <====================
private int enemyType; // Estado del enemigo || Tipo de enemigo
protected BufferedImage[][] animations;

// ====================> CONSTRUCTOR <====================
public Enemy(float x, float y, int width, int height, int enemyType) {
Expand Down
64 changes: 21 additions & 43 deletions src/entities/EnemyManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package entities;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import gameState.Playing;
Expand All @@ -12,24 +11,20 @@
import static utilz.Constants.EnemyConstants.*;
import static utilz.HelpMethods.DetectCollision;

public class EnemyManager {
public class EnemyManager <T extends Enemy> {

// ====================> ATRIBUTOS <====================
private Playing playing; // Traemos el State Playing
private BufferedImage[][] alien1Arr, alien2Arr; // Matriz con las animaciones del Alien1

private ArrayList<Alien1> enemies = new ArrayList<>(); // ArrayList con los aliens, (revisar, cambiar a Enemy)
private ArrayList<T> enemies = new ArrayList<>(); // ArrayList con los aliens, (revisar, cambiar a Enemy)
private int alienRows = 4; // Cantidad de Filas de aliens
private int alienColumns = 6; // Cantidad de Columnas de aliens
private int alienCount = 0; // Numero de Aliens a vencer
private float alienVelocityX = 0.1f; // Velocidad de los aliens (Revisar)
private float xDrawOffset = 6 * Game.SCALE; // Centraliza la hitbox en el jugador (ancho)
private float yDrawOffset = 4 * Game.SCALE; // Centraliza la hitbox en el jugador (largo)
private float alienVelocityX = 0.05f; // Velocidad de los aliens (Revisar)

// ====================> CONSTRUCTOR <====================
public EnemyManager(Playing playing) {
this.playing = playing;
loadEnemyImgs();
createAliens();
}

Expand All @@ -38,27 +33,32 @@ public int getAlienCount() {
return alienCount;
}

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

// ====================> METODOS <====================
public void update(){
for(Alien1 alien1 : enemies){
alien1.update(); // Revisar
for(T alien : enemies){
alien.update(); // Revisar
move();

}
}

public void draw(Graphics g){
drawAlien1(g);
public void draw(Graphics g){ // cambiar !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for(T alien : enemies){
if(alien.active){
// alien.drawHitbox(g);
alien.draw(g);
}
}
}

/** move() ==> Se encarga de mover la ubicacion de los aliens1. */
public void move(){
for (int i = 0; i < enemies.size(); i++) {
Alien1 alien = enemies.get(i);
T alien = enemies.get(i);
if (alien.active) { // Si el Alien esta vivo
alien.hitbox.x += alienVelocityX;

Expand All @@ -78,46 +78,24 @@ public void move(){
playing.getPlayer().newState(DEAD);
}
}

}
}

/** drawAlien1() ==> Method "sub-draw" para dibujar los Aliens 1*/
private void drawAlien1(Graphics g){
for(Alien1 alien1 : enemies){ // Hasta que se recorra el Array completo
if (alien1.active){ // Si esta vivo
alien1.drawHitbox(g);
g.drawImage(
alien1Arr[alien1.state][alien1.getAniIndex()],
(int)(alien1.hitbox.x - xDrawOffset),
(int)(alien1.hitbox.y - yDrawOffset),
Alien_WIDTH,
Alien_HEIGHT,
null);
} // Dibuja en Alien, UTILIZANDO SU HITBOX
}
}

/** createAliens() ==> Ubica los aliens según las filas y columnas y los agrega al
* ArrayList. */
public void createAliens() {
for (int i = 0; i < alienColumns; i++) {
/*for (int i = 0; i < alienColumns; i++) {
for (int j = 0; j < alienRows; j++) {
Alien1 alien = new Alien1(
T alien = (T) new Alien1(
Game.TILES_SIZE + i * Alien_WIDTH,
Game.TILES_SIZE + j * Alien_HEIGHT);
enemies.add(alien); // Lo agregamos al ArrayList
}
}
alienCount = enemies.size(); // Lo agregamos al contador
}
}*/

/** loadEnemyImgs() ==> Separa el SpriteSheat y los ubica en una matriz. */
private void loadEnemyImgs() {
alien1Arr = new BufferedImage[2][7];
BufferedImage temp = LoadSave.GetSpritesAtlas(LoadSave.Alien1_ATLAS);
for (int j = 0; j < alien1Arr.length; j++)
for (int i = 0; i < alien1Arr[j].length; i++)
alien1Arr[j][i] = temp.getSubimage(i * Alien_WIDHT_DEFAULT, j * Alien_HEIGHT_DEFAULT, Alien_WIDHT_DEFAULT, Alien_HEIGHT_DEFAULT);
enemies.add((T) new Alien1(100, 200));
enemies.add((T) new Alien2(150, 200));

alienCount = enemies.size(); // Lo agregamos al contador
}
}
6 changes: 6 additions & 0 deletions src/entities/Entity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package entities;

import main.Game;
import utilz.IRenderable;

import java.awt.Graphics;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
Expand All @@ -20,6 +23,9 @@ public abstract class Entity {
protected float speed= 1.0f;
protected boolean active = true;

protected float xDrawOffset = 6 * Game.SCALE; // Centraliza la hitbox en el jugador (ancho)
protected float yDrawOffset = 4 * Game.SCALE; // Centraliza la hitbox en el jugador (largo)

// ====================> CONSTRUCTOR <====================
public Entity(float x, float y, int width, int height) {
this.x = x;
Expand Down
7 changes: 3 additions & 4 deletions src/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.awt.image.BufferedImage;

import main.Game;
import utilz.IRenderable;
import utilz.LoadSave;

/**
Expand All @@ -16,14 +17,12 @@
* (animación, movimiento, ataque, etc).
*/

public class Player extends Entity {
public class Player extends Entity implements IRenderable {

// ====================> ATRIBUTOS <====================
private BufferedImage[][] animations; // Matriz con animaciones (SpriteSheat)
private boolean moving = false; // Si el jugador se está moviendo o no
private boolean left, right; // Direcciones del jugador
private float xDrawOffset = 6 * Game.SCALE; // Centraliza la hitbox en el jugador (ancho)
private float yDrawOffset = 4 * Game.SCALE; // Centraliza la hitbox en el jugador (largo)

// ====================> CONSTRUCTOR <====================
public Player(float x, float y, int width, int height) {
Expand Down Expand Up @@ -62,7 +61,7 @@ public void update(){
}

public void draw(Graphics g){
drawHitbox(g); // COMENTAR DESPUES !!!!!!!!!!!!!!!
// drawHitbox(g); // COMENTAR DESPUES !!!!!!!!!!!!!!!
g.drawImage(animations[state][aniIndex],
(int) (hitbox.x - xDrawOffset),
(int) (hitbox.y - yDrawOffset),
Expand Down
3 changes: 2 additions & 1 deletion src/gameState/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import main.Game;
import ui.MenuButton;
import utilz.IRenderable;

import java.awt.event.MouseEvent;

Expand All @@ -10,7 +11,7 @@
* Clase padre abstracta de los estados del juego.
*/

public abstract class State implements Statemethods{
public abstract class State implements Statemethods, IRenderable {

// ====================> ATRIBUTOS <====================
protected Game game;
Expand Down
2 changes: 0 additions & 2 deletions src/gameState/Statemethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

public interface Statemethods {

public void update();
public void draw(Graphics g);
public void mouseClicked(MouseEvent e);
public void mousePressed(MouseEvent e);
public void mouseReleased(MouseEvent e);
Expand Down
7 changes: 5 additions & 2 deletions src/main/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gameState.GameState;
import gameState.Menu;
import gameState.Playing;
import utilz.IRenderable;

/***
* Game ==>
Expand All @@ -16,7 +17,7 @@
* Una vez que un hilo se inicia, no para hasta que se lo quiera detener.
*/

public class Game implements Runnable {
public class Game implements Runnable, IRenderable {

// ====================> ATRIBUTOS <====================
private GameWindow gameWindow;
Expand Down Expand Up @@ -74,6 +75,7 @@ private void startGameLoop(){
}

/** update() ==> Actualizar la información por momento. */
@Override
public void update(){
switch (GameState.state){ // Llamar method update() según el gameState
case MENU:
Expand All @@ -91,7 +93,8 @@ public void update(){
}

/*** render() ==> Renderiza cada clase. Dibuja la ventana, con la informacion actualizada. */
public void render(Graphics g){
@Override
public void draw(Graphics g){
switch (GameState.state){
case MENU:
menu.draw(g);
Expand Down
2 changes: 1 addition & 1 deletion src/main/GamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public void paintComponent(Graphics g) {
super.paintComponent(g); /* Obligatorio siempre en esta función. Llama al method de la superclase
para que el Panel funcione correctamente. */

game.render(g);
game.draw(g);
}
}
7 changes: 7 additions & 0 deletions src/utilz/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public static int GetSpriteAmount(int enemy_type, int enemy_state){
case DEAD:
return 1;
}
case Alien2:
switch (enemy_state){
case MOVING:
return 5;
case DEAD:
return 1;
}
}
return 0;
}
Expand Down
Loading

0 comments on commit 2d56f1f

Please sign in to comment.