📄 gamelisteners.java
字号:
package fr.umlv.fourmIR2000;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import fr.umlv.fourmIR2000.frame.GameFrame;
import fr.umlv.fourmIR2000.frame.SelectBuilder;
import fr.umlv.fourmIR2000.insect.Insect;
import fr.umlv.fourmIR2000.insect.Rank;
import fr.umlv.fourmIR2000.insect.Team;
import fr.umlv.fourmIR2000.insect.Insect.Order;
import fr.umlv.fourmIR2000.pictures.Values;
import fr.umlv.fourmIR2000.world.Floor;
import fr.umlv.fourmIR2000.world.World;
import fr.umlv.fourmIR2000.world.WorldPoint;
import fr.umlv.lawrence.InputListener;
import fr.umlv.lawrence.Key;
/**
* Listeners of the game.
* This class dialog make the interface between the game and the graphical components
*
* >> The visibility MUST BE 'PACKAGE', not 'PUBLIC' !!!
*/
final class GameListeners {
/** Main listeners : level editor or game ? */
enum MainLst {
LST_Level, LST_Game
}
/** Mouse button used for selections */
private final static int BTN_MOUSE_SELECT = MouseEvent.BUTTON1;
/** Mouse button used for movements */
private final static int BTN_MOUSE_MOVE = MouseEvent.BUTTON3;
/** Game to work with */
private final Game game;
/** Graphical frame we will dialog with */
private final GameFrame gameFrame;
/** InputListener for the level editor */
private InputListener inputEditor;
private MouseAdapter gridPaneListener;
/** For the creation of the map : current background element to use */
private Values curBackElem;
/** For the creation of the map : current element to use */
private Values curElem;
/**
* Create a new GameListener : this object dialog with the graphical window and assign / unassign listeners
* @param game the game object who launched us
* @param gameFrame the frame to dialog with
*/
GameListeners(Game game, GameFrame gameFrame) {
this.game = game;
this.gameFrame = gameFrame;
inputEditor = null;
gridPaneListener = null;
curBackElem = Values.grass;
curElem = null;
}
/**
* Part 0 of the game : initialization of all the listeners of the frame
* >> We are ready to build levels
*/
void initializeListeners() {
// Exit menu
gameFrame.addActionListener(GameFrame.MnuControls.MNU_FileQuit, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// New level
gameFrame.addActionListener(GameFrame.MnuControls.MNU_FileLevelNew, new ActionListener() {
public void actionPerformed(ActionEvent e) {
game.defineNewWorld(Game.typeWorld.plain);
}
});
// About
gameFrame.addActionListener(GameFrame.MnuControls.MNU_About, new ActionListener() {
public void actionPerformed(ActionEvent e) {
game.setGamePaused(true);
gameFrame.showAbout();
game.setGamePaused(false);
}
});
/* worlds pre-builds */
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_MapPlain, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
if (game.confirmLostModifications())
game.defineNewWorld(Game.typeWorld.plain);
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_MapMarsh, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
if (game.confirmLostModifications())
game.defineNewWorld(Game.typeWorld.marsh);
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_MapMountain, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
if (game.confirmLostModifications())
game.defineNewWorld(Game.typeWorld.mountain);
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_MapDesert, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
if (game.confirmLostModifications())
game.defineNewWorld(Game.typeWorld.desert);
}
});
/* Random world (values = user select) */
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_MapUserSelect, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
if (game.confirmLostModifications()) {
int prctDesert = gameFrame.getQtyOfSand();
int prctWater = gameFrame.getQtyOfWater();
int prctRocks = gameFrame.getQtyOfRocks();
int prctFood = gameFrame.getQtyOfFood();
game.defineNewWorld(prctDesert, prctWater, prctRocks, prctFood);
}
}
});
/* Modification 'by hand' of the world */
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_Grass, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
curBackElem = Values.grass;
curElem = null;
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_Desert, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
curBackElem = Values.desert;
curElem = null;
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_Water, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
curBackElem = Values.water;
curElem = null;
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_RemoveTop, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
curBackElem = null;
curElem = null;
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_Rock, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
curBackElem = null;
curElem = Values.rock;
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_Food, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
curBackElem = null;
curElem = Values.food;
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_AntHill, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
curBackElem = null;
curElem = Values.antHill;
}
});
gameFrame.addButtonListener(GameFrame.BtnControls.BTN_AntHillEn, new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
curBackElem = null;
curElem = Values.antHill_enemy;
}
});
// Listener for the level editor
inputEditor = new InputListener() {
public void keyTyped(int x, int y, Key key) {/* NOTHING */}
public void mouseClicked(int x, int y, int button) {
if (game.getWorld().isPositionAvailable(x, y)) {
game.setLevelModified(true);
World world = game.getWorld();
WorldPoint p = world.getPoint(x, y);
WorldPoint antHill = game.getAntHill();
ArrayList<WorldPoint> antHillOpp = game.getAntHillOpp();
// If we have already an antHill in our color, we can't add
// another one. So we remove the last before !
if (curElem == Values.antHill) {
if (antHill != null) {
world.setElement(antHill.getX(), antHill.getY(), Values.grass, true);
game.setAntHill(null);
}
}
// If on this position there is an antHill, we remove it from the list
if (world.getElement(x, y) == Values.antHill) {
world.setElement(x, y, null, true);
game.setAntHill(null);
} else if (world.getElement(x, y) == Values.antHill_enemy) {
world.setElement(x, y, null, true);
antHillOpp.remove(p);
}
// If we add an antHill, we add it to the arrays too
if (curElem == Values.antHill)
game.setAntHill(p);
else if (curElem == Values.antHill_enemy)
antHillOpp.add(p);
// Anf finally we update the world with the new element
Values foreVal;
Values backVal;
if (curBackElem != null) {
backVal = curBackElem;
foreVal = world.getElement(x, y);
} else {
backVal = world.getBackgroundElement(x, y);
foreVal = curElem;
}
world.setElement(new Floor(foreVal, backVal, x, y), true);
}
}
};
// Listener for the game
gridPaneListener = new MouseAdapter() {
private WorldPoint pos1;
private WorldPoint pos2;
private WorldPoint getCoords(MouseEvent e) {
// Variables
WorldPoint p = gameFrame.getTilesSize();
int x = e.getX() / p.getX();
int y = e.getY() / p.getY();
return game.getWorld().getPoint(x, y);
}
// When the mouse button is pressed (but not already released)
@Override public void mousePressed(MouseEvent e) {
if (e.getButton() == BTN_MOUSE_SELECT)
pos1 = getCoords(e);
}
// When the mouse button is released
@Override public void mouseReleased(MouseEvent e) {
pos2 = getCoords(e);
if (pos1 == null) pos1 = pos2; // Avoid error when the mouse was pressed 'out' of the game and released 'in'
switch (e.getButton())
{
// Selection button
case BTN_MOUSE_SELECT:
// Same tile at the begining ant at the end ? We show informations
if (pos1.equals(pos2)) {
// Player's antHill ?
Team team = game.getWorld().getTeams().get(0);
if (team.getAntHill().equals(pos2))
gameFrame.setLeftPanelAntHill();
// Samantha (by default)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -