public boolean isColliding(int x, int y) { for (Block b : blocks) // if the block is a down block. if (b.getX() < x && x - b.getX() < b.getWidth() && b.getY() < y) return true; return false; }
@Override public void paint(BufferedImage buf) { Graphics g = buf.getGraphics(); g.setColor(Color.green); for (Block b : blocks) g.fillRoundRect(b.getX(), b.getY(), b.getWidth(), b.getHeight() + 10, 5, 5); }
@Override public void update(int shiftX, int shiftY) { if (blocks.isEmpty()) return; for (Block b : blocks) { b.setX(b.getX() + shiftX); // removes the block and instantly adds a new one. if (b.getX() < 0 - b.getWidth()) { blocks.remove(b); int rand = (int) (Math.random() * BLOCK_HEIGHT_MAX + 1); if (auto) { // if( change) createBlockDown(BLOCK_HEIGHT_BASE + rand, 30); // else // createBlockUp( BLOCK_HEIGHT_BASE + rand, 30); } change = !change; } } }
// process a mouseClicked event // select the loop if the mouse was clicked within the edges of // the border public void mouseClicked(MouseEvent mouseEvent) { int x = mouseEvent.getX(); int y = mouseEvent.getY(); Rectangle bounds = getBounds(); // left, top, bottom, right edge if ((x >= 0 && x <= SELECT_BORDER_SIZE) || (y >= 0 && y <= SELECT_BORDER_SIZE) || (y >= bounds.height - SELECT_BORDER_SIZE && y <= bounds.height) || (x >= bounds.width - SELECT_BORDER_SIZE && x <= bounds.width)) { if (isSelected()) program.setAllSelected(false); else { program.setAllSelected(false); setSelected(true); program.updateLoopDialog(); } program.updateState(); repaint(); } else super.mouseClicked(mouseEvent); }
void place(int mx, int my) { if (drme) { drme = false; return; } int n = data.a.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (((mx - data.a[i][j].x) * (mx - data.a[i][j].x) + (my - data.a[i][j].y) * (my - data.a[i][j].y) < bst.scale * bst.scale * .75) && data.a[i][j].getState() == 0) { if (count == 1 && gst.swap) { int ch = JOptionPane.showConfirmDialog( this, "Swap moves?", "Swap", JOptionPane.YES_NO_OPTION); if (ch == JOptionPane.YES_OPTION) { lmv.setState(cst); switch (cst) { case 1: data.a[i][j].setState(2); break; case 2: data.a[i][j].setState(1); break; } repaint( (int) (lmv.x - 0.8 * bst.scale), (int) (lmv.y - 0.8 * bst.scale), (int) (1.6 * bst.scale), (int) (1.6 * bst.scale)); if (AIMethods.gameOver(this, lmv.i, lmv.j, count)) { newGame(); } if (gst.ai != null) gst.ai.updateAI(lmv.i, lmv.j, lmv.getState(), count); } else data.a[i][j].setState(cst); } else data.a[i][j].setState(cst); lmv = data.a[i][j]; repaint( (int) (data.a[i][j].x - 0.8 * bst.scale), (int) (data.a[i][j].y - 0.8 * bst.scale), (int) (1.6 * bst.scale), (int) (1.6 * bst.scale)); switch (cst) { case 1: cst = 2; break; case 2: cst = 1; break; } if (AIMethods.gameOver(this, lmv.i, lmv.j, count)) { System.out.println("Game Over"); firstrun = true; String win = ""; if (lmv.getState() == Block.RED_BEAD) { switch (gst.theme) { case 0: win = "Red"; break; case 1: win = "Black"; break; case 2: win = "Gold"; break; } } else { switch (gst.theme) { case 0: win = "Blue"; break; case 1: win = "Green"; break; case 2: win = "Silver"; break; } } if (gst.audio) AePlayWave.play(AePlayWave.END); JOptionPane.showMessageDialog( this, win + " wins", "Game Over", JOptionPane.INFORMATION_MESSAGE); newGame(); return; } if (gst.audio) { if (gst.ai == null) AePlayWave.play(AePlayWave.PLACE); else if (userMove) AePlayWave.play(AePlayWave.AIMOVE); } count++; if (gst.ai != null && userMove) { userMove = false; gst.ai.nextMove(lmv.i, lmv.j, lmv.getState(), count); } else userMove = true; return; } } } if (gst.audio) AePlayWave.play(AePlayWave.BLOCK); }
/** preforms propogation and collision detected amongst local neighoubourhood */ public Block[] updateWind() { Block[] n = new Block[neighbours.length + 1]; for (int i = 0; i < n.length; i++) { if (i == n.length - 1) { n[i] = new EarthBlock(); n[i].setXPos(this.getXPos()); n[i].setYPos(this.getYPos()); n[i].setZPos(this.getZPos()); } else { n[i] = new EarthBlock(); n[i].setXPos(neighbours[i].getXPos()); n[i].setYPos(neighbours[i].getYPos()); n[i].setZPos(neighbours[i].getZPos()); } } int tempVelX = 0; int tempVelY = 0; int tempVelZ = 0; int xcounter = 0; int ycounter = 0; int zcounter = 0; double xWind = 0.0; double yWind = 0.0; double zWind = 0.0; int windcounter = 0; for (int i = 0; i < n.length - 1; i++) { Block b = neighbours[i]; // System.out.println("boo "+b.getXPos()+","+b.getVelX()+","+b.getBlockState()); if (b.getXPos() != -1) { // System.out.println("boo "+b.getXPos()+","+b.getVelX()+","+b.getBlockState()); if ((b.getXPos() + b.getVelX() == this.getXPos()) && (b.getYPos() + b.getVelY() == this.getYPos()) && (b.getZPos() + b.getVelZ() == this.getZPos())) { tempVelX = tempVelX + b.getVelX(); tempVelY = tempVelY + b.getVelY(); tempVelZ = tempVelZ + b.getVelZ(); // xWind = xWind+b.getXWindStrength()*b.getVelX(); // yWind = yWind+b.getYWindStrength()*b.getVelY(); // zWind = zWind+b.getZWindStrength()*b.getVelZ(); // System.out.println("hello"); // windcounter++; // add transported pheromones // System.out.println("transfered: // "+b.getWindQueenPheromone()+","+b.getWindStrength()+","+b.getQueenPheromone()); n[n.length - 1].addCementPheromone(b.getWindCementPheromone()); n[n.length - 1].addQueenPheromone(b.getWindQueenPheromone()); n[i].addCementPheromone(-b.getWindCementPheromone()); n[i].addQueenPheromone(-b.getWindQueenPheromone()); // System.out.println(b.getBlockState()); } // if(b.getVelX() != this.getVelX() && b.getVelX() != 0) xcounter++; // if(b.getVelY() != this.getVelY()&& b.getVelY() != 0) ycounter++; // if(b.getVelZ() != this.getVelZ()&& b.getVelZ() != 0) zcounter++; } } // System.out.println(xWind); // System.out.println(windcounter); // System.out.println("currentStrength: "+this.getWindStrength()); // if(windcounter != 0 ) n[n.length-1].setXWindStrength(Math.abs(xWind)); // if(windcounter != 0 ) n[n.length-1].setYWindStrength(Math.abs(yWind)); // if(windcounter != 0 ) n[n.length-1].setZWindStrength(Math.abs(zWind)); // System.out.println("new air wind strength: "+n[n.length-1].getWindStrength()); // use average velocities to calculate update // System.out.println("Air Collisions: "+tempVelX+","+tempVelY+","+tempVelZ); n = xVelocity(tempVelX, xcounter, n); n = yVelocity(tempVelY, ycounter, n); n = zVelocity(tempVelZ, zcounter, n); // keeps a constant flow accross x plane if (this.getXPos() == 0) n[n.length - 1].setVelX(1); if (this.getXPos() == 19) { n[n.length - 1].addCementPheromone(-this.getWindCementPheromone()); n[n.length - 1].addQueenPheromone(-this.getWindQueenPheromone()); } return n; }
/** Called when run from the command line. The train module will run (mostly) individually. */ public static void main(String[] args) { try { isSolo = true; // Get the number of trains. if (args.length != 1) { System.out.println("Invalid number of arguments."); System.exit(1); } soloNumTrains = Integer.parseInt(args[0], 10); if ((soloNumTrains < 1) || (soloNumTrains > 9)) { System.out.println( "Invalid argument. The number of trains must be greater than 0 and less than 10."); System.exit(1); } // Create the test block loop. Block bYard = new Block( 0, "A", "I", 0.0, 0.0, 5.0, true, false, true, false, false, "", false, false, false); Block b01 = new Block( 1, "B", "II", 500.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b02 = new Block( 2, "C", "II", 500.0, -22.2, 1.0, true, false, false, false, false, "", false, false, false); Block b03 = new Block( 3, "D", "II", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b04 = new Block( 4, "E", "II", 500.0, 11.1, 30.0, true, true, false, false, false, "", false, false, false); Block b05 = new Block( 5, "F", "III", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b06 = new Block( 6, "G", "III", 100.0, 0.0, 5.0, true, false, false, true, false, "Alpha", false, false, false); Block b07 = new Block( 7, "H", "III", 500.0, -11.1, 30.0, true, false, false, false, false, "", false, false, false); Block b08 = new Block( 8, "I", "III", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b09 = new Block( 9, "J", "IV", 500.0, 22.2, 1.0, true, false, false, false, false, "", false, false, false); Block b10 = new Block( 10, "K", "IV", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b11 = new Block( 11, "L", "IV", 100.0, 0.0, 5.0, true, false, false, true, false, "Beta", false, false, false); Block b12 = new Block( 12, "M", "IV", 50.0, 11.1, 15.0, true, false, false, false, false, "", false, false, false); Block b13 = new Block( 13, "N", "V", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b14 = new Block( 14, "O", "V", 50.0, -11.1, 15.0, true, false, false, false, false, "", false, false, false); Block b15 = new Block( 15, "P", "V", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); Block b16 = new Block( 16, "Q", "V", 100.0, 0.0, 5.0, true, false, false, true, false, "Gamma", false, false, false); Block b17 = new Block( 17, "R", "V", 50.0, 0.0, 15.0, true, false, false, false, false, "", false, false, false); bYard.connect(b17, b01); b01.connect(bYard, b02); b02.connect(b01, b03); b03.connect(b02, b04); b04.connect(b03, b05); b05.connect(b04, b06); b06.connect(b05, b07); b07.connect(b08, b06); b08.connect(b09, b07); b09.connect(b10, b08); b10.connect(b11, b09); b11.connect(b12, b10); b12.connect(b11, b13); b13.connect(b14, b12); b14.connect(b13, b15); b15.connect(b16, b14); b16.connect(b15, b17); b17.connect(bYard, b16); ArrayList<Block> route = new ArrayList<Block>(); route.add(bYard); route.add(b01); route.add(b02); route.add(b03); route.add(b04); route.add(b05); route.add(b06); route.add(b07); route.add(b08); route.add(b09); route.add(b10); route.add(b11); route.add(b12); route.add(b13); route.add(b14); route.add(b15); route.add(b16); route.add(b17); route.add(bYard); route.add(b01); route.add(b02); route.add(b03); route.add(b04); route.add(b05); route.add(b06); route.add(b07); route.add(b08); route.add(b09); route.add(b10); route.add(b11); route.add(b12); route.add(b13); route.add(b14); route.add(b15); route.add(b16); route.add(b17); route.add(bYard); /* * Create the trains. * The first will depart at 8:00 AM, the second at 8:30 AM, the third at 9:00 AM, etc. * The first will depart at 8:00 AM, the second at 8:15 AM, the third at 8:30 AM, etc. * All will have break time set to 4 hours after their departure times. */ trainList = new ArrayList<Train>(); idArray = new String[soloNumTrains]; for (int i = 0; i < soloNumTrains; i++) { trainList.add( new Train( i + 1, "T" + (i + 1), "Test", (8 * 60 * 60 + i * 15 * 60) % (24 * 60 * 60), route, new Engineer( true, false, 0.0, (8 * 60 * 60 + i * 15 * 60 + 5 * 60) % (24 * 60 * 60)), bYard)); idArray[i] = new String("T" + (i + 1)); } // Create the UI. TrainModelUI tnmUI = new TrainModelUI(); tnmUI.setTrainList(trainList); // Setup the timer. soloTime = 7 * 60 * 60 + 59 * 60 + 55; soloDate = new Date(93, 2, 2, 7, 59, 55); ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { timeTick(soloDate, soloDelta); } }; new javax.swing.Timer(20, taskPerformer).start(); // Disable all "Toggle" buttons that are associated with a manual entry. btnToggleManRecPower.setEnabled(false); btnToggleManDesSpdLmt.setEnabled(false); btnToggleManLights.setEnabled(false); btnToggleManDoors.setEnabled(false); btnToggleManTarTemperature.setEnabled(false); // Make it so only manual values are used. for (int i = 0; i < soloNumTrains; i++) { Train t = trainList.get(i); t.issetManualPower = true; t.issetManualSpeedLimit = true; t.issetLightsOnUseManual = true; t.issetDoorsOpenUseManual = true; t.issetTargetTemperatureManual = true; } btnPauseResume.setEnabled(true); tnmUI.setSelectedId(trainList.get(0).id); tnmUI.setIsPaused(tnmUI.getIsPaused()); tnmUI.setIsVisible(true); } catch (Exception e) { e.printStackTrace(System.err); JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE); } }
public Block(Block block) { this(block.getX(), block.getY(), block.getColor()); }
// set the selection state and update the GUI and loop dialog public void setSelected(boolean flag) { super.setSelected(flag); program.updateState(); program.updateLoopDialog(); }
// set the selection state of all sub-components public void setAllSelected(boolean flag) { setSelected(flag); super.setAllSelected(flag); }
public void setProgram(Program program) { this.program = program; super.setProgram(program); }