From c584472bb7753261d515202156f8c6ea168bede6 Mon Sep 17 00:00:00 2001 From: Kleme Date: Thu, 14 Sep 2017 12:43:24 +0200 Subject: [PATCH] 1.7.1 : spawn multiple mario within instance and more options --- src/fr/klemek/minimario/Launch.java | 148 ++++++++++++++++++++++- src/fr/klemek/minimario/MarioAI.java | 49 ++++++-- src/fr/klemek/minimario/MarioWindow.java | 142 +++++++++++----------- 3 files changed, 253 insertions(+), 86 deletions(-) diff --git a/src/fr/klemek/minimario/Launch.java b/src/fr/klemek/minimario/Launch.java index c8e3a55..36a8bbf 100644 --- a/src/fr/klemek/minimario/Launch.java +++ b/src/fr/klemek/minimario/Launch.java @@ -1,23 +1,167 @@ package fr.klemek.minimario; +import java.awt.AWTException; +import java.awt.Menu; +import java.awt.MenuItem; +import java.awt.PopupMenu; +import java.awt.SystemTray; +import java.awt.TrayIcon; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; import javax.swing.SwingUtilities; -public class Launch { +public abstract class Launch { + private static final String VERSION = "1.7.1"; + + private static List windows; + + private static PopupMenu popup; + private static Menu sizeMenu; + private static MenuItem add, split, kill, exit; + + private static int currentFactor = 2; + public static void main(String[] args) { + windows = new ArrayList<>(); + SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { - new MarioWindow(); + windows.add(new MarioWindow(null, currentFactor, null)); + addTrayIcon(windows.get(0)); } catch (IOException e) { e.printStackTrace(); } } }); } + + public static void addTrayIcon(MarioWindow mwRef){ + if (!SystemTray.isSupported()) { + System.out.println("SystemTray is not supported"); + System.exit(0); + } + + TrayIcon trayIcon = new TrayIcon(Utils.createImage("/icon_"+mwRef.getTilesetName()+".png", "icon")); + trayIcon.setImageAutoSize(true); + popup = new PopupMenu(); + + sizeMenu = new Menu("Change size"); + + for(int i = 0; i < 5; i++){ + final int f = (int)Math.pow(2, i); + MenuItem size = new MenuItem(f+"x"); + size.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + for(MarioWindow mw:windows) + mw.setFactor(f); + currentFactor = f; + refreshPopupMenu(); + } + }); + sizeMenu.add(size); + } + + add = new MenuItem("Another one"); + add.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + try { + windows.add(new MarioWindow(null, currentFactor, null)); + refreshPopupMenu(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + }); + + split = new MenuItem("Split"); + split.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + List save = new ArrayList<>(); + save.addAll(windows); + windows.clear(); + currentFactor /= 2; + for(MarioWindow mw:save){ + try { + MarioWindow child1 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName()); + MarioWindow child2 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName()); + child1.getAi().run(false); + child2.getAi().run(true); + windows.add(child1); + windows.add(child2); + } catch (IOException e1) { + e1.printStackTrace(); + } + mw.kill(); + } + save = null; + System.gc(); + refreshPopupMenu(); + } + }); + + kill = new MenuItem("Kill one randomly"); + kill.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + Random r = new Random(); + int index = r.nextInt(windows.size()); + windows.get(index).kill(); + windows.remove(index); + System.gc(); + refreshPopupMenu(); + } + }); + + exit = new MenuItem("Kill it !"); + exit.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }); + + + + refreshPopupMenu(); + trayIcon.setPopupMenu(popup); + trayIcon.setToolTip("MiniMario ! (version "+VERSION+")\nBy Klemek"); + + final SystemTray tray = SystemTray.getSystemTray(); + try { + tray.add(trayIcon); + } catch (AWTException e) { + System.out.println("TrayIcon could not be added."); + System.exit(0); + } + } + + private static void refreshPopupMenu(){ + popup.removeAll(); + popup.add(sizeMenu); + popup.add(add); + if(currentFactor > 1){ + popup.add(split); + } + if(windows.size()>1){ + exit.setLabel("Kill them all !"); + popup.add(kill); + }else{ + exit.setLabel("Kill it !"); + } + popup.add(exit); + } + } diff --git a/src/fr/klemek/minimario/MarioAI.java b/src/fr/klemek/minimario/MarioAI.java index f1185e1..64e3f18 100644 --- a/src/fr/klemek/minimario/MarioAI.java +++ b/src/fr/klemek/minimario/MarioAI.java @@ -1,6 +1,7 @@ package fr.klemek.minimario; import java.awt.Point; +import java.awt.Rectangle; import java.awt.geom.Point2D; import java.util.Random; @@ -38,6 +39,8 @@ public class MarioAI { STILL, STILL_BACK, STILL_WIN, STILL_DUCK, STILL_LOOK_UP, WALKING, RUNNING, JUMPING, LOOSING } + private int id; + private State state; private int time, time2; private float spdy, maxspdy; @@ -45,12 +48,15 @@ public class MarioAI { private Point2D.Float pos; private boolean left, wait, turn; - private Random rand; + private static Random rand = new Random(); private final int minx,maxx; private int sizex, sizey, speedf; + //constructor + public MarioAI(int sizex, int sizey, int speedf){ + this.id = rand.nextInt(); this.minx = Utils.getMinX(); this.maxx = Utils.getMaxX(); this.sizex = sizex; @@ -59,18 +65,9 @@ public class MarioAI { this.state = State.STILL; this.time = 0; this.pos = new Point2D.Float(); - this.rand = new Random(); } - public void setSize(int sizex, int sizey, int speedf){ - this.sizex = sizex; - this.sizey = sizey; - this.speedf = speedf; - } - - public void setPos(Point newpos){ - this.pos = new Point2D.Float(newpos.x, newpos.y); - } + //functions public void moved(Point newpos){ this.pos = new Point2D.Float(newpos.x, newpos.y); @@ -227,6 +224,28 @@ public class MarioAI { return new Point((int)this.pos.x, (int)this.pos.y); } + public void run(boolean left){ + this.turn = false; + this.left = left; + this.time2 = this.time+rand.nextInt(MAX_WALKING_TIME); + this.wait = false; + this.spdy = -rand.nextFloat()*speedf*MAX_JUMP_SPEED_Y-speedf*MIN_JUMP_SPEED_Y; + this.maxspdy = rand.nextFloat()*speedf*MAX_JUMP_SPEED_Y+speedf*MIN_JUMP_SPEED_Y; + this.state = State.JUMPING; + } + + //getter/setter + + public void setSize(int sizex, int sizey, int speedf){ + this.sizex = sizex; + this.sizey = sizey; + this.speedf = speedf; + } + + public void setPos(Point newpos){ + this.pos = new Point2D.Float(newpos.x, newpos.y); + } + public int getTile(){ if(this.turn){ if(this.time%200>=100){ @@ -262,4 +281,12 @@ public class MarioAI { return this.left; } + public Rectangle getBounds(){ + return new Rectangle((int)pos.x,(int)pos.y,sizex,sizey); + } + + public int getId() { + return id; + } + } diff --git a/src/fr/klemek/minimario/MarioWindow.java b/src/fr/klemek/minimario/MarioWindow.java index c42ad9e..924389a 100644 --- a/src/fr/klemek/minimario/MarioWindow.java +++ b/src/fr/klemek/minimario/MarioWindow.java @@ -1,13 +1,7 @@ package fr.klemek.minimario; -import java.awt.AWTException; import java.awt.Color; -import java.awt.Menu; -import java.awt.MenuItem; import java.awt.Point; -import java.awt.PopupMenu; -import java.awt.SystemTray; -import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; @@ -24,8 +18,6 @@ import javax.swing.Timer; public class MarioWindow extends JWindow implements ActionListener { private static final long serialVersionUID = 1L; - - private static final String VERSION = "1.6.2"; private static final int REFRESH_MS = 20; private static final int TILE_W = 20; @@ -33,32 +25,46 @@ public class MarioWindow extends JWindow implements ActionListener { private TilePanel p; private final Timer refresh; - + private int factor = 2; private MarioAI ai; private Point initialClick; - public MarioWindow() throws IOException { + private String tilesetName; + + //constructor + + public MarioWindow(Point start, int factor, String tilesetName) throws IOException { this.setBackground(new Color(0, 0, 0, 0)); - this.setLocationRelativeTo(null); + if(start == null) + this.setLocationRelativeTo(null); + else + this.setLocation((int)(start.x + TILE_W*factor/2f), (int)( start.y + TILE_H*factor/2f)); + + this.factor = factor; + this.ai = new MarioAI(TILE_W*factor,TILE_H*factor, factor); - Random r = new Random(); - String tileset_name = "mario"; - if(r.nextInt(100)>=90){ //90-99 - 10% - tileset_name = "luigi"; + if(tilesetName == null){ + Random r = new Random(); + tilesetName = "mario"; + if(r.nextInt(100)>=90){ //90-99 - 10% + tilesetName = "luigi"; + } + + if(r.nextInt(100)>=99){ //99 - 1% + tilesetName += "_fire"; + } } - if(r.nextInt(100)>=99){ //99 - 1% - tileset_name += "_fire"; - } + this.tilesetName = tilesetName; - BufferedImage tileset = ImageIO.read(this.getClass().getResource("/"+tileset_name+".png")); + BufferedImage tileset = ImageIO.read(this.getClass().getResource("/"+tilesetName+".png")); this.p = new TilePanel(this, tileset, TILE_W, TILE_H, factor); this.p.addMouseListener(new MouseAdapter() { @@ -94,70 +100,22 @@ public class MarioWindow extends JWindow implements ActionListener { this.pack(); this.setAlwaysOnTop(true); - - if (!SystemTray.isSupported()) { - System.out.println("SystemTray is not supported"); - return; - } - TrayIcon trayIcon = new TrayIcon(Utils.createImage("/icon_"+tileset_name+".png", "icon")); - trayIcon.setImageAutoSize(true); - - final PopupMenu popup = new PopupMenu(); - - - - Menu sizeMenu = new Menu("Change size"); - popup.add(sizeMenu); - - for(int i = 0; i < 5; i++){ - final int f = (int)Math.pow(2, i); - MenuItem size = new MenuItem(f+"x"); - size.addActionListener(new ActionListener(){ - @Override - public void actionPerformed(ActionEvent e) { - setFactor(f); - } - }); - sizeMenu.add(size); - } - - MenuItem exit = new MenuItem("Kill it !"); - exit.addActionListener(new ActionListener(){ - @Override - public void actionPerformed(ActionEvent e) { - System.exit(0); - } - }); - popup.add(exit); - - trayIcon.setPopupMenu(popup); - trayIcon.setToolTip("MiniMario ! (version "+VERSION+")\nBy Klemek"); - - final SystemTray tray = SystemTray.getSystemTray(); - try { - tray.add(trayIcon); - } catch (AWTException e) { - System.out.println("TrayIcon could not be added."); - } - - this.setVisible(true); - + this.ai.setPos(this.getLocation()); this.refresh.start(); } - private void setFactor(int factor){ + //functions + + public void kill(){ this.setVisible(false); - this.factor = factor; - this.ai.setSize(TILE_W*factor,TILE_H*factor, factor); - this.p.setFactor(factor); - this.pack(); - this.setVisible(true); } + //events + @Override public void actionPerformed(ActionEvent e) { if (e.getSource().equals(this.refresh)) { @@ -166,4 +124,42 @@ public class MarioWindow extends JWindow implements ActionListener { this.setAlwaysOnTop(true); } } + + //getter/setter + + public Point getCenter(){ + return new Point((int)this.getBounds().getCenterX(),(int)this.getBounds().getCenterY()); + } + + public String getTilesetName() { + return tilesetName; + } + + public MarioAI getAi() { + return ai; + } + + public void setFactor(int factor){ + + int stx = this.getX(); + int sty = this.getY(); + + this.setVisible(false); + + int dpx = TILE_W*(factor-this.factor); + int dpy = TILE_H*(factor-this.factor); + + this.factor = factor; + + this.ai.setSize(TILE_W*factor,TILE_H*factor, factor); + this.p.setFactor(factor); + + this.pack(); + + this.setLocation((int)(stx-dpx/2f), (int)(sty-dpy/2f)); + this.ai.setPos(this.getLocation()); + + this.setVisible(true); + + } }