📄 helkcraft.java
字号:
public void setSelected(boolean s) {
isSlct = s;
}
public boolean isSelected() {
return isSlct;
}
public void update() {
if(!isMove) {
return;
}
double distance = distanceBetween(curPos, desPos);
if(distance < rngStop) {
isMove = false;
return;
}
sclX = (desPos.x - curPos.x) / distance;
sclY = (desPos.y - curPos.y) / distance;
curPos.x += vel * sclX;
curPos.y += vel * sclY;
}
public void show(Graphics g, Point p) {
if (isSlct) {
g.setColor(Color.GREEN);
g.drawOval((int)(curPos.x - size.width / 2) - p.x, (int)(curPos.y - size.height / 2) - p.y, size.width, size.height);
}
g.setColor(clr);
g.drawOval((int)(curPos.x - sizeDraw.width / 2) - p.x, (int)(curPos.y - sizeDraw.height / 2) - p.y, sizeDraw.width, sizeDraw.height);
if (sclX == sclY && sclX == 0) {
g.drawLine((int)(curPos.x) - p.x, (int)(curPos.y) - p.y, (int)(sizeDraw.width / 2 + curPos.x) - p.x, (int)(curPos.y) - p.y);
} else {
g.drawLine((int)(curPos.x) - p.x, (int)(curPos.y) - p.y, (int)(sizeDraw.width * sclX / 2 + curPos.x) - p.x, (int)(sizeDraw.height * sclY / 2 + curPos.y) - p.y);
}
}
public void miniShow(Graphics g, Point p, int scale) {
g.setColor(miniClr);
g.fillRect((int)(curPos.x / scale) + p.x - 1, (int)(curPos.y / scale) + p.y - 1, 3, 3);
}
public void executeCommand(Command cmd) {
if(cmd instanceof MoveCommand) {
gotoDestination(((MoveCommand)cmd).getPosition());
}
}
public void gotoDestination(Position p) {
desPos.setPosition(p);
isMove = true;
}
private double distanceBetween(Position p1, Position p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
}
/**
* @version 0.0
* @author Liu Fangran
*/
class MiniMap {
public Rectangle miniMap;
public int scale;
public Color frameColor = Color.RED;
public Color bgColor = Color.BLACK;
public Color rectColor = Color.WHITE;
public MiniMap(Rectangle miniMap, Dimension fullMapSize) {
this.miniMap = miniMap;
scale = 2 * fullMapSize.width / miniMap.width + 1;
}
public void show(Graphics g) {
g.setColor(bgColor);
g.fillRect(miniMap.x - 1, miniMap.y - 1, miniMap.width, miniMap.height);
g.setColor(frameColor);
g.drawRect(miniMap.x - 2, miniMap.y - 2, miniMap.width, miniMap.height);
}
public void showRect(Graphics g, Rectangle screen) {
g.setColor(rectColor);
g.drawRect(screen.x / scale + miniMap.x, screen.y / scale + miniMap.y, screen.width / scale, screen.height / scale);
}
public int getScale() {
return scale;
}
public Point getLocation() {
return miniMap.getLocation();
}
public Rectangle getRect() {
return miniMap;
}
}
/**
* @version 0.0
* @author Liu Fangran
*/
class ExitableJFrame extends JFrame {
public ExitableJFrame() {
}
public ExitableJFrame(String title) {
super(title);
}
protected void frameInit() {
super.frameInit();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
/**
* @version 0.0
* @author Liu Fangran
*/
public class Helkcraft extends ExitableJFrame {
MiniMap miniMap = new MiniMap(new Rectangle(2, 335, 145, 475), new Dimension(640, 480));
Rectangle quitRect = new Rectangle(new Rectangle(1, 315, 71, 18));
Rectangle infoRect = new Rectangle(new Rectangle(74, 315, 71, 18));
Color infoClr = Color.MAGENTA;
Color infoFrameClr = Color.YELLOW;
Color quitClr = Color.MAGENTA;
Color quitFrameClr = Color.YELLOW;
Color bgColor = new Color(115, 164, 235);
Color rectColor = Color.GREEN;
Point rectStartPnt = new Point();
Point rectEndPnt = new Point();
Point coordPnt = new Point();
Point mousePnt = new Point();
boolean isShowRect;
Graphics gfx;
UnitManager manager = new UnitManager();
Rectangle observer = new Rectangle(0, 0, 640, 480);
Rectangle rect = new Rectangle();
UnitControler controler = new UnitControler();
Dimension mapSize = new Dimension(1280, 1280);
Dimension scrSize = new Dimension(640, 480);
static DisplayMode displayMode = new DisplayMode(640, 480, 16,0);
boolean isDone = false;
boolean isSingleSlct = false;
int showInfoCounterMax = 60;
int showInfoCounter = 0;
int moveBarWidth = 5;
int moveCoSpeed = 25;
public Helkcraft() {
MouseAdapter mouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.getButton() == e.BUTTON3) {
isShowRect = false;
if (miniMap.getRect().contains(e.getX(), e.getY())) {
controler.executeCommand(
new MoveCommand(
new Position((e.getX() - miniMap.getRect().x) * miniMap.getScale(), (e.getY() - miniMap.getRect().y) * miniMap.getScale()
)
)
);
} else {
controler.executeCommand(
new MoveCommand(
new Position(e.getX() + coordPnt.getX(), e.getY() + coordPnt.getY()
)
)
);
}
} else if (e.getButton() == e.BUTTON1) {
if(quitRect.contains(e.getX(), e.getY())) {
isDone = true;
return;
}
if(infoRect.contains(e.getX(), e.getY())) {
showInfoCounter = showInfoCounterMax;
return;
}
if (miniMap.getRect().contains(e.getX(), e.getY())) {
coordPnt.x = (e.getX() - miniMap.getRect().x) * miniMap.getScale() - miniMap.getRect().width / 2;
if(coordPnt.x < 0) {
coordPnt.x = 0;
} else if (coordPnt.x > mapSize.width - scrSize.width) {
coordPnt.x = Math.max(0, mapSize.width - scrSize.width);
}
coordPnt.y = (e.getY() - miniMap.getRect().y) * miniMap.getScale() - miniMap.getRect().height / 2;
if(coordPnt.y < 0) {
coordPnt.y = 0;
} else if (coordPnt.y > mapSize.height - scrSize.height) {
coordPnt.y = Math.max(0, mapSize.height - scrSize.height);
}
return;
}
Unit u = manager.getUnitAt(new Point(e.getX() + coordPnt.x, e.getY() + coordPnt.y));
if(u != null) {
isSingleSlct = true;
controler.setUnitGroup(u);
return;
}
isSingleSlct = false;
isShowRect = true;
rectStartPnt.setLocation(e.getX(), e.getY());
rectEndPnt.setLocation(e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
if(isSingleSlct) {
isSingleSlct = false;
return;
}
if(e.getButton() == e.BUTTON1) {
int x1 = Math.min(rectStartPnt.x, rectEndPnt.x),
x2 = Math.max(rectStartPnt.x, rectEndPnt.x),
y1 = Math.min(rectStartPnt.y, rectEndPnt.y),
y2 = Math.max(rectStartPnt.y, rectEndPnt.y);
controler.setUnitGroup(manager.getUnitGroup(), new Rectangle(x1 + coordPnt.x, y1 + coordPnt.y, x2 - x1, y2 - y1));
isShowRect = false;
}
}
};
addMouseListener(mouseListener);
MouseMotionAdapter mouseMotionListener = new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if(isShowRect) {
rectEndPnt.setLocation(e.getX(), e.getY());
}
}
public void mouseMoved(MouseEvent e) {
mousePnt.setLocation(e.getX(), e.getY());
}
};
addMouseMotionListener(mouseMotionListener);
}
void updateCoordinate() {
if (mousePnt.x < moveBarWidth) {
coordPnt.x -= moveCoSpeed;
if(coordPnt.x < 0) {
coordPnt.x = 0;
}
} else if (mousePnt.x > scrSize.width - moveBarWidth) {
coordPnt.x += moveCoSpeed;
if(coordPnt.x > mapSize.width - scrSize.width) {
coordPnt.x = Math.max(0, mapSize.width - scrSize.width);
}
}
if(mousePnt.y < moveBarWidth) {
coordPnt.y -= moveCoSpeed;
if(coordPnt.y < 0) {
coordPnt.y = 0;
}
} else if (mousePnt.y > scrSize.height - moveBarWidth) {
coordPnt.y += moveCoSpeed;
if(coordPnt.y > mapSize.height - scrSize.height) {
coordPnt.y = Math.max(0, mapSize.height - scrSize.height);
}
}
}
void show(Graphics g) {
g.setColor(bgColor);
g.fillRect(observer.x, observer.y, observer.width, observer.height);
manager.showAll(g, new Rectangle(coordPnt.x, coordPnt.y, 640, 480));
if (isShowRect) {
g.setColor(rectColor);
int x1 = Math.min(rectStartPnt.x, rectEndPnt.x),
x2 = Math.max(rectStartPnt.x, rectEndPnt.x),
y1 = Math.min(rectStartPnt.y, rectEndPnt.y),
y2 = Math.max(rectStartPnt.y, rectEndPnt.y);
g.drawRect(x1,y1,x2-x1,y2-y1);
}
miniMap.show(g);
manager.miniShowAll(g, miniMap.getLocation(), miniMap.getScale());
miniMap.showRect(g, new Rectangle(coordPnt.x, coordPnt.y, observer.width, observer.height));
g.setColor(quitClr);
g.fillRect(quitRect.x, quitRect.y, quitRect.width, quitRect.height);
g.setColor(quitFrameClr);
g.drawRect(quitRect.x - 1, quitRect.y - 1, quitRect.width + 1, quitRect.height + 1);
g.setColor(Color.BLUE);
g.drawString("QUIT",18, 331);
g.setColor(infoClr);
g.fillRect(infoRect.x, infoRect.y, infoRect.width, infoRect.height);
g.setColor(infoFrameClr);
g.drawRect(infoRect.x - 1, infoRect.y - 1, infoRect.width + 1, infoRect.height + 1);
g.setColor(Color.BLUE);
g.drawString("ABOUT..",87, 331);
if(showInfoCounter > 0) {
showInfo(g);
}
}
void showInfo(Graphics g) {
g.setColor(Color.black);
Font f = g.getFont();
g.setFont(new Font("Arial", Font.BOLD,22));
g.drawString("Helkcraft -- Programmed by Liu Fangran", 100, 200);
g.drawString("Email: hellomyworld@163.com", 160, 240);
g.setFont(f);
showInfoCounter--;
}
public static void main(String args[]) {
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
DisplayMode originalDisplayMode = graphicsDevice.getDisplayMode();
try {
final Helkcraft helk = new Helkcraft();
helk.setUndecorated(true);
graphicsDevice.setFullScreenWindow(helk);
if(graphicsDevice.isDisplayChangeSupported()) {
graphicsDevice.setDisplayMode(displayMode);
}
helk.createBufferStrategy(2);
Rectangle bounds = helk.getBounds();
BufferStrategy bufferStrategy = helk.getBufferStrategy();
helk.manager.addUnitGroup(UnitLayout.square(6, new Position(50.0, 50.0), 80));
while(!helk.isDone) {
helk.gfx = null;
try {
helk.gfx = bufferStrategy.getDrawGraphics();
helk.manager.updateAll();
helk.updateCoordinate();
helk.show(helk.gfx);
bufferStrategy.show();
} finally {
if(helk.gfx != null) {
helk.gfx.dispose();
}
}
try {
Thread.sleep(50);
} catch(InterruptedException ignored) {
}
}
} finally {
graphicsDevice.setDisplayMode(originalDisplayMode);
graphicsDevice.setFullScreenWindow(null);
}
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -