AI optimization
Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
parent
b718d7b40a
commit
e6624b527d
@ -38,6 +38,10 @@ public interface Constantes {
|
|||||||
* Whether or not the player should be controlled by AI
|
* Whether or not the player should be controlled by AI
|
||||||
*/
|
*/
|
||||||
boolean PLAYER_AI = true;
|
boolean PLAYER_AI = true;
|
||||||
|
/**
|
||||||
|
* Move the player to the portal if attacked
|
||||||
|
*/
|
||||||
|
boolean TRANSPORT_PLAYER_ON_ATTACK = false;
|
||||||
/**
|
/**
|
||||||
* Use a global log if true or individual logs if false
|
* Use a global log if true or individual logs if false
|
||||||
*/
|
*/
|
||||||
@ -69,7 +73,7 @@ public interface Constantes {
|
|||||||
/**
|
/**
|
||||||
* The amount of chests to draw
|
* The amount of chests to draw
|
||||||
*/
|
*/
|
||||||
int CHESTS = 2;
|
int CHESTS = 5;
|
||||||
/**
|
/**
|
||||||
* The amount of enemies to draw
|
* The amount of enemies to draw
|
||||||
*/
|
*/
|
||||||
@ -207,7 +211,7 @@ public interface Constantes {
|
|||||||
/**
|
/**
|
||||||
* The global log level is used if the individual log levels are not
|
* The global log level is used if the individual log levels are not
|
||||||
*/
|
*/
|
||||||
GLOBAL(Level.ALL),
|
GLOBAL(Level.SEVERE),
|
||||||
/**
|
/**
|
||||||
* The main log level
|
* The main log level
|
||||||
*/
|
*/
|
||||||
|
@ -64,9 +64,9 @@ public class Escenario extends JComponent implements Constantes {
|
|||||||
*/
|
*/
|
||||||
private Sheet textureSheet;
|
private Sheet textureSheet;
|
||||||
/**
|
/**
|
||||||
* Whether or not the door is closed yet
|
* Whether or not the door is open
|
||||||
*/
|
*/
|
||||||
private boolean doorClosed = false;
|
private boolean doorOpen = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the scene
|
* Initialize the scene
|
||||||
@ -181,7 +181,7 @@ public class Escenario extends JComponent implements Constantes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final Lock lock = new ReentrantLock(false);
|
final Lock lock = new ReentrantLock(true);
|
||||||
for (int i = 0; i < ENEMIES; i++) {
|
for (int i = 0; i < ENEMIES; i++) {
|
||||||
random = randomCoordinates();
|
random = randomCoordinates();
|
||||||
celdas[random[0]][random[1]].setObject(new Enemy(this, celdas[random[0]][random[1]], lock));
|
celdas[random[0]][random[1]].setObject(new Enemy(this, celdas[random[0]][random[1]], lock));
|
||||||
@ -470,21 +470,21 @@ public class Escenario extends JComponent implements Constantes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if door is closed or not
|
* Check if door is open
|
||||||
*
|
*
|
||||||
* @return Returns true if closed or false if open
|
* @return Returns true if open or false if closed
|
||||||
*/
|
*/
|
||||||
public boolean isDoorClosed() {
|
public boolean isDoorOpen() {
|
||||||
return doorClosed;
|
return doorOpen;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change the state of the door
|
* Change the state of the door
|
||||||
*
|
*
|
||||||
* @param doorClosed Set to true to the close the door or false to open it
|
* @param doorOpen Set to true to open the door or false to close it
|
||||||
*/
|
*/
|
||||||
public void setDoorClosed(boolean doorClosed) {
|
public void openDoor(boolean doorOpen) {
|
||||||
if (doorClosed && !isDoorClosed()) {
|
if (!doorOpen && isDoorOpen()) {
|
||||||
celdas[2][0].setObject(new Obstacle(this, celdas[2][0]));
|
celdas[2][0].setObject(new Obstacle(this, celdas[2][0]));
|
||||||
try {
|
try {
|
||||||
celdas[2][0].addTexture(textureSheet.getTexture(193), 193);
|
celdas[2][0].addTexture(textureSheet.getTexture(193), 193);
|
||||||
@ -492,12 +492,12 @@ public class Escenario extends JComponent implements Constantes {
|
|||||||
catch (SheetException e) {
|
catch (SheetException e) {
|
||||||
logger.warning(e.getMessage());
|
logger.warning(e.getMessage());
|
||||||
}
|
}
|
||||||
this.doorClosed = true;
|
this.doorOpen = false;
|
||||||
}
|
}
|
||||||
else if (!doorClosed && isDoorClosed()) {
|
else if (doorOpen && !isDoorOpen()) {
|
||||||
celdas[2][0].removeTexture(193);
|
celdas[2][0].removeTexture(193);
|
||||||
celdas[2][0].setObject(null);
|
celdas[2][0].setObject(null);
|
||||||
this.doorClosed = false;
|
this.doorOpen = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package cl.cromer.azaraka;
|
package cl.cromer.azaraka;
|
||||||
|
|
||||||
import cl.cromer.azaraka.ai.AI;
|
import cl.cromer.azaraka.ai.AI;
|
||||||
|
import cl.cromer.azaraka.ai.State;
|
||||||
import cl.cromer.azaraka.object.Object;
|
import cl.cromer.azaraka.object.Object;
|
||||||
import cl.cromer.azaraka.object.*;
|
import cl.cromer.azaraka.object.*;
|
||||||
import cl.cromer.azaraka.sound.Sound;
|
import cl.cromer.azaraka.sound.Sound;
|
||||||
@ -28,9 +29,7 @@ import javax.swing.*;
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.KeyAdapter;
|
import java.awt.event.KeyAdapter;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -209,7 +208,7 @@ public class Lienzo extends Canvas implements Constantes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (PLAYER_AI) {
|
if (PLAYER_AI) {
|
||||||
playerAiLauncher();
|
setupPlayerAI();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
addKeyListener(new KeyAdapter() {
|
addKeyListener(new KeyAdapter() {
|
||||||
@ -226,67 +225,27 @@ public class Lienzo extends Canvas implements Constantes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Launch a new player AI task
|
* Set up the player AI
|
||||||
*/
|
*/
|
||||||
public void playerAiLauncher() {
|
public void setupPlayerAI() {
|
||||||
player.resetAi();
|
player.getAi().addDestination(new State(2, 0, State.Type.EXIT, null));
|
||||||
Thread aiThread = aiThreads.get(player.getAi());
|
|
||||||
if (aiThread != null) {
|
|
||||||
if (aiThread.isAlive()) {
|
|
||||||
aiThread.interrupt();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
aiThread.join();
|
|
||||||
}
|
|
||||||
catch (InterruptedException e) {
|
|
||||||
logger.info(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!player.hasKey()) {
|
|
||||||
for (Key key : keys) {
|
|
||||||
if (key.getState() == Key.State.UNUSED) {
|
|
||||||
player.getAi().search(player.getCelda().getX(), player.getCelda().getY(), key.getCelda().getX(), key.getCelda().getY());
|
|
||||||
player.getAi().calculateRoute();
|
|
||||||
Thread thread = new Thread(player.getAi());
|
|
||||||
thread.start();
|
|
||||||
aiThreads.put(player.getAi(), thread);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (portal.getState() == Portal.State.ACTIVE) {
|
//player.getAi().addDestination(new State(portal.getCelda().getX(), portal.getCelda().getY(), State.Type.PORTAL, null));
|
||||||
player.getAi().search(player.getCelda().getX(), player.getCelda().getY(), portal.getCelda().getX(), portal.getCelda().getY());
|
|
||||||
player.getAi().calculateRoute();
|
|
||||||
Thread thread = new Thread(player.getAi());
|
|
||||||
thread.start();
|
|
||||||
aiThreads.put(player.getAi(), thread);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Shuffle the chests so that the AI doesn't open the correct chests on the first go
|
||||||
|
Collections.shuffle(chests, new Random(23));
|
||||||
for (Chest chest : chests) {
|
for (Chest chest : chests) {
|
||||||
if (chest.getState() == Chest.State.CLOSED) {
|
player.getAi().addDestination(new State(chest.getCelda().getX(), chest.getCelda().getY() + 1, State.Type.CHEST, null));
|
||||||
player.getAi().search(player.getCelda().getX(), player.getCelda().getY(), chest.getCelda().getX(), chest.getCelda().getY() + 1);
|
|
||||||
player.getAi().calculateRoute();
|
|
||||||
player.getAi().setInteract(true);
|
|
||||||
Thread thread = new Thread(player.getAi());
|
|
||||||
thread.start();
|
|
||||||
aiThreads.put(player.getAi(), thread);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!escenario.isDoorClosed()) {
|
for (Key key : keys) {
|
||||||
if (player.getCelda().getX() == 2 && player.getCelda().getY() == 0) {
|
player.getAi().addDestination(new State(key.getCelda().getX(), key.getCelda().getY(), State.Type.KEY, null));
|
||||||
player.keyPressed(KeyEvent.VK_UP);
|
|
||||||
}
|
}
|
||||||
player.getAi().search(player.getCelda().getX(), player.getCelda().getY(), 2, 0);
|
|
||||||
player.getAi().calculateRoute();
|
|
||||||
Thread thread = new Thread(player.getAi());
|
Thread thread = new Thread(player.getAi());
|
||||||
thread.start();
|
thread.start();
|
||||||
aiThreads.put(player.getAi(), thread);
|
aiThreads.put(player.getAi(), thread);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Override the paint method of Canvas to paint all the scene components
|
* Override the paint method of Canvas to paint all the scene components
|
||||||
@ -462,6 +421,7 @@ public class Lienzo extends Canvas implements Constantes {
|
|||||||
* Called when the game is won
|
* Called when the game is won
|
||||||
*/
|
*/
|
||||||
public void win() {
|
public void win() {
|
||||||
|
stopThreads();
|
||||||
stopBackgroundMusic();
|
stopBackgroundMusic();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -472,7 +432,6 @@ public class Lienzo extends Canvas implements Constantes {
|
|||||||
logger.warning(e.getMessage());
|
logger.warning(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
stopThreads();
|
|
||||||
JOptionPane.showMessageDialog(null, "Ganaste!");
|
JOptionPane.showMessageDialog(null, "Ganaste!");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
@ -15,15 +15,45 @@
|
|||||||
|
|
||||||
package cl.cromer.azaraka.ai;
|
package cl.cromer.azaraka.ai;
|
||||||
|
|
||||||
|
import cl.cromer.azaraka.Escenario;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AI algorithms extends this class
|
* AI algorithms extends this class
|
||||||
*/
|
*/
|
||||||
public class AI implements Runnable {
|
public class AI implements Runnable {
|
||||||
|
/**
|
||||||
|
* The scene of the game
|
||||||
|
*/
|
||||||
|
private final Escenario escenario;
|
||||||
|
/**
|
||||||
|
* The logger
|
||||||
|
*/
|
||||||
|
private Logger logger;
|
||||||
/**
|
/**
|
||||||
* Whether or not the run loop of the AI is active
|
* Whether or not the run loop of the AI is active
|
||||||
*/
|
*/
|
||||||
private boolean active;
|
private boolean active;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the AI
|
||||||
|
*
|
||||||
|
* @param escenario The scene of the game
|
||||||
|
*/
|
||||||
|
protected AI(Escenario escenario) {
|
||||||
|
this.escenario = escenario;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get teh scene
|
||||||
|
*
|
||||||
|
* @return Returns the scene
|
||||||
|
*/
|
||||||
|
protected Escenario getEscenario() {
|
||||||
|
return escenario;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the active state of the AI
|
* Get the active state of the AI
|
||||||
*
|
*
|
||||||
@ -42,6 +72,24 @@ public class AI implements Runnable {
|
|||||||
this.active = active;
|
this.active = active;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the logger
|
||||||
|
*
|
||||||
|
* @return Returns a logger
|
||||||
|
*/
|
||||||
|
protected Logger getLogger() {
|
||||||
|
return logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the logger
|
||||||
|
*
|
||||||
|
* @param logger The logger to set
|
||||||
|
*/
|
||||||
|
protected void setLogger(Logger logger) {
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The run method
|
* The run method
|
||||||
*/
|
*/
|
||||||
|
30
src/main/java/cl/cromer/azaraka/ai/AIException.java
Normal file
30
src/main/java/cl/cromer/azaraka/ai/AIException.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* 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.azaraka.ai;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class handles exceptions thrown by the AI
|
||||||
|
*/
|
||||||
|
public class AIException extends Exception {
|
||||||
|
/**
|
||||||
|
* Initialize the AI exception
|
||||||
|
*
|
||||||
|
* @param errorMessage The message thrown
|
||||||
|
*/
|
||||||
|
public AIException(String errorMessage) {
|
||||||
|
super(errorMessage);
|
||||||
|
}
|
||||||
|
}
|
@ -18,22 +18,12 @@ package cl.cromer.azaraka.ai;
|
|||||||
import cl.cromer.azaraka.Constantes;
|
import cl.cromer.azaraka.Constantes;
|
||||||
import cl.cromer.azaraka.Escenario;
|
import cl.cromer.azaraka.Escenario;
|
||||||
|
|
||||||
import java.awt.event.KeyEvent;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an implementation of the Breadth-First search algorithm
|
* This is an implementation of the Breadth-First search algorithm with multiple objectives
|
||||||
*/
|
*/
|
||||||
public class BreadthFirstSearch extends AI implements Constantes {
|
public class BreadthFirstSearch extends AI implements Constantes {
|
||||||
/**
|
|
||||||
* The logger
|
|
||||||
*/
|
|
||||||
private final Logger logger;
|
|
||||||
/**
|
|
||||||
* The scene the AI needs to search
|
|
||||||
*/
|
|
||||||
private final Escenario escenario;
|
|
||||||
/**
|
/**
|
||||||
* The queued states to check
|
* The queued states to check
|
||||||
*/
|
*/
|
||||||
@ -45,52 +35,47 @@ public class BreadthFirstSearch extends AI implements Constantes {
|
|||||||
/**
|
/**
|
||||||
* The steps to get to the objective
|
* The steps to get to the objective
|
||||||
*/
|
*/
|
||||||
private final ArrayList<State.Direction> steps = new ArrayList<>();
|
private final ArrayList<State.Type> steps = new ArrayList<>();
|
||||||
/**
|
/**
|
||||||
* Which step the object is on
|
* The destinations the player should visit
|
||||||
*/
|
*/
|
||||||
private int stepIndex = 0;
|
private final ArrayList<State> destinations = new ArrayList<>();
|
||||||
/**
|
/**
|
||||||
* The state of the objective
|
* The state of the search objective
|
||||||
*/
|
*/
|
||||||
private State objective;
|
private State searchObjective;
|
||||||
/**
|
/**
|
||||||
* If the search was successful or not
|
* If the search was successful or not
|
||||||
*/
|
*/
|
||||||
private boolean success = false;
|
private boolean success = false;
|
||||||
/**
|
/**
|
||||||
* Interact with object once the objective is reached
|
* The subInitial point to start searching from
|
||||||
*/
|
*/
|
||||||
private boolean interact = false;
|
private State initial;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the algorithm
|
* Initialize the algorithm
|
||||||
*
|
*
|
||||||
* @param escenario The scene the AI is in
|
* @param escenario The scene the AI is in
|
||||||
*/
|
*/
|
||||||
public BreadthFirstSearch(Escenario escenario) {
|
protected BreadthFirstSearch(Escenario escenario) {
|
||||||
this.escenario = escenario;
|
super(escenario);
|
||||||
logger = getLogger(this.getClass(), LogLevel.AI);
|
setLogger(getLogger(this.getClass(), LogLevel.AI));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for a path that gets from the start point to the end point
|
* Find a path to the objective
|
||||||
*
|
*
|
||||||
* @param startX The start x coordinate
|
* @param searchInitial The start point
|
||||||
* @param startY The start y coordinate
|
* @param searchObjective The objective
|
||||||
* @param endX The end x coordinate
|
* @return Returns true if a path was found or false otherwise
|
||||||
* @param endY The end y coordinate
|
|
||||||
*/
|
*/
|
||||||
public void search(int startX, int startY, int endX, int endY) {
|
private boolean search(State searchInitial, State searchObjective) {
|
||||||
State initial = new State(startX, startY, State.Direction.START, null);
|
queuedStates.add(searchInitial);
|
||||||
objective = new State(endX, endY, State.Direction.END, null);
|
history.add(searchInitial);
|
||||||
|
this.searchObjective = searchObjective;
|
||||||
|
|
||||||
queuedStates.add(initial);
|
success = searchInitial.equals(searchObjective);
|
||||||
history.add(initial);
|
|
||||||
|
|
||||||
if (initial.equals(objective)) {
|
|
||||||
success = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!queuedStates.isEmpty() && !success) {
|
while (!queuedStates.isEmpty() && !success) {
|
||||||
State temp = queuedStates.get(0);
|
State temp = queuedStates.get(0);
|
||||||
@ -103,10 +88,13 @@ public class BreadthFirstSearch extends AI implements Constantes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
logger.info("Route to objective found!");
|
getLogger().info("Route to objective found!");
|
||||||
|
calculateRoute();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
logger.info("Route to objective not possible!");
|
getLogger().info("Route to objective not found!");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,14 +105,14 @@ public class BreadthFirstSearch extends AI implements Constantes {
|
|||||||
*/
|
*/
|
||||||
private void moveUp(State state) {
|
private void moveUp(State state) {
|
||||||
if (state.getY() > 0) {
|
if (state.getY() > 0) {
|
||||||
if (escenario.getCeldas()[state.getX()][state.getY() - 1].getObject() == null) {
|
if (getEscenario().getCeldas()[state.getX()][state.getY() - 1].getObject() == null) {
|
||||||
State up = new State(state.getX(), state.getY() - 1, State.Direction.UP, state);
|
State up = new State(state.getX(), state.getY() - 1, State.Type.UP, state);
|
||||||
if (!history.contains(up)) {
|
if (!history.contains(up)) {
|
||||||
queuedStates.add(up);
|
queuedStates.add(up);
|
||||||
history.add(up);
|
history.add(up);
|
||||||
|
|
||||||
if (up.equals(objective)) {
|
if (up.equals(searchObjective)) {
|
||||||
objective = up;
|
searchObjective = up;
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,14 +127,14 @@ public class BreadthFirstSearch extends AI implements Constantes {
|
|||||||
*/
|
*/
|
||||||
private void moveDown(State state) {
|
private void moveDown(State state) {
|
||||||
if (state.getY() < VERTICAL_CELLS - 1) {
|
if (state.getY() < VERTICAL_CELLS - 1) {
|
||||||
if (escenario.getCeldas()[state.getX()][state.getY() + 1].getObject() == null) {
|
if (getEscenario().getCeldas()[state.getX()][state.getY() + 1].getObject() == null) {
|
||||||
State down = new State(state.getX(), state.getY() + 1, State.Direction.DOWN, state);
|
State down = new State(state.getX(), state.getY() + 1, State.Type.DOWN, state);
|
||||||
if (!history.contains(down)) {
|
if (!history.contains(down)) {
|
||||||
queuedStates.add(down);
|
queuedStates.add(down);
|
||||||
history.add(down);
|
history.add(down);
|
||||||
|
|
||||||
if (down.equals(objective)) {
|
if (down.equals(searchObjective)) {
|
||||||
objective = down;
|
searchObjective = down;
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -161,14 +149,14 @@ public class BreadthFirstSearch extends AI implements Constantes {
|
|||||||
*/
|
*/
|
||||||
private void moveLeft(State state) {
|
private void moveLeft(State state) {
|
||||||
if (state.getX() > 0) {
|
if (state.getX() > 0) {
|
||||||
if (escenario.getCeldas()[state.getX() - 1][state.getY()].getObject() == null) {
|
if (getEscenario().getCeldas()[state.getX() - 1][state.getY()].getObject() == null) {
|
||||||
State left = new State(state.getX() - 1, state.getY(), State.Direction.LEFT, state);
|
State left = new State(state.getX() - 1, state.getY(), State.Type.LEFT, state);
|
||||||
if (!history.contains(left)) {
|
if (!history.contains(left)) {
|
||||||
queuedStates.add(left);
|
queuedStates.add(left);
|
||||||
history.add(left);
|
history.add(left);
|
||||||
|
|
||||||
if (left.equals(objective)) {
|
if (left.equals(searchObjective)) {
|
||||||
objective = left;
|
searchObjective = left;
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -183,14 +171,14 @@ public class BreadthFirstSearch extends AI implements Constantes {
|
|||||||
*/
|
*/
|
||||||
private void moveRight(State state) {
|
private void moveRight(State state) {
|
||||||
if (state.getX() < HORIZONTAL_CELLS - 1) {
|
if (state.getX() < HORIZONTAL_CELLS - 1) {
|
||||||
if (escenario.getCeldas()[state.getX() + 1][state.getY()].getObject() == null) {
|
if (getEscenario().getCeldas()[state.getX() + 1][state.getY()].getObject() == null) {
|
||||||
State right = new State(state.getX() + 1, state.getY(), State.Direction.RIGHT, state);
|
State right = new State(state.getX() + 1, state.getY(), State.Type.RIGHT, state);
|
||||||
if (!history.contains(right)) {
|
if (!history.contains(right)) {
|
||||||
queuedStates.add(right);
|
queuedStates.add(right);
|
||||||
history.add(right);
|
history.add(right);
|
||||||
|
|
||||||
if (right.equals(objective)) {
|
if (right.equals(searchObjective)) {
|
||||||
objective = right;
|
searchObjective = right;
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -201,24 +189,89 @@ public class BreadthFirstSearch extends AI implements Constantes {
|
|||||||
/**
|
/**
|
||||||
* Calculate the route to the object
|
* Calculate the route to the object
|
||||||
*/
|
*/
|
||||||
public void calculateRoute() {
|
private void calculateRoute() {
|
||||||
logger.info("Calculate the route!");
|
getLogger().info("Calculate the route!");
|
||||||
State predecessor = objective;
|
State predecessor = searchObjective;
|
||||||
do {
|
do {
|
||||||
steps.add(predecessor.getOperation());
|
steps.add(0, predecessor.getOperation());
|
||||||
predecessor = predecessor.getPredecessor();
|
predecessor = predecessor.getPredecessor();
|
||||||
}
|
}
|
||||||
while (predecessor != null);
|
while (predecessor != null);
|
||||||
stepIndex = steps.size() - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set whether or not the object should interact when it arrives at the destination
|
* Add a destination to the AI
|
||||||
*
|
*
|
||||||
* @param interact Set to true to interact or false otherwise
|
* @param state The new state to add
|
||||||
*/
|
*/
|
||||||
public void setInteract(boolean interact) {
|
public void addDestination(State state) {
|
||||||
this.interact = interact;
|
destinations.add(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a priority destination to the AI
|
||||||
|
*
|
||||||
|
* @param state The new state to add
|
||||||
|
*/
|
||||||
|
protected void addPriorityDestination(State state) {
|
||||||
|
destinations.add(0, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called when the player arrives at a destination
|
||||||
|
*
|
||||||
|
* @param subObjective The objective the player arrived at
|
||||||
|
*/
|
||||||
|
protected void destinationArrived(State subObjective) {
|
||||||
|
destinations.remove(subObjective);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the condition is true go to the objective
|
||||||
|
*
|
||||||
|
* @param subObjective The objective to check
|
||||||
|
* @return Returns true or false based on whether the objective can be obtained
|
||||||
|
*/
|
||||||
|
protected boolean checkCondition(State subObjective) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the steps needed to arrive at the objective
|
||||||
|
*
|
||||||
|
* @return Returns an array of steps
|
||||||
|
*/
|
||||||
|
protected ArrayList<State.Type> getSteps() {
|
||||||
|
return steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The child class should call this to set a new initial point
|
||||||
|
*
|
||||||
|
* @param initial The new state to start from
|
||||||
|
*/
|
||||||
|
protected void setInitial(State initial) {
|
||||||
|
this.initial = initial;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The child class should override this to trigger a new initial state
|
||||||
|
*
|
||||||
|
* @throws AIException Thrown if the method is called via super
|
||||||
|
*/
|
||||||
|
protected void getNewInitial() throws AIException {
|
||||||
|
String methodName = new Throwable().getStackTrace()[0].getMethodName();
|
||||||
|
throw new AIException("Do not call " + methodName + "using super!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The child class should override this to do actions
|
||||||
|
*
|
||||||
|
* @throws AIException Thrown if the method is called via super
|
||||||
|
*/
|
||||||
|
protected void doAction() throws AIException {
|
||||||
|
String methodName = new Throwable().getStackTrace()[0].getMethodName();
|
||||||
|
throw new AIException("Do not call " + methodName + "using super!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -228,51 +281,68 @@ public class BreadthFirstSearch extends AI implements Constantes {
|
|||||||
public void run() {
|
public void run() {
|
||||||
super.run();
|
super.run();
|
||||||
while (getActive()) {
|
while (getActive()) {
|
||||||
if (stepIndex >= 0) {
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(500);
|
Thread.sleep(500);
|
||||||
}
|
}
|
||||||
catch (InterruptedException e) {
|
catch (InterruptedException e) {
|
||||||
logger.info(e.getMessage());
|
getLogger().info(e.getMessage());
|
||||||
}
|
}
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
boolean moved = false;
|
queuedStates.clear();
|
||||||
if (steps.size() - 1 >= stepIndex && stepIndex >= 0) {
|
history.clear();
|
||||||
switch (steps.get(stepIndex)) {
|
steps.clear();
|
||||||
case UP:
|
|
||||||
moved = escenario.getCanvas().getPlayer().keyPressed(KeyEvent.VK_UP);
|
|
||||||
break;
|
|
||||||
case DOWN:
|
|
||||||
moved = escenario.getCanvas().getPlayer().keyPressed(KeyEvent.VK_DOWN);
|
|
||||||
break;
|
|
||||||
case LEFT:
|
|
||||||
moved = escenario.getCanvas().getPlayer().keyPressed(KeyEvent.VK_LEFT);
|
|
||||||
break;
|
|
||||||
case RIGHT:
|
|
||||||
moved = escenario.getCanvas().getPlayer().keyPressed(KeyEvent.VK_RIGHT);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
stepIndex--;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
escenario.getCanvas().repaint();
|
State objective;
|
||||||
if (moved) {
|
boolean found;
|
||||||
stepIndex--;
|
int destinationIndex = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
try {
|
||||||
|
getNewInitial();
|
||||||
}
|
}
|
||||||
|
catch (AIException e) {
|
||||||
|
getLogger().warning(e.getMessage());
|
||||||
}
|
}
|
||||||
|
objective = destinations.get(destinationIndex);
|
||||||
|
|
||||||
|
if (checkCondition(objective)) {
|
||||||
|
found = search(initial, objective);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
setActive(false);
|
found = false;
|
||||||
|
}
|
||||||
if (interact) {
|
|
||||||
escenario.getCanvas().getPlayer().keyPressed(KeyEvent.VK_UP);
|
if (initial.equals(objective)) {
|
||||||
escenario.getCanvas().getPlayer().interact();
|
destinationArrived(objective);
|
||||||
escenario.getCanvas().repaint();
|
destinationIndex = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!found) {
|
||||||
|
queuedStates.clear();
|
||||||
|
history.clear();
|
||||||
|
steps.clear();
|
||||||
|
// Don't run this because the destination might return to be available again at some point
|
||||||
|
//destinationArrived(subObjective);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (destinations.isEmpty()) {
|
||||||
|
getLogger().info("No more destinations!");
|
||||||
|
setActive(false);
|
||||||
|
}
|
||||||
|
destinationIndex++;
|
||||||
|
if (destinationIndex >= destinations.size()) {
|
||||||
|
destinationIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (!found && !destinations.isEmpty());
|
||||||
|
|
||||||
|
try {
|
||||||
|
doAction();
|
||||||
|
}
|
||||||
|
catch (AIException e) {
|
||||||
|
getLogger().warning(e.getMessage());
|
||||||
}
|
}
|
||||||
// Launch the next objective
|
|
||||||
escenario.getCanvas().playerAiLauncher();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
143
src/main/java/cl/cromer/azaraka/ai/PlayerAI.java
Normal file
143
src/main/java/cl/cromer/azaraka/ai/PlayerAI.java
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
/*
|
||||||
|
* 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.azaraka.ai;
|
||||||
|
|
||||||
|
import cl.cromer.azaraka.Escenario;
|
||||||
|
import cl.cromer.azaraka.object.Player;
|
||||||
|
import cl.cromer.azaraka.object.Portal;
|
||||||
|
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class handles player based interactions mixed with AI
|
||||||
|
*/
|
||||||
|
public class PlayerAI extends BreadthFirstSearch {
|
||||||
|
/**
|
||||||
|
* The player
|
||||||
|
*/
|
||||||
|
private final Player player;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the algorithm
|
||||||
|
*
|
||||||
|
* @param escenario The scene the AI is in
|
||||||
|
* @param player The player controlled by the AI
|
||||||
|
*/
|
||||||
|
public PlayerAI(Escenario escenario, Player player) {
|
||||||
|
super(escenario);
|
||||||
|
this.player = player;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This handles what to do when the player arrives at a destination
|
||||||
|
*
|
||||||
|
* @param objective The objective the player arrived at
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void destinationArrived(State objective) {
|
||||||
|
switch (objective.getOperation()) {
|
||||||
|
case CHEST:
|
||||||
|
if (player.hasKey()) {
|
||||||
|
player.keyPressed(KeyEvent.VK_UP);
|
||||||
|
player.interact();
|
||||||
|
Portal portal = getEscenario().getCanvas().getPortal();
|
||||||
|
if (portal.getState() == Portal.State.ACTIVE) {
|
||||||
|
addPriorityDestination(new State(portal.getCelda().getX(), portal.getCelda().getY(), State.Type.PORTAL, null));
|
||||||
|
}
|
||||||
|
// Only call parent method if player opened a chest
|
||||||
|
super.destinationArrived(objective);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case EXIT:
|
||||||
|
super.destinationArrived(objective);
|
||||||
|
player.keyPressed(KeyEvent.VK_UP);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
super.destinationArrived(objective);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check conditions to to make sure that the AI doesn't go after unobtainable objectives
|
||||||
|
*
|
||||||
|
* @param objective The objective to check
|
||||||
|
* @return Returns true if the objective is obtainable
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean checkCondition(State objective) {
|
||||||
|
super.checkCondition(objective);
|
||||||
|
switch (objective.getOperation()) {
|
||||||
|
case KEY:
|
||||||
|
// If the player doesn't have the gems yet, get keys
|
||||||
|
if (player.getGemCount() < 2) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CHEST:
|
||||||
|
// If the player has a key and doesn't have both gems yet
|
||||||
|
if (player.hasKey() && player.getGemCount() < 2) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PORTAL:
|
||||||
|
// If the portal is active head towards it
|
||||||
|
if (getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case EXIT:
|
||||||
|
if (getEscenario().isDoorOpen()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle actions based on the states
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doAction() {
|
||||||
|
if (getSteps().size() > 1) {
|
||||||
|
switch (getSteps().get(1)) {
|
||||||
|
case UP:
|
||||||
|
player.keyPressed(KeyEvent.VK_UP);
|
||||||
|
break;
|
||||||
|
case DOWN:
|
||||||
|
player.keyPressed(KeyEvent.VK_DOWN);
|
||||||
|
break;
|
||||||
|
case LEFT:
|
||||||
|
player.keyPressed(KeyEvent.VK_LEFT);
|
||||||
|
break;
|
||||||
|
case RIGHT:
|
||||||
|
player.keyPressed(KeyEvent.VK_RIGHT);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getEscenario().getCanvas().repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called when the algorithm wants to know where the player is located at now
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void getNewInitial() {
|
||||||
|
setInitial(new State(player.getCelda().getX(), player.getCelda().getY(), State.Type.START, null));
|
||||||
|
}
|
||||||
|
}
|
@ -30,7 +30,7 @@ public class State {
|
|||||||
/**
|
/**
|
||||||
* The direction to move
|
* The direction to move
|
||||||
*/
|
*/
|
||||||
private final Direction operation;
|
private final Type operation;
|
||||||
/**
|
/**
|
||||||
* The previous step
|
* The previous step
|
||||||
*/
|
*/
|
||||||
@ -44,7 +44,7 @@ public class State {
|
|||||||
* @param operation The operation to perform
|
* @param operation The operation to perform
|
||||||
* @param predecessor The previous state
|
* @param predecessor The previous state
|
||||||
*/
|
*/
|
||||||
public State(int x, int y, Direction operation, State predecessor) {
|
public State(int x, int y, Type operation, State predecessor) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
@ -74,7 +74,7 @@ public class State {
|
|||||||
*
|
*
|
||||||
* @return The operation to perform
|
* @return The operation to perform
|
||||||
*/
|
*/
|
||||||
public Direction getOperation() {
|
public Type getOperation() {
|
||||||
return operation;
|
return operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,17 +108,29 @@ public class State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The direction to move in and the start and end positions
|
* The type of operation
|
||||||
*/
|
*/
|
||||||
public enum Direction {
|
public enum Type {
|
||||||
/**
|
/**
|
||||||
* Where to start the search
|
* Where to start the search
|
||||||
*/
|
*/
|
||||||
START,
|
START,
|
||||||
/**
|
/**
|
||||||
* Where to end the search
|
* Arrive at the key
|
||||||
*/
|
*/
|
||||||
END,
|
KEY,
|
||||||
|
/**
|
||||||
|
* Arrive at the chest
|
||||||
|
*/
|
||||||
|
CHEST,
|
||||||
|
/**
|
||||||
|
* Arrive at the portal
|
||||||
|
*/
|
||||||
|
PORTAL,
|
||||||
|
/**
|
||||||
|
* Arrive at the exit
|
||||||
|
*/
|
||||||
|
EXIT,
|
||||||
/**
|
/**
|
||||||
* Move up
|
* Move up
|
||||||
*/
|
*/
|
||||||
|
@ -187,6 +187,7 @@ public class Chest extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (state == State.OPENED) {
|
if (state == State.OPENED) {
|
||||||
|
if (gem != null) {
|
||||||
if (gemLoops > 0) {
|
if (gemLoops > 0) {
|
||||||
gemLoops--;
|
gemLoops--;
|
||||||
}
|
}
|
||||||
@ -200,6 +201,7 @@ public class Chest extends Object implements Constantes {
|
|||||||
gemLoops--;
|
gemLoops--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (state == State.OPENING) {
|
else if (state == State.OPENING) {
|
||||||
animate();
|
animate();
|
||||||
getEscenario().getCanvas().repaint();
|
getEscenario().getCanvas().repaint();
|
||||||
|
@ -239,6 +239,7 @@ public class Enemy extends Object implements Constantes {
|
|||||||
catch (SheetException e) {
|
catch (SheetException e) {
|
||||||
getLogger().warning(e.getMessage());
|
getLogger().warning(e.getMessage());
|
||||||
}
|
}
|
||||||
|
getEscenario().getCanvas().getPlayer().attacked();
|
||||||
|
|
||||||
if (direction == Direction.UP) {
|
if (direction == Direction.UP) {
|
||||||
getAnimation().setCurrentDirection(Animation.Direction.LEFT);
|
getAnimation().setCurrentDirection(Animation.Direction.LEFT);
|
||||||
|
@ -18,7 +18,7 @@ package cl.cromer.azaraka.object;
|
|||||||
import cl.cromer.azaraka.Celda;
|
import cl.cromer.azaraka.Celda;
|
||||||
import cl.cromer.azaraka.Constantes;
|
import cl.cromer.azaraka.Constantes;
|
||||||
import cl.cromer.azaraka.Escenario;
|
import cl.cromer.azaraka.Escenario;
|
||||||
import cl.cromer.azaraka.ai.BreadthFirstSearch;
|
import cl.cromer.azaraka.ai.PlayerAI;
|
||||||
import cl.cromer.azaraka.sprite.Animation;
|
import cl.cromer.azaraka.sprite.Animation;
|
||||||
import cl.cromer.azaraka.sprite.AnimationException;
|
import cl.cromer.azaraka.sprite.AnimationException;
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ public class Player extends Object implements Constantes {
|
|||||||
/**
|
/**
|
||||||
* The artificial intelligence of the player
|
* The artificial intelligence of the player
|
||||||
*/
|
*/
|
||||||
private BreadthFirstSearch ai;
|
private final PlayerAI ai;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the player
|
* Initialize the player
|
||||||
@ -56,6 +56,7 @@ public class Player extends Object implements Constantes {
|
|||||||
super(escenario, celda);
|
super(escenario, celda);
|
||||||
setLogger(getLogger(this.getClass(), LogLevel.PLAYER));
|
setLogger(getLogger(this.getClass(), LogLevel.PLAYER));
|
||||||
loadPlayerAnimation();
|
loadPlayerAnimation();
|
||||||
|
ai = new PlayerAI(escenario, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,44 +79,44 @@ public class Player extends Object implements Constantes {
|
|||||||
* Handle keys being pressed in the game
|
* Handle keys being pressed in the game
|
||||||
*
|
*
|
||||||
* @param keyCode The key code to handle
|
* @param keyCode The key code to handle
|
||||||
* @return Returns true if player moved
|
|
||||||
*/
|
*/
|
||||||
public boolean keyPressed(int keyCode) {
|
public void keyPressed(int keyCode) {
|
||||||
if (!getEscenario().isDoorClosed()) {
|
if (getEscenario().isDoorOpen()) {
|
||||||
ArrayList<Gem> gems = getInventoryGems();
|
ArrayList<Gem> gems = getInventoryGems();
|
||||||
if (gems.size() < 2) {
|
if (gems.size() < 2) {
|
||||||
getEscenario().setDoorClosed(true);
|
getEscenario().openDoor(false);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (Gem gem : gems) {
|
for (Gem gem : gems) {
|
||||||
if (gem.getState() == Gem.State.TAINTED) {
|
if (gem.getState() == Gem.State.TAINTED) {
|
||||||
getEscenario().setDoorClosed(true);
|
getEscenario().openDoor(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (keyCode) {
|
switch (keyCode) {
|
||||||
case KeyEvent.VK_UP:
|
case KeyEvent.VK_UP:
|
||||||
return moveUp();
|
moveUp();
|
||||||
|
break;
|
||||||
case KeyEvent.VK_DOWN:
|
case KeyEvent.VK_DOWN:
|
||||||
return moveDown();
|
moveDown();
|
||||||
|
break;
|
||||||
case KeyEvent.VK_LEFT:
|
case KeyEvent.VK_LEFT:
|
||||||
return moveLeft();
|
moveLeft();
|
||||||
|
break;
|
||||||
case KeyEvent.VK_RIGHT:
|
case KeyEvent.VK_RIGHT:
|
||||||
return moveRight();
|
moveRight();
|
||||||
|
break;
|
||||||
case KeyEvent.VK_SPACE:
|
case KeyEvent.VK_SPACE:
|
||||||
interact();
|
interact();
|
||||||
return false;
|
break;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the player up
|
* Move the player up
|
||||||
*
|
|
||||||
* @return Returns true if player moved
|
|
||||||
*/
|
*/
|
||||||
private boolean moveUp() {
|
private void moveUp() {
|
||||||
int x = getX();
|
int x = getX();
|
||||||
int y = getY();
|
int y = getY();
|
||||||
getLogger().info("Up key pressed");
|
getLogger().info("Up key pressed");
|
||||||
@ -153,7 +154,6 @@ public class Player extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setY(getY() - 1);
|
setY(getY() - 1);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (changeDirection(Animation.Direction.UP)) {
|
if (changeDirection(Animation.Direction.UP)) {
|
||||||
@ -176,15 +176,12 @@ public class Player extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the player down
|
* Move the player down
|
||||||
*
|
|
||||||
* @return Returns true if player moved
|
|
||||||
*/
|
*/
|
||||||
private boolean moveDown() {
|
private void moveDown() {
|
||||||
int x = getX();
|
int x = getX();
|
||||||
int y = getY();
|
int y = getY();
|
||||||
getLogger().info("Down key pressed");
|
getLogger().info("Down key pressed");
|
||||||
@ -219,7 +216,6 @@ public class Player extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setY(getY() + 1);
|
setY(getY() + 1);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (changeDirection(Animation.Direction.DOWN)) {
|
if (changeDirection(Animation.Direction.DOWN)) {
|
||||||
@ -242,15 +238,12 @@ public class Player extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the player to the left
|
* Move the player to the left
|
||||||
*
|
|
||||||
* @return Returns true if player moved
|
|
||||||
*/
|
*/
|
||||||
private boolean moveLeft() {
|
private void moveLeft() {
|
||||||
int x = getX();
|
int x = getX();
|
||||||
int y = getY();
|
int y = getY();
|
||||||
getLogger().info("Left key pressed");
|
getLogger().info("Left key pressed");
|
||||||
@ -285,7 +278,6 @@ public class Player extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setX(getX() - 1);
|
setX(getX() - 1);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (changeDirection(Animation.Direction.LEFT)) {
|
if (changeDirection(Animation.Direction.LEFT)) {
|
||||||
@ -308,15 +300,12 @@ public class Player extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the player to the right
|
* Move the player to the right
|
||||||
*
|
|
||||||
* @return Returns true if player moved
|
|
||||||
*/
|
*/
|
||||||
private boolean moveRight() {
|
private void moveRight() {
|
||||||
int x = getX();
|
int x = getX();
|
||||||
int y = getY();
|
int y = getY();
|
||||||
getLogger().info("Right key pressed");
|
getLogger().info("Right key pressed");
|
||||||
@ -351,7 +340,6 @@ public class Player extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setX(getX() + 1);
|
setX(getX() + 1);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (changeDirection(Animation.Direction.RIGHT)) {
|
if (changeDirection(Animation.Direction.RIGHT)) {
|
||||||
@ -374,7 +362,6 @@ public class Player extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -427,9 +414,11 @@ public class Player extends Object implements Constantes {
|
|||||||
if (chest.getState() == Chest.State.CLOSED) {
|
if (chest.getState() == Chest.State.CLOSED) {
|
||||||
chest.setState(Chest.State.OPENING);
|
chest.setState(Chest.State.OPENING);
|
||||||
Gem gem = chest.getGem();
|
Gem gem = chest.getGem();
|
||||||
|
if (gem != null) {
|
||||||
gem.getCelda().setObjectOnTop(gem);
|
gem.getCelda().setObjectOnTop(gem);
|
||||||
useKey();
|
|
||||||
getEscenario().getCanvas().getPortal().setState(Portal.State.ACTIVE);
|
getEscenario().getCanvas().getPortal().setState(Portal.State.ACTIVE);
|
||||||
|
}
|
||||||
|
useKey();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -453,6 +442,21 @@ public class Player extends Object implements Constantes {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of gems the player has
|
||||||
|
*
|
||||||
|
* @return Returns the number of gems the player has
|
||||||
|
*/
|
||||||
|
public int getGemCount() {
|
||||||
|
int count = 0;
|
||||||
|
for (Object object : carrying) {
|
||||||
|
if (object instanceof Gem) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a key from the player inventory
|
* Removes a key from the player inventory
|
||||||
*/
|
*/
|
||||||
@ -467,6 +471,26 @@ public class Player extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is called when the player gets attacked
|
||||||
|
*/
|
||||||
|
public void attacked() {
|
||||||
|
if (TRANSPORT_PLAYER_ON_ATTACK) {
|
||||||
|
if (PLAYER_AI) {
|
||||||
|
ai.setActive(false);
|
||||||
|
}
|
||||||
|
getCelda().setObject(null);
|
||||||
|
getAnimation().setCurrentDirection(Animation.Direction.DOWN);
|
||||||
|
setCelda(getEscenario().getCanvas().getPortal().getCelda());
|
||||||
|
getCelda().setObject(this);
|
||||||
|
setX(getCelda().getX());
|
||||||
|
setY(getCelda().getY());
|
||||||
|
if (PLAYER_AI) {
|
||||||
|
getEscenario().getCanvas().setupPlayerAI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current health of the player
|
* Get the current health of the player
|
||||||
*
|
*
|
||||||
@ -490,17 +514,10 @@ public class Player extends Object implements Constantes {
|
|||||||
*
|
*
|
||||||
* @return Returns the current AI in use
|
* @return Returns the current AI in use
|
||||||
*/
|
*/
|
||||||
public BreadthFirstSearch getAi() {
|
public PlayerAI getAi() {
|
||||||
return ai;
|
return ai;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Reset the AI state to start looking for a new objective
|
|
||||||
*/
|
|
||||||
public void resetAi() {
|
|
||||||
ai = new BreadthFirstSearch(getEscenario());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the gems the player has
|
* Get the gems the player has
|
||||||
*
|
*
|
||||||
|
@ -95,7 +95,7 @@ public class Portal extends Object implements Constantes {
|
|||||||
}
|
}
|
||||||
setState(State.INACTIVE);
|
setState(State.INACTIVE);
|
||||||
if (gems.size() == 2) {
|
if (gems.size() == 2) {
|
||||||
getEscenario().setDoorClosed(false);
|
getEscenario().openDoor(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user