added more battle graphics and effects

This commit is contained in:
musilto9 2020-06-08 03:07:16 +02:00
parent 3c91433a23
commit 58c1ed49f0
15 changed files with 196 additions and 66 deletions

View File

@ -44,9 +44,14 @@ public class Battle {
public void setPlayerAttack(boolean playerAttack) {
windowOfBattle.getMenuOfActions().setVisible(false);
windowOfBattle.getEnemyDescription().setVisible(false);
windowOfBattle.getSituationText().setVisible(true);
this.playerAttack = playerAttack;
}
public void addToTurnOrder(BattlePerson person) {
turnOrder.add(person);
}
public Battle(LinkedList<Hero> heroes, LinkedList<Enemy> enemies, WindowOfBattle window) {
this.heroes = heroes;
this.enemies = enemies;
@ -73,7 +78,7 @@ public class Battle {
public void startBattle() {
turnBelongTo = heroes.getFirst();
windowOfBattle.getMenuOfActions().setVisible(true);
windowOfBattle.getEnemyDescription().setVisible(true);
windowOfBattle.getEnemyDescription().updateDescription();
windowOfBattle.repaint();
turnOrder = new LinkedList<>();
timer = new Timer(1000, actionEvent -> {
@ -93,7 +98,7 @@ public class Battle {
});
}
private void endBattle(boolean b) {
public void endBattle(boolean b) {
timer.stop();
timerStopped = true;
windowOfBattle.setVisible(false);
@ -113,7 +118,7 @@ public class Battle {
windowOfFreeMove.setVisible(true);
windowOfFreeMove.requestFocus();
windowOfBattle.getGame().remove(windowOfBattle);
windowOfBattle.getMusicManager().stopMusic();
if (windowOfBattle.getMusicManager() != null) windowOfBattle.getMusicManager().stopMusic();
}
// SaveManager saveManager = game.getFreeMove().getMapManager().getSaveManager();
// saveManager.saveGame();
@ -124,18 +129,18 @@ public class Battle {
timerStopped = true;
windowOfBattle.getMenuOfActions().setVisible(true);
for (Component e: windowOfBattle.getMenuOfActions().getComponents()) e.setEnabled(true);
windowOfBattle.getEnemyDescription().setVisible(true);
windowOfBattle.getEnemyDescription().updateDescription();
windowOfBattle.getSituationText().hideText();
}
private void attackByHero(Enemy e) {
turnCount++;
windowOfBattle.repaint();
turnBelongTo.attack(e);
if (windowOfBattle.getHitSound() != null) soundManager.playSound(windowOfBattle.getHitSound(), 6.0f);
windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " damage to " + e.getName()+"!");
//TODO Animation of Death
if (e.health <= 0) enemies.remove(e);
turnBelongTo.setImageEffect("attack");
if (windowOfBattle.getHitSound() != null) soundManager.playSound(windowOfBattle.getHitSound(), -10.0f);
windowOfBattle.writeToOutput(turnBelongTo.getName() + " deals " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " damage to " + e.getName()+"!");
if (e.health <= 0) e.getKilled(this);
turnOrder.add(turnBelongTo);
playerAttack = false;
@ -169,11 +174,11 @@ public class Battle {
private void attackByMonster(Hero e) {
turnCount++;
turnBelongTo.attack(e);
if (windowOfBattle.getHitSound() != null) soundManager.playSound(windowOfBattle.getHitSound(), 6.0f);
turnBelongTo.setImageEffect("attack");
if (windowOfBattle.getHitSound() != null) soundManager.playSound(windowOfBattle.getHitSound(), -10.0f);
windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() + " damage to " + e.getName()+"!");
//TODO Animation of Death
if (e.health <= 0) heroes.remove(e);
windowOfBattle.writeToOutput(turnBelongTo.getName() + " deals " + turnBelongTo.getLevel() + " damage to " + e.getName()+"!");
if (e.health <= 0) e.getKilled(this);
turnOrder.add(turnBelongTo);
delay = 2;
@ -206,4 +211,9 @@ public class Battle {
}
return true;
}
public void removePersonFromList(BattlePerson person) {
if (person.getClass().getName().equals("ruscz.game.gameOfProd.Battle.Hero")) heroes.remove(person);
else enemies.remove(person);
}
}

View File

@ -48,6 +48,7 @@ public abstract class BattlePerson {
}
private void effectCoords(String effect) {
System.out.println(effect);
if (effect.equals("hover")) {
final boolean[] isGoingUp = {true};
effectTimer = new Timer(100, actionEvent -> {
@ -74,11 +75,32 @@ public abstract class BattlePerson {
i[0]++;
});
}
else if (effect.equals("attack")) {
final boolean[] isAttacking = {true};
if (getClass().getName().equals("ruscz.game.gameOfProd.Battle.Hero")) {
effectedCoords[0] = -1;
effectTimer = new Timer(10, actionEvent -> {
if (effectedCoords[0] >= 0) effectTimer.stop();
else if (effectedCoords[0] <= -40) isAttacking[0] = false;
if (isAttacking[0]) effectedCoords[0] -= 7;
else effectedCoords[0] += 2;
});
}
else {
effectedCoords[0] = 1;
effectTimer = new Timer(10, actionEvent -> {
if (effectedCoords[0] <= 0) effectTimer.stop();
else if (effectedCoords[0] >= 40) isAttacking[0] = false;
if (isAttacking[0]) effectedCoords[0] += 7;
else effectedCoords[0] -= 2;
});
}
}
effectTimer.start();
}
private void stopEffectTimer() {
effectTimer.stop();
protected void stopEffectTimer() {
if (effectTimer!= null) effectTimer.stop();
}
private void getHurt() {
@ -89,8 +111,8 @@ public abstract class BattlePerson {
rescaleOp.filter(originalImage, whiteImage);
rescaleOp.filter(whiteImage, whiteImage);
Graphics2D g = newImage.createGraphics();
g.setComposite(AlphaComposite.SrcOver);
g.drawImage(originalImage, 0, 0, null);
g.setComposite(AlphaComposite.SrcOver);
final float[] a = {0.1f};
final boolean[] isGoingWhite = {true};
timer = new Timer(10, actionEvent -> {
@ -99,7 +121,7 @@ public abstract class BattlePerson {
g.setComposite(AlphaComposite.SrcOver.derive(a[0]));
g.drawImage(whiteImage, 0, 0, null);
image = newImage;
if (a[0] < 0.1f) stopTimer();
if (a[0] < 0.1f) timer.stop();
if (a[0] >= 0.9f) isGoingWhite[0] = false;
if (isGoingWhite[0]) a[0] += 0.1f;
else a[0] -= 0.1f;
@ -107,9 +129,73 @@ public abstract class BattlePerson {
timer.start();
}
private void stopTimer() {
protected void getHealed() {
BufferedImage originalImage = image;
BufferedImage whiteImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
RescaleOp rescaleOp = new RescaleOp(255.0f, 15, null);
rescaleOp.filter(originalImage, whiteImage);
rescaleOp.filter(whiteImage, whiteImage);
Graphics2D g = newImage.createGraphics();
final int[] i = {whiteImage.getHeight() - 2};
timer = new Timer(5, actionEvent -> {
g.drawImage(originalImage, 0, 0, null);
g.drawImage(whiteImage.getSubimage(0, i[0], whiteImage.getWidth(), 2), 0, i[0], null);
i[0]--;
image = newImage;
if (i[0] == 0) {
image = originalImage;
timer.stop();
}
});
timer.start();
}
public void getKilled(Battle battle) {
BufferedImage originalImage = image;
BufferedImage whiteImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
RescaleOp rescaleOp = new RescaleOp(255.0f, 15, null);
rescaleOp.filter(originalImage, whiteImage);
rescaleOp.filter(whiteImage, whiteImage);
Graphics2D g = newImage.createGraphics();
final int[] i = {1};
final int[] j = {1};
int xLines = whiteImage.getHeight() / 5;
int yLines = whiteImage.getWidth() / 5;
final int[] m = {0};
final int[] n = {0};
timer = new Timer(5, actionEvent -> {
if (i[0] < whiteImage.getHeight() || j[0] < whiteImage.getWidth()) {
g.drawImage(originalImage, 0, 0, null);
for (int k = 0; k < yLines; k++) {
g.drawImage(whiteImage.getSubimage(k * 5, 0, 1, i[0]), k * 5, 0, null);
}
for (int k = 0; k < xLines; k++) {
g.drawImage(whiteImage.getSubimage(0, k * 5, j[0], 1), 0, k * 5, null);
}
if (i[0] < whiteImage.getHeight()) i[0]++;
if (j[0] < whiteImage.getWidth()) j[0]++;
image = newImage;
}
else {
g.setColor(new Color(0, 0, 0, 0));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
g.fillRect(m[0] * 5, n[0] * 5, 5, 5);
image = newImage;
if (m[0] == 0 && n[0] == xLines) {
battle.removePersonFromList(this);
timer.stop();
}
if (m[0] < yLines) m[0]++;
if (m[0] == yLines) {
n[0]++;
m[0] = 0;
}
}
});
timer.start();
}
public int[] getEffectedCoords() {
if (effectedCoords == null) return new int[]{0, 0};
@ -139,12 +225,12 @@ public abstract class BattlePerson {
}
public void setHealth(int health) {
if (this.health > health && image != null) {
if (this.health > health && image != null && health > 0) {
getHurt();
setImageEffect("hurt");
if (health <= maxHealth/10) setImageEffect("shake");
else setImageEffect("hurt");
}
this.health = health;
if (health <= maxHealth/10) setImageEffect("shake");
}
public int getLevel() {

View File

@ -21,7 +21,10 @@ public class Hero extends BattlePerson {
@Override
public void heal(int heal) {
health += heal;
if (health + heal < maxHealth) health += heal;
else health = maxHealth;
stopEffectTimer();
getHealed();
}
@Override

View File

@ -12,10 +12,13 @@ public class CommandList extends JPanel {
public CommandList(WindowOfBattle window){
setSize(CELL_SIZE * 8, CELL_SIZE * 6);
setLocation(CELL_SIZE * 4, CELL_SIZE * 2);
setBackground(new Color(255, 255, 255, 150));
setBorder(BorderFactory.createLineBorder(new Color(213, 108, 18), 2));
setVisible(false);
JLabel text = new JLabel("It's not a situation to use command line, idiot!");
JButton jButton = new JButton("Hide this field");
JLabel text = new JLabel("It's not a situation to use command line, nerd!");
text.setFont(new Font(Font.SERIF, Font.PLAIN, 20));
JButton jButton = window.style(new JButton("Hide this field"));
jButton.addActionListener(e -> {
this.setVisible(false);
});

View File

@ -19,12 +19,14 @@ public class ConsoleInput extends JPanel {
setSize(CELL_SIZE * (CANVAS_WIDTH - 4), CELL_SIZE);
setLocation(CELL_SIZE * 4, CELL_SIZE * CANVAS_HEIGHT * 3 / 4 - CELL_SIZE);
setBackground(new Color(255, 255, 255, 150));
setBorder(BorderFactory.createLineBorder(new Color(213, 108, 18), 2));
setVisible(false);
input = new JTextPane();
submit = new JButton("Submit");
list = new JButton("List");
exit = new JButton("x");
submit = window.style(new JButton("Submit"));
list = window.style(new JButton("List"));
exit = window.style(new JButton("x"));
submit.addActionListener(e -> {
//TODO Take text from Field and work with parser

View File

@ -5,7 +5,6 @@ import ruscz.game.gameOfProd.Windows.WindowOfBattle;
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.LinkedList;
import static ruscz.game.gameOfProd.Windows.WindowOfBattle.*;
@ -19,24 +18,18 @@ public class EnemyDescription extends JPanel {
setSize(CELL_SIZE * CANVAS_WIDTH * 2 / 3, CELL_SIZE * CANVAS_HEIGHT / 4);
setLocation(0, CELL_SIZE * CANVAS_HEIGHT * 3 / 4);
setBackground(Color.cyan);
setBackground(new Color(255, 255, 255, 150));
setBorder(BorderFactory.createLineBorder(new Color(213, 108, 18), 2));
setVisible(false);
updateDescription();
}
public void updateDescription() {
Timer timer = new Timer(1000, e -> {
if (isVisible()){
setVisible(true);
deletePreviousDescription();
addNewDescription();
revalidate();
repaint();
}
});
timer.start();
}
private void addNewDescription() {
if (getComponentCount() != 0) return;
@ -45,18 +38,20 @@ public class EnemyDescription extends JPanel {
LinkedList<BattlePerson> he = new LinkedList<>(windowOfBattle.getBattle().getHeroes());
he.addAll(windowOfBattle.getBattle().getEnemies());
setLayout(new GridLayout (he.size() + 1, 3));
setLayout(new GridLayout (he.size(), 3));
for (BattlePerson person: he){
Font f = new Font(Font.DIALOG, Font.BOLD, 20);
Font f = new Font(Font.SERIF, Font.BOLD, 20);
Font t = new Font(Font.SERIF, Font.PLAIN, 20);
JPanel jPanel = new JPanel();
jPanel.setOpaque(false);
JLabel name = new JLabel(person.getName());
name.setFont(f);
JLabel level = new JLabel("Level: " + person.getLevel());
level.setFont(f);
level.setFont(t);
JLabel health = new JLabel("Health: " + person.getHealth() + "/" + person.getMaxHealth());
health.setFont(f);
health.setFont(t);
jPanel.add(name);
jPanel.add(level);
jPanel.add(health);

View File

@ -24,12 +24,13 @@ public class ItemsInventory extends JPanel {
setSize(CELL_SIZE * 8, CELL_SIZE * 6);
setLocation(CELL_SIZE * 4, CELL_SIZE * 2);
setBackground(Color.yellow);
setBackground(new Color(255, 255, 255, 150));
setBorder(BorderFactory.createLineBorder(new Color(213, 108, 18), 2));
setVisible(false);
items = new JPanel();
JButton returnToMenu = new JButton("x");
JButton returnToMenu = window.style(new JButton(" x "));
returnToMenu.addActionListener(e -> {
window.getMenuOfActions().getAttack().setEnabled(true);
window.getMenuOfActions().getConsole().setEnabled(true);
@ -61,14 +62,16 @@ public class ItemsInventory extends JPanel {
jPanel.add(new JLabel(String.valueOf(item.getItemAction())));
if (item.getItemAction().equals(ItemAction.HEAL)){
JButton use = new JButton("Use");
JButton use = windowOfBattle.style(new JButton("Use"));
use.addActionListener(e -> {
setVisible(false);
windowOfBattle.getBattle().getHeroes().getFirst().heal(100);
windowOfBattle.getMenuOfActions().setVisible(false);
windowOfBattle.getEnemyDescription().setVisible(false);
windowOfBattle.getSituationText().setVisible(true);
windowOfBattle.writeToOutput(windowOfBattle.getBattle().getHeroes().getFirst().getName() + " heals 100 HP");
InventoryManager.removeItem(item, 1);
windowOfBattle.getBattle().addToTurnOrder(windowOfBattle.getBattle().getHeroes().getFirst());
windowOfBattle.getBattle().setTurnBelongTo(null);
windowOfBattle.getBattle().setDelay(2);
windowOfBattle.getBattle().getTimer().start();
@ -82,6 +85,4 @@ public class ItemsInventory extends JPanel {
items.setVisible(true);
if (items.getComponents().length == 0) items.setVisible(false);
}
}

View File

@ -18,13 +18,13 @@ public class MenuOfActions extends JPanel {
setSize(CELL_SIZE * CANVAS_WIDTH / 3, CELL_SIZE * CANVAS_HEIGHT / 4);
setLocation(CELL_SIZE * CANVAS_WIDTH * 2 / 3, CELL_SIZE * CANVAS_HEIGHT * 3 / 4);
setBackground(Color.green);
setVisible(false);
setBorder(BorderFactory.createLineBorder(new Color(213, 108, 18), 2));
attack = new JButton("Attack");
console = new JButton("Console");
inventory = new JButton("Items");
escape = new JButton("Run Away");
attack = window.style(new JButton("Attack"));
console = window.style(new JButton("Console"));
inventory = window.style(new JButton("Items"));
escape = window.style(new JButton("Run Away"));
attack.addActionListener(e -> {
attack.setEnabled(false);
@ -61,7 +61,7 @@ public class MenuOfActions extends JPanel {
inventory.setEnabled(false);
escape.setEnabled(false);
//TODO RunAway
window.getBattle().endBattle(true);
});
add(attack);

View File

@ -11,6 +11,13 @@ public class SituationText extends JPanel {
public SituationText(WindowOfBattle window) {
setSize(CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT / 4);
setLocation(0, CELL_SIZE * CANVAS_HEIGHT * 3 / 4);
setBackground(Color.BLUE);
setBackground(new Color(255, 255, 255, 150));
setBorder(BorderFactory.createLineBorder(new Color(213, 108, 18), 2));
}
public void hideText() {
JLabel label = (JLabel) getComponent(0);
label.setText("");
setVisible(false);
}
}

View File

@ -47,7 +47,11 @@ public class Person extends Cell {
}
public void setDirectionMove(int directionMove) {
if ((directionMove >= WindowOfFreeMove.KEY_LEFT) && (directionMove <= WindowOfFreeMove.KEY_EVENT))
if (directionMove == WindowOfFreeMove.KEY_LEFT ||
directionMove == WindowOfFreeMove.KEY_RIGHT ||
directionMove == WindowOfFreeMove.KEY_UP ||
directionMove == WindowOfFreeMove.KEY_DOWN ||
directionMove == WindowOfFreeMove.KEY_EVENT)
this.directionMove = directionMove;
}

View File

@ -13,9 +13,9 @@ public class Game extends JFrame {
private static final int WINDOW_HEIGHT = 13;
public static final int CELL_SIZE = 60;
private final WindowOfMenu menu;
private final WindowOfFreeMove freeMove;
private final WindowOfBattle battle;
private WindowOfMenu menu;
private WindowOfFreeMove freeMove;
private WindowOfBattle battle;
private TilesetOverview tilesetOverview;
public WindowOfMenu getMenu() {

View File

@ -142,7 +142,7 @@ public class WindowOfBattle extends JPanel {
enemies.add(enemy);
}
else if (data[0].equals("<music>")) {
musicManager = new MusicManager(data[1], Float.parseFloat(data[2]));
// musicManager = new MusicManager(data[1], Float.parseFloat(data[2]));
}
else if (data[0].equals("<image>")) {
if (data[1].equals("flicker")) canvas.flicker(Integer.parseInt(data[2]));
@ -197,6 +197,19 @@ public class WindowOfBattle extends JPanel {
repaint();
}
public JButton style(JButton button) {
Font f = new Font(Font.SERIF, Font.BOLD, 16);
Color c = new Color(255, 217, 112);
Color b = new Color(213, 108, 18);
button.setFont(f);
button.setBackground(c);
button.setForeground(b);
button.setBorder(BorderFactory.createLineBorder(b, 2));
button.setFocusPainted(false);
return button;
}
public Game getGame() {
return game;
}

View File

@ -14,7 +14,7 @@
<trigger>
<x>6</x>
<y>4</y>
<active>true</active>
<active>false</active>
</trigger>
<trigger>
<x>14</x>
@ -24,7 +24,7 @@
<chest>
<x>1</x>
<y>2</y>
<active>true</active>
<active>false</active>
</chest>
<chest>
<x>15</x>

View File

@ -0,0 +1,6 @@
<item>
<name>cup-o'-coffee</name>
<multiplicity>1</multiplicity>
<image>295</image>
<action>heal</action>
</item>

View File

@ -74,7 +74,7 @@ public class WindowOfFreeMoveTest {
@MethodSource("argumentsStream")
public void move_moveCharacterOnMap_CharacterChangesPositionWithCollisionDetection(int[] directions, int xPath, int yPath) {
//Arrage
//is in setUp()
freeMove.revalidate();
//Act
final int[] i = {0};