Refactor all files and add some camera controls
This commit is contained in:
1 parent
04f8b8b399
commit
ac6de165c2
13 files changed
+286
-212
No files matched your search
@@ -0,0 +1,24 @@
|
||||
package net.xfe.jmtd;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Dimension;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import net.xfe.jmtd.panels.GamePanel;
|
||||
|
||||
public class JMTD {
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame();
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
Container contentPane = frame.getContentPane();
|
||||
|
||||
JPanel panel = new GamePanel();
|
||||
panel.setPreferredSize(new Dimension(400, 400));
|
||||
contentPane.add(panel, BorderLayout.CENTER);
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.xfe.jmtd.math;
|
||||
|
||||
public class iVec2d {
|
||||
public Integer x;
|
||||
public Integer y;
|
||||
|
||||
public iVec2d() {
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
}
|
||||
|
||||
public iVec2d(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public iVec2d add(iVec2d other) {
|
||||
return new iVec2d(this.x + other.x, this.y + other.y);
|
||||
}
|
||||
|
||||
public iVec2d sub(iVec2d other) {
|
||||
return new iVec2d(this.x - other.x, this.y - other.y);
|
||||
}
|
||||
|
||||
public Integer mult(iVec2d other) {
|
||||
return (this.x * other.x) + (this.y * other.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + x + ", " + y + ")";
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package main.java.xfe.model;
|
||||
package net.xfe.jmtd.model;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import net.xfe.jmtd.math.iVec2d;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@@ -12,8 +12,9 @@ import lombok.AllArgsConstructor;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public abstract class Entity {
|
||||
private Integer x;
|
||||
private Integer y;
|
||||
protected iVec2d pos;
|
||||
|
||||
public abstract void draw(Graphics2D g2d, Point p);
|
||||
public abstract void draw(Graphics2D g2d, iVec2d p);
|
||||
|
||||
public void doTick() {}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package net.xfe.jmtd.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
|
||||
import lombok.Getter;
|
||||
import net.xfe.jmtd.math.iVec2d;
|
||||
import net.xfe.jmtd.model.entities.Player;
|
||||
|
||||
@Getter
|
||||
public class World {
|
||||
private Player player;
|
||||
private List<Entity> entities;
|
||||
|
||||
public World() {
|
||||
this.entities = new ArrayList<>();
|
||||
this.player = new Player(0, 0);
|
||||
}
|
||||
|
||||
public void addEntity(Entity entity) {
|
||||
this.entities.add(entity);
|
||||
}
|
||||
|
||||
public void doTick() {
|
||||
for (Entity entity : this.entities) {
|
||||
entity.doTick();
|
||||
}
|
||||
|
||||
player.doTick();
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d, final iVec2d size) {
|
||||
for (Entity entity : this.entities) {
|
||||
final iVec2d entityPos = entity.getPos();
|
||||
|
||||
entity.draw(g2d, translatePoint(entityPos, size));
|
||||
}
|
||||
|
||||
player.draw(g2d, new iVec2d(size.x / 2, size.y / 2));
|
||||
}
|
||||
|
||||
// Translate from World pos -> Screen pos
|
||||
private iVec2d translatePoint(iVec2d p, final iVec2d size) {
|
||||
// First, treat the center of the screen like (0, 0)
|
||||
iVec2d p2 = new iVec2d(size.x / 2, size.y / 2);
|
||||
|
||||
return p.add(p2).add(this.player.getPos());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package net.xfe.jmtd.model.entities;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
|
||||
import net.xfe.jmtd.math.iVec2d;
|
||||
import net.xfe.jmtd.model.Entity;
|
||||
|
||||
public class Player extends Entity {
|
||||
|
||||
private final Color color = Color.PINK;
|
||||
private static final Integer WIDTH = 20;
|
||||
private static final Integer RADIUS = WIDTH / 2;
|
||||
private iVec2d velocity;
|
||||
|
||||
public Player(Integer x, Integer y) {
|
||||
super(new iVec2d(x, y));
|
||||
this.velocity = new iVec2d();
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d, iVec2d p) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(p.x - RADIUS, p.y - RADIUS, WIDTH, WIDTH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doTick() {
|
||||
this.pos = pos.add(velocity);
|
||||
}
|
||||
|
||||
public iVec2d getVelocity() {
|
||||
return this.velocity;
|
||||
}
|
||||
|
||||
public void setVelocity(iVec2d newV) {
|
||||
this.velocity = newV;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package net.xfe.jmtd.model.entities;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
|
||||
import net.xfe.jmtd.math.iVec2d;
|
||||
import net.xfe.jmtd.model.Entity;
|
||||
|
||||
public class Turret extends Entity {
|
||||
|
||||
private static final Integer WIDTH = 30;
|
||||
private static final Integer RADIUS = WIDTH / 2;
|
||||
|
||||
public Turret(Integer x, Integer y) {
|
||||
super(new iVec2d(x, y));
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d, iVec2d p) {
|
||||
g2d.setColor(Color.GRAY);
|
||||
|
||||
// Because it's a circle and we draw from the corner we gotta offset it
|
||||
g2d.fillOval(p.x - RADIUS, p.y - RADIUS, WIDTH, WIDTH);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package net.xfe.jmtd.panels;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import net.xfe.jmtd.math.iVec2d;
|
||||
import net.xfe.jmtd.model.World;
|
||||
import net.xfe.jmtd.model.entities.Turret;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.Timer;
|
||||
|
||||
public class GamePanel extends JPanel {
|
||||
World world;
|
||||
|
||||
public GamePanel() {
|
||||
this.addKeyListener(new GameKeyListener());
|
||||
this.addMouseListener(new GameMouseListener());
|
||||
|
||||
this.world = new World();
|
||||
world.addEntity(new Turret(0, -50));
|
||||
this.setFocusable(true);
|
||||
|
||||
Timer timer = new Timer(10, e -> gameLoop(e));
|
||||
timer.start();
|
||||
}
|
||||
|
||||
private void gameLoop(ActionEvent e) {
|
||||
// System.out.println(world.getPlayer().getPos().y);
|
||||
world.doTick();
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
final iVec2d size = new iVec2d(getWidth(), getHeight());
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillRect(0, 0, size.x, size.y);
|
||||
|
||||
// Main draw function here
|
||||
this.world.draw(g2d, size);
|
||||
}
|
||||
|
||||
class GameKeyListener implements KeyListener {
|
||||
|
||||
public GameKeyListener() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
iVec2d deltaV = new iVec2d();
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_W -> deltaV = deltaV.add(new iVec2d(0, 1));
|
||||
case KeyEvent.VK_S -> deltaV = deltaV.add(new iVec2d(0, -1));
|
||||
case KeyEvent.VK_D -> deltaV = deltaV.add(new iVec2d(-1, 0));
|
||||
case KeyEvent.VK_A -> deltaV = deltaV.add(new iVec2d(1, 0));
|
||||
}
|
||||
|
||||
world.getPlayer().setVelocity(world.getPlayer().getVelocity().add(deltaV));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
final iVec2d stop = new iVec2d();
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_W -> world.getPlayer().setVelocity(stop);
|
||||
case KeyEvent.VK_S -> world.getPlayer().setVelocity(stop);
|
||||
case KeyEvent.VK_D -> world.getPlayer().setVelocity(stop);
|
||||
case KeyEvent.VK_A -> world.getPlayer().setVelocity(stop);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class GameMouseListener implements MouseListener {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package xfe;
|
||||
|
||||
public class JMTD {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package main.java.xfe.model;
|
||||
|
||||
public class Camera {
|
||||
private Integer xPos;
|
||||
private Integer yPos;
|
||||
|
||||
private boolean movingUp;
|
||||
private boolean movingDown;
|
||||
private boolean movingRight;
|
||||
private boolean movingLeft;
|
||||
|
||||
public static final Integer CAMERA_SPEED = 2;
|
||||
|
||||
public Camera() {
|
||||
this.xPos = 0;
|
||||
this.yPos = 0;
|
||||
|
||||
this.movingDown = false;
|
||||
this.movingUp = false;
|
||||
this.movingLeft = false;
|
||||
this.movingRight = false;
|
||||
}
|
||||
|
||||
public void doTick() {
|
||||
if (movingDown) {
|
||||
yPos -= CAMERA_SPEED;
|
||||
}
|
||||
|
||||
if (movingUp) {
|
||||
yPos += CAMERA_SPEED;
|
||||
}
|
||||
|
||||
if (movingLeft) {
|
||||
xPos -= CAMERA_SPEED;
|
||||
}
|
||||
|
||||
if (movingRight) {
|
||||
xPos += CAMERA_SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
// Getters & Setters
|
||||
|
||||
public Integer getXPos() {
|
||||
return this.xPos;
|
||||
}
|
||||
|
||||
public Integer getYPos() {
|
||||
return this.yPos;
|
||||
}
|
||||
|
||||
public void setXPos(Integer newX) {
|
||||
this.xPos = newX;
|
||||
}
|
||||
|
||||
public void setYPos(Integer newY) {
|
||||
this.yPos = newY;
|
||||
}
|
||||
|
||||
public void toggleMovingUp() {
|
||||
this.movingUp = !this.movingUp;
|
||||
}
|
||||
|
||||
public void toggleMovingDown() {
|
||||
this.movingDown = !this.movingDown;
|
||||
}
|
||||
|
||||
public void toggleMovingLeft() {
|
||||
this.movingLeft = !this.movingLeft;
|
||||
}
|
||||
|
||||
public void toggleMovingRight() {
|
||||
this.movingRight = !this.movingRight;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package main.java.xfe.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class World {
|
||||
private Camera camera;
|
||||
private List<Entity> entities;
|
||||
|
||||
public World() {
|
||||
this.camera = new Camera();
|
||||
this.entities = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addEntity(Entity entity) {
|
||||
this.entities.add(entity);
|
||||
}
|
||||
|
||||
public void doTick() {
|
||||
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d) {
|
||||
for (Entity entity : this.entities) {
|
||||
Point entityPos = new Point(entity.getX(), entity.getY());
|
||||
|
||||
entity.draw(g2d, translatePoint(entityPos));
|
||||
}
|
||||
}
|
||||
|
||||
private Point translatePoint(Point p) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package main.java.xfe.model.entities;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
|
||||
import main.java.xfe.model.Entity;
|
||||
|
||||
public class Turret extends Entity {
|
||||
|
||||
public void draw(Graphics2D g2d, Point p) {
|
||||
g2d.drawOval(p.x, p.y, 10, 10);
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package xfe.panels;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import main.java.xfe.model.World;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class GamePanel extends JPanel {
|
||||
World world;
|
||||
|
||||
public GamePanel() {
|
||||
this.addKeyListener(new GameKeyListener());
|
||||
this.addMouseListener(new GameMouseListener());
|
||||
|
||||
this.world = new World();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillRect(0, 0, getWidth(), getHeight());
|
||||
|
||||
// Main draw function here
|
||||
this.world.draw(g2d);
|
||||
}
|
||||
|
||||
class GameKeyListener implements KeyListener {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_W -> world.getCamera().toggleMovingUp();
|
||||
case KeyEvent.VK_S -> world.getCamera().toggleMovingDown();
|
||||
case KeyEvent.VK_D -> world.getCamera().toggleMovingRight();
|
||||
case KeyEvent.VK_A -> world.getCamera().toggleMovingLeft();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class GameMouseListener implements MouseListener {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {}
|
||||
}
|
||||
Reference in new issue
Block a user