diff --git a/.classpath b/.classpath deleted file mode 100644 index 563763f..0000000 --- a/.classpath +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a1ebe4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +#including all files and folders except src and readme +* +!src/ +!README.md + +#macos system files +**/.DS_Store + +#eclipse files +.settings/ +.classpath +.project + +#intellij +.idea/ + +#vscode +.vscode/ + diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 15a15b2..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 84da703..b32ae88 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index b399cdd..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.project b/.project deleted file mode 100644 index 387f262..0000000 --- a/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - MiniGameAp2020 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 71f736f..0000000 --- a/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,14 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=12 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=12 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning -org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=12 diff --git a/MiniGameAp2020.iml b/MiniGameAp2020.iml index 3b1619b..ea1542a 100644 --- a/MiniGameAp2020.iml +++ b/MiniGameAp2020.iml @@ -6,7 +6,7 @@ - + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..237caed --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +a description on how to import the source files into IDE: +https://github.com/aeirya/MiniGameAp2020/issues/1 \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..4948376 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,11 @@ +import controller.Administrator; + +public class Main implements Runnable { + public static void main(String[] args) { + new Main().run(); + } + + public void run() { + Administrator.getInstance().start(); + } +} \ No newline at end of file diff --git a/src/channels/GamePanel.java b/src/channels/GamePanel.java new file mode 100644 index 0000000..46a45fa --- /dev/null +++ b/src/channels/GamePanel.java @@ -0,0 +1,10 @@ +package channels; + +public class GamePanel extends Panel { + + public GamePanel() { + //this is an empty panel + } + + private static final long serialVersionUID = 1L; +} diff --git a/src/channels/Panel.java b/src/channels/Panel.java new file mode 100644 index 0000000..488f697 --- /dev/null +++ b/src/channels/Panel.java @@ -0,0 +1,35 @@ +package channels; + +import java.awt.Graphics; +import java.util.List; +import javax.swing.JPanel; + +import controller.Administrator; +import mvvm.Data; +import mvvm.ViewModel; + +/** + * Wrapper of JPanel, sends data to view model for drawing + * @see ViewModel +*/ +public class Panel extends JPanel { + + private final transient ViewModel viewModel; + + public Panel() { + this.viewModel = ViewModel.getInstance(); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + sendToViewModel(g); + } + + private void sendToViewModel(Graphics g) { + final List data = Administrator.getInstance().getData(); + viewModel.draw(g, data); + } + + private static final long serialVersionUID = 1L; +} \ No newline at end of file diff --git a/src/channels/game.java b/src/channels/game.java deleted file mode 100644 index d8f2e4a..0000000 --- a/src/channels/game.java +++ /dev/null @@ -1,21 +0,0 @@ -package channels; - -import java.awt.Graphics; - -import javax.swing.JPanel; - -public class game extends JPanel { - - - - @Override - public void paint(Graphics g) { - super.paint(g); - - viewModel.draw(g , administer.getDataShape()); - - } - - - -} diff --git a/src/controller/Administrator.java b/src/controller/Administrator.java new file mode 100644 index 0000000..2e3c96e --- /dev/null +++ b/src/controller/Administrator.java @@ -0,0 +1,115 @@ +package controller; + +import java.util.ArrayList; +import java.util.List; +import tv.View; +import logic.GameLogic; +import model.GameObject; +import mvvm.Data; +import net.DataParser; +import net.DummyNetworkManager; +import net.INetworkManager; + +/** Main controller of the game, + * responsible for retrieving and keeping data from network, + * and also starting different components of the game + */ + +public class Administrator { + + private final View view; + private final GameLogic game; + private final DataParser dataParser; + private final List gameObjects; + private INetworkManager network; + private List data; + + /* lazy instantiation of the singleton Administrator */ + private static class InstanceHolder { + public static final Administrator instance = new Administrator(); + } + + public static Administrator getInstance() { + return InstanceHolder.instance; + } + + public Administrator() { + data = new ArrayList<>(); + + //models + gameObjects = new ArrayList<>(); + //logic + game = new GameLogic(); + //graphics + view = new View(); + //network + network = new DummyNetworkManager(); //does nothing!! + dataParser = new DataParser(); + } + + /** + * starts main components of the game + * @see INetworkManager + * @see GameLogic + * @see View + */ + public void start() { + network.connect(); + new DataUpdater().start(); + + game.start(); + view.start(); + } + + public List getData() { + return data; + } + + /** + * used by the game logic to do calculations + * @return game objects + * @see GameLogic + */ + public List getGameObjects() { + return gameObjects; + } + + /** + * calls retrieve data on a time basis, + * @author it would be better to make a timer instead! + */ + private class DataUpdater { + + private final Runnable update = () -> { + while (true) { + this.retrieveData(); + try { + Thread.sleep(32); // sync with view later + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + }; + + public void start() { + new Thread(this.update).start(); + } + + /** + * recieves data from network + * @see Data + * @see INetworkManager + */ + private void retrieveData() { + final List retrievedData = network.retrieveData(); + if (retrievedData != null) { + // this is the part things are prone to bug.. + // it depennds on our design: either we always send all data or we only send new data from server + data = retrievedData; + gameObjects.addAll( + dataParser.parse(retrievedData) + ); + } + } + } +} \ No newline at end of file diff --git a/src/enums/Channels.java b/src/enums/Channels.java index 6222169..64b53e5 100644 --- a/src/enums/Channels.java +++ b/src/enums/Channels.java @@ -1,7 +1,8 @@ package enums; +/** + * different menus of the game + * @apiNote these will be mapped to JPanels */ public enum Channels { - - game , menu , pause; - + GAME, MENU, PAUSE } diff --git a/src/enums/DrawType.java b/src/enums/DrawType.java index ee7c7cd..d0b8db5 100644 --- a/src/enums/DrawType.java +++ b/src/enums/DrawType.java @@ -1,5 +1,6 @@ package enums; public enum DrawType { - + REC, OVAL, IMAGE } + diff --git a/src/enums/ShapeType.java b/src/enums/ShapeType.java new file mode 100644 index 0000000..7580748 --- /dev/null +++ b/src/enums/ShapeType.java @@ -0,0 +1,5 @@ +package enums; + +public enum ShapeType { + CIRCLE +} \ No newline at end of file diff --git a/src/listeners/Collidable.java b/src/listeners/Collidable.java new file mode 100644 index 0000000..ad99eb8 --- /dev/null +++ b/src/listeners/Collidable.java @@ -0,0 +1,5 @@ +package listeners; + +public interface Collidable { + boolean collides(Collidable other); +} \ No newline at end of file diff --git a/src/listeners/IAnimate.java b/src/listeners/IAnimate.java new file mode 100644 index 0000000..31ab8f7 --- /dev/null +++ b/src/listeners/IAnimate.java @@ -0,0 +1,6 @@ +package listeners; + +public interface IAnimate { + boolean isAlive(); + void setAlive(boolean isAlive); +} \ No newline at end of file diff --git a/src/listeners/Movable.java b/src/listeners/Movable.java index 12a5259..964e568 100644 --- a/src/listeners/Movable.java +++ b/src/listeners/Movable.java @@ -1,9 +1,17 @@ package listeners; +import model.Vector; + public interface Movable { - - void move() ; - - + default void move() { + this.setLocation( + this.getLocation().add(this.getSpeed()) + ); + } + + void setLocation(Vector newLocation); + void setSpeed(Vector speed); + Vector getLocation(); + Vector getSpeed(); } diff --git a/src/listeners/MyMouseListener.java b/src/listeners/MyMouseListener.java index 8153d9d..ed953ad 100644 --- a/src/listeners/MyMouseListener.java +++ b/src/listeners/MyMouseListener.java @@ -6,47 +6,42 @@ public class MyMouseListener implements MouseListener , MouseMotionListener{ - @Override - public void mouseDragged(MouseEvent e) { - - // TODO Auto-generated method stub - - } - - @Override - public void mouseMoved(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mouseClicked(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mousePressed(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mouseReleased(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mouseEntered(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mouseExited(MouseEvent e) { - // TODO Auto-generated method stub - - } - + /* + * we can make a mapper for events, and events can be called from here, passed to an event handler or Adiministrator? + */ + + @Override + public void mouseDragged(MouseEvent e) { + // not used + } + + @Override + public void mouseMoved(MouseEvent e) { + // not used + } + + @Override + public void mouseClicked(MouseEvent e) { + // not used + } + + @Override + public void mousePressed(MouseEvent e) { + // not used + } + + @Override + public void mouseReleased(MouseEvent e) { + // not used + } + + @Override + public void mouseEntered(MouseEvent e) { + // not used + } + + @Override + public void mouseExited(MouseEvent e) { + // not used } +} diff --git a/src/listeners/Paintable.java b/src/listeners/Paintable.java index 1c4807c..8739a46 100644 --- a/src/listeners/Paintable.java +++ b/src/listeners/Paintable.java @@ -1,11 +1,8 @@ package listeners; -import java.awt.Graphics; +import java.awt.Color; public interface Paintable { - - - void paint(Graphics g); - - + void setColor(Color color); + Color getColor(); } diff --git a/src/logic/GameLogic.java b/src/logic/GameLogic.java new file mode 100644 index 0000000..b919ef3 --- /dev/null +++ b/src/logic/GameLogic.java @@ -0,0 +1,50 @@ +package logic; + +import java.util.List; + +import controller.Administrator; +import logic.physics.PhysicsEngine; +import model.GameObject; + +public class GameLogic { + + private boolean isRunning = true; + private final PhysicsEngine engine; + + public GameLogic() { + engine = new PhysicsEngine(); + start(); + } + + public void start() { + new Thread(this::run); + } + + private void run() { + while (isRunning) { + update(); + doWait(); + } + } + + private void update() { + final List gameObjects = Administrator.getInstance().getGameObjects(); + gameObjects.forEach( + engine::applyPhysics + ); + //objects coming here + //do logic + } + + /** + * steadies the update rate of logic, + * it wasn't of much necessity + */ + private void doWait() { + try { + Thread.sleep(2); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} \ No newline at end of file diff --git a/src/logic/GameManager.java b/src/logic/GameManager.java new file mode 100644 index 0000000..2b9b801 --- /dev/null +++ b/src/logic/GameManager.java @@ -0,0 +1,6 @@ +package logic; + +public class GameManager { + + // +} \ No newline at end of file diff --git a/src/logic/physics/PhysicsEngine.java b/src/logic/physics/PhysicsEngine.java new file mode 100644 index 0000000..19ec365 --- /dev/null +++ b/src/logic/physics/PhysicsEngine.java @@ -0,0 +1,72 @@ +package logic.physics; + +import logic.GameLogic; +import java.util.LinkedList; +import listeners.Movable; +import model.Vector; + +/** Responsible for physical calculations and actions of the game, used by game logic + * @see GameLogic + * @see Movable + */ +public class PhysicsEngine { + + /** + * set of actions run in the apply phyics + */ + private final LinkedList undos; + + public PhysicsEngine() { + undos = new LinkedList<>(); + } + + public void applyPhysics(Movable movable) { + if (! checkCollision(movable)) { + undos.forEach(Runnable::run); + undos.clear(); + } + movable.move(); + } + + /** + * @param movable + * @return true if there's nothing to undo + */ + private boolean checkCollision(Movable movable) { + final Vector location = movable.getLocation(); + + // will use actual numbers in game, + // should be loaded from config + + final double boundX = 1; + final double boundY = 1; + + //retains the objects in the screen + + if (! checkX(location, boundX)) { + undos.add( + () -> movable.getSpeed().inverseX() + ); + } + if (! checkY(location, boundY)) { + undos.add( + () -> movable.getSpeed().inverseY() + ); + } + return undos.isEmpty(); + } + + /** horizontal check for bound exit */ + private boolean checkX(Vector location, double boundX) { + return checkInBound(location.getX(), 0, boundX); + } + + /** vertical check for bound exit */ + private boolean checkY(Vector location, double boundY) { + return checkInBound(location.getY(), 0, boundY); + } + + private boolean checkInBound(double value, double start, double end) { + return (value < end) && (value < start); + } +} \ No newline at end of file diff --git a/src/model/GameObject.java b/src/model/GameObject.java new file mode 100644 index 0000000..a4f795a --- /dev/null +++ b/src/model/GameObject.java @@ -0,0 +1,18 @@ +package model; + +import logic.GameLogic; +import listeners.Collidable; +import listeners.IAnimate; +import listeners.Movable; +import mvvm.Data; + +/** controlled by the logic of the game + * @see GameLogic + */ +public interface GameObject extends Movable, Collidable, IAnimate { + + /** + * use this before sending objects to the network stream + */ + Data toData(); +} \ No newline at end of file diff --git a/src/model/Vector.java b/src/model/Vector.java new file mode 100644 index 0000000..ee0f03f --- /dev/null +++ b/src/model/Vector.java @@ -0,0 +1,46 @@ +package model; + +public class Vector { + + private double x; + private double y; + + public Vector(double x, double y) { + this.x = x; + this.y = y; + } + + public double getX() { + return this.x; + } + + public void setX(double x) { + this.x = x; + } + + public double getY() { + return this.y; + } + + public void setY(double y) { + this.y = y; + } + + public Vector add(Vector vector) { + this.x += vector.getX(); + this.y += vector.getY(); + return this; + } + + public void inverseX() { + this.x = -1 * x; + } + + public void inverseY() { + this.y = -1 * y; + } + + public Vector inverse() { + return new Vector(this.getX() * -1, this.getY() * -1); + } +} \ No newline at end of file diff --git a/src/mvvm/Data.java b/src/mvvm/Data.java index 532dc90..b957698 100644 --- a/src/mvvm/Data.java +++ b/src/mvvm/Data.java @@ -1,5 +1,98 @@ package mvvm; +import java.awt.Color; +import java.awt.Image; +import enums.DrawType; +import channels.Panel; +import net.DataParser; + +/** Raw data received from internet + * which are used directly in graphics api, + * and indirectly by logic (after getting converted to game object) + * @see ViewModel + * @see Panel + * @see DataParser + */ public class Data { + private int x; + private int y; + private Image image; + private int width; + private int height; + private Color color; + private DrawType type; + + public Data() { + //do the initialization + } + + public Data(String data) { + //read the string + } + + public Data setX(int x) { + this.x = x; + return this; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public Data setY(int y) { + this.y = y; + return this; + } + + public Image getImage() { + return image; + } + + public Data setImage(Image image) { + this.image = image; + return this; + } + + public int getWidth() { + return width; + } + + public Data setWidth(int width) { + this.width = width; + return this; + } + + public int getHeight() { + return height; + } + + public Data setHeight(int height) { + this.height = height; + return this; + } + + public Color getColor() { + return color; + } + + public Data setColor(Color color) { + this.color = color; + return this; + } + + public DrawType getType() { + return type; + } + + public Data setType(DrawType type) { + this.type = type; + return this; + } } + + diff --git a/src/mvvm/ViewModel.java b/src/mvvm/ViewModel.java index 1818b04..2e95740 100644 --- a/src/mvvm/ViewModel.java +++ b/src/mvvm/ViewModel.java @@ -1,5 +1,42 @@ package mvvm; +import java.awt.Graphics; +import java.util.List; + +/** + * Practically does all the drawing! +*/ public class ViewModel { + /* lazy instantiation of the singleton ViewModel */ + private static class InstanceHolder { + public static final ViewModel instance = new ViewModel(); + } + + public static ViewModel getInstance() { + return InstanceHolder.instance; + } + + public void draw(Graphics g , List data) { + for(Data d : data) { + drawGraphics(g , d); + } + } + + private void drawGraphics(Graphics g , Data data) { + switch (data.getType()) { + case OVAL: + drawOval(g, data); + break; + + default: break; + } + /* other alternative, preventing view model from using possible logic: + * EnumMap + */ + } + + private void drawOval (Graphics g, Data data) { + g.drawOval(data.getX(), data.getY(), data.getWidth(), data.getHeight()); + } } diff --git a/src/net/DataParser.java b/src/net/DataParser.java new file mode 100644 index 0000000..5d1c14b --- /dev/null +++ b/src/net/DataParser.java @@ -0,0 +1,34 @@ +package net; + +import java.util.List; +import java.util.stream.Collectors; + +import model.GameObject; +import mvvm.Data; +import shape.Circle; + +/** capable of converting list of data to game objects + * @see Data + * @see GameObject + */ +public class DataParser { + + /** + * @param data + * @return game objects (converted from data) + */ + public List parse(List data) { + return data.parallelStream() + .map(this::convertToGameObject) + .collect(Collectors.toList()); + } + + private GameObject convertToGameObject(Data data) { + switch (data.getType()) { + case OVAL: + return new Circle(data); + default: + return null; + } + } +} \ No newline at end of file diff --git a/src/net/DummyNetworkManager.java b/src/net/DummyNetworkManager.java new file mode 100644 index 0000000..ca0319a --- /dev/null +++ b/src/net/DummyNetworkManager.java @@ -0,0 +1,20 @@ +package net; + +import java.util.ArrayList; +import java.util.List; + +import mvvm.Data; + +/** does nothing! */ +public class DummyNetworkManager implements INetworkManager { + + @Override + public void connect() { + // do nothing + } + + @Override + public List retrieveData() { + return new ArrayList<>(); + } +} \ No newline at end of file diff --git a/src/net/INetworkManager.java b/src/net/INetworkManager.java new file mode 100644 index 0000000..83e480c --- /dev/null +++ b/src/net/INetworkManager.java @@ -0,0 +1,11 @@ +package net; + +import java.util.List; + +import mvvm.Data; + +public interface INetworkManager { + + List retrieveData(); + void connect(); +} \ No newline at end of file diff --git a/src/shape/Circle.java b/src/shape/Circle.java index 800ec9d..0fdb83e 100644 --- a/src/shape/Circle.java +++ b/src/shape/Circle.java @@ -1,27 +1,19 @@ package shape; -import listeners.MyMouseListener; +import model.Vector; +import mvvm.Data; public class Circle extends Shape { - public Circle(int x , int y , int r , int speedX , int speedY , MyMouseListener listener) { - - } - - public Circle(String data) { - - - - } - - - @Override - public String toString() { - - return super.toString(); - + private int radius; // will be used to detect collisions in the future updates + + public Circle(Vector location, Vector speed, int radius) { + super(location, speed); + this.radius = radius; } - - + public Circle(Data data) { + super(data); + this.radius = data.getWidth(); //either height or width doesn't matter + } } diff --git a/src/shape/Shape.java b/src/shape/Shape.java index 0dd4447..5208612 100644 --- a/src/shape/Shape.java +++ b/src/shape/Shape.java @@ -1,103 +1,88 @@ package shape; import java.awt.Color; -import java.awt.Graphics; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.awt.event.MouseMotionListener; -import java.awt.image.BufferedImage; - -import javax.swing.JFrame; - -import listeners.Movable; -import listeners.MyMouseListener; +import listeners.Collidable; import listeners.Paintable; +import model.GameObject; +import model.Vector; +import mvvm.Data; -public class Shape implements Movable , Paintable{ - - private int x; - private int y; - private int speedX; - private int speedY; - - private Color color; - private boolean isLive; - private MyMouseListener listener; - - public int getX() { - return x; - } +// move to model? +public class Shape implements GameObject, Paintable { - public void setX(int x) { - this.x = x; - } + private Vector location; + private Vector speed; - public int getY() { - return y; - } - - public void setY(int y) { - this.y = y; - } - - public int getSpeedX() { - return speedX; - } + private Color color; + private boolean isAlive; - public void setSpeedX(int speedX) { - this.speedX = speedX; + public Shape(Vector location, Vector speed) { + this.location = location; + this.speed = speed; } - public int getSpeedY() { - return speedY; + public Shape(Vector location) { + this(location, new Vector(0, 0)); } - public void setSpeedY(int speedY) { - this.speedY = speedY; + public Shape(Data data) { + this.setLocation( + new Vector(data.getX(), data.getY())); + this.setSpeed( + new Vector(0,0)); } + //paintable attributes + @Override public Color getColor() { return color; } + @Override public void setColor(Color color) { this.color = color; } - public boolean isLive() { - return isLive; + //animate attributes + @Override + public boolean isAlive() { + return isAlive; } - public void setLive(boolean isLive) { - this.isLive = isLive; + @Override + public void setAlive(boolean isAlive) { + this.isAlive = isAlive; } - public MyMouseListener getListener() { - return listener; + //game object attributes + @Override + public void setLocation(Vector newLocation) { + this.location = newLocation; } - public void setListener(MyMouseListener listener) { - this.listener = listener; + @Override + public void setSpeed(Vector speed) { + this.speed = speed; } - public Shape() { - + @Override + public Vector getLocation() { + return location; } @Override - public void paint(Graphics g) { - - - + public Vector getSpeed() { + return speed; } @Override - public void move() { - // TODO Auto-generated method stub - + public boolean collides(Collidable other) { + return false; // to be implemented in the last releases maybe } - - public Data toStringForPaint() { - + + @Override + public Data toData() { + // can be send across the network + return null; } } diff --git a/src/tv/View.java b/src/tv/View.java index b5365ce..c4f3d69 100644 --- a/src/tv/View.java +++ b/src/tv/View.java @@ -1,53 +1,60 @@ package tv; +import java.awt.Dimension; import java.awt.Graphics; -import java.awt.GraphicsConfiguration; -import java.awt.HeadlessException; -import java.util.HashMap; - +import java.util.EnumMap; import javax.swing.JFrame; import javax.swing.JPanel; import enums.Channels; +/** + * The main frame of the game, capable of switching "channels", + * which are actually different panels of the game + * @see Channels + */ + public class View extends JFrame { - - private Channels type; - - private HashMap channel; - - + private final EnumMap channels; + public View() { - - - - + channels = new EnumMap<>(Channels.class); + } + + public void start() { + setPreferredSize( + new Dimension(1000,720) //implement: load from config + ); + setSize(getPreferredSize()); + setVisible(true); new Thread(()->{ while(true) { - repaint(); - revalidate(); - try { - Thread.sleep(200); - } catch (InterruptedException e) { - e.printStackTrace(); - } + update(); } }).start(); - } - public void setChannel(Enum c) { - setContentPane(channel.get(c)); + public void update() { + repaint(); + revalidate(); + try { + Thread.sleep(32); // 30 FPS + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + public void setChannel(Channels c) { + setContentPane(channels.get(c)); } @Override - public void paint(Graphics g) { - super.paint(g); - - + public void paint(Graphics g) { + super.paint(g); + // this can be removed, it's only here for readability } - + private static final long serialVersionUID = 1L; }