@@ -15,10 +15,12 @@ public class GameControl implements MouseListener, KeyListener, WindowFocusListe
|
||||
GameModel model;
|
||||
GameView view;
|
||||
|
||||
|
||||
public GameControl(GameModel model, GameView view)
|
||||
{
|
||||
this.model = model;
|
||||
this.view = view;
|
||||
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {}
|
||||
@@ -45,4 +47,6 @@ public class GameControl implements MouseListener, KeyListener, WindowFocusListe
|
||||
public void windowGainedFocus(WindowEvent e) {}
|
||||
|
||||
public void windowLostFocus(WindowEvent e) {}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package control;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import model.gameState.GameState;
|
||||
import model.gameState.MenuState;
|
||||
import model.gameState.PlayState;
|
||||
|
||||
public class GameStateManager {
|
||||
|
||||
private List<GameState> gamestates;
|
||||
public GameState currentState;
|
||||
private int index = 0;
|
||||
|
||||
public GameStateManager(){
|
||||
gamestates = new ArrayList<GameState>();
|
||||
gamestates.add(new MenuState(this));
|
||||
gamestates.add(new PlayState(this));
|
||||
currentState = gamestates.get(0);
|
||||
}
|
||||
|
||||
public void next() {
|
||||
index++;
|
||||
index %= gamestates.size();
|
||||
}
|
||||
|
||||
public void update(){
|
||||
currentState = gamestates.get(index);
|
||||
currentState.update();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package image;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import main.Main;
|
||||
|
||||
public class Images{
|
||||
|
||||
public static ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
|
||||
|
||||
public Images(){
|
||||
|
||||
}
|
||||
|
||||
static{
|
||||
try{
|
||||
images.add(ImageIO.read(Main.class.getResource("/image/player.png")));
|
||||
|
||||
}catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static BufferedImage getImage(ImageType img)
|
||||
{
|
||||
return images.get(img.ordinal());
|
||||
}
|
||||
|
||||
public enum ImageType
|
||||
{
|
||||
player
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 8.1 KiB |
@@ -10,6 +10,11 @@ import view.GameView;
|
||||
import control.GameControl;
|
||||
|
||||
public class Window extends JFrame {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public Window()
|
||||
{
|
||||
//Create window
|
||||
|
||||
+22
-1
@@ -1,13 +1,34 @@
|
||||
package model;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.Timer;
|
||||
|
||||
import view.GameView;
|
||||
|
||||
public class GameModel {
|
||||
public class GameModel implements ActionListener{
|
||||
|
||||
GameView view;
|
||||
Timer update;
|
||||
Player player;
|
||||
|
||||
|
||||
public GameModel(GameView view)
|
||||
{
|
||||
this.view = view;
|
||||
player = new Player(1920/2, 1080/2);
|
||||
update = new Timer(1000/10, this);
|
||||
update.start();
|
||||
}
|
||||
|
||||
public void update(){
|
||||
player.update();
|
||||
view.setPlayer(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package model;
|
||||
|
||||
import image.Images;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Player {
|
||||
|
||||
private Point2D middlePoint;
|
||||
private int index = 2,width,height;
|
||||
private AffineTransform transform;
|
||||
private BufferedImage img;
|
||||
|
||||
public Player(int x, int y){
|
||||
middlePoint = new Point2D.Double(x, y);
|
||||
transform = new AffineTransform();
|
||||
img = Images.getImage(Images.ImageType.player);
|
||||
width = img.getWidth();
|
||||
height = img.getHeight();
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2){//
|
||||
g2.drawImage(img, transform, null);
|
||||
g2.setPaint(Color.RED);
|
||||
}
|
||||
|
||||
public void update(){
|
||||
transform = new AffineTransform();
|
||||
transform.rotate(Math.toRadians(index*45),middlePoint.getX(),middlePoint.getY());
|
||||
transform.translate(middlePoint.getX() - width/2, middlePoint.getY() - height*2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package model.gameState;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import control.GameStateManager;
|
||||
|
||||
public abstract class GameState {
|
||||
|
||||
protected GameStateManager gsm;
|
||||
|
||||
public GameState(GameStateManager gsm) {
|
||||
super();
|
||||
this.gsm = gsm;
|
||||
}
|
||||
|
||||
public abstract void init();
|
||||
public abstract void update();
|
||||
public abstract void draw(Graphics2D g2);
|
||||
public abstract void keyPressed(KeyEvent e);
|
||||
public abstract void keyReleased(KeyEvent e);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package model.gameState;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import control.GameStateManager;
|
||||
|
||||
public class MenuState extends GameState {
|
||||
|
||||
public MenuState(GameStateManager gsm) {
|
||||
super(gsm);
|
||||
}
|
||||
@Override
|
||||
public void init() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2) {
|
||||
g2.fillRect(10, 10, 100, 10);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if(e.getKeyCode() == KeyEvent.VK_Q){
|
||||
gsm.next();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package model.gameState;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
import model.objects.InfoPanel;
|
||||
import model.objects.Lines;
|
||||
import control.GameStateManager;
|
||||
|
||||
public class PlayState extends GameState{
|
||||
|
||||
public static final Rectangle2D borderRect = new Rectangle2D.Double(256, 0, 1024, 1024);
|
||||
private Lines lines;
|
||||
private InfoPanel hsb;
|
||||
public PlayState(GameStateManager gsm) {
|
||||
super(gsm);
|
||||
lines = new Lines((int) borderRect.getX(),100);
|
||||
hsb = new InfoPanel(0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2) {
|
||||
hsb.draw(g2);
|
||||
g2.setClip(borderRect);
|
||||
lines.draw(g2);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package model.objects;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
|
||||
public class InfoPanel {
|
||||
|
||||
private int x, y;
|
||||
|
||||
public InfoPanel(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2){
|
||||
g2.drawRect(x, y, 256, 1024);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package model.objects;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Polygon;
|
||||
import java.awt.Shape;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.geom.Line2D;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Lines {
|
||||
|
||||
private List<Shape> lines;
|
||||
private Polygon octagon;
|
||||
|
||||
|
||||
public Lines(int xToRight, int sizeOctagon) {
|
||||
super();
|
||||
lines = new ArrayList<Shape>();
|
||||
int middlePointX = 512+xToRight;
|
||||
int middlePointY = 512;
|
||||
int amountOfAngles = 8;
|
||||
octagon = new Polygon();
|
||||
for(int i = 0; i < amountOfAngles; i++){
|
||||
octagon.addPoint((int)(middlePointX+sizeOctagon*Math.cos(i*Math.PI/(amountOfAngles/2))),
|
||||
(int)(middlePointY+sizeOctagon*Math.sin(i*Math.PI/(amountOfAngles/2))));
|
||||
}
|
||||
AffineTransform t = new AffineTransform();
|
||||
for(int i = 0; i < 8; i++){
|
||||
t.rotate(Math.toRadians(45), middlePointX, 512);
|
||||
lines.add(t.createTransformedShape(new Line2D.Double(middlePointX, -213, middlePointX, 512)));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2){
|
||||
for(Shape s : lines){
|
||||
g2.draw(s);
|
||||
}
|
||||
g2.draw(octagon);
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,16 @@ import java.awt.event.ActionListener;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
import model.Player;
|
||||
|
||||
public class GameView extends JPanel implements ActionListener{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1939480784205689618L;
|
||||
Timer t;
|
||||
Player player;
|
||||
|
||||
public GameView()
|
||||
{
|
||||
@@ -26,5 +33,12 @@ public class GameView extends JPanel implements ActionListener{
|
||||
{
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
if(player != null)
|
||||
player.draw(g2d);
|
||||
}
|
||||
|
||||
public void setPlayer(Player player){
|
||||
this.player = player;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user