public void victory() { battleWon = true; player.expGain(totalExperience); enemies.removeAll(enemies); spellsThrown.removeAll(spellsThrown); staminaTimer.stop(); }
public void drawVictory(Graphics g) { g.setColor(new Color(0, 0, 0, 200)); g.fillRect(195, 220, 410, 110); g.setColor(new Color(255, 255, 255, 200)); g.fillRect(200, 225, 400, 100); g.setColor(new Color(0, 0, 0, 200)); g.setFont(new Font("Bell MT", Font.BOLD, 20)); FontMetrics metrics = g.getFontMetrics(new Font("Bell MT", Font.BOLD, 20)); int hgt = metrics.getHeight(); String initialMessage; String followupMessage; if (nextWindow) { initialMessage = "You have gotten stronger."; followupMessage = player.getName() + " is now level " + player.getLevel() + "!"; } else { initialMessage = "You survived!"; followupMessage = "You and your allies gain " + totalExperience + " experience!"; } // Hgt = 26 int adv = metrics.stringWidth(initialMessage); g.drawString(initialMessage, getWidth() / 2 - adv / 2, 234 + hgt); adv = metrics.stringWidth(followupMessage); g.drawString(followupMessage, getWidth() / 2 - adv / 2, 269 + hgt); }
public void mouseClicked(MouseEvent e) { if (scene == BATTLE) { if (!battleWon) { int target = 0; if (kickDirt.contains(mX, mY) && player.getStamina() >= 33.3 && !player.getDeath()) { spellsThrown.add( new Spell( 0, new Color(153, 76, 0), player.centerX(), player.centerY(), enemies.get(target).centerX(), enemies.get(target).centerY(), target)); player.staminaGain(-333); } if (hurlPebble.contains(mX, mY) && player.getStamina() >= 100 && !player.getDeath()) { spellsThrown.add( new Spell( 1, Color.LIGHT_GRAY, player.centerX(), player.centerY(), enemies.get(target).centerX(), enemies.get(target).centerY(), target)); player.staminaGain(-1000); } } } }
public void paintComponent(Graphics g) { super.paintComponent(g); if (scene == OVERWORLD) g.drawImage(background, 0, 0, this); if (scene == BATTLE) { if (battleBG == 1) g.drawImage(background, 0, -130, this); else g.drawImage(background, 0, 0, this); } int loop; for (loop = 0; loop < enemies.size(); loop++) { if (scene == OVERWORLD) { enemies.get(loop).drawEnemy(g); } if (scene == BATTLE) { if (enemies.get(loop).getActivity()) { enemies.get(loop).drawEnemy(g); } } } for (loop = 0; loop < spellsThrown.size(); loop++) { spellsThrown.get(loop).drawSpell(g); } player.drawPlayer(g); for (loop = 0; loop < enemies.size(); loop++) { enemies.get(loop).drawSpells(g); } if (scene == BATTLE) { drawMenu(g); if (battleWon) drawVictory(g); else if (battleLost) drawDefeat(g); } }
// Stops the music from playing public void pause() { try { player.stop(); } catch (Exception e) { e.printStackTrace(); } }
// Play starts the track from the beginning or from the last played part public void play() { try { player.start(); } catch (Exception e) { e.printStackTrace(); } }
public void stop() { currentAudioFile = null; if (currentPlayer != null) { doFade(mainLine, 0); currentPlayer.stopAfter(FADE_DURATION); currentPlayer = null; } }
public void battleEnd() { scene = OVERWORLD; ImageIcon bg = new ImageIcon("OverworldBG.jpg"); background = bg.getImage(); player.battleEnd(); totalExperience = 0; nextWindow = false; battleWon = false; enemies.add(new Stalker(175, 200)); }
public void checkAllyPresence() { if (scene == BATTLE) { // int loop; int deadCounter = 0; if (player.getDeath()) deadCounter++; /*for (loop = 0; loop < allies.size(); loop++){ if (allies.get(loop).getDeath()) deadCounter++; }*/ if (deadCounter == allies.size() + 1) defeat(); } }
public void playLoop() { if (clip != null) { double playStartTime = (player.getSeekTime() / 100) * (playGetLength()); clip.setMicrosecondPosition((long) playStartTime); if (playState != PLAYING) { clip.start(); playState = PLAYING; } } }
// Transitions from Overworld to Battle scene public void battleBegin() { scene = BATTLE; moveUp = false; moveDown = false; moveLeft = false; moveRight = false; // Background changer int bgSelector = (int) (10 * Math.random() + 1); if (bgSelector > 5) { ImageIcon bg = new ImageIcon("BattleBG1.jpg"); background = bg.getImage(); battleBG = 1; player.battleBegin(235, 380); int placed = 0; for (int enemyNum = 0; enemyNum < enemies.size(); enemyNum++) { Enemy enemy = enemies.get(enemyNum); if (enemy.getActivity()) { enemy.battleBegin(440 - 20 * placed, 125); placed++; totalExperience += enemy.getEXP(); } } } else { ImageIcon bg = new ImageIcon("BattleBG2.png"); background = bg.getImage(); battleBG = 2; player.battleBegin(650, 365); int placed = 0; for (int loop = 0; loop < enemies.size(); loop++) { if (enemies.get(loop).getActivity()) { enemies.get(loop).battleBegin(340 - 20 * placed, 145); placed++; totalExperience += enemies.get(loop).getEXP(); } } } menuScreen.setVisible(false); staminaTimer.start(); }
public boolean play() { try { if (playState != STOPPED) playStop(); if (audioBytes == null) return false; DataLine.Info info = new DataLine.Info(Clip.class, format); clip = (Clip) AudioSystem.getLine(info); clip.addLineListener(new ClipListener()); long clipStart = (long) (audioBytes.length * getStartTime() / (getDuration() * 1000.0)); long clipEnd = (long) (audioBytes.length * getEndTime() / (getDuration() * 1000.0)); if ((clipEnd - clipStart) > MAX_CLIP_LENGTH) clipEnd = clipStart + MAX_CLIP_LENGTH; byte[] clipBytes = new byte[(int) (clipEnd - clipStart)]; System.arraycopy(audioBytes, (int) clipStart, clipBytes, 0, clipBytes.length); clip.open(format, clipBytes, 0, clipBytes.length); FloatControl panControl = (FloatControl) clip.getControl(FloatControl.Type.PAN); panControl.setValue((float) panSetting / 100.0f); double value = (double) gainSetting; FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0); gainControl.setValue(dB); double playStartTime = (player.getSeekTime() / 100) * (playGetLength()); clip.setMicrosecondPosition((long) playStartTime); clip.start(); playState = PLAYING; return true; } catch (Exception ex) { ex.printStackTrace(); playState = STOPPED; clip = null; return false; } }
public HotSMain() { // General setPreferredSize(new Dimension(800, 600)); canvas.add(this); setBackground(Color.GRAY); frame.setResizable(false); frame.setTitle("Heroes of the Forest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Overworld menuScreen.setPreferredSize(new Dimension(800, 150)); menuScreen.setBackground(Color.DARK_GRAY); menuScreen.setVisible(false); menuButtons.add(itemsButton); menuButtons.add(equipButton); menuButtons.add(skillsButton); menuButtons.add(statusButton); menuButtons.add(settingsButton); menuButtons.add(dataButton); for (JButton button : menuButtons) { menuScreen.add(button); button.addActionListener(this); } canvas.add(menuScreen, BorderLayout.SOUTH); ImageIcon bg = new ImageIcon("OverworldBG.jpg"); background = bg.getImage(); frame.pack(); frame.setVisible(true); player = new Player(); enemies.add(new Stalker(375, 200)); player.defaultPlayer(); frame.addKeyListener(this); frame.addMouseListener(this); frame.addMouseMotionListener(this); moveTimer.start(); }
// returns the length of the track in the form of seconds public double getTime() { return player.getDuration().getSeconds(); }
// Stops the track, resets it to the beginning public void rewind() { player.stop(); player.setMediaTime(Player.RESET); }
// Draws the Battle Menu public void drawMenu(Graphics g) { if (scene == BATTLE) { // Background g.setColor(new Color(0, 100, 0)); g.fillRect(0, 450, 800, 150); g.setColor(Color.BLACK); g.fillRect(10, 460, 780, 130); // Player // Health g.setColor(Color.WHITE); g.setFont(new Font("Bell MT", Font.BOLD, 20)); FontMetrics metrics = g.getFontMetrics(new Font("Bell MT", Font.BOLD, 20)); int hgt = metrics.getHeight(); // Hgt = 26 int adv = metrics.stringWidth(player.getHealth() + "/" + player.getMaxHealth(0)); g.setColor(Color.DARK_GRAY); g.fillRect(getWidth() - 170, 470, 150, hgt - 6); if ((double) player.getHealth() / player.getMaxHealth(0) > .25) g.setColor(Color.RED); else g.setColor(player.getLowHealth()); g.drawString( player.getHealth() + "/" + player.getMaxHealth(0), getWidth() - adv - 180, 461 + hgt); g.fillRect( getWidth() - 167, 473, (int) (144 * player.getHealth() / player.getMaxHealth(0)), hgt - 12); // Stamina g.setColor(Color.DARK_GRAY); g.fillRect(getWidth() - 170, 500, 150, hgt - 6); adv = metrics.stringWidth((int) player.getStamina() + "%"); if (player.getStamina() < 33.3) g.setColor(Color.WHITE); else if (player.getStamina() == 100) g.setColor(Color.GREEN); else g.setColor(Color.CYAN); g.drawString((int) player.getStamina() + "%", getWidth() - adv - 180, 491 + hgt); g.fillRect(getWidth() - 167, 503, (int) (144 * player.getStamina() / 100), hgt - 12); // Basic Attack adv = metrics.stringWidth("Kick Dirt"); g.setColor(Color.DARK_GRAY); g.fillRect(20, 470, adv + 10, 30); g.setColor(Color.WHITE); g.drawString("Kick Dirt", 25, 492); // Spell 1 adv = metrics.stringWidth("Hurl Pebble"); g.setColor(Color.DARK_GRAY); g.fillRect(20, 510, adv + 10, 30); g.setColor(Color.WHITE); g.drawString("Hurl Pebble", 25, 532); } }
// Hotkeys! public void keyPressed(KeyEvent e) { int keyID = e.getKeyCode(); if (scene == OVERWORLD) { if (keyID == KeyEvent.VK_UP || keyID == KeyEvent.VK_W) { moveUp = true; } if (keyID == KeyEvent.VK_DOWN || keyID == KeyEvent.VK_S) { moveDown = true; } if (keyID == KeyEvent.VK_LEFT || keyID == KeyEvent.VK_A) { moveLeft = true; } if (keyID == KeyEvent.VK_RIGHT || keyID == KeyEvent.VK_D) { moveRight = true; } if (keyID == KeyEvent.VK_ENTER) { // Toggle the visible state of the Game Menu Boolean visibleState = menuScreen.isVisible(); menuScreen.setVisible(!visibleState); } for (int enemyNum = 0; enemyNum < enemies.size(); enemyNum++) { Enemy enemy = enemies.get(enemyNum); if (enemy.hitbox().intersects(player.hitbox())) { player.combatChange(true); enemy.combatChange(true); battleBegin(); } } } else if (scene == BATTLE) { if (keyID == KeyEvent.VK_SPACE && player.getStamina() >= 33.3 && !player.getDeath() && !battleWon) { int target = 0; spellsThrown.add( new Spell( 0, new Color(153, 76, 0), player.centerX(), player.centerY(), enemies.get(target).centerX(), enemies.get(target).centerY(), target)); player.staminaGain(-333); } if (keyID == KeyEvent.VK_ENTER && player.getStamina() == 100 && !player.getDeath() && !battleWon) { int target = 0; spellsThrown.add( new Spell( 1, Color.LIGHT_GRAY, player.centerX(), player.centerY(), enemies.get(target).centerX(), enemies.get(target).centerY(), target)); player.staminaGain(-1000); } if ((keyID == KeyEvent.VK_ENTER || keyID == KeyEvent.VK_SPACE) && battleWon) { if (player.levelUp()) { nextWindow = true; } else { battleEnd(); } } } repaint(); }
public void actionPerformed(ActionEvent a) { Object source = a.getSource(); if (scene == OVERWORLD) { if (source == moveTimer) { if (moveUp && !moveDown) player.moveUp(); else if (!moveUp && moveDown) player.moveDown(); if (moveLeft && !moveRight) player.moveLeft(); else if (!moveLeft && moveRight) player.moveRight(); } if (source == itemsButton) itemsButton.setText("ITEMS!"); else if (source == equipButton) equipButton.setText("EQUIPMENT!"); else if (source == skillsButton) skillsButton.setText("SKILLS"); else if (source == settingsButton) settingsButton.setText("SETTINGS"); else if (source == statusButton) statusButton.setText("STATUS"); else if (source == dataButton) dataButton.setText("DATA"); } if (scene == BATTLE) { if (source == moveTimer) { // Spell Movement for (int spellNum = 0; spellNum < spellsThrown.size(); spellNum++) { Spell spell = spellsThrown.get(spellNum); spell.move(); if (spell.spellHit()) { enemies.get(spell.getTarget()).takeDamage(spell.getDamage()); enemies.get(spell.getTarget()).statusPresent(spell.getEffect()); spellsThrown.remove(spellNum); checkEnemyPresence(); } } for (int enemyLoop = 0; enemyLoop < enemies.size(); enemyLoop++) { Enemy enemy = enemies.get(enemyLoop); for (int spellNum = 0; spellNum < enemy.spellsThrown.size(); spellNum++) { Spell spell = enemy.spellsThrown.get(spellNum); spell.move(); if (spell.spellHit()) { player.takeDamage(spell.getDamage()); enemy.spellsThrown.remove(spellNum); checkAllyPresence(); } } } } if (source == staminaTimer) { // Stamina Regeneration player.staminaGain(16); // Enemy Attacks int enemyLoop; for (enemyLoop = 0; enemyLoop < enemies.size(); enemyLoop++) { enemies.get(enemyLoop).attack(player.centerX(), player.centerY(), 12); } } } repaint(); // To check collision if (scene == OVERWORLD) { for (int loop = 0; loop < enemies.size(); loop++) { Enemy enemy = enemies.get(loop); if (enemy.hitbox().intersects(player.hitbox())) { player.combatChange(true); enemy.combatChange(true); battleBegin(); } } } }