Heyho,
PixelWars ist ein kleines 2D-Strategie-Spiel, an welchem ich gerade arbeite.
Erste Mockups und das vorläufige Tileset:
PixelWars
- Zweistein2
- Administrator
- Beiträge: 93
- Registriert: Di Apr 23, 2013 6:43 pm
- Zweistein2
- Administrator
- Beiträge: 93
- Registriert: Di Apr 23, 2013 6:43 pm
Re: PixelWars
Der Code vom Karteneditor:
textures:
window:
editor (main):
Sowie die dafür verwendeten Tilesets als Dateianhang
textures:
Code: Alles auswählen
package main;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class textures
{
static BufferedImage tileset;
static BufferedImage tilesetSmall;
final static int WIDTH = 28;
final static int HEIGHT = 18;
static short[][] ARGB;
public textures()
{
try
{
tileset = ImageIO.read(new File("C:\\Development\\workspaces\\jee\\PixelWars\\bin\\builds\\images\\tileset.png"));
tilesetSmall = ImageIO.read(new File("C:\\Development\\workspaces\\jee\\PixelWars\\bin\\builds\\images\\tilesetOrg.png"));
} catch (IOException e)
{
e.printStackTrace();
}
}
public static BufferedImage getTile(int PosX, int PosY, int iWidth)
{
int x = 0, y = 0;
int iIndex = iWidth * PosY + PosX;
if(editor.map[iIndex][0] >= 0 && editor.map[iIndex][0] < editor.TILES)
{
x = editor.map[iIndex][0] % 11;
y = editor.map[iIndex][0] / 11;
}
BufferedImage tile;
tile = tileset.getSubimage(x * editor.TILEWIDTH + x * editor.FACTOR, y * editor.TILEHEIGHT + y * editor.FACTOR, editor.TILEWIDTH, editor.TILEHEIGHT);
return tile;
}
public static BufferedImage getTileByName(int iIndex)
{
int x = 0, y = 0;
if(editor.map[iIndex][0] >= 0 && editor.map[iIndex][0] < editor.TILES)
{
x = editor.map[iIndex][0] % 11;
y = editor.map[iIndex][0] / 11;
}
BufferedImage tile;
tile = tileset.getSubimage(x * editor.TILEWIDTH + x * editor.FACTOR, y * editor.TILEHEIGHT + y * editor.FACTOR, editor.TILEWIDTH, editor.TILEHEIGHT);
return tile;
}
public static BufferedImage getSmallTileByID(int iID)
{
int x = 0, y = 0, Width = 28, Height = 18;
if(iID >= 0 && iID < editor.TILES)
{
x = iID % 11;
y = iID / 11;
}
BufferedImage tile;
tile = tilesetSmall.getSubimage(x * Width + x * 2, y * Height + y * 2, Width, Height);
return tile;
}
public static BufferedImage makeTransparent(BufferedImage image, int A)
{
BufferedImage target = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
ARGB = new short[image.getWidth()*image.getHeight()][4];
for(int x = 0; x < image.getWidth(); x++)
{
for(int y = 0; y < image.getHeight(); y++)
{
int iRGB = image.getRGB(x, y);
int iIndex = image.getWidth() * y + x;
ARGB[iIndex][0] = (short) A; //A
ARGB[iIndex][1] = (short) ((iRGB >> 16) & 0xFF); //R
ARGB[iIndex][2] = (short) ((iRGB >> 8) & 0xFF); //G
ARGB[iIndex][3] = (short) (iRGB & 0xFF); //B
}
}
for(int x = 0; x < image.getWidth(); x++)
{
for(int y = 0; y < image.getHeight(); y++)
{
int iIndex = image.getWidth() * y + x;
int iRGB = ((ARGB[iIndex][0] & 0xFF) << 24) | ((ARGB[iIndex][1] & 0xFF) << 16) | ((ARGB[iIndex][2] & 0xFF) << 8) | (ARGB[iIndex][3] & 0xFF);
target.setRGB(x, y, iRGB);
}
}
return target;
}
public static BufferedImage[] getAllTiles()
{
int x = 0, y = 0, Width = 28, Height = 18;
BufferedImage[] tiles = new BufferedImage[63];
for(int i = 0; i < editor.TILES; i++)
{
x = i % 11;
y = i / 11;
tiles[i] = tilesetSmall.getSubimage(x * Width + x * 2, y * Height + y * 2, Width, Height);
}
return tiles;
}
}
window:
Code: Alles auswählen
package main;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;
public class window
{
public window()
{
//
}
public void createMenu(int width, int height)
{
final JFrame frame = new JFrame("PixelWars! - Editor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
frame.setLayout(gridBagLayout);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 3;
c.gridy = 3;
c.insets = new Insets(220, 0, 0, 0);
final JPanel menu = new JPanel(new GridLayout(0, 1, 0, 5));
JButton startButton = new JButton("Editor starten");
JButton loadButton = new JButton("Karte laden");
JButton closeButton = new JButton("Editor beenden");
startButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JTextField xField = new JTextField(5);
JTextField yField = new JTextField(5);
JPanel mapBox = new JPanel();
mapBox.add(new JLabel("Breite:"));
mapBox.add(xField);
mapBox.add(Box.createHorizontalStrut(5)); // a spacer
mapBox.add(new JLabel("Höhe:"));
mapBox.add(yField);
int result = JOptionPane.showConfirmDialog(null, mapBox, "Bitte gib die Anzahl der Tiles der Karte an", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION)
{
if(xField.getText().trim().matches("[0-9]+") == true && xField.getText().length() != 0 && yField.getText().trim().matches("[0-9]+") == true && yField.getText().length() != 0)
{
editor.iWidth = Integer.parseInt(xField.getText().trim());
editor.iHeight = Integer.parseInt(yField.getText().trim());
editor.map = new short[(editor.iWidth*editor.iHeight)][4];
for(int x = 0; x < editor.iWidth; x++)
{
for(int y = 0; y < editor.iHeight; y++)
{
int iIndex = editor.iWidth * y + x;
editor.map[iIndex][0] = editor.TILE_WATER; //A Tile
editor.map[iIndex][1] = 0; //R Owner
editor.map[iIndex][2] = 0; //G Unit
editor.map[iIndex][3] = 0; //B Owner
}
}
frame.remove(menu);
startEditor(frame);
}else
{
return;
}
}else
{
frame.dispose();
}
}
});
loadButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final JFileChooser filedialoge = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Karte (.png)", "png");
filedialoge.setFileFilter(filter);
int iReturnVal = filedialoge.showOpenDialog(frame);
if(iReturnVal == JFileChooser.APPROVE_OPTION)
{
String sPath = filedialoge.getSelectedFile().getPath();
loadmap(sPath);
}
frame.remove(menu);
startEditor(frame);
}
});
closeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
menu.add(startButton);
menu.add(loadButton);
menu.add(closeButton);
frame.add(menu, c);
frame.setSize(width, height);
frame.setVisible(true);
}
private void startEditor(final JFrame frame)
{
frame.setLayout(new BorderLayout());
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("Datei");
JMenuItem openfile = new JMenuItem("Karte öffnen");
JMenuItem savefile = new JMenuItem("Karte speichern");
JMenuItem exportfile = new JMenuItem("Karte exportieren");
JMenuItem close = new JMenuItem("Speichern und beenden");
final JFileChooser filedialoge = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Karte (.png)", "png");
filedialoge.setFileFilter(filter);
openfile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int iReturnVal = filedialoge.showOpenDialog(frame);
if(iReturnVal == JFileChooser.APPROVE_OPTION)
{
String sPath = filedialoge.getSelectedFile().getPath();
loadmap(sPath);
frame.removeAll();
startEditor(frame);
}
}
});
savefile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int iReturnVal = filedialoge.showSaveDialog(frame);
if(iReturnVal == JFileChooser.APPROVE_OPTION)
{
String sPath = filedialoge.getSelectedFile().getPath();
try
{
savemap(sPath, editor.iWidth, editor.iHeight);
}catch (IOException e1)
{
e1.printStackTrace();
}
}
}
});
exportfile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int iReturnVal = filedialoge.showSaveDialog(frame);
if(iReturnVal == JFileChooser.APPROVE_OPTION)
{
String sPath = filedialoge.getSelectedFile().getPath();
exportmap(sPath, editor.iWidth, editor.iHeight);
}
}
});
close.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int iReturnVal = filedialoge.showSaveDialog(frame);
if(iReturnVal == JFileChooser.APPROVE_OPTION)
{
String sPath = filedialoge.getSelectedFile().getPath();
try
{
savemap(sPath, editor.iWidth, editor.iHeight);
}catch (IOException e1)
{
e1.printStackTrace();
}
}
frame.dispose();
}
});
file.add(openfile);
file.add(savefile);
file.add(exportfile);
file.addSeparator();
file.add(close);
final JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
toolbar.setFloatable(false);
toolbar.setPreferredSize(new Dimension(1920, 61));
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
BufferedImage b = new BufferedImage(editor.iWidth * editor.TILEWIDTH, editor.iHeight * editor.TILEHEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics g = b.getGraphics();
for(int x = 0; x < editor.iWidth; x++)
{
for(int y = 0; y < editor.iHeight; y++)
{
g.drawImage(textures.getTile(x, y, editor.iWidth), x * editor.TILEWIDTH, y * editor.TILEHEIGHT, null);
}
}
final ImageIcon picture = new ImageIcon(b);
frame.add(toolbar, BorderLayout.NORTH);
JTabbedPane tabPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
JScrollPane objectLayer = new JScrollPane();
JScrollPane overlayLayer = new JScrollPane();
final JLabel tilelabel = new JLabel(picture);
tilelabel.setPreferredSize(new Dimension(editor.iWidth * editor.TILEWIDTH, editor.iHeight * editor.TILEHEIGHT));
final JScrollPane tileLayer = new JScrollPane(tilelabel);
tilelabel.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
int xFix = 0, yFix = 0, xRand = 0, yRand = 0, iIndex = 0;
xRand = (int) (tilelabel.getBounds().getWidth() - (editor.iWidth * editor.TILEWIDTH)) / 2;
yRand = (int) (tilelabel.getBounds().getHeight() - (editor.iHeight * editor.TILEHEIGHT)) / 2;
xFix = (e.getX() - xRand) / (editor.TILEWIDTH);
yFix = (e.getY() - yRand) / (editor.TILEHEIGHT);
iIndex = editor.iWidth * yFix + xFix;
if(editor.iCursor >= 0 && editor.iCursor < editor.TILES)
{
editor.map[iIndex][0] = editor.iCursor;
Graphics g = picture.getImage().getGraphics();
for(int x = 0; x < editor.iWidth; x++)
{
for(int y = 0; y < editor.iHeight; y++)
{
g.drawImage(textures.getTile(x, y, editor.iWidth), x * editor.TILEWIDTH, y * editor.TILEHEIGHT, null);
}
}
tilelabel.updateUI();
}
}
public void mousePressed(MouseEvent e)
{
//
}
public void mouseReleased(MouseEvent e)
{
//
}
public void mouseEntered(MouseEvent e)
{
//
}
public void mouseExited(MouseEvent e)
{
//
}
});
tabPane.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
changeToolbar(tileLayer.isShowing(), toolbar, frame);
}
});
tabPane.addTab("Kachelebene", tileLayer);
tabPane.addTab("Objektebene", objectLayer);
tabPane.addTab("Overlayebene", overlayLayer);
menu.add(file);
frame.add(tabPane);
frame.setJMenuBar(menu);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
changeToolbar(true, toolbar, frame);
editor.iCursor = 999;
}
private void changeToolbar(boolean bIsTileLayerVisible, JToolBar toolbar, final JFrame frame)
{
if(bIsTileLayerVisible == true)
{
toolbar.removeAll();
BufferedImage[] tiles = new BufferedImage[63];
ImageIcon[] icons = new ImageIcon[63];
final JLabel[] tileLabels = new JLabel[63];
tiles = textures.getAllTiles();
for(int i = 0; i < editor.TILES; i++)
{
icons[i] = new ImageIcon(tiles[i]);
tileLabels[i] = new JLabel(icons[i]);
tileLabels[i].addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
for(int i = 0; i < editor.TILES; i++)
{
if(tileLabels[i].equals(e.getSource()))
{
changeCursor(frame, i);
}
}
}
public void mousePressed(MouseEvent e)
{
//
}
public void mouseReleased(MouseEvent e)
{
//
}
public void mouseEntered(MouseEvent e)
{
//
}
public void mouseExited(MouseEvent e)
{
//
}
});
toolbar.add(tileLabels[i]);
}
toolbar.updateUI();
}else
{
toolbar.removeAll();
toolbar.updateUI();
}
}
private void changeCursor(JFrame frame, int i)
{
Toolkit toolkit = Toolkit.getDefaultToolkit();
BufferedImage bImg = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
for(int x = 0; x < 32; x++)
{
for(int y = 0; y < 32; y++)
{
bImg.setRGB(x, y, 0);
}
}
Graphics g = bImg.getGraphics();
g.drawImage(textures.getSmallTileByID(i), 0, 0, null);
Image image = bImg;
Cursor c = toolkit.createCustomCursor(image , new Point(0, 0), "img");
frame.getRootPane().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
frame.getRootPane().setCursor(c);
editor.iCursor = (short) i;
}
private short[][] loadmap(String sPath)
{
File file = new File(sPath);
if(file.exists() && !file.isDirectory())
{
try
{
BufferedImage bImg = ImageIO.read(file);
editor.iWidth = bImg.getWidth();
editor.iHeight = bImg.getHeight();
editor.map = new short[(editor.iWidth*editor.iHeight)][4];
for(int x = 0; x < editor.iWidth; x++)
{
for(int y = 0; y < editor.iHeight; y++)
{
int iRGB = bImg.getRGB(x, y);
int iIndex = editor.iWidth * y + x;
editor.map[iIndex][0] = (short) ((iRGB >> 24) & 0xFF); //A Tile
editor.map[iIndex][1] = (short) ((iRGB >> 16) & 0xFF); //R Owner
editor.map[iIndex][2] = (short) ((iRGB >> 8) & 0xFF); //G Unit
editor.map[iIndex][3] = (short) (iRGB & 0xFF); //B Owner
}
}
}catch (IOException e)
{
e.printStackTrace();
}
}
return editor.map;
}
private void savemap(String sPath, int iWidth, int iHeight) throws IOException
{
File file = new File(sPath + ".png");
if(file.exists())
{
file.delete();
}
BufferedImage bImg = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_ARGB);
for(int x = 0; x < iWidth; x++)
{
for(int y = 0; y < iHeight; y++)
{
int iIndex = iWidth * y + x;
int iRGB = ((editor.map[iIndex][0] & 0xFF) << 24) | ((editor.map[iIndex][1] & 0xFF) << 16) | ((editor.map[iIndex][2] & 0xFF) << 8) | (editor.map[iIndex][3] & 0xFF);
bImg.setRGB(x, y, iRGB);
}
}
ImageIO.write(bImg, "png", file);
}
private void exportmap(String sPath, int iWidth, int iHeight)
{
File file = new File(sPath + ".png");
if(file.exists())
{
file.delete();
}
BufferedImage bImg = new BufferedImage(iWidth * editor.TILEWIDTH, iHeight * editor.TILEHEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics g = bImg.getGraphics();
for(int x = 0; x < iWidth; x++)
{
for(int y = 0; y < iHeight; y++)
{
g.drawImage(textures.getTile(x, y, iWidth), x * editor.TILEWIDTH, y * editor.TILEHEIGHT, null);
}
}
try
{
ImageIO.write(bImg, "png", file);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
editor (main):
Code: Alles auswählen
package main;
public class editor
{
//Map-Zeugs
public static short[][] map;
final static short FACTOR = 4;
final static short TILEWIDTH = 14 * FACTOR;
final static short TILEHEIGHT = 9 * FACTOR;
static int iWidth;
static int iHeight;
static short iCursor;
final static short COLORS = 5;
final static short TILES = 63;
final static short UNITS = 4;
//Owner
final static short OWNER_NEUTRAL = 0;
final static short OWNER_ORANGE = 1;
final static short OWNER_BLAU = 2;
final static short OWNER_GRUEN = 3;
final static short OWNER_GELB = 4;
//Tiles
final static short TILE_GRASS_OBEN_LINKS = 0;
final static short TILE_GRASS_OBEN_MITTE = 1;
final static short TILE_GRASS_OBEN_RECHTS = 2;
final static short TILE_STRASSE_OBEN_LINKS_WASSER = 3;
final static short TILE_STRASSE_OBEN_MITTE_WASSER = 4;
final static short TILE_STRASSE_OBEN_RECHTS_WASSER = 5;
final static short TILE_T_STRASSE_RECHTS = 6;
final static short TILE_T_STRASSE_LINKS = 7;
final static short TILE_WEG_OBEN_LINKS_WASSER = 8;
final static short TILE_WEG_OBEN_MITTE_WASSER = 9;
final static short TILE_WEG_OBEN_RECHTS_WASSER = 10;
final static short TILE_GRASS_UNTEN_LINKS = 11;
final static short TILE_GRASS_UNTEN_MITTE = 12;
final static short TILE_GRASS_UNTEN_RECHTS = 13;
final static short TILE_STRASSE_OBEN_LINKS = 14;
final static short TILE_STRASSE_OBEN_MITTE = 15;
final static short TILE_STRASSE_OBEN_RECHTS = 16;
final static short TILE_T_STRASSE_UNTEN = 17;
final static short TILE_T_STRASSE_OBEN = 18;
final static short TILE_WEG_OBEN_LINKS = 19;
final static short TILE_WEG_OBEN_MITTE = 20;
final static short TILE_WEG_OBEN_RECHTS = 21;
final static short TILE_DIRT_OBEN_LINKS = 22;
final static short TILE_DIRT_OBEN_MITTE = 23;
final static short TILE_DIRT_OBEN_RECHTS = 24;
final static short TILE_STRASSE_LINKS = 25;
final static short TILE_GRASS = 26;
final static short TILE_STRASSE_RECHTS = 27;
final static short TILE_KREUZUNG_STRASSE = 28;
final static short TILE_KREUZUNG_WEG = 29;
final static short TILE_WEG_LINKS = 30;
final static short TILE_DIRT = 31;
final static short TILE_WEG_RECHTS = 32;
final static short TILE_DIRT_UNTEN_LINKS = 33;
final static short TILE_DIRT_UNTEN_MITTE = 34;
final static short TILE_DIRT_UNTEN_RECHTS = 35;
final static short TILE_STRASSE_UNTEN_LINKS = 36;
final static short TILE_STRASSE_UNTEN_MITTE = 37;
final static short TILE_STRASSE_UNTEN_RECHTS = 38;
final static short TILE_T_WEG_RECHTS = 39;
final static short TILE_T_WEG_LINKS = 40;
final static short TILE_WEG_UNTEN_LINKS = 41;
final static short TILE_WEG_UNTEN_MITTE = 42;
final static short TILE_WEG_UNTEN_RECHTS = 43;
final static short TILE_WATER = 44;
final static short TILE_WOODEN_BRIDGE_SHADOW = 45;
final static short TILE_WOODEN_BRIDGE = 46;
final static short TILE_STRASSE_UNTEN_LINKS_WASSER = 47;
final static short TILE_STRASSE_UNTEN_MITTE_WASSER = 48;
final static short TILE_STRASSE_UNTEN_RECHTS_WASSER = 49;
final static short TILE_T_WEG_UNTEN = 50;
final static short TILE_T_WEG_OBEN = 51;
final static short TILE_WEG_UNTEN_LINKS_WASSER = 52;
final static short TILE_WEG_UNTEN_MITTE_WASSER = 53;
final static short TILE_WEG_UNTEN_RECHTS_WASSER = 54;
final static short TILE_STRAND_UNTEN_LINKS = 55;
final static short TILE_STRAND_UNTEN_MITTE = 56;
final static short TILE_STRAND_UNTEN_RECHTS = 57;
final static short TILE_KLIPPEN_UNTEN_LINKS = 58;
final static short TILE_KLIPPEN_UNTEN_MITTE = 59;
final static short TILE_KLIPPEN_UNTEN_RECHTS = 60;
final static short TILE_STONE_BRIDGE_SHADOW = 61;
final static short TILE_STONE_BRIDGE = 62; //
final static short TILE_GRASS_CITY = 63;
final static short TILE_DIRT_CITY = 64;
//Units / Sonstiges
final static short UNIT_TANK = 0;
final static short UNIT_SMALL_TANK = 1;
final static short UNIT_JEEP = 2;
//final static short UNIT_SOLDIER = 3;
final static short MISC_TREES = 4;
final static short MISC_ROCKS = 5;
public static void main(String[] args)
{
@SuppressWarnings("unused")
textures textureLoader = new textures();
window gameWindow = new window();
gameWindow.createMenu(800, 600);
}
}
Sowie die dafür verwendeten Tilesets als Dateianhang
Du hast keine ausreichende Berechtigung, um die Dateianhänge dieses Beitrags anzusehen.
Re: PixelWars
Okay, ich war schon wieder viel zu lange nicht mehr hier. Bin gerade auf einem GameJam. ^^
Also Strategie in Neuzeit und happy? (wegen der hellen/grellen Farben)
Also Strategie in Neuzeit und happy? (wegen der hellen/grellen Farben)
- Zweistein2
- Administrator
- Beiträge: 93
- Registriert: Di Apr 23, 2013 6:43 pm
Re: PixelWars
Advance Wars habe ich nie gespielt... hatte aber auch nie eine Nintendo Konsole.
Wer ist online?
Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast