Add chest object creation

Remove menu panel

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
Chris Cromer 2019-09-23 11:05:47 -03:00
parent a61220d873
commit 671a2abd29
8 changed files with 81 additions and 228 deletions

View File

@ -5,7 +5,7 @@
<option name="OPTION_SCOPE" value="private" />
<option name="OPEN_IN_BROWSER" value="false" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8 Oracle" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11 OpenJDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -8,6 +8,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/doc" />
<excludeFolder url="file://$MODULE_DIR$/javadoc" />
<excludeFolder url="file://$MODULE_DIR$/log" />
@ -15,5 +16,21 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="lib" level="project" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.4">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-5.4.2.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-api-5.4.2.jar!/" />
<root url="jar://$MODULE_DIR$/lib/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MODULE_DIR$/lib/opentest4j-1.1.1.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-platform-commons-1.4.2.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-params-5.4.2.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-engine-5.4.2.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-platform-engine-1.4.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@ -48,10 +48,6 @@ public class Celda extends JComponent implements Constantes {
* The type of cell
*/
private Type type = Type.SPACE;
/**
* If the cell is selected
*/
private boolean selected = false;
/**
* The sprites that can be used in the cell
*/
@ -224,46 +220,6 @@ public class Celda extends JComponent implements Constantes {
}
break;
}
// The cell is selected
if (isSelected()) {
g.setColor(Color.black);
g.drawRect(xPixels, yPixels, CELL_PIXELS - 1, CELL_PIXELS - 1);
}
}
/**
* Check if the cell is selected
*
* @return Returns true if the cell is selected
*/
public boolean isSelected() {
return selected;
}
/**
* Set the call as selected or unselected
*
* @param selected True if the cell is selected
*/
public void setSelected(boolean selected) {
this.selected = selected;
}
/**
* Called when the player uses mouse to select cell
*
* @param clickX The x coordinate
* @param clickY They y coordinate
* @return Returns true if the cell is selected
*/
public boolean selected(int clickX, int clickY) {
Rectangle rectangle = new Rectangle(xPixels, yPixels, CELL_PIXELS, CELL_PIXELS);
if (rectangle.contains(new Point(clickX, clickY))) {
selected = !selected;
return true;
}
return false;
}
/**

View File

@ -84,11 +84,11 @@ public interface Constantes {
/**
* The amount of chests to draw
*/
int CHESTS = 4;
int CHESTS = 2;
/**
* The amount of enemies to draw
*/
int ENEMIES = 4;
int ENEMIES = 2;
/**
* The player's start x position
*/

View File

