Updated Formatting Rules. Fixed wildcard imports to explicit imports.

This commit is contained in:
Aaron Helton
2017-08-06 14:09:36 -04:00
parent b2726a583f
commit 1cbad267cc
13 changed files with 429 additions and 217 deletions

View File

@@ -1,6 +1,6 @@
package com.aargonian.editor;
import java.awt.*;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
@@ -14,7 +14,8 @@ import java.util.List;
* keep each tile instance as a subimage within a larger sheet, unless written out by a TileSheetWriter. Instead,
* individual tile images can be added or removed from the set at will.
*/
public class EditableTileSheet {
public class EditableTileSheet
{
/*
* An EditableTileSheet is the backing model for the TileSheets dock of the editor, and as such implements the
* Observer
@@ -25,11 +26,13 @@ public class EditableTileSheet {
/**
* Creates an empty EditableTileSheet.
*/
public EditableTileSheet() {
public EditableTileSheet()
{
this.tileImages = new ArrayList<>();
}
public EditableTileSheet(List<Image> tileImages) {
public EditableTileSheet(List<Image> tileImages)
{
this.tileImages = tileImages;
}
}

View File

@@ -1,6 +1,10 @@
package com.aargonian.editor;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/**
* Created by aargonian on 7/16/17.
@@ -10,57 +14,71 @@ import java.awt.event.*;
* model, and receives input events from the TileMapDisplay. It is the job of the EditorTool to translate these input
* events into corresponding actions on the underlying TileMap or in the TileMapDisplay.
*/
public class EditorTool implements MouseListener, MouseMotionListener, KeyListener {
public class EditorTool implements MouseListener, MouseMotionListener, KeyListener
{
private final TileMapDisplay display;
protected EditorTool(TileMapDisplay display) {
if (display == null) {
protected EditorTool(TileMapDisplay display)
{
if(display == null)
{
throw new NullPointerException("Given TileMapDisplay was Null!");
}
this.display = display;
}
protected TileMapDisplay getCurrentDisplay() {
protected TileMapDisplay getCurrentDisplay()
{
return this.display;
}
@Override
public void keyTyped(KeyEvent e) {
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e) {
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e) {
public void keyReleased(KeyEvent e)
{
}
@Override
public void mousePressed(MouseEvent e) {
public void mousePressed(MouseEvent e)
{
}
@Override
public void mouseClicked(MouseEvent e) {
public void mouseClicked(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent e) {
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e) {
public void mouseEntered(MouseEvent e)
{
}
@Override
public void mouseExited(MouseEvent e) {
public void mouseExited(MouseEvent e)
{
}
@Override
public void mouseDragged(MouseEvent e) {
public void mouseDragged(MouseEvent e)
{
}
@Override
public void mouseMoved(MouseEvent e) {
public void mouseMoved(MouseEvent e)
{
}
}

View File

@@ -4,29 +4,40 @@ import com.aargonian.resource.ImageResource;
import com.aargonian.resource.ResourceLoader;
import com.aargonian.tile.TileMap;
import javax.swing.*;
import java.awt.*;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.util.ArrayList;
/**
* Created by aargonian on 7/8/17.
*/
public class GameFrame {
public class GameFrame
{
private static final String PROG_TITLE = "TileEditor 0.1.0";
public static void main(String[] args) {
public static void main(String[] args)
{
EventQueue.invokeLater(GameFrame::setupUI);
}
//TODO: Add an Icon Image
private static JFrame setupFrame() {
private static JFrame setupFrame()
{
JFrame frame = new JFrame(PROG_TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if (screenSize.width <= 1024 || screenSize.height <= 800) {
if(screenSize.width <= 1024 || screenSize.height <= 800)
{
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //maximize by default if the screen is really small
} else {
}
else
{
frame.setSize((int) Math.round(screenSize.width * 0.8), (int) Math.round(screenSize.height * 0.8));
frame.setLocationRelativeTo(null); //Centers the frame
}
@@ -38,16 +49,19 @@ public class GameFrame {
*
* @return The Tile Pane Component, Fully Constructed
*/
private static JComponent setupTilesPanel() {
private static JComponent setupTilesPanel()
{
throw new UnsupportedOperationException("Not Supported Yet.");
}
private static void createTools(TileMapDisplay tileMapDisplay, TilesetDisplay tilesetDisplay) {
private static void createTools(TileMapDisplay tileMapDisplay, TilesetDisplay tilesetDisplay)
{
PencilTool pencil = new PencilTool(tileMapDisplay, tilesetDisplay);
tileMapDisplay.setCurrentActiveTool(pencil);
}
private static void setupUI() {
private static void setupUI()
{
JFrame frame = setupFrame();
TileMap map = new TileMap(10, 10);

View File

@@ -8,26 +8,31 @@ import java.awt.event.MouseEvent;
/**
* Created by aargonian on 7/23/17.
*/
public class PencilTool extends EditorTool implements TilesetDisplay.TilesetListener {
public class PencilTool extends EditorTool implements TilesetDisplay.TilesetListener
{
private ImageResource currentImage;
public PencilTool(TileMapDisplay display, TilesetDisplay tilesetDisplay) {
public PencilTool(TileMapDisplay display, TilesetDisplay tilesetDisplay)
{
super(display);
tilesetDisplay.addSetListener(this);
}
@Override
public void imageSelected(ImageResource resource) {
public void imageSelected(ImageResource resource)
{
this.currentImage = resource;
}
@Override
public void mouseClicked(MouseEvent e) {
if (this.currentImage != null) {
public void mouseClicked(MouseEvent e)
{
if(this.currentImage != null)
{
int tileX = e.getX() / super.getCurrentDisplay().getOptions().tileWidth();
int tileY = e.getY() / super.getCurrentDisplay().getOptions().tileHeight();
this.getCurrentDisplay().getCurrentTileMap()
.setTileResourceAt(TileImpl.PROPERTY_IMG, this.currentImage, tileX, tileY);
.setTileResourceAt(TileImpl.PROPERTY_IMG, this.currentImage, tileX, tileY);
this.getCurrentDisplay().repaint();
}
}

View File

@@ -5,57 +5,72 @@ import com.aargonian.tile.TileImpl;
import com.aargonian.tile.TileMap;
import com.aargonian.util.Pair;
import javax.swing.*;
import java.awt.*;
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.List;
/**
* Created by aargonian on 7/8/17.
*/
public class TileMapDisplay extends JComponent {
public class TileMapDisplay extends JComponent
{
private static final OptionsBuilder DEFAULT_OPTIONS =
new OptionsBuilder().borderColor(Color.black).displaySize(640, 480).tileSize(32, 32);
private final OptionsBuilder options;
private TileMap tileMap;
private EditorTool currentActiveTool;
private List<Pair<Integer, Integer>> selectedTiles;
private final OptionsBuilder options;
private TileMap tileMap;
private EditorTool currentActiveTool;
private List<Pair<Integer, Integer>> selectedTiles;
public TileMapDisplay() {
public TileMapDisplay()
{
this.options = DEFAULT_OPTIONS;
}
public TileMapDisplay(OptionsBuilder options) {
public TileMapDisplay(OptionsBuilder options)
{
this.options = options;
this.setPreferredSize(new Dimension(options.displayWidth(), options.displayHeight()));
}
public OptionsBuilder getOptions() {
public OptionsBuilder getOptions()
{
return this.options;
}
public TileMap getCurrentTileMap() {
public TileMap getCurrentTileMap()
{
return this.tileMap;
}
public void setCurrentTileMap(TileMap map) {
public void setCurrentTileMap(TileMap map)
{
this.tileMap = map;
}
public List<Pair<Integer, Integer>> getSelectedTiles() {
public List<Pair<Integer, Integer>> getSelectedTiles()
{
return this.selectedTiles;
}
public void setSelectedTiles(List<Pair<Integer, Integer>> tiles) {
public void setSelectedTiles(List<Pair<Integer, Integer>> tiles)
{
this.selectedTiles = tiles;
this.repaint();
}
public EditorTool getCurrentActiveTool() {
public EditorTool getCurrentActiveTool()
{
return this.currentActiveTool;
}
public void setCurrentActiveTool(EditorTool tool) {
if (this.currentActiveTool != null) {
public void setCurrentActiveTool(EditorTool tool)
{
if(this.currentActiveTool != null)
{
this.removeMouseListener(this.currentActiveTool);
this.removeMouseMotionListener(this.currentActiveTool);
this.removeKeyListener(this.currentActiveTool);
@@ -68,20 +83,26 @@ public class TileMapDisplay extends JComponent {
}
@Override
public void paintComponent(Graphics g) {
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(this.options.borderColor());
g2.fillRect(0, 0, this.getWidth(), this.getHeight());
for (int x = 0; x < this.tileMap.getColumns(); x++) {
for (int y = 0; y < this.tileMap.getRows(); y++) {
for(int x = 0; x < this.tileMap.getColumns(); x++)
{
for(int y = 0; y < this.tileMap.getRows(); y++)
{
final int tileXLoc = x * this.options.tileWidth();
final int tileYLoc = y * this.options.tileHeight();
ImageResource imgRes = (ImageResource) this.tileMap.getTileResourceAt(TileImpl.PROPERTY_IMG, x, y);
Image img = imgRes == null ? null : imgRes.getImage();
if (img != null) {
if(img != null)
{
g2.drawImage(img, tileXLoc, tileYLoc, this.options.tileWidth(), this.options.tileHeight(), null);
} else {
}
else
{
final int HALF_WIDTH = this.options.tileWidth() / 2;
final int HALF_HEIGHT = this.options.tileHeight() / 2;
@@ -94,14 +115,17 @@ public class TileMapDisplay extends JComponent {
g2.fillRect(tileXLoc + HALF_WIDTH, tileYLoc, HALF_WIDTH, HALF_HEIGHT);
}
if (this.selectedTiles != null) {
for (Pair<Integer, Integer> selectedTile : this.selectedTiles) {
if (selectedTile.getFirst() == x && selectedTile.getSecond() == y) {
if(this.selectedTiles != null)
{
for(Pair<Integer, Integer> selectedTile : this.selectedTiles)
{
if(selectedTile.getFirst() == x && selectedTile.getSecond() == y)
{
g2.setColor(Color.YELLOW);
g2.fillRect(tileXLoc, tileYLoc, this.options.tileWidth(), this.options.tileHeight());
g2.setColor(Color.YELLOW.brighter());
g2.fillRect(tileXLoc + 2, tileYLoc + 2, this.options.tileWidth() - 2,
this.options.tileHeight() - 2);
this.options.tileHeight() - 2);
}
}
}
@@ -109,73 +133,88 @@ public class TileMapDisplay extends JComponent {
}
}
public static final class OptionsBuilder {
private int tileWidth = 32;
private int tileHeight = 32;
private int displayWidth = 640;
private int displayHeight = 480;
private Color borderColor = Color.black;
private boolean fullscreen = false;
public static final class OptionsBuilder
{
private int tileWidth = 32;
private int tileHeight = 32;
private int displayWidth = 640;
private int displayHeight = 480;
private Color borderColor = Color.black;
private boolean fullscreen = false;
public OptionsBuilder tileWidth(int width) {
public OptionsBuilder tileWidth(int width)
{
this.tileWidth = width <= 0 ? 0 : width;
return this;
}
public OptionsBuilder tileHeight(int height) {
public OptionsBuilder tileHeight(int height)
{
this.tileHeight = height <= 0 ? 0 : height;
return this;
}
public OptionsBuilder tileSize(int width, int height) {
public OptionsBuilder tileSize(int width, int height)
{
return this.tileWidth(width).tileHeight(height);
}
public OptionsBuilder borderColor(Color c) {
public OptionsBuilder borderColor(Color c)
{
this.borderColor = c;
return this;
}
public OptionsBuilder displayWidth(int width) {
public OptionsBuilder displayWidth(int width)
{
this.displayWidth = width <= 0 ? 0 : width;
return this;
}
public OptionsBuilder displayHeight(int height) {
public OptionsBuilder displayHeight(int height)
{
this.displayHeight = height <= 0 ? 0 : height;
return this;
}
public OptionsBuilder displaySize(int width, int height) {
public OptionsBuilder displaySize(int width, int height)
{
return this.displayWidth(width).displayHeight(height);
}
public OptionsBuilder fullscreen(boolean value) {
public OptionsBuilder fullscreen(boolean value)
{
this.fullscreen = value;
return this;
}
public int tileWidth() {
public int tileWidth()
{
return this.tileWidth;
}
public int tileHeight() {
public int tileHeight()
{
return this.tileHeight;
}
public int displayWidth() {
public int displayWidth()
{
return this.displayWidth;
}
public int displayHeight() {
public int displayHeight()
{
return this.displayHeight;
}
public Color borderColor() {
public Color borderColor()
{
return this.borderColor;
}
public boolean fulllscreen() {
public boolean fulllscreen()
{
return this.fullscreen;
}
}

View File

@@ -8,30 +8,35 @@ import java.awt.event.MouseEvent;
/**
* Created by aargonian on 7/16/17.
*/
public class TileSelectTool extends EditorTool {
public class TileSelectTool extends EditorTool
{
private int currentTileX = 0;
private int currentTileY = 0;
public TileSelectTool(TileMapDisplay display) {
public TileSelectTool(TileMapDisplay display)
{
super(display);
}
@Override
public void mouseClicked(MouseEvent e) {
public void mouseClicked(MouseEvent e)
{
int tileX = e.getX() / super.getCurrentDisplay().getOptions().tileWidth();
int tileY = e.getY() / super.getCurrentDisplay().getOptions().tileHeight();
//getCurrentDisplay().setSelectedTiles(Arrays.asList(new Pair<Integer, Integer>(tileX, tileY)));
this.getCurrentDisplay().getCurrentTileMap()
.setTileResourceAt(TileImpl.PROPERTY_IMG, new ImageResource("res/Water.png"), tileX, tileY);
.setTileResourceAt(TileImpl.PROPERTY_IMG, new ImageResource("res/Water.png"), tileX, tileY);
this.getCurrentDisplay().repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
public void mouseMoved(MouseEvent e)
{
int tileX = e.getX() / super.getCurrentDisplay().getOptions().tileWidth();
int tileY = e.getY() / super.getCurrentDisplay().getOptions().tileHeight();
if (tileX != this.currentTileX || tileY != this.currentTileY) {
if(tileX != this.currentTileX || tileY != this.currentTileY)
{
this.currentTileX = tileX;
this.currentTileY = tileY;
}

View File

@@ -2,8 +2,9 @@ package com.aargonian.editor;
import com.aargonian.resource.ImageResource;
import javax.swing.*;
import java.awt.*;
import javax.swing.JComponent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
@@ -13,17 +14,21 @@ import java.util.ArrayList;
* <p>
* This class implements a display for a set of tiles.
*/
public final class TilesetDisplay extends JComponent implements MouseListener {
private final ArrayList<ImageResource> images;
public final class TilesetDisplay extends JComponent implements MouseListener
{
private final ArrayList<ImageResource> images;
private final ArrayList<TilesetListener> listeners;
private int currentlySelectedImage = 0;
private int tileSize = 0;
private int tileSize = 0;
public TilesetDisplay(int tileSize, ArrayList<ImageResource> tileImages) {
if (tileImages == null) {
public TilesetDisplay(int tileSize, ArrayList<ImageResource> tileImages)
{
if(tileImages == null)
{
throw new NullPointerException("Passed Image Set is Null.");
}
if (tileSize < 0) {
if(tileSize < 0)
{
throw new IllegalArgumentException("Tilesize Cannot Be Negative!");
}
this.images = tileImages;
@@ -36,56 +41,69 @@ public final class TilesetDisplay extends JComponent implements MouseListener {
this.setPreferredSize(new Dimension(squareSize, squareSize));
}
public void addSetListener(TilesetListener listener) {
public void addSetListener(TilesetListener listener)
{
this.listeners.add(listener);
}
public void removeSetListener(TilesetListener listener) {
public void removeSetListener(TilesetListener listener)
{
this.listeners.remove(listener);
}
private void setCurrentlySelectedImage(int index) {
private void setCurrentlySelectedImage(int index)
{
this.currentlySelectedImage = index;
if (index < this.images.size() && index >= 0) {
for (TilesetListener listener : this.listeners) {
if(index < this.images.size() && index >= 0)
{
for(TilesetListener listener : this.listeners)
{
listener.imageSelected(this.images.get(index));
}
}
}
public ImageResource getCurrentlySelectedImage(ImageResource res) {
if (this.currentlySelectedImage >= 0 && this.currentlySelectedImage < this.images.size()) {
public ImageResource getCurrentlySelectedImage(ImageResource res)
{
if(this.currentlySelectedImage >= 0 && this.currentlySelectedImage < this.images.size())
{
return this.images.get(this.currentlySelectedImage);
}
return null;
}
public void addImageToDisplay(ImageResource img) {
public void addImageToDisplay(ImageResource img)
{
this.images.add(img);
this.repaint();
}
public void removeImageFromDisplay(ImageResource img) {
public void removeImageFromDisplay(ImageResource img)
{
this.images.remove(img);
this.repaint();
}
//Todo: Update this to use a scrollpane and avoid divide by zero.
@Override
public void paintComponent(Graphics g) {
public void paintComponent(Graphics g)
{
int columns = this.getWidth() / this.tileSize;
if (columns != 0) // Avoid divide by zero and simply display nothing.
if(columns != 0) // Avoid divide by zero and simply display nothing.
{
for (int i = 0; i < this.images.size(); i++) {
for(int i = 0; i < this.images.size(); i++)
{
g.drawImage(this.images.get(i).getImage(), (i % columns) * this.tileSize, i / columns * this.tileSize,
this.tileSize, this.tileSize, null);
this.tileSize, this.tileSize, null);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (this.images.size() < 1) {
public void mouseClicked(MouseEvent e)
{
if(this.images.size() < 1)
{
return;
}
@@ -96,26 +114,31 @@ public final class TilesetDisplay extends JComponent implements MouseListener {
}
@Override
public void mousePressed(MouseEvent e) {
public void mousePressed(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent e) {
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e) {
public void mouseEntered(MouseEvent e)
{
}
@Override
public void mouseExited(MouseEvent e) {
public void mouseExited(MouseEvent e)
{
}
public interface TilesetListener {
public interface TilesetListener
{
void imageSelected(ImageResource image);
}
}

View File

@@ -12,36 +12,54 @@ import java.util.Arrays;
* This is a simple extension to the existing BufferedImage class that provides an implementation of equals() and
* hashCode().
*/
public class ImageResource extends Resource {
public class ImageResource extends Resource
{
private final BufferedImage res;
private int hash = 0;
public ImageResource(String imagePath) {
public ImageResource(String imagePath) throws IOException
{
super(imagePath, ResourceType.IMAGE);
BufferedImage temp = null;
if (imagePath != null && !imagePath.isEmpty()) {
try {
if(imagePath != null && !imagePath.isEmpty())
{
try
{
File imageFile = new File(imagePath);
if (imageFile.exists()) {
if(imageFile.exists())
{
temp = ImageIO.read(imageFile);
} else {
}
else
{
throw new IOException("Image File Does Not Exist: " + imagePath);
}
} catch (IOException ex) {
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
this.res = temp;
}
public BufferedImage getImage() {
public ImageResource(String name, BufferedImage image)
{
super(name, ResourceType.IMAGE);
this.res = image;
}
public BufferedImage getImage()
{
return this.res;
}
@Override
public int hashCode() {
if (this.hash == 0 && this.res != null) {
public int hashCode()
{
if(this.hash == 0 && this.res != null)
{
int[] data = new int[this.res.getWidth() * this.res.getHeight()];
this.res.getRGB(0, 0, this.res.getWidth(), this.res.getHeight(), data, 0, 0);
this.hash = Arrays.hashCode(data);
@@ -50,28 +68,37 @@ public class ImageResource extends Resource {
}
@Override
public boolean equals(Object o) {
if (this == o) {
public boolean equals(Object o)
{
if(this == o)
{
return true;
}
if (o == null) {
if(o == null)
{
return false;
}
if (o.getClass() != this.getClass()) {
if(o.getClass() != this.getClass())
{
return false;
}
ImageResource other = (ImageResource) o;
if (this.res.getWidth() != other.res.getWidth()) {
if(this.res.getWidth() != other.res.getWidth())
{
return false;
}
if (this.res.getHeight() != other.res.getHeight()) {
if(this.res.getHeight() != other.res.getHeight())
{
return false;
}
for (int x = 0; x < this.res.getHeight(); x++) {
for (int y = 0; y < this.res.getWidth(); y++) {
if (this.res.getRGB(x, y) != other.res.getRGB(x, y)) {
for(int x = 0; x < this.res.getHeight(); x++)
{
for(int y = 0; y < this.res.getWidth(); y++)
{
if(this.res.getRGB(x, y) != other.res.getRGB(x, y))
{
return false;
}
}

View File

@@ -7,20 +7,24 @@ package com.aargonian.resource;
* or anything else that may be limited or is unique in some fashion. Resources are named entities, and are cacheable
* and ownable. Resources are required to override the equals() and hashCode() methods, for convenience.
*/
abstract public class Resource {
private final String name;
abstract public class Resource
{
private final String name;
private final ResourceType type;
protected Resource(String name, ResourceType type) {
protected Resource(String name, ResourceType type)
{
this.name = name;
this.type = type;
}
public String getName() {
public String getName()
{
return this.name;
}
public ResourceType getType() {
public ResourceType getType()
{
return this.type;
}
@@ -31,7 +35,12 @@ abstract public class Resource {
abstract public int hashCode();
//We know at compile-time what sort of resources exist, so they are enumerated here for checking
public enum ResourceType {
FILE, IMAGE, SOUND, SCRIPT, SOCKET
public enum ResourceType
{
FILE,
IMAGE,
SOUND,
SCRIPT,
SOCKET
}
}

View File

@@ -9,15 +9,22 @@ import java.util.Map;
* A utility class for loading and caching common resources used by the editor. Primarily used to load images and sound
* files.
*/
public final class ResourceLoader {
public final class ResourceLoader
{
private static final Map<String, Resource> resourceCache = new HashMap<>(100);
public static ImageResource loadImage(String imagePath) {
if (imagePath == null || imagePath.isEmpty()) {
public static ImageResource loadImage(String imagePath)
{
if(imagePath == null || imagePath.isEmpty())
{
return null;
} else if (resourceCache.containsKey(imagePath) && resourceCache.get(imagePath) != null) {
}
else if(resourceCache.containsKey(imagePath) && resourceCache.get(imagePath) != null)
{
return (ImageResource) resourceCache.get(imagePath);
} else {
}
else
{
ImageResource resource = new ImageResource(imagePath);
resourceCache.put(imagePath, resource);
return resource;

View File

@@ -9,56 +9,70 @@ import java.util.Map;
/**
* Created by aargonian on 7/4/17.
*/
public class TileImpl {
public class TileImpl
{
/**
* Very important magic string values used by the tile class for common properties/values
*/
public static final String PROPERTY_TILETYPE = "TileType";
public static final String PROPERTY_IMG = "ImageRef";
public static final String VALUE_TRUE = "True";
public static final String VALUE_FALSE = "False";
public static final String PROPERTY_IMG = "ImageRef";
public static final String VALUE_TRUE = "True";
public static final String VALUE_FALSE = "False";
private final HashPMap<String, String> properties;
private final HashPMap<String, String> properties;
private final HashPMap<String, Resource> resources;
private int hash = 0;
public TileImpl(Map<String, String> startingProperties, Map<String, Resource> startingResources) {
if (startingProperties != null && startingProperties.size() > 0) {
public TileImpl(Map<String, String> startingProperties, Map<String, Resource> startingResources)
{
if(startingProperties != null && startingProperties.size() > 0)
{
this.properties = HashTreePMap.from(startingProperties);
} else {
}
else
{
this.properties = null;
}
if (startingResources != null && startingResources.size() > 0) {
if(startingResources != null && startingResources.size() > 0)
{
this.resources = HashTreePMap.from(startingResources);
} else {
}
else
{
this.resources = null;
}
}
private TileImpl(HashPMap<String, String> implProperties, HashPMap<String, Resource> implResources) {
private TileImpl(HashPMap<String, String> implProperties, HashPMap<String, Resource> implResources)
{
this.properties = implProperties;
this.resources = implResources;
}
@Override
public boolean equals(Object o) {
if (this == o) {
public boolean equals(Object o)
{
if(this == o)
{
return true;
}
if (o == null || this.getClass() != o.getClass()) {
if(o == null || this.getClass() != o.getClass())
{
return false;
}
TileImpl tile = (TileImpl) o;
return (this.properties != null ? this.properties.equals(tile.properties) : tile.properties == null) &&
(this.resources != null ? this.resources.equals(tile.resources) : tile.resources == null);
(this.resources != null ? this.resources.equals(tile.resources) : tile.resources == null);
}
@Override
public int hashCode() {
if (this.hash == 0) {
public int hashCode()
{
if(this.hash == 0)
{
int result = this.properties != null ? this.properties.hashCode() : 0;
result = 31 * result + (this.resources != null ? this.resources.hashCode() : 0);
this.hash = result;
@@ -66,56 +80,70 @@ public class TileImpl {
return this.hash;
}
public TileImpl removeProperty(String property) {
if (this.properties == null) {
public TileImpl removeProperty(String property)
{
if(this.properties == null)
{
return this;
}
return new TileImpl(this.properties.minus(property), this.resources);
}
public String getProperty(String property) {
if (this.properties == null) {
public String getProperty(String property)
{
if(this.properties == null)
{
return null;
}
return this.properties.get(property);
}
public TileImpl setProperty(String property, String value) {
if (this.properties == null) {
public TileImpl setProperty(String property, String value)
{
if(this.properties == null)
{
return new TileImpl(HashTreePMap.singleton(property, value), this.resources);
}
return new TileImpl(this.properties.plus(property, value), this.resources);
}
public boolean hasProperty(String property) {
public boolean hasProperty(String property)
{
return this.properties != null && this.properties.containsKey(property) &&
this.properties.get(property) != null;
this.properties.get(property) != null;
}
public TileImpl removeResource(String resource) {
if (this.resources == null) {
public TileImpl removeResource(String resource)
{
if(this.resources == null)
{
return this;
}
return new TileImpl(this.properties, this.resources.minus(resource));
}
public Resource getResource(String resource) {
if (this.resources == null) {
public Resource getResource(String resource)
{
if(this.resources == null)
{
return null;
}
return this.resources.get(resource);
}
public TileImpl setResource(String resourceKey, Resource resource) {
if (this.resources == null) {
public TileImpl setResource(String resourceKey, Resource resource)
{
if(this.resources == null)
{
return new TileImpl(this.properties, HashTreePMap.singleton(resourceKey, resource));
}
return new TileImpl(this.properties, this.resources.plus(resourceKey, resource));
}
public boolean hasResource(String resourceKey) {
public boolean hasResource(String resourceKey)
{
return this.resources != null && this.resources.containsKey(resourceKey) &&
this.resources.get(resourceKey) != null;
this.resources.get(resourceKey) != null;
}
}

View File

@@ -7,44 +7,55 @@ import java.util.ArrayList;
/**
* Created by aargonian on 7/4/17.
*/
public class TileMap {
public class TileMap
{
private final ArrayList<TileImpl> tilesImplementations;
private final Tile[] tiles;
private final int rows;
private final int columns;
private final Tile[] tiles;
private final int rows;
private final int columns;
public TileMap(int columns, int rows) {
public TileMap(int columns, int rows)
{
this.columns = columns;
this.rows = rows;
this.tilesImplementations = new ArrayList<>();
this.tilesImplementations.add(new TileImpl(null, null));
this.tilesImplementations.get(0).setProperty(TileImpl.PROPERTY_TILETYPE, "EmptyTile");
this.tiles = new Tile[columns * rows];
for (int i = 0; i < this.tiles.length; i++) {
for(int i = 0; i < this.tiles.length; i++)
{
this.tiles[i] = new Tile(this.tilesImplementations.get(0));
}
}
public void addTileImplementation(TileImpl impl) {
public void addTileImplementation(TileImpl impl)
{
this.tilesImplementations.add(impl);
}
public ArrayList<TileImpl> getTileImplementations() {
public ArrayList<TileImpl> getTileImplementations()
{
return this.tilesImplementations;
}
public int getRows() {
public int getRows()
{
return this.rows;
}
public int getColumns() {
public int getColumns()
{
return this.columns;
}
private void setTileImplementation(Tile tile, TileImpl newImpl) {
for (TileImpl implementation : this.tilesImplementations) {
if (implementation.hashCode() == newImpl.hashCode()) {
if (implementation.equals(newImpl)) {
private void setTileImplementation(Tile tile, TileImpl newImpl)
{
for(TileImpl implementation : this.tilesImplementations)
{
if(implementation.hashCode() == newImpl.hashCode())
{
if(implementation.equals(newImpl))
{
tile.setTileImplementation(implementation);
return;
}
@@ -55,19 +66,24 @@ public class TileMap {
tile.setTileImplementation(newImpl);
}
private boolean inBounds(int x, int y) {
private boolean inBounds(int x, int y)
{
return !(x < 0 || x >= this.columns || y < 0 || y >= this.rows);
}
public String getTilePropertyAt(String property, int x, int y) {
if (this.inBounds(x, y)) {
public String getTilePropertyAt(String property, int x, int y)
{
if(this.inBounds(x, y))
{
return this.tiles[(y * this.columns) + x].getTileImplementation().getProperty(property);
}
return null;
}
public void setTilePropertyAt(String property, String value, int x, int y) {
if (!this.inBounds(x, y)) {
public void setTilePropertyAt(String property, String value, int x, int y)
{
if(!this.inBounds(x, y))
{
return;
}
Tile tile = this.tiles[(y * this.columns) + x];
@@ -75,15 +91,19 @@ public class TileMap {
this.setTileImplementation(tile, newImpl);
}
public Resource getTileResourceAt(String resource, int x, int y) {
if (!this.inBounds(x, y)) {
public Resource getTileResourceAt(String resource, int x, int y)
{
if(!this.inBounds(x, y))
{
return null;
}
return this.tiles[(y * this.columns) + x].getTileImplementation().getResource(resource);
}
public void setTileResourceAt(String resourceKey, Resource resource, int x, int y) {
if (!this.inBounds(x, y)) {
public void setTileResourceAt(String resourceKey, Resource resource, int x, int y)
{
if(!this.inBounds(x, y))
{
return;
}
Tile tile = this.tiles[(y * this.columns) + x];
@@ -91,19 +111,24 @@ public class TileMap {
this.setTileImplementation(tile, newImpl);
}
private final class Tile {
private final class Tile
{
private TileImpl implementation;
private Tile(TileImpl impl) {
private Tile(TileImpl impl)
{
this.implementation = impl;
}
@Override
public boolean equals(Object o) {
if (this == o) {
public boolean equals(Object o)
{
if(this == o)
{
return true;
}
if (o == null || this.getClass() != o.getClass()) {
if(o == null || this.getClass() != o.getClass())
{
return false;
}
@@ -113,15 +138,18 @@ public class TileMap {
}
@Override
public int hashCode() {
public int hashCode()
{
return this.implementation.hashCode();
}
private TileImpl getTileImplementation() {
private TileImpl getTileImplementation()
{
return this.implementation;
}
private void setTileImplementation(TileImpl impl) {
private void setTileImplementation(TileImpl impl)
{
this.implementation = impl;
}
}

View File

@@ -3,28 +3,34 @@ package com.aargonian.util;
/**
* Created by aargonian on 7/17/17.
*/
public class Pair<K, T> {
public class Pair<K, T>
{
private K first;
private T second;
public Pair(K first, T second) {
public Pair(K first, T second)
{
this.first = first;
this.second = second;
}
public K getFirst() {
public K getFirst()
{
return this.first;
}
public void setFirst(K first) {
public void setFirst(K first)
{
this.first = first;
}
public T getSecond() {
public T getSecond()
{
return this.second;
}
public void setSecond(T second) {
public void setSecond(T second)
{
this.second = second;
}
}