diff --git a/res/alien2_sprites.png b/res/alien2_sprites.png new file mode 100644 index 0000000..4346fc2 Binary files /dev/null and b/res/alien2_sprites.png differ diff --git a/src/entities/Alien1.java b/src/entities/Alien1.java index bf7fc56..059ee9b 100644 --- a/src/entities/Alien1.java +++ b/src/entities/Alien1.java @@ -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.*; @@ -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); + } } + diff --git a/src/entities/Alien2.java b/src/entities/Alien2.java index ed54874..0f77060 100644 --- a/src/entities/Alien2.java +++ b/src/entities/Alien2.java @@ -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{ @@ -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); + } } diff --git a/src/entities/BulletManager.java b/src/entities/BulletManager.java index 1ed94ae..9e04738 100644 --- a/src/entities/BulletManager.java +++ b/src/entities/BulletManager.java @@ -2,6 +2,7 @@ import gameState.Playing; import main.Game; +import utilz.IRenderable; import java.awt.*; import java.util.ArrayList; @@ -9,7 +10,7 @@ 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 bulletArr = new ArrayList<>(); // Arraylist con las balas @@ -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 diff --git a/src/entities/Enemy.java b/src/entities/Enemy.java index bbf4658..32025d6 100644 --- a/src/entities/Enemy.java +++ b/src/entities/Enemy.java @@ -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) { diff --git a/src/entities/EnemyManager.java b/src/entities/EnemyManager.java index 19b83cb..56383dc 100644 --- a/src/entities/EnemyManager.java +++ b/src/entities/EnemyManager.java @@ -1,7 +1,6 @@ package entities; import java.awt.Graphics; -import java.awt.image.BufferedImage; import java.util.ArrayList; import gameState.Playing; @@ -12,24 +11,20 @@ import static utilz.Constants.EnemyConstants.*; import static utilz.HelpMethods.DetectCollision; -public class EnemyManager { +public class EnemyManager { // ====================> ATRIBUTOS <==================== private Playing playing; // Traemos el State Playing - private BufferedImage[][] alien1Arr, alien2Arr; // Matriz con las animaciones del Alien1 - private ArrayList enemies = new ArrayList<>(); // ArrayList con los aliens, (revisar, cambiar a Enemy) + private ArrayList 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(); } @@ -38,27 +33,32 @@ public int getAlienCount() { return alienCount; } - public ArrayList getEnemies() { + public ArrayList 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; @@ -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 } } diff --git a/src/entities/Entity.java b/src/entities/Entity.java index 209daee..a8ae9f5 100644 --- a/src/entities/Entity.java +++ b/src/entities/Entity.java @@ -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; @@ -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; diff --git a/src/entities/Player.java b/src/entities/Player.java index 41f4610..ee56f01 100644 --- a/src/entities/Player.java +++ b/src/entities/Player.java @@ -8,6 +8,7 @@ import java.awt.image.BufferedImage; import main.Game; +import utilz.IRenderable; import utilz.LoadSave; /** @@ -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) { @@ -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), diff --git a/src/gameState/State.java b/src/gameState/State.java index 7c139a1..cc6aa3e 100644 --- a/src/gameState/State.java +++ b/src/gameState/State.java @@ -2,6 +2,7 @@ import main.Game; import ui.MenuButton; +import utilz.IRenderable; import java.awt.event.MouseEvent; @@ -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; diff --git a/src/gameState/Statemethods.java b/src/gameState/Statemethods.java index da138f4..3e78640 100644 --- a/src/gameState/Statemethods.java +++ b/src/gameState/Statemethods.java @@ -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); diff --git a/src/main/Game.java b/src/main/Game.java index 7016b92..f9dde6f 100644 --- a/src/main/Game.java +++ b/src/main/Game.java @@ -4,6 +4,7 @@ import gameState.GameState; import gameState.Menu; import gameState.Playing; +import utilz.IRenderable; /*** * Game ==> @@ -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; @@ -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: @@ -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); diff --git a/src/main/GamePanel.java b/src/main/GamePanel.java index 930de25..cfc5812 100644 --- a/src/main/GamePanel.java +++ b/src/main/GamePanel.java @@ -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); } } diff --git a/src/utilz/Constants.java b/src/utilz/Constants.java index 2a7061d..ef97645 100644 --- a/src/utilz/Constants.java +++ b/src/utilz/Constants.java @@ -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; } diff --git a/src/utilz/IRenderable.java b/src/utilz/IRenderable.java new file mode 100644 index 0000000..417fe24 --- /dev/null +++ b/src/utilz/IRenderable.java @@ -0,0 +1,9 @@ +package utilz; + +import java.awt.Graphics; + +public interface IRenderable { + + public void update(); + public void draw(Graphics g); +} diff --git a/src/utilz/LoadSave.java b/src/utilz/LoadSave.java index 5f0ce83..24bed64 100644 --- a/src/utilz/LoadSave.java +++ b/src/utilz/LoadSave.java @@ -10,6 +10,7 @@ public class LoadSave { // Constante con cada Sprite que utilizemos public static final String Player_ATLAS = "player_sprites.png"; public static final String Alien1_ATLAS = "alien1_sprites.png"; + public static final String Alien2_ATLAS = "alien2_sprites.png"; public static final String MENU_BUTTONS = "button_atlas.png"; // Funcion que retorna esas imagenes