@ -82,6 +82,10 @@ public class Escenario extends JComponent implements Constantes {
* The enemies
*/
private ArrayList<Celda> enemies = new ArrayList<>();
/**
* The chests
*/
private ArrayList<Celda> chests = new ArrayList<>();
/**
* Initialize the scene
@ -225,6 +229,8 @@ public class Escenario extends JComponent implements Constantes {
break;
case CHEST:
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.CHEST));
celdas[x][y].setCoords(x, y);
chests.add(celdas[x][y]);
break;
case PORTAL:
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.PORTAL));
@ -514,7 +520,7 @@ public class Escenario extends JComponent implements Constantes {
stringBuilder.insert(0, 0);
}
stringBuilder.append(".png");
animation.addImage(Animation.Direction.NONE, "/img/portal/gray/" + stringBuilder.toString());
animation.addImage(Animation.Direction.NONE, "/img/portal/green/" + stringBuilder.toString());
}
sprites.put(Animation.SpriteType.PORTAL, animation);
@ -546,19 +552,6 @@ public class Escenario extends JComponent implements Constantes {
animation.setYOffset(0);
}
/**
* This method will remove the selected attribute from all cells
*/
public void emptyEscenario() {
for (int i = 0; i < HORIZONTAL_CELLS; i++) {
for (int j = 0; j < VERTICAL_CELLS; j++) {
if (celdas[i][j].isSelected()) {
celdas[i][j].setSelected(false);
}
}
}
}
/**
* Get the cells of the game
*
@ -783,6 +776,15 @@ public class Escenario extends JComponent implements Constantes {
return enemies;
}
/**
* Get the chests
*
* @return Returns an array list containing the chests
*/
public ArrayList<Celda> getChests() {
return chests;
}
/**
* Get the parent canvas of this scene
* @return Returns the parent canvas

View File

@ -15,15 +15,16 @@
package cl.cromer.game;
import cl.cromer.game.object.Chest;
import cl.cromer.game.object.Enemy;
import cl.cromer.game.object.Portal;
import cl.cromer.game.sound.Sound;
import cl.cromer.game.sound.SoundException;
import cl.cromer.game.sprite.AnimationException;
import javax.sound.sampled.Clip;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@ -37,22 +38,6 @@ public class Lienzo extends Canvas implements Constantes {
* The game scene
*/
private Escenario escenario;
/**
* Check if the mouse button is being held down or not
*/
private boolean holdMouseButton = false;
/**
* Check if the selected cell is the player or not
*/
private boolean playerSelected = false;
/**
* The current mouse x position
*/
private int mouseX;
/**
* The current mouse y position
*/
private int mouseY;
/**
* The graphics buffer
*/
@ -95,47 +80,10 @@ public class Lienzo extends Canvas implements Constantes {
setBackground(Color.black);
setSize(escenario.width, escenario.height);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent event) {
super.mouseClicked(event);
if (event.getButton() == MouseEvent.BUTTON1) {
escenario.emptyEscenario();
setMousePosition(event);
holdMouseButton = true;
repaint();
}
}
@Override
public void mouseReleased(MouseEvent event) {
super.mouseClicked(event);
if (event.getButton() == MouseEvent.BUTTON1) {
escenario.emptyEscenario();
setMousePosition(event);
holdMouseButton = false;
activateCell(event);
repaint();
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent event) {
super.mouseDragged(event);
escenario.emptyEscenario();
setMousePosition(event);
repaint();
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent event) {
super.keyPressed(event);
escenario.emptyEscenario();
playerSelected = false;
escenario.keyPressed(event);
repaint();
}
@ -162,6 +110,11 @@ public class Lienzo extends Canvas implements Constantes {
threads.add(new Thread(enemy));
}
for (Celda celda : escenario.getChests()) {
Chest chest = new Chest(escenario, celda);
threads.add(new Thread(chest));
}
portal = new Portal(escenario);
threads.add(new Thread(portal));
@ -179,48 +132,6 @@ public class Lienzo extends Canvas implements Constantes {
}
}
/**
* Activate the cell that was clicked
*
* @param event The mouse click event
*/
private void activateCell(MouseEvent event) {
for (int i = 0; i < HORIZONTAL_CELLS; i++) {
for (int j = 0; j < VERTICAL_CELLS; j++) {
if (escenario.getCeldas()[i][j].selected(event.getX(), event.getY())) {
logger.info("Cell x: " + i + " y: " + j + " selected");
escenario.getCeldas()[i][j].setSelected(true);
if (playerSelected) {
if (escenario.getCeldas()[i][j].getType() == Celda.Type.SPACE) {
int x = escenario.getPlayer().getX();
int y = escenario.getPlayer().getY();
// Put the player in the new place
escenario.getCeldas()[i][j].setType(Celda.Type.PLAYER);
escenario.getCeldas()[i][j].setAnimation(escenario.getCeldas()[x][y].getAnimation());
escenario.getPlayer().setCoords(i, j);
playerSelected = false;
// Remove the player from previous place
escenario.getCeldas()[x][y].setType(Celda.Type.SPACE);
escenario.getCeldas()[x][y].setAnimation(null);
escenario.emptyEscenario();
break;
}
}
if (escenario.getCeldas()[i][j].getType() == Celda.Type.PLAYER) {
playerSelected = true;
}
break;
}
}
}
}
/**
* Override the paint method of Canvas to paint all the scene components
* @param g The graphics object to paint
@ -244,34 +155,12 @@ public class Lienzo extends Canvas implements Constantes {
graphicBuffer.setColor(getBackground());
graphicBuffer.fillRect(0, 0, this.getWidth(), this.getHeight());
//graphicBuffer.drawImage()
// This is needed if there is a background image
//graphicBuffer.drawImage();
escenario.paintComponent(graphicBuffer);
g.drawImage(imageBuffer, 0, 0, null);
if (holdMouseButton && playerSelected) {
try {
int x = escenario.getPlayer().getX();
int y = escenario.getPlayer().getY();
Celda celda = escenario.getCeldas()[x][y];
if (celda.getAnimation() != null) {
g.drawImage(celda.getAnimation().getNextFrame(), mouseX, mouseY, null);
}
}
catch (AnimationException e) {
logger.warning(e.getMessage());
}
}
}
/**
* Set the position of the mouse while it is being dragged
*
* @param event The event
*/
private void setMousePosition(MouseEvent event) {
mouseX = event.getX();
mouseY = event.getY();
}
/**

View File

@ -15,14 +15,44 @@
package cl.cromer.game.object;
import cl.cromer.game.Celda;
import cl.cromer.game.Escenario;
/**
* This class handles the chests
*/
public class Chest {
public class Chest implements Runnable {
/**
* The current state of the chest
*/
private State state = State.CLOSED;
/**
* The scene the chest is in
*/
private Escenario escenario;
/**
* The cell the chest is in
*/
private Celda celda;
/**
* Initialize the chest
*
* @param escenario The scene the chest is in
* @param celda The cell that contains the chest
*/
public Chest(Escenario escenario, Celda celda) {
this.escenario = escenario;
this.celda = celda;
}
/**
* This method is run when the thread starts
*/
@Override
public void run() {
}
/**
* The possible states of the chest
@ -32,6 +62,10 @@ public class Chest {
* The chest is closed
*/
CLOSED,
/**
* The chest is opening
*/
OPENING,
/**
* The chest is opened
*/

View File

@ -1,45 +0,0 @@
/*
* Copyright 2019 Chris Cromer
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package cl.cromer.game.panel;
import cl.cromer.game.Constantes;
import javax.swing.*;
import java.awt.*;
/**
* The menu panel
*/
public class Menu extends JPanel implements Constantes {
/**
* Initialize the menu panel
*
* @param ventanaPrincipal The main window
*/
public Menu(JFrame ventanaPrincipal) {
this.setName("La Aventura de Azaraka");
this.setLayout(null);
this.setSize(SCREEN_SIZE.width, SCREEN_SIZE.height);
JLabel name = new JLabel("NAME: ");
name.setFont(new Font("monospaced", Font.BOLD, 40));
name.setForeground(Color.orange);
name.setBounds(20, 20, 400, 200);
add(name);
}
